1 //! Compiler intrinsics.
3 //! The corresponding definitions are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>.
4 //! The corresponding const implementations are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>.
8 //! Note: any changes to the constness of intrinsics should be discussed with the language team.
9 //! This includes changes in the stability of the constness.
11 //! In order to make an intrinsic usable at compile-time, one needs to copy the implementation
12 //! from <https://github.com/rust-lang/miri/blob/master/src/shims/intrinsics.rs> to
13 //! <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs> and add a
14 //! `#[rustc_const_unstable(feature = "const_such_and_such", issue = "01234")]` to the intrinsic declaration.
16 //! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
17 //! the intrinsic's attribute must be `rustc_const_stable`, too. Such a change should not be done
18 //! without T-lang consultation, because it bakes a feature into the language that cannot be
19 //! replicated in user code without compiler support.
23 //! The volatile intrinsics provide operations intended to act on I/O
24 //! memory, which are guaranteed to not be reordered by the compiler
25 //! across other volatile intrinsics. See the LLVM documentation on
28 //! [volatile]: https://llvm.org/docs/LangRef.html#volatile-memory-accesses
32 //! The atomic intrinsics provide common atomic operations on machine
33 //! words, with multiple possible memory orderings. They obey the same
34 //! semantics as C++11. See the LLVM documentation on [[atomics]].
36 //! [atomics]: https://llvm.org/docs/Atomics.html
38 //! A quick refresher on memory ordering:
40 //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
41 //! take place after the barrier.
42 //! * Release - a barrier for releasing a lock. Preceding reads and writes
43 //! take place before the barrier.
44 //! * Sequentially consistent - sequentially consistent operations are
45 //! guaranteed to happen in order. This is the standard mode for working
46 //! with atomic types and is equivalent to Java's `volatile`.
49 feature = "core_intrinsics",
50 reason = "intrinsics are unlikely to ever be stabilized, instead \
51 they should be used through stabilized interfaces \
52 in the rest of the standard library",
55 #![allow(missing_docs)]
57 use crate::marker::DiscriminantKind;
58 use crate::marker::Tuple;
61 #[cfg(not(bootstrap))]
64 // These imports are used for simplifying intra-doc links
65 #[allow(unused_imports)]
66 #[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))]
67 use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering};
69 #[stable(feature = "drop_in_place", since = "1.8.0")]
70 #[rustc_allowed_through_unstable_modules]
71 #[deprecated(note = "no longer an intrinsic - use `ptr::drop_in_place` directly", since = "1.52.0")]
73 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
74 // SAFETY: see `ptr::drop_in_place`
75 unsafe { crate::ptr::drop_in_place(to_drop) }
78 extern "rust-intrinsic" {
79 // N.B., these intrinsics take raw pointers because they mutate aliased
80 // memory, which is not valid for either `&` or `&mut`.
82 /// Stores a value if the current value is the same as the `old` value.
84 /// The stabilized version of this intrinsic is available on the
85 /// [`atomic`] types via the `compare_exchange` method by passing
86 /// [`Ordering::Relaxed`] as both the success and failure parameters.
87 /// For example, [`AtomicBool::compare_exchange`].
88 pub fn atomic_cxchg_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
89 /// Stores a value if the current value is the same as the `old` value.
91 /// The stabilized version of this intrinsic is available on the
92 /// [`atomic`] types via the `compare_exchange` method by passing
93 /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
94 /// For example, [`AtomicBool::compare_exchange`].
95 pub fn atomic_cxchg_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
96 /// Stores a value if the current value is the same as the `old` value.
98 /// The stabilized version of this intrinsic is available on the
99 /// [`atomic`] types via the `compare_exchange` method by passing
100 /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
101 /// For example, [`AtomicBool::compare_exchange`].
102 pub fn atomic_cxchg_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
103 /// Stores a value if the current value is the same as the `old` value.
105 /// The stabilized version of this intrinsic is available on the
106 /// [`atomic`] types via the `compare_exchange` method by passing
107 /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
108 /// For example, [`AtomicBool::compare_exchange`].
109 pub fn atomic_cxchg_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
110 /// Stores a value if the current value is the same as the `old` value.
112 /// The stabilized version of this intrinsic is available on the
113 /// [`atomic`] types via the `compare_exchange` method by passing
114 /// [`Ordering::Acquire`] as both the success and failure parameters.
115 /// For example, [`AtomicBool::compare_exchange`].
116 pub fn atomic_cxchg_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
117 /// Stores a value if the current value is the same as the `old` value.
119 /// The stabilized version of this intrinsic is available on the
120 /// [`atomic`] types via the `compare_exchange` method by passing
121 /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
122 /// For example, [`AtomicBool::compare_exchange`].
123 pub fn atomic_cxchg_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
124 /// Stores a value if the current value is the same as the `old` value.
126 /// The stabilized version of this intrinsic is available on the
127 /// [`atomic`] types via the `compare_exchange` method by passing
128 /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
129 /// For example, [`AtomicBool::compare_exchange`].
130 pub fn atomic_cxchg_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
131 /// Stores a value if the current value is the same as the `old` value.
133 /// The stabilized version of this intrinsic is available on the
134 /// [`atomic`] types via the `compare_exchange` method by passing
135 /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
136 /// For example, [`AtomicBool::compare_exchange`].
137 pub fn atomic_cxchg_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
138 /// Stores a value if the current value is the same as the `old` value.
140 /// The stabilized version of this intrinsic is available on the
141 /// [`atomic`] types via the `compare_exchange` method by passing
142 /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
143 /// For example, [`AtomicBool::compare_exchange`].
144 pub fn atomic_cxchg_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
145 /// Stores a value if the current value is the same as the `old` value.
147 /// The stabilized version of this intrinsic is available on the
148 /// [`atomic`] types via the `compare_exchange` method by passing
149 /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
150 /// For example, [`AtomicBool::compare_exchange`].
151 pub fn atomic_cxchg_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
152 /// Stores a value if the current value is the same as the `old` value.
154 /// The stabilized version of this intrinsic is available on the
155 /// [`atomic`] types via the `compare_exchange` method by passing
156 /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
157 /// For example, [`AtomicBool::compare_exchange`].
158 pub fn atomic_cxchg_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
159 /// Stores a value if the current value is the same as the `old` value.
161 /// The stabilized version of this intrinsic is available on the
162 /// [`atomic`] types via the `compare_exchange` method by passing
163 /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
164 /// For example, [`AtomicBool::compare_exchange`].
165 pub fn atomic_cxchg_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
166 /// Stores a value if the current value is the same as the `old` value.
168 /// The stabilized version of this intrinsic is available on the
169 /// [`atomic`] types via the `compare_exchange` method by passing
170 /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
171 /// For example, [`AtomicBool::compare_exchange`].
172 pub fn atomic_cxchg_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
173 /// Stores a value if the current value is the same as the `old` value.
175 /// The stabilized version of this intrinsic is available on the
176 /// [`atomic`] types via the `compare_exchange` method by passing
177 /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
178 /// For example, [`AtomicBool::compare_exchange`].
179 pub fn atomic_cxchg_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
180 /// Stores a value if the current value is the same as the `old` value.
182 /// The stabilized version of this intrinsic is available on the
183 /// [`atomic`] types via the `compare_exchange` method by passing
184 /// [`Ordering::SeqCst`] as both the success and failure parameters.
185 /// For example, [`AtomicBool::compare_exchange`].
186 pub fn atomic_cxchg_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
188 /// Stores a value if the current value is the same as the `old` value.
190 /// The stabilized version of this intrinsic is available on the
191 /// [`atomic`] types via the `compare_exchange_weak` method by passing
192 /// [`Ordering::Relaxed`] as both the success and failure parameters.
193 /// For example, [`AtomicBool::compare_exchange_weak`].
194 pub fn atomic_cxchgweak_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
195 /// Stores a value if the current value is the same as the `old` value.
197 /// The stabilized version of this intrinsic is available on the
198 /// [`atomic`] types via the `compare_exchange_weak` method by passing
199 /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
200 /// For example, [`AtomicBool::compare_exchange_weak`].
201 pub fn atomic_cxchgweak_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
202 /// Stores a value if the current value is the same as the `old` value.
204 /// The stabilized version of this intrinsic is available on the
205 /// [`atomic`] types via the `compare_exchange_weak` method by passing
206 /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
207 /// For example, [`AtomicBool::compare_exchange_weak`].
208 pub fn atomic_cxchgweak_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
209 /// Stores a value if the current value is the same as the `old` value.
211 /// The stabilized version of this intrinsic is available on the
212 /// [`atomic`] types via the `compare_exchange_weak` method by passing
213 /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
214 /// For example, [`AtomicBool::compare_exchange_weak`].
215 pub fn atomic_cxchgweak_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
216 /// Stores a value if the current value is the same as the `old` value.
218 /// The stabilized version of this intrinsic is available on the
219 /// [`atomic`] types via the `compare_exchange_weak` method by passing
220 /// [`Ordering::Acquire`] as both the success and failure parameters.
221 /// For example, [`AtomicBool::compare_exchange_weak`].
222 pub fn atomic_cxchgweak_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
223 /// Stores a value if the current value is the same as the `old` value.
225 /// The stabilized version of this intrinsic is available on the
226 /// [`atomic`] types via the `compare_exchange_weak` method by passing
227 /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
228 /// For example, [`AtomicBool::compare_exchange_weak`].
229 pub fn atomic_cxchgweak_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
230 /// Stores a value if the current value is the same as the `old` value.
232 /// The stabilized version of this intrinsic is available on the
233 /// [`atomic`] types via the `compare_exchange_weak` method by passing
234 /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
235 /// For example, [`AtomicBool::compare_exchange_weak`].
236 pub fn atomic_cxchgweak_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
237 /// Stores a value if the current value is the same as the `old` value.
239 /// The stabilized version of this intrinsic is available on the
240 /// [`atomic`] types via the `compare_exchange_weak` method by passing
241 /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
242 /// For example, [`AtomicBool::compare_exchange_weak`].
243 pub fn atomic_cxchgweak_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
244 /// Stores a value if the current value is the same as the `old` value.
246 /// The stabilized version of this intrinsic is available on the
247 /// [`atomic`] types via the `compare_exchange_weak` method by passing
248 /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
249 /// For example, [`AtomicBool::compare_exchange_weak`].
250 pub fn atomic_cxchgweak_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
251 /// Stores a value if the current value is the same as the `old` value.
253 /// The stabilized version of this intrinsic is available on the
254 /// [`atomic`] types via the `compare_exchange_weak` method by passing
255 /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
256 /// For example, [`AtomicBool::compare_exchange_weak`].
257 pub fn atomic_cxchgweak_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
258 /// Stores a value if the current value is the same as the `old` value.
260 /// The stabilized version of this intrinsic is available on the
261 /// [`atomic`] types via the `compare_exchange_weak` method by passing
262 /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
263 /// For example, [`AtomicBool::compare_exchange_weak`].
264 pub fn atomic_cxchgweak_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
265 /// Stores a value if the current value is the same as the `old` value.
267 /// The stabilized version of this intrinsic is available on the
268 /// [`atomic`] types via the `compare_exchange_weak` method by passing
269 /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
270 /// For example, [`AtomicBool::compare_exchange_weak`].
271 pub fn atomic_cxchgweak_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
272 /// Stores a value if the current value is the same as the `old` value.
274 /// The stabilized version of this intrinsic is available on the
275 /// [`atomic`] types via the `compare_exchange_weak` method by passing
276 /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
277 /// For example, [`AtomicBool::compare_exchange_weak`].
278 pub fn atomic_cxchgweak_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
279 /// Stores a value if the current value is the same as the `old` value.
281 /// The stabilized version of this intrinsic is available on the
282 /// [`atomic`] types via the `compare_exchange_weak` method by passing
283 /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
284 /// For example, [`AtomicBool::compare_exchange_weak`].
285 pub fn atomic_cxchgweak_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
286 /// Stores a value if the current value is the same as the `old` value.
288 /// The stabilized version of this intrinsic is available on the
289 /// [`atomic`] types via the `compare_exchange_weak` method by passing
290 /// [`Ordering::SeqCst`] as both the success and failure parameters.
291 /// For example, [`AtomicBool::compare_exchange_weak`].
292 pub fn atomic_cxchgweak_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
294 /// Loads the current value of the pointer.
296 /// The stabilized version of this intrinsic is available on the
297 /// [`atomic`] types via the `load` method by passing
298 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`].
299 pub fn atomic_load_seqcst<T: Copy>(src: *const T) -> T;
300 /// Loads the current value of the pointer.
302 /// The stabilized version of this intrinsic is available on the
303 /// [`atomic`] types via the `load` method by passing
304 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`].
305 pub fn atomic_load_acquire<T: Copy>(src: *const T) -> T;
306 /// Loads the current value of the pointer.
308 /// The stabilized version of this intrinsic is available on the
309 /// [`atomic`] types via the `load` method by passing
310 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`].
311 pub fn atomic_load_relaxed<T: Copy>(src: *const T) -> T;
312 pub fn atomic_load_unordered<T: Copy>(src: *const T) -> T;
314 /// Stores the value at the specified memory location.
316 /// The stabilized version of this intrinsic is available on the
317 /// [`atomic`] types via the `store` method by passing
318 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`].
319 pub fn atomic_store_seqcst<T: Copy>(dst: *mut T, val: T);
320 /// Stores the value at the specified memory location.
322 /// The stabilized version of this intrinsic is available on the
323 /// [`atomic`] types via the `store` method by passing
324 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`].
325 pub fn atomic_store_release<T: Copy>(dst: *mut T, val: T);
326 /// Stores the value at the specified memory location.
328 /// The stabilized version of this intrinsic is available on the
329 /// [`atomic`] types via the `store` method by passing
330 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`].
331 pub fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T);
332 pub fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T);
334 /// Stores the value at the specified memory location, returning the old value.
336 /// The stabilized version of this intrinsic is available on the
337 /// [`atomic`] types via the `swap` method by passing
338 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`].
339 pub fn atomic_xchg_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
340 /// Stores the value at the specified memory location, returning the old value.
342 /// The stabilized version of this intrinsic is available on the
343 /// [`atomic`] types via the `swap` method by passing
344 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`].
345 pub fn atomic_xchg_acquire<T: Copy>(dst: *mut T, src: T) -> T;
346 /// Stores the value at the specified memory location, returning the old value.
348 /// The stabilized version of this intrinsic is available on the
349 /// [`atomic`] types via the `swap` method by passing
350 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`].
351 pub fn atomic_xchg_release<T: Copy>(dst: *mut T, src: T) -> T;
352 /// Stores the value at the specified memory location, returning the old value.
354 /// The stabilized version of this intrinsic is available on the
355 /// [`atomic`] types via the `swap` method by passing
356 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`].
357 pub fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
358 /// Stores the value at the specified memory location, returning the old value.
360 /// The stabilized version of this intrinsic is available on the
361 /// [`atomic`] types via the `swap` method by passing
362 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`].
363 pub fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
365 /// Adds to the current value, returning the previous value.
367 /// The stabilized version of this intrinsic is available on the
368 /// [`atomic`] types via the `fetch_add` method by passing
369 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`].
370 pub fn atomic_xadd_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
371 /// Adds to the current value, returning the previous value.
373 /// The stabilized version of this intrinsic is available on the
374 /// [`atomic`] types via the `fetch_add` method by passing
375 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`].
376 pub fn atomic_xadd_acquire<T: Copy>(dst: *mut T, src: T) -> T;
377 /// Adds to the current value, returning the previous value.
379 /// The stabilized version of this intrinsic is available on the
380 /// [`atomic`] types via the `fetch_add` method by passing
381 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`].
382 pub fn atomic_xadd_release<T: Copy>(dst: *mut T, src: T) -> T;
383 /// Adds to the current value, returning the previous value.
385 /// The stabilized version of this intrinsic is available on the
386 /// [`atomic`] types via the `fetch_add` method by passing
387 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`].
388 pub fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
389 /// Adds to the current value, returning the previous value.
391 /// The stabilized version of this intrinsic is available on the
392 /// [`atomic`] types via the `fetch_add` method by passing
393 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`].
394 pub fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
396 /// Subtract from the current value, returning the previous value.
398 /// The stabilized version of this intrinsic is available on the
399 /// [`atomic`] types via the `fetch_sub` method by passing
400 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
401 pub fn atomic_xsub_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
402 /// Subtract from the current value, returning the previous value.
404 /// The stabilized version of this intrinsic is available on the
405 /// [`atomic`] types via the `fetch_sub` method by passing
406 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
407 pub fn atomic_xsub_acquire<T: Copy>(dst: *mut T, src: T) -> T;
408 /// Subtract from the current value, returning the previous value.
410 /// The stabilized version of this intrinsic is available on the
411 /// [`atomic`] types via the `fetch_sub` method by passing
412 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
413 pub fn atomic_xsub_release<T: Copy>(dst: *mut T, src: T) -> T;
414 /// Subtract from the current value, returning the previous value.
416 /// The stabilized version of this intrinsic is available on the
417 /// [`atomic`] types via the `fetch_sub` method by passing
418 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
419 pub fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
420 /// Subtract from the current value, returning the previous value.
422 /// The stabilized version of this intrinsic is available on the
423 /// [`atomic`] types via the `fetch_sub` method by passing
424 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
425 pub fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
427 /// Bitwise and with the current value, returning the previous value.
429 /// The stabilized version of this intrinsic is available on the
430 /// [`atomic`] types via the `fetch_and` method by passing
431 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`].
432 pub fn atomic_and_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
433 /// Bitwise and with the current value, returning the previous value.
435 /// The stabilized version of this intrinsic is available on the
436 /// [`atomic`] types via the `fetch_and` method by passing
437 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`].
438 pub fn atomic_and_acquire<T: Copy>(dst: *mut T, src: T) -> T;
439 /// Bitwise and with the current value, returning the previous value.
441 /// The stabilized version of this intrinsic is available on the
442 /// [`atomic`] types via the `fetch_and` method by passing
443 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`].
444 pub fn atomic_and_release<T: Copy>(dst: *mut T, src: T) -> T;
445 /// Bitwise and with the current value, returning the previous value.
447 /// The stabilized version of this intrinsic is available on the
448 /// [`atomic`] types via the `fetch_and` method by passing
449 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`].
450 pub fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
451 /// Bitwise and with the current value, returning the previous value.
453 /// The stabilized version of this intrinsic is available on the
454 /// [`atomic`] types via the `fetch_and` method by passing
455 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`].
456 pub fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
458 /// Bitwise nand with the current value, returning the previous value.
460 /// The stabilized version of this intrinsic is available on the
461 /// [`AtomicBool`] type via the `fetch_nand` method by passing
462 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`].
463 pub fn atomic_nand_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
464 /// Bitwise nand with the current value, returning the previous value.
466 /// The stabilized version of this intrinsic is available on the
467 /// [`AtomicBool`] type via the `fetch_nand` method by passing
468 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`].
469 pub fn atomic_nand_acquire<T: Copy>(dst: *mut T, src: T) -> T;
470 /// Bitwise nand with the current value, returning the previous value.
472 /// The stabilized version of this intrinsic is available on the
473 /// [`AtomicBool`] type via the `fetch_nand` method by passing
474 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`].
475 pub fn atomic_nand_release<T: Copy>(dst: *mut T, src: T) -> T;
476 /// Bitwise nand with the current value, returning the previous value.
478 /// The stabilized version of this intrinsic is available on the
479 /// [`AtomicBool`] type via the `fetch_nand` method by passing
480 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`].
481 pub fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
482 /// Bitwise nand with the current value, returning the previous value.
484 /// The stabilized version of this intrinsic is available on the
485 /// [`AtomicBool`] type via the `fetch_nand` method by passing
486 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`].
487 pub fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
489 /// Bitwise or with the current value, returning the previous value.
491 /// The stabilized version of this intrinsic is available on the
492 /// [`atomic`] types via the `fetch_or` method by passing
493 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`].
494 pub fn atomic_or_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
495 /// Bitwise or with the current value, returning the previous value.
497 /// The stabilized version of this intrinsic is available on the
498 /// [`atomic`] types via the `fetch_or` method by passing
499 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`].
500 pub fn atomic_or_acquire<T: Copy>(dst: *mut T, src: T) -> T;
501 /// Bitwise or with the current value, returning the previous value.
503 /// The stabilized version of this intrinsic is available on the
504 /// [`atomic`] types via the `fetch_or` method by passing
505 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`].
506 pub fn atomic_or_release<T: Copy>(dst: *mut T, src: T) -> T;
507 /// Bitwise or with the current value, returning the previous value.
509 /// The stabilized version of this intrinsic is available on the
510 /// [`atomic`] types via the `fetch_or` method by passing
511 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`].
512 pub fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
513 /// Bitwise or with the current value, returning the previous value.
515 /// The stabilized version of this intrinsic is available on the
516 /// [`atomic`] types via the `fetch_or` method by passing
517 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`].
518 pub fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
520 /// Bitwise xor with the current value, returning the previous value.
522 /// The stabilized version of this intrinsic is available on the
523 /// [`atomic`] types via the `fetch_xor` method by passing
524 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`].
525 pub fn atomic_xor_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
526 /// Bitwise xor with the current value, returning the previous value.
528 /// The stabilized version of this intrinsic is available on the
529 /// [`atomic`] types via the `fetch_xor` method by passing
530 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`].
531 pub fn atomic_xor_acquire<T: Copy>(dst: *mut T, src: T) -> T;
532 /// Bitwise xor with the current value, returning the previous value.
534 /// The stabilized version of this intrinsic is available on the
535 /// [`atomic`] types via the `fetch_xor` method by passing
536 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`].
537 pub fn atomic_xor_release<T: Copy>(dst: *mut T, src: T) -> T;
538 /// Bitwise xor with the current value, returning the previous value.
540 /// The stabilized version of this intrinsic is available on the
541 /// [`atomic`] types via the `fetch_xor` method by passing
542 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`].
543 pub fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
544 /// Bitwise xor with the current value, returning the previous value.
546 /// The stabilized version of this intrinsic is available on the
547 /// [`atomic`] types via the `fetch_xor` method by passing
548 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`].
549 pub fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
551 /// Maximum with the current value using a signed comparison.
553 /// The stabilized version of this intrinsic is available on the
554 /// [`atomic`] signed integer types via the `fetch_max` method by passing
555 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`].
556 pub fn atomic_max_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
557 /// Maximum with the current value using a signed comparison.
559 /// The stabilized version of this intrinsic is available on the
560 /// [`atomic`] signed integer types via the `fetch_max` method by passing
561 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`].
562 pub fn atomic_max_acquire<T: Copy>(dst: *mut T, src: T) -> T;
563 /// Maximum with the current value using a signed comparison.
565 /// The stabilized version of this intrinsic is available on the
566 /// [`atomic`] signed integer types via the `fetch_max` method by passing
567 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`].
568 pub fn atomic_max_release<T: Copy>(dst: *mut T, src: T) -> T;
569 /// Maximum with the current value using a signed comparison.
571 /// The stabilized version of this intrinsic is available on the
572 /// [`atomic`] signed integer types via the `fetch_max` method by passing
573 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`].
574 pub fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
575 /// Maximum with the current value.
577 /// The stabilized version of this intrinsic is available on the
578 /// [`atomic`] signed integer types via the `fetch_max` method by passing
579 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`].
580 pub fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
582 /// Minimum with the current value using a signed comparison.
584 /// The stabilized version of this intrinsic is available on the
585 /// [`atomic`] signed integer types via the `fetch_min` method by passing
586 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`].
587 pub fn atomic_min_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
588 /// Minimum with the current value using a signed comparison.
590 /// The stabilized version of this intrinsic is available on the
591 /// [`atomic`] signed integer types via the `fetch_min` method by passing
592 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`].
593 pub fn atomic_min_acquire<T: Copy>(dst: *mut T, src: T) -> T;
594 /// Minimum with the current value using a signed comparison.
596 /// The stabilized version of this intrinsic is available on the
597 /// [`atomic`] signed integer types via the `fetch_min` method by passing
598 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`].
599 pub fn atomic_min_release<T: Copy>(dst: *mut T, src: T) -> T;
600 /// Minimum with the current value using a signed comparison.
602 /// The stabilized version of this intrinsic is available on the
603 /// [`atomic`] signed integer types via the `fetch_min` method by passing
604 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`].
605 pub fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
606 /// Minimum with the current value using a signed comparison.
608 /// The stabilized version of this intrinsic is available on the
609 /// [`atomic`] signed integer types via the `fetch_min` method by passing
610 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`].
611 pub fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
613 /// Minimum with the current value using an unsigned comparison.
615 /// The stabilized version of this intrinsic is available on the
616 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
617 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`].
618 pub fn atomic_umin_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
619 /// Minimum with the current value using an unsigned comparison.
621 /// The stabilized version of this intrinsic is available on the
622 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
623 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`].
624 pub fn atomic_umin_acquire<T: Copy>(dst: *mut T, src: T) -> T;
625 /// Minimum with the current value using an unsigned comparison.
627 /// The stabilized version of this intrinsic is available on the
628 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
629 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`].
630 pub fn atomic_umin_release<T: Copy>(dst: *mut T, src: T) -> T;
631 /// Minimum with the current value using an unsigned comparison.
633 /// The stabilized version of this intrinsic is available on the
634 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
635 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`].
636 pub fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
637 /// Minimum with the current value using an unsigned comparison.
639 /// The stabilized version of this intrinsic is available on the
640 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
641 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`].
642 pub fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
644 /// Maximum with the current value using an unsigned comparison.
646 /// The stabilized version of this intrinsic is available on the
647 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
648 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`].
649 pub fn atomic_umax_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
650 /// Maximum with the current value using an unsigned comparison.
652 /// The stabilized version of this intrinsic is available on the
653 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
654 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`].
655 pub fn atomic_umax_acquire<T: Copy>(dst: *mut T, src: T) -> T;
656 /// Maximum with the current value using an unsigned comparison.
658 /// The stabilized version of this intrinsic is available on the
659 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
660 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`].
661 pub fn atomic_umax_release<T: Copy>(dst: *mut T, src: T) -> T;
662 /// Maximum with the current value using an unsigned comparison.
664 /// The stabilized version of this intrinsic is available on the
665 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
666 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`].
667 pub fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
668 /// Maximum with the current value using an unsigned comparison.
670 /// The stabilized version of this intrinsic is available on the
671 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
672 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`].
673 pub fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
677 /// The stabilized version of this intrinsic is available in
678 /// [`atomic::fence`] by passing [`Ordering::SeqCst`]
680 pub fn atomic_fence_seqcst();
683 /// The stabilized version of this intrinsic is available in
684 /// [`atomic::fence`] by passing [`Ordering::Acquire`]
686 pub fn atomic_fence_acquire();
689 /// The stabilized version of this intrinsic is available in
690 /// [`atomic::fence`] by passing [`Ordering::Release`]
692 pub fn atomic_fence_release();
695 /// The stabilized version of this intrinsic is available in
696 /// [`atomic::fence`] by passing [`Ordering::AcqRel`]
698 pub fn atomic_fence_acqrel();
700 /// A compiler-only memory barrier.
702 /// Memory accesses will never be reordered across this barrier by the
703 /// compiler, but no instructions will be emitted for it. This is
704 /// appropriate for operations on the same thread that may be preempted,
705 /// such as when interacting with signal handlers.
707 /// The stabilized version of this intrinsic is available in
708 /// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`]
710 pub fn atomic_singlethreadfence_seqcst();
711 /// A compiler-only memory barrier.
713 /// Memory accesses will never be reordered across this barrier by the
714 /// compiler, but no instructions will be emitted for it. This is
715 /// appropriate for operations on the same thread that may be preempted,
716 /// such as when interacting with signal handlers.
718 /// The stabilized version of this intrinsic is available in
719 /// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`]
721 pub fn atomic_singlethreadfence_acquire();
722 /// A compiler-only memory barrier.
724 /// Memory accesses will never be reordered across this barrier by the
725 /// compiler, but no instructions will be emitted for it. This is
726 /// appropriate for operations on the same thread that may be preempted,
727 /// such as when interacting with signal handlers.
729 /// The stabilized version of this intrinsic is available in
730 /// [`atomic::compiler_fence`] by passing [`Ordering::Release`]
732 pub fn atomic_singlethreadfence_release();
733 /// A compiler-only memory barrier.
735 /// Memory accesses will never be reordered across this barrier by the
736 /// compiler, but no instructions will be emitted for it. This is
737 /// appropriate for operations on the same thread that may be preempted,
738 /// such as when interacting with signal handlers.
740 /// The stabilized version of this intrinsic is available in
741 /// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`]
743 pub fn atomic_singlethreadfence_acqrel();
745 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
746 /// if supported; otherwise, it is a no-op.
747 /// Prefetches have no effect on the behavior of the program but can change its performance
750 /// The `locality` argument must be a constant integer and is a temporal locality specifier
751 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
753 /// This intrinsic does not have a stable counterpart.
754 pub fn prefetch_read_data<T>(data: *const T, locality: i32);
755 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
756 /// if supported; otherwise, it is a no-op.
757 /// Prefetches have no effect on the behavior of the program but can change its performance
760 /// The `locality` argument must be a constant integer and is a temporal locality specifier
761 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
763 /// This intrinsic does not have a stable counterpart.
764 pub fn prefetch_write_data<T>(data: *const T, locality: i32);
765 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
766 /// if supported; otherwise, it is a no-op.
767 /// Prefetches have no effect on the behavior of the program but can change its performance
770 /// The `locality` argument must be a constant integer and is a temporal locality specifier
771 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
773 /// This intrinsic does not have a stable counterpart.
774 pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
775 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
776 /// if supported; otherwise, it is a no-op.
777 /// Prefetches have no effect on the behavior of the program but can change its performance
780 /// The `locality` argument must be a constant integer and is a temporal locality specifier
781 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
783 /// This intrinsic does not have a stable counterpart.
784 pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
786 /// Magic intrinsic that derives its meaning from attributes
787 /// attached to the function.
789 /// For example, dataflow uses this to inject static assertions so
790 /// that `rustc_peek(potentially_uninitialized)` would actually
791 /// double-check that dataflow did indeed compute that it is
792 /// uninitialized at that point in the control flow.
794 /// This intrinsic should not be used outside of the compiler.
795 #[rustc_safe_intrinsic]
796 pub fn rustc_peek<T>(_: T) -> T;
798 /// Aborts the execution of the process.
800 /// Note that, unlike most intrinsics, this is safe to call;
801 /// it does not require an `unsafe` block.
802 /// Therefore, implementations must not require the user to uphold
803 /// any safety invariants.
805 /// [`std::process::abort`](../../std/process/fn.abort.html) is to be preferred if possible,
806 /// as its behavior is more user-friendly and more stable.
808 /// The current implementation of `intrinsics::abort` is to invoke an invalid instruction,
809 /// on most platforms.
811 /// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or
812 /// `SIGBUS`. The precise behaviour is not guaranteed and not stable.
813 #[rustc_safe_intrinsic]
816 /// Informs the optimizer that this point in the code is not reachable,
817 /// enabling further optimizations.
819 /// N.B., this is very different from the `unreachable!()` macro: Unlike the
820 /// macro, which panics when it is executed, it is *undefined behavior* to
821 /// reach code marked with this function.
823 /// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`].
824 #[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
825 pub fn unreachable() -> !;
827 /// Informs the optimizer that a condition is always true.
828 /// If the condition is false, the behavior is undefined.
830 /// No code is generated for this intrinsic, but the optimizer will try
831 /// to preserve it (and its condition) between passes, which may interfere
832 /// with optimization of surrounding code and reduce performance. It should
833 /// not be used if the invariant can be discovered by the optimizer on its
834 /// own, or if it does not enable any significant optimizations.
836 /// This intrinsic does not have a stable counterpart.
837 #[rustc_const_unstable(feature = "const_assume", issue = "76972")]
838 pub fn assume(b: bool);
840 /// Hints to the compiler that branch condition is likely to be true.
841 /// Returns the value passed to it.
843 /// Any use other than with `if` statements will probably not have an effect.
845 /// Note that, unlike most intrinsics, this is safe to call;
846 /// it does not require an `unsafe` block.
847 /// Therefore, implementations must not require the user to uphold
848 /// any safety invariants.
850 /// This intrinsic does not have a stable counterpart.
851 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
852 #[rustc_safe_intrinsic]
853 pub fn likely(b: bool) -> bool;
855 /// Hints to the compiler that branch condition is likely to be false.
856 /// Returns the value passed to it.
858 /// Any use other than with `if` statements will probably not have an effect.
860 /// Note that, unlike most intrinsics, this is safe to call;
861 /// it does not require an `unsafe` block.
862 /// Therefore, implementations must not require the user to uphold
863 /// any safety invariants.
865 /// This intrinsic does not have a stable counterpart.
866 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
867 #[rustc_safe_intrinsic]
868 pub fn unlikely(b: bool) -> bool;
870 /// Executes a breakpoint trap, for inspection by a debugger.
872 /// This intrinsic does not have a stable counterpart.
875 /// The size of a type in bytes.
877 /// Note that, unlike most intrinsics, this is safe to call;
878 /// it does not require an `unsafe` block.
879 /// Therefore, implementations must not require the user to uphold
880 /// any safety invariants.
882 /// More specifically, this is the offset in bytes between successive
883 /// items of the same type, including alignment padding.
885 /// The stabilized version of this intrinsic is [`core::mem::size_of`].
886 #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
887 #[rustc_safe_intrinsic]
888 pub fn size_of<T>() -> usize;
890 /// The minimum alignment of a type.
892 /// Note that, unlike most intrinsics, this is safe to call;
893 /// it does not require an `unsafe` block.
894 /// Therefore, implementations must not require the user to uphold
895 /// any safety invariants.
897 /// The stabilized version of this intrinsic is [`core::mem::align_of`].
898 #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
899 #[rustc_safe_intrinsic]
900 pub fn min_align_of<T>() -> usize;
901 /// The preferred alignment of a type.
903 /// This intrinsic does not have a stable counterpart.
904 /// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
905 #[rustc_const_unstable(feature = "const_pref_align_of", issue = "91971")]
906 pub fn pref_align_of<T>() -> usize;
908 /// The size of the referenced value in bytes.
910 /// The stabilized version of this intrinsic is [`mem::size_of_val`].
911 #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
912 pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
913 /// The required alignment of the referenced value.
915 /// The stabilized version of this intrinsic is [`core::mem::align_of_val`].
916 #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
917 pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
919 /// Gets a static string slice containing the name of a type.
921 /// Note that, unlike most intrinsics, this is safe to call;
922 /// it does not require an `unsafe` block.
923 /// Therefore, implementations must not require the user to uphold
924 /// any safety invariants.
926 /// The stabilized version of this intrinsic is [`core::any::type_name`].
927 #[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
928 #[rustc_safe_intrinsic]
929 pub fn type_name<T: ?Sized>() -> &'static str;
931 /// Gets an identifier which is globally unique to the specified type. This
932 /// function will return the same value for a type regardless of whichever
933 /// crate it is invoked in.
935 /// Note that, unlike most intrinsics, this is safe to call;
936 /// it does not require an `unsafe` block.
937 /// Therefore, implementations must not require the user to uphold
938 /// any safety invariants.
940 /// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
941 #[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
942 #[rustc_safe_intrinsic]
943 pub fn type_id<T: ?Sized + 'static>() -> u64;
945 /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
946 /// This will statically either panic, or do nothing.
948 /// This intrinsic does not have a stable counterpart.
949 #[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
950 #[rustc_safe_intrinsic]
951 pub fn assert_inhabited<T>();
953 /// A guard for unsafe functions that cannot ever be executed if `T` does not permit
954 /// zero-initialization: This will statically either panic, or do nothing.
956 /// This intrinsic does not have a stable counterpart.
957 #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
958 #[rustc_safe_intrinsic]
959 pub fn assert_zero_valid<T>();
961 /// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing.
963 /// This intrinsic does not have a stable counterpart.
964 #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
965 #[rustc_safe_intrinsic]
966 #[cfg(not(bootstrap))]
967 pub fn assert_mem_uninitialized_valid<T>();
969 /// Gets a reference to a static `Location` indicating where it was called.
971 /// Note that, unlike most intrinsics, this is safe to call;
972 /// it does not require an `unsafe` block.
973 /// Therefore, implementations must not require the user to uphold
974 /// any safety invariants.
976 /// Consider using [`core::panic::Location::caller`] instead.
977 #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
978 #[rustc_safe_intrinsic]
979 pub fn caller_location() -> &'static crate::panic::Location<'static>;
981 /// Moves a value out of scope without running drop glue.
983 /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses
984 /// `ManuallyDrop` instead.
986 /// Note that, unlike most intrinsics, this is safe to call;
987 /// it does not require an `unsafe` block.
988 /// Therefore, implementations must not require the user to uphold
989 /// any safety invariants.
990 #[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")]
991 #[rustc_safe_intrinsic]
992 pub fn forget<T: ?Sized>(_: T);
994 /// Reinterprets the bits of a value of one type as another type.
996 /// Both types must have the same size. Compilation will fail if this is not guaranteed.
998 /// `transmute` is semantically equivalent to a bitwise move of one type
999 /// into another. It copies the bits from the source value into the
1000 /// destination value, then forgets the original. Note that source and destination
1001 /// are passed by-value, which means if `Src` or `Dst` contain padding, that padding
1002 /// is *not* guaranteed to be preserved by `transmute`.
1004 /// Both the argument and the result must be [valid](../../nomicon/what-unsafe-does.html) at
1005 /// their given type. Violating this condition leads to [undefined behavior][ub]. The compiler
1006 /// will generate code *assuming that you, the programmer, ensure that there will never be
1007 /// undefined behavior*. It is therefore your responsibility to guarantee that every value
1008 /// passed to `transmute` is valid at both types `Src` and `Dst`. Failing to uphold this condition
1009 /// may lead to unexpected and unstable compilation results. This makes `transmute` **incredibly
1010 /// unsafe**. `transmute` should be the absolute last resort.
1012 /// Transmuting pointers to integers in a `const` context is [undefined behavior][ub].
1013 /// Any attempt to use the resulting value for integer operations will abort const-evaluation.
1014 /// (And even outside `const`, such transmutation is touching on many unspecified aspects of the
1015 /// Rust memory model and should be avoided. See below for alternatives.)
1017 /// Because `transmute` is a by-value operation, alignment of the *transmuted values
1018 /// themselves* is not a concern. As with any other function, the compiler already ensures
1019 /// both `Src` and `Dst` are properly aligned. However, when transmuting values that *point
1020 /// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper
1021 /// alignment of the pointed-to values.
1023 /// The [nomicon](../../nomicon/transmutes.html) has additional documentation.
1025 /// [ub]: ../../reference/behavior-considered-undefined.html
1029 /// There are a few things that `transmute` is really useful for.
1031 /// Turning a pointer into a function pointer. This is *not* portable to
1032 /// machines where function pointers and data pointers have different sizes.
1035 /// fn foo() -> i32 {
1038 /// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer.
1039 /// // This avoids an integer-to-pointer `transmute`, which can be problematic.
1040 /// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine.
1041 /// let pointer = foo as *const ();
1042 /// let function = unsafe {
1043 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
1045 /// assert_eq!(function(), 0);
1048 /// Extending a lifetime, or shortening an invariant lifetime. This is
1049 /// advanced, very unsafe Rust!
1052 /// struct R<'a>(&'a i32);
1053 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
1054 /// std::mem::transmute::<R<'b>, R<'static>>(r)
1057 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
1058 /// -> &'b mut R<'c> {
1059 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
1065 /// Don't despair: many uses of `transmute` can be achieved through other means.
1066 /// Below are common applications of `transmute` which can be replaced with safer
1069 /// Turning raw bytes (`&[u8]`) into `u32`, `f64`, etc.:
1072 /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
1074 /// let num = unsafe {
1075 /// std::mem::transmute::<[u8; 4], u32>(raw_bytes)
1078 /// // use `u32::from_ne_bytes` instead
1079 /// let num = u32::from_ne_bytes(raw_bytes);
1080 /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
1081 /// let num = u32::from_le_bytes(raw_bytes);
1082 /// assert_eq!(num, 0x12345678);
1083 /// let num = u32::from_be_bytes(raw_bytes);
1084 /// assert_eq!(num, 0x78563412);
1087 /// Turning a pointer into a `usize`:
1091 /// let ptr_num_transmute = unsafe {
1092 /// std::mem::transmute::<&i32, usize>(ptr)
1095 /// // Use an `as` cast instead
1096 /// let ptr_num_cast = ptr as *const i32 as usize;
1099 /// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined
1100 /// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave
1101 /// as expected -- this is touching on many unspecified aspects of the Rust memory model.
1102 /// Depending on what the code is doing, the following alternatives are preferable to
1103 /// pointer-to-integer transmutation:
1104 /// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a
1105 /// type for that buffer, it can use [`MaybeUninit`][mem::MaybeUninit].
1106 /// - If the code actually wants to work on the address the pointer points to, it can use `as`
1107 /// casts or [`ptr.addr()`][pointer::addr].
1109 /// Turning a `*mut T` into an `&mut T`:
1112 /// let ptr: *mut i32 = &mut 0;
1113 /// let ref_transmuted = unsafe {
1114 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
1117 /// // Use a reborrow instead
1118 /// let ref_casted = unsafe { &mut *ptr };
1121 /// Turning an `&mut T` into an `&mut U`:
1124 /// let ptr = &mut 0;
1125 /// let val_transmuted = unsafe {
1126 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
1129 /// // Now, put together `as` and reborrowing - note the chaining of `as`
1130 /// // `as` is not transitive
1131 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
1134 /// Turning an `&str` into a `&[u8]`:
1137 /// // this is not a good way to do this.
1138 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
1139 /// assert_eq!(slice, &[82, 117, 115, 116]);
1141 /// // You could use `str::as_bytes`
1142 /// let slice = "Rust".as_bytes();
1143 /// assert_eq!(slice, &[82, 117, 115, 116]);
1145 /// // Or, just use a byte string, if you have control over the string
1147 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
1150 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`.
1152 /// To transmute the inner type of the contents of a container, you must make sure to not
1153 /// violate any of the container's invariants. For `Vec`, this means that both the size
1154 /// *and alignment* of the inner types have to match. Other containers might rely on the
1155 /// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't
1156 /// be possible at all without violating the container invariants.
1159 /// let store = [0, 1, 2, 3];
1160 /// let v_orig = store.iter().collect::<Vec<&i32>>();
1162 /// // clone the vector as we will reuse them later
1163 /// let v_clone = v_orig.clone();
1165 /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
1166 /// // bad idea and could cause Undefined Behavior.
1167 /// // However, it is no-copy.
1168 /// let v_transmuted = unsafe {
1169 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
1172 /// let v_clone = v_orig.clone();
1174 /// // This is the suggested, safe way.
1175 /// // It does copy the entire vector, though, into a new array.
1176 /// let v_collected = v_clone.into_iter()
1178 /// .collect::<Vec<Option<&i32>>>();
1180 /// let v_clone = v_orig.clone();
1182 /// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the
1183 /// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but
1184 /// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`),
1185 /// // this has all the same caveats. Besides the information provided above, also consult the
1186 /// // [`from_raw_parts`] documentation.
1187 /// let v_from_raw = unsafe {
1188 // FIXME Update this when vec_into_raw_parts is stabilized
1189 /// // Ensure the original vector is not dropped.
1190 /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
1191 /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
1193 /// v_clone.capacity())
1197 /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
1199 /// Implementing `split_at_mut`:
1202 /// use std::{slice, mem};
1204 /// // There are multiple ways to do this, and there are multiple problems
1205 /// // with the following (transmute) way.
1206 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
1207 /// -> (&mut [T], &mut [T]) {
1208 /// let len = slice.len();
1209 /// assert!(mid <= len);
1211 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
1212 /// // first: transmute is not type safe; all it checks is that T and
1213 /// // U are of the same size. Second, right here, you have two
1214 /// // mutable references pointing to the same memory.
1215 /// (&mut slice[0..mid], &mut slice2[mid..len])
1219 /// // This gets rid of the type safety problems; `&mut *` will *only* give
1220 /// // you an `&mut T` from an `&mut T` or `*mut T`.
1221 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
1222 /// -> (&mut [T], &mut [T]) {
1223 /// let len = slice.len();
1224 /// assert!(mid <= len);
1226 /// let slice2 = &mut *(slice as *mut [T]);
1227 /// // however, you still have two mutable references pointing to
1228 /// // the same memory.
1229 /// (&mut slice[0..mid], &mut slice2[mid..len])
1233 /// // This is how the standard library does it. This is the best method, if
1234 /// // you need to do something like this
1235 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
1236 /// -> (&mut [T], &mut [T]) {
1237 /// let len = slice.len();
1238 /// assert!(mid <= len);
1240 /// let ptr = slice.as_mut_ptr();
1241 /// // This now has three mutable references pointing at the same
1242 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
1243 /// // `slice` is never used after `let ptr = ...`, and so one can
1244 /// // treat it as "dead", and therefore, you only have two real
1245 /// // mutable slices.
1246 /// (slice::from_raw_parts_mut(ptr, mid),
1247 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
1251 #[stable(feature = "rust1", since = "1.0.0")]
1252 #[rustc_allowed_through_unstable_modules]
1253 #[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
1254 #[rustc_diagnostic_item = "transmute"]
1255 pub fn transmute<Src, Dst>(src: Src) -> Dst;
1257 /// Returns `true` if the actual type given as `T` requires drop
1258 /// glue; returns `false` if the actual type provided for `T`
1259 /// implements `Copy`.
1261 /// If the actual type neither requires drop glue nor implements
1262 /// `Copy`, then the return value of this function is unspecified.
1264 /// Note that, unlike most intrinsics, this is safe to call;
1265 /// it does not require an `unsafe` block.
1266 /// Therefore, implementations must not require the user to uphold
1267 /// any safety invariants.
1269 /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1270 #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1271 #[rustc_safe_intrinsic]
1272 pub fn needs_drop<T: ?Sized>() -> bool;
1274 /// Calculates the offset from a pointer.
1276 /// This is implemented as an intrinsic to avoid converting to and from an
1277 /// integer, since the conversion would throw away aliasing information.
1281 /// Both the starting and resulting pointer must be either in bounds or one
1282 /// byte past the end of an allocated object. If either pointer is out of
1283 /// bounds or arithmetic overflow occurs then any further use of the
1284 /// returned value will result in undefined behavior.
1286 /// The stabilized version of this intrinsic is [`pointer::offset`].
1287 #[must_use = "returns a new pointer rather than modifying its argument"]
1288 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1289 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1291 /// Calculates the offset from a pointer, potentially wrapping.
1293 /// This is implemented as an intrinsic to avoid converting to and from an
1294 /// integer, since the conversion inhibits certain optimizations.
1298 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1299 /// resulting pointer to point into or one byte past the end of an allocated
1300 /// object, and it wraps with two's complement arithmetic. The resulting
1301 /// value is not necessarily valid to be used to actually access memory.
1303 /// The stabilized version of this intrinsic is [`pointer::wrapping_offset`].
1304 #[must_use = "returns a new pointer rather than modifying its argument"]
1305 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1306 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1308 /// Masks out bits of the pointer according to a mask.
1310 /// Note that, unlike most intrinsics, this is safe to call;
1311 /// it does not require an `unsafe` block.
1312 /// Therefore, implementations must not require the user to uphold
1313 /// any safety invariants.
1315 /// Consider using [`pointer::mask`] instead.
1316 #[rustc_safe_intrinsic]
1317 pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;
1319 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1320 /// a size of `count` * `size_of::<T>()` and an alignment of
1321 /// `min_align_of::<T>()`
1323 /// The volatile parameter is set to `true`, so it will not be optimized out
1324 /// unless size is equal to zero.
1326 /// This intrinsic does not have a stable counterpart.
1327 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1328 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1329 /// a size of `count * size_of::<T>()` and an alignment of
1330 /// `min_align_of::<T>()`
1332 /// The volatile parameter is set to `true`, so it will not be optimized out
1333 /// unless size is equal to zero.
1335 /// This intrinsic does not have a stable counterpart.
1336 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1337 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1338 /// size of `count * size_of::<T>()` and an alignment of
1339 /// `min_align_of::<T>()`.
1341 /// The volatile parameter is set to `true`, so it will not be optimized out
1342 /// unless size is equal to zero.
1344 /// This intrinsic does not have a stable counterpart.
1345 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1347 /// Performs a volatile load from the `src` pointer.
1349 /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`].
1350 pub fn volatile_load<T>(src: *const T) -> T;
1351 /// Performs a volatile store to the `dst` pointer.
1353 /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`].
1354 pub fn volatile_store<T>(dst: *mut T, val: T);
1356 /// Performs a volatile load from the `src` pointer
1357 /// The pointer is not required to be aligned.
1359 /// This intrinsic does not have a stable counterpart.
1360 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1361 /// Performs a volatile store to the `dst` pointer.
1362 /// The pointer is not required to be aligned.
1364 /// This intrinsic does not have a stable counterpart.
1365 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1367 /// Returns the square root of an `f32`
1369 /// The stabilized version of this intrinsic is
1370 /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1371 pub fn sqrtf32(x: f32) -> f32;
1372 /// Returns the square root of an `f64`
1374 /// The stabilized version of this intrinsic is
1375 /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1376 pub fn sqrtf64(x: f64) -> f64;
1378 /// Raises an `f32` to an integer power.
1380 /// The stabilized version of this intrinsic is
1381 /// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1382 pub fn powif32(a: f32, x: i32) -> f32;
1383 /// Raises an `f64` to an integer power.
1385 /// The stabilized version of this intrinsic is
1386 /// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1387 pub fn powif64(a: f64, x: i32) -> f64;
1389 /// Returns the sine of an `f32`.
1391 /// The stabilized version of this intrinsic is
1392 /// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1393 pub fn sinf32(x: f32) -> f32;
1394 /// Returns the sine of an `f64`.
1396 /// The stabilized version of this intrinsic is
1397 /// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1398 pub fn sinf64(x: f64) -> f64;
1400 /// Returns the cosine of an `f32`.
1402 /// The stabilized version of this intrinsic is
1403 /// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1404 pub fn cosf32(x: f32) -> f32;
1405 /// Returns the cosine of an `f64`.
1407 /// The stabilized version of this intrinsic is
1408 /// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1409 pub fn cosf64(x: f64) -> f64;
1411 /// Raises an `f32` to an `f32` power.
1413 /// The stabilized version of this intrinsic is
1414 /// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1415 pub fn powf32(a: f32, x: f32) -> f32;
1416 /// Raises an `f64` to an `f64` power.
1418 /// The stabilized version of this intrinsic is
1419 /// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1420 pub fn powf64(a: f64, x: f64) -> f64;
1422 /// Returns the exponential of an `f32`.
1424 /// The stabilized version of this intrinsic is
1425 /// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1426 pub fn expf32(x: f32) -> f32;
1427 /// Returns the exponential of an `f64`.
1429 /// The stabilized version of this intrinsic is
1430 /// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1431 pub fn expf64(x: f64) -> f64;
1433 /// Returns 2 raised to the power of an `f32`.
1435 /// The stabilized version of this intrinsic is
1436 /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1437 pub fn exp2f32(x: f32) -> f32;
1438 /// Returns 2 raised to the power of an `f64`.
1440 /// The stabilized version of this intrinsic is
1441 /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1442 pub fn exp2f64(x: f64) -> f64;
1444 /// Returns the natural logarithm of an `f32`.
1446 /// The stabilized version of this intrinsic is
1447 /// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1448 pub fn logf32(x: f32) -> f32;
1449 /// Returns the natural logarithm of an `f64`.
1451 /// The stabilized version of this intrinsic is
1452 /// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1453 pub fn logf64(x: f64) -> f64;
1455 /// Returns the base 10 logarithm of an `f32`.
1457 /// The stabilized version of this intrinsic is
1458 /// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1459 pub fn log10f32(x: f32) -> f32;
1460 /// Returns the base 10 logarithm of an `f64`.
1462 /// The stabilized version of this intrinsic is
1463 /// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1464 pub fn log10f64(x: f64) -> f64;
1466 /// Returns the base 2 logarithm of an `f32`.
1468 /// The stabilized version of this intrinsic is
1469 /// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1470 pub fn log2f32(x: f32) -> f32;
1471 /// Returns the base 2 logarithm of an `f64`.
1473 /// The stabilized version of this intrinsic is
1474 /// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1475 pub fn log2f64(x: f64) -> f64;
1477 /// Returns `a * b + c` for `f32` values.
1479 /// The stabilized version of this intrinsic is
1480 /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1481 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1482 /// Returns `a * b + c` for `f64` values.
1484 /// The stabilized version of this intrinsic is
1485 /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1486 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1488 /// Returns the absolute value of an `f32`.
1490 /// The stabilized version of this intrinsic is
1491 /// [`f32::abs`](../../std/primitive.f32.html#method.abs)
1492 pub fn fabsf32(x: f32) -> f32;
1493 /// Returns the absolute value of an `f64`.
1495 /// The stabilized version of this intrinsic is
1496 /// [`f64::abs`](../../std/primitive.f64.html#method.abs)
1497 pub fn fabsf64(x: f64) -> f64;
1499 /// Returns the minimum of two `f32` values.
1501 /// Note that, unlike most intrinsics, this is safe to call;
1502 /// it does not require an `unsafe` block.
1503 /// Therefore, implementations must not require the user to uphold
1504 /// any safety invariants.
1506 /// The stabilized version of this intrinsic is
1508 #[rustc_safe_intrinsic]
1509 pub fn minnumf32(x: f32, y: f32) -> f32;
1510 /// Returns the minimum of two `f64` values.
1512 /// Note that, unlike most intrinsics, this is safe to call;
1513 /// it does not require an `unsafe` block.
1514 /// Therefore, implementations must not require the user to uphold
1515 /// any safety invariants.
1517 /// The stabilized version of this intrinsic is
1519 #[rustc_safe_intrinsic]
1520 pub fn minnumf64(x: f64, y: f64) -> f64;
1521 /// Returns the maximum of two `f32` values.
1523 /// Note that, unlike most intrinsics, this is safe to call;
1524 /// it does not require an `unsafe` block.
1525 /// Therefore, implementations must not require the user to uphold
1526 /// any safety invariants.
1528 /// The stabilized version of this intrinsic is
1530 #[rustc_safe_intrinsic]
1531 pub fn maxnumf32(x: f32, y: f32) -> f32;
1532 /// Returns the maximum of two `f64` values.
1534 /// Note that, unlike most intrinsics, this is safe to call;
1535 /// it does not require an `unsafe` block.
1536 /// Therefore, implementations must not require the user to uphold
1537 /// any safety invariants.
1539 /// The stabilized version of this intrinsic is
1541 #[rustc_safe_intrinsic]
1542 pub fn maxnumf64(x: f64, y: f64) -> f64;
1544 /// Copies the sign from `y` to `x` for `f32` values.
1546 /// The stabilized version of this intrinsic is
1547 /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
1548 pub fn copysignf32(x: f32, y: f32) -> f32;
1549 /// Copies the sign from `y` to `x` for `f64` values.
1551 /// The stabilized version of this intrinsic is
1552 /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
1553 pub fn copysignf64(x: f64, y: f64) -> f64;
1555 /// Returns the largest integer less than or equal to an `f32`.
1557 /// The stabilized version of this intrinsic is
1558 /// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1559 pub fn floorf32(x: f32) -> f32;
1560 /// Returns the largest integer less than or equal to an `f64`.
1562 /// The stabilized version of this intrinsic is
1563 /// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1564 pub fn floorf64(x: f64) -> f64;
1566 /// Returns the smallest integer greater than or equal to an `f32`.
1568 /// The stabilized version of this intrinsic is
1569 /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1570 pub fn ceilf32(x: f32) -> f32;
1571 /// Returns the smallest integer greater than or equal to an `f64`.
1573 /// The stabilized version of this intrinsic is
1574 /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1575 pub fn ceilf64(x: f64) -> f64;
1577 /// Returns the integer part of an `f32`.
1579 /// The stabilized version of this intrinsic is
1580 /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1581 pub fn truncf32(x: f32) -> f32;
1582 /// Returns the integer part of an `f64`.
1584 /// The stabilized version of this intrinsic is
1585 /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1586 pub fn truncf64(x: f64) -> f64;
1588 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1589 /// if the argument is not an integer.
1590 pub fn rintf32(x: f32) -> f32;
1591 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1592 /// if the argument is not an integer.
1593 pub fn rintf64(x: f64) -> f64;
1595 /// Returns the nearest integer to an `f32`.
1597 /// This intrinsic does not have a stable counterpart.
1598 pub fn nearbyintf32(x: f32) -> f32;
1599 /// Returns the nearest integer to an `f64`.
1601 /// This intrinsic does not have a stable counterpart.
1602 pub fn nearbyintf64(x: f64) -> f64;
1604 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1606 /// The stabilized version of this intrinsic is
1607 /// [`f32::round`](../../std/primitive.f32.html#method.round)
1608 pub fn roundf32(x: f32) -> f32;
1609 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1611 /// The stabilized version of this intrinsic is
1612 /// [`f64::round`](../../std/primitive.f64.html#method.round)
1613 pub fn roundf64(x: f64) -> f64;
1615 /// Float addition that allows optimizations based on algebraic rules.
1616 /// May assume inputs are finite.
1618 /// This intrinsic does not have a stable counterpart.
1619 pub fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1621 /// Float subtraction that allows optimizations based on algebraic rules.
1622 /// May assume inputs are finite.
1624 /// This intrinsic does not have a stable counterpart.
1625 pub fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1627 /// Float multiplication that allows optimizations based on algebraic rules.
1628 /// May assume inputs are finite.
1630 /// This intrinsic does not have a stable counterpart.
1631 pub fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1633 /// Float division that allows optimizations based on algebraic rules.
1634 /// May assume inputs are finite.
1636 /// This intrinsic does not have a stable counterpart.
1637 pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1639 /// Float remainder that allows optimizations based on algebraic rules.
1640 /// May assume inputs are finite.
1642 /// This intrinsic does not have a stable counterpart.
1643 pub fn frem_fast<T: Copy>(a: T, b: T) -> T;
1645 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1646 /// (<https://github.com/rust-lang/rust/issues/10184>)
1648 /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1649 pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
1651 /// Returns the number of bits set in an integer type `T`
1653 /// Note that, unlike most intrinsics, this is safe to call;
1654 /// it does not require an `unsafe` block.
1655 /// Therefore, implementations must not require the user to uphold
1656 /// any safety invariants.
1658 /// The stabilized versions of this intrinsic are available on the integer
1659 /// primitives via the `count_ones` method. For example,
1660 /// [`u32::count_ones`]
1661 #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1662 #[rustc_safe_intrinsic]
1663 pub fn ctpop<T: Copy>(x: T) -> T;
1665 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1667 /// Note that, unlike most intrinsics, this is safe to call;
1668 /// it does not require an `unsafe` block.
1669 /// Therefore, implementations must not require the user to uphold
1670 /// any safety invariants.
1672 /// The stabilized versions of this intrinsic are available on the integer
1673 /// primitives via the `leading_zeros` method. For example,
1674 /// [`u32::leading_zeros`]
1679 /// #![feature(core_intrinsics)]
1681 /// use std::intrinsics::ctlz;
1683 /// let x = 0b0001_1100_u8;
1684 /// let num_leading = ctlz(x);
1685 /// assert_eq!(num_leading, 3);
1688 /// An `x` with value `0` will return the bit width of `T`.
1691 /// #![feature(core_intrinsics)]
1693 /// use std::intrinsics::ctlz;
1696 /// let num_leading = ctlz(x);
1697 /// assert_eq!(num_leading, 16);
1699 #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1700 #[rustc_safe_intrinsic]
1701 pub fn ctlz<T: Copy>(x: T) -> T;
1703 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1704 /// given an `x` with value `0`.
1706 /// This intrinsic does not have a stable counterpart.
1711 /// #![feature(core_intrinsics)]
1713 /// use std::intrinsics::ctlz_nonzero;
1715 /// let x = 0b0001_1100_u8;
1716 /// let num_leading = unsafe { ctlz_nonzero(x) };
1717 /// assert_eq!(num_leading, 3);
1719 #[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
1720 pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
1722 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1724 /// Note that, unlike most intrinsics, this is safe to call;
1725 /// it does not require an `unsafe` block.
1726 /// Therefore, implementations must not require the user to uphold
1727 /// any safety invariants.
1729 /// The stabilized versions of this intrinsic are available on the integer
1730 /// primitives via the `trailing_zeros` method. For example,
1731 /// [`u32::trailing_zeros`]
1736 /// #![feature(core_intrinsics)]
1738 /// use std::intrinsics::cttz;
1740 /// let x = 0b0011_1000_u8;
1741 /// let num_trailing = cttz(x);
1742 /// assert_eq!(num_trailing, 3);
1745 /// An `x` with value `0` will return the bit width of `T`:
1748 /// #![feature(core_intrinsics)]
1750 /// use std::intrinsics::cttz;
1753 /// let num_trailing = cttz(x);
1754 /// assert_eq!(num_trailing, 16);
1756 #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1757 #[rustc_safe_intrinsic]
1758 pub fn cttz<T: Copy>(x: T) -> T;
1760 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1761 /// given an `x` with value `0`.
1763 /// This intrinsic does not have a stable counterpart.
1768 /// #![feature(core_intrinsics)]
1770 /// use std::intrinsics::cttz_nonzero;
1772 /// let x = 0b0011_1000_u8;
1773 /// let num_trailing = unsafe { cttz_nonzero(x) };
1774 /// assert_eq!(num_trailing, 3);
1776 #[rustc_const_stable(feature = "const_cttz_nonzero", since = "1.53.0")]
1777 pub fn cttz_nonzero<T: Copy>(x: T) -> T;
1779 /// Reverses the bytes in an integer type `T`.
1781 /// Note that, unlike most intrinsics, this is safe to call;
1782 /// it does not require an `unsafe` block.
1783 /// Therefore, implementations must not require the user to uphold
1784 /// any safety invariants.
1786 /// The stabilized versions of this intrinsic are available on the integer
1787 /// primitives via the `swap_bytes` method. For example,
1788 /// [`u32::swap_bytes`]
1789 #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1790 #[rustc_safe_intrinsic]
1791 pub fn bswap<T: Copy>(x: T) -> T;
1793 /// Reverses the bits in an integer type `T`.
1795 /// Note that, unlike most intrinsics, this is safe to call;
1796 /// it does not require an `unsafe` block.
1797 /// Therefore, implementations must not require the user to uphold
1798 /// any safety invariants.
1800 /// The stabilized versions of this intrinsic are available on the integer
1801 /// primitives via the `reverse_bits` method. For example,
1802 /// [`u32::reverse_bits`]
1803 #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1804 #[rustc_safe_intrinsic]
1805 pub fn bitreverse<T: Copy>(x: T) -> T;
1807 /// Performs checked integer addition.
1809 /// Note that, unlike most intrinsics, this is safe to call;
1810 /// it does not require an `unsafe` block.
1811 /// Therefore, implementations must not require the user to uphold
1812 /// any safety invariants.
1814 /// The stabilized versions of this intrinsic are available on the integer
1815 /// primitives via the `overflowing_add` method. For example,
1816 /// [`u32::overflowing_add`]
1817 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1818 #[rustc_safe_intrinsic]
1819 pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1821 /// Performs checked integer subtraction
1823 /// Note that, unlike most intrinsics, this is safe to call;
1824 /// it does not require an `unsafe` block.
1825 /// Therefore, implementations must not require the user to uphold
1826 /// any safety invariants.
1828 /// The stabilized versions of this intrinsic are available on the integer
1829 /// primitives via the `overflowing_sub` method. For example,
1830 /// [`u32::overflowing_sub`]
1831 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1832 #[rustc_safe_intrinsic]
1833 pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1835 /// Performs checked integer multiplication
1837 /// Note that, unlike most intrinsics, this is safe to call;
1838 /// it does not require an `unsafe` block.
1839 /// Therefore, implementations must not require the user to uphold
1840 /// any safety invariants.
1842 /// The stabilized versions of this intrinsic are available on the integer
1843 /// primitives via the `overflowing_mul` method. For example,
1844 /// [`u32::overflowing_mul`]
1845 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1846 #[rustc_safe_intrinsic]
1847 pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1849 /// Performs an exact division, resulting in undefined behavior where
1850 /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1852 /// This intrinsic does not have a stable counterpart.
1853 #[rustc_const_unstable(feature = "const_exact_div", issue = "none")]
1854 pub fn exact_div<T: Copy>(x: T, y: T) -> T;
1856 /// Performs an unchecked division, resulting in undefined behavior
1857 /// where `y == 0` or `x == T::MIN && y == -1`
1859 /// Safe wrappers for this intrinsic are available on the integer
1860 /// primitives via the `checked_div` method. For example,
1861 /// [`u32::checked_div`]
1862 #[rustc_const_stable(feature = "const_int_unchecked_div", since = "1.52.0")]
1863 pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1864 /// Returns the remainder of an unchecked division, resulting in
1865 /// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
1867 /// Safe wrappers for this intrinsic are available on the integer
1868 /// primitives via the `checked_rem` method. For example,
1869 /// [`u32::checked_rem`]
1870 #[rustc_const_stable(feature = "const_int_unchecked_rem", since = "1.52.0")]
1871 pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1873 /// Performs an unchecked left shift, resulting in undefined behavior when
1874 /// `y < 0` or `y >= N`, where N is the width of T in bits.
1876 /// Safe wrappers for this intrinsic are available on the integer
1877 /// primitives via the `checked_shl` method. For example,
1878 /// [`u32::checked_shl`]
1879 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1880 pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
1881 /// Performs an unchecked right shift, resulting in undefined behavior when
1882 /// `y < 0` or `y >= N`, where N is the width of T in bits.
1884 /// Safe wrappers for this intrinsic are available on the integer
1885 /// primitives via the `checked_shr` method. For example,
1886 /// [`u32::checked_shr`]
1887 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1888 pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
1890 /// Returns the result of an unchecked addition, resulting in
1891 /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1893 /// This intrinsic does not have a stable counterpart.
1894 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1895 pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1897 /// Returns the result of an unchecked subtraction, resulting in
1898 /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1900 /// This intrinsic does not have a stable counterpart.
1901 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1902 pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1904 /// Returns the result of an unchecked multiplication, resulting in
1905 /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1907 /// This intrinsic does not have a stable counterpart.
1908 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1909 pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1911 /// Performs rotate left.
1913 /// Note that, unlike most intrinsics, this is safe to call;
1914 /// it does not require an `unsafe` block.
1915 /// Therefore, implementations must not require the user to uphold
1916 /// any safety invariants.
1918 /// The stabilized versions of this intrinsic are available on the integer
1919 /// primitives via the `rotate_left` method. For example,
1920 /// [`u32::rotate_left`]
1921 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1922 #[rustc_safe_intrinsic]
1923 pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
1925 /// Performs rotate right.
1927 /// Note that, unlike most intrinsics, this is safe to call;
1928 /// it does not require an `unsafe` block.
1929 /// Therefore, implementations must not require the user to uphold
1930 /// any safety invariants.
1932 /// The stabilized versions of this intrinsic are available on the integer
1933 /// primitives via the `rotate_right` method. For example,
1934 /// [`u32::rotate_right`]
1935 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1936 #[rustc_safe_intrinsic]
1937 pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
1939 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1941 /// Note that, unlike most intrinsics, this is safe to call;
1942 /// it does not require an `unsafe` block.
1943 /// Therefore, implementations must not require the user to uphold
1944 /// any safety invariants.
1946 /// The stabilized versions of this intrinsic are available on the integer
1947 /// primitives via the `wrapping_add` method. For example,
1948 /// [`u32::wrapping_add`]
1949 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1950 #[rustc_safe_intrinsic]
1951 pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
1952 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1954 /// Note that, unlike most intrinsics, this is safe to call;
1955 /// it does not require an `unsafe` block.
1956 /// Therefore, implementations must not require the user to uphold
1957 /// any safety invariants.
1959 /// The stabilized versions of this intrinsic are available on the integer
1960 /// primitives via the `wrapping_sub` method. For example,
1961 /// [`u32::wrapping_sub`]
1962 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1963 #[rustc_safe_intrinsic]
1964 pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
1965 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1967 /// Note that, unlike most intrinsics, this is safe to call;
1968 /// it does not require an `unsafe` block.
1969 /// Therefore, implementations must not require the user to uphold
1970 /// any safety invariants.
1972 /// The stabilized versions of this intrinsic are available on the integer
1973 /// primitives via the `wrapping_mul` method. For example,
1974 /// [`u32::wrapping_mul`]
1975 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1976 #[rustc_safe_intrinsic]
1977 pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
1979 /// Computes `a + b`, saturating at numeric bounds.
1981 /// Note that, unlike most intrinsics, this is safe to call;
1982 /// it does not require an `unsafe` block.
1983 /// Therefore, implementations must not require the user to uphold
1984 /// any safety invariants.
1986 /// The stabilized versions of this intrinsic are available on the integer
1987 /// primitives via the `saturating_add` method. For example,
1988 /// [`u32::saturating_add`]
1989 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1990 #[rustc_safe_intrinsic]
1991 pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
1992 /// Computes `a - b`, saturating at numeric bounds.
1994 /// Note that, unlike most intrinsics, this is safe to call;
1995 /// it does not require an `unsafe` block.
1996 /// Therefore, implementations must not require the user to uphold
1997 /// any safety invariants.
1999 /// The stabilized versions of this intrinsic are available on the integer
2000 /// primitives via the `saturating_sub` method. For example,
2001 /// [`u32::saturating_sub`]
2002 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
2003 #[rustc_safe_intrinsic]
2004 pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
2006 /// Returns the value of the discriminant for the variant in 'v';
2007 /// if `T` has no discriminant, returns `0`.
2009 /// Note that, unlike most intrinsics, this is safe to call;
2010 /// it does not require an `unsafe` block.
2011 /// Therefore, implementations must not require the user to uphold
2012 /// any safety invariants.
2014 /// The stabilized version of this intrinsic is [`core::mem::discriminant`].
2015 #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
2016 #[rustc_safe_intrinsic]
2017 pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
2019 /// Returns the number of variants of the type `T` cast to a `usize`;
2020 /// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
2022 /// Note that, unlike most intrinsics, this is safe to call;
2023 /// it does not require an `unsafe` block.
2024 /// Therefore, implementations must not require the user to uphold
2025 /// any safety invariants.
2027 /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
2028 #[rustc_const_unstable(feature = "variant_count", issue = "73662")]
2029 #[rustc_safe_intrinsic]
2030 pub fn variant_count<T>() -> usize;
2032 /// Rust's "try catch" construct which invokes the function pointer `try_fn`
2033 /// with the data pointer `data`.
2035 /// The third argument is a function called if a panic occurs. This function
2036 /// takes the data pointer and a pointer to the target-specific exception
2037 /// object that was caught. For more information see the compiler's
2038 /// source as well as std's catch implementation.
2039 pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
2041 /// Emits a `!nontemporal` store according to LLVM (see their docs).
2042 /// Probably will never become stable.
2043 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
2045 /// See documentation of `<*const T>::offset_from` for details.
2046 #[rustc_const_stable(feature = "const_ptr_offset_from", since = "1.65.0")]
2047 pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
2049 /// See documentation of `<*const T>::sub_ptr` for details.
2050 #[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
2051 pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
2053 /// See documentation of `<*const T>::guaranteed_eq` for details.
2054 /// Returns `2` if the result is unknown.
2055 /// Returns `1` if the pointers are guaranteed equal
2056 /// Returns `0` if the pointers are guaranteed inequal
2058 /// Note that, unlike most intrinsics, this is safe to call;
2059 /// it does not require an `unsafe` block.
2060 /// Therefore, implementations must not require the user to uphold
2061 /// any safety invariants.
2062 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
2063 #[rustc_safe_intrinsic]
2064 pub fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8;
2066 /// Allocates a block of memory at compile time.
2067 /// At runtime, just returns a null pointer.
2071 /// - The `align` argument must be a power of two.
2072 /// - At compile time, a compile error occurs if this constraint is violated.
2073 /// - At runtime, it is not checked.
2074 #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2075 pub fn const_allocate(size: usize, align: usize) -> *mut u8;
2077 /// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
2078 /// At runtime, does nothing.
2082 /// - The `align` argument must be a power of two.
2083 /// - At compile time, a compile error occurs if this constraint is violated.
2084 /// - At runtime, it is not checked.
2085 /// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
2086 /// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
2087 #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2088 pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);
2090 /// Determines whether the raw bytes of the two values are equal.
2092 /// This is particularly handy for arrays, since it allows things like just
2093 /// comparing `i96`s instead of forcing `alloca`s for `[6 x i16]`.
2095 /// Above some backend-decided threshold this will emit calls to `memcmp`,
2096 /// like slice equality does, instead of causing massive code size.
2100 /// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized or carry a
2102 /// Note that this is a stricter criterion than just the *values* being
2103 /// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
2105 /// (The implementation is allowed to branch on the results of comparisons,
2106 /// which is UB if any of their inputs are `undef`.)
2107 #[rustc_const_unstable(feature = "const_intrinsic_raw_eq", issue = "none")]
2108 pub fn raw_eq<T>(a: &T, b: &T) -> bool;
2110 /// See documentation of [`std::hint::black_box`] for details.
2112 /// [`std::hint::black_box`]: crate::hint::black_box
2113 #[rustc_const_unstable(feature = "const_black_box", issue = "none")]
2114 #[rustc_safe_intrinsic]
2115 pub fn black_box<T>(dummy: T) -> T;
2117 /// `ptr` must point to a vtable.
2118 /// The intrinsic will return the size stored in that vtable.
2119 pub fn vtable_size(ptr: *const ()) -> usize;
2121 /// `ptr` must point to a vtable.
2122 /// The intrinsic will return the alignment stored in that vtable.
2123 pub fn vtable_align(ptr: *const ()) -> usize;
2125 /// Selects which function to call depending on the context.
2127 /// If this function is evaluated at compile-time, then a call to this
2128 /// intrinsic will be replaced with a call to `called_in_const`. It gets
2129 /// replaced with a call to `called_at_rt` otherwise.
2131 /// # Type Requirements
2133 /// The two functions must be both function items. They cannot be function
2134 /// pointers or closures. The first function must be a `const fn`.
2136 /// `arg` will be the tupled arguments that will be passed to either one of
2137 /// the two functions, therefore, both functions must accept the same type of
2138 /// arguments. Both functions must return RET.
2142 /// The two functions must behave observably equivalent. Safe code in other
2143 /// crates may assume that calling a `const fn` at compile-time and at run-time
2144 /// produces the same result. A function that produces a different result when
2145 /// evaluated at run-time, or has any other observable side-effects, is
2148 /// Here is an example of how this could cause a problem:
2150 /// #![feature(const_eval_select)]
2151 /// #![feature(core_intrinsics)]
2152 /// use std::hint::unreachable_unchecked;
2153 /// use std::intrinsics::const_eval_select;
2156 /// pub const fn inconsistent() -> i32 {
2157 /// fn runtime() -> i32 { 1 }
2158 /// const fn compiletime() -> i32 { 2 }
2161 // // ⚠ This code violates the required equivalence of `compiletime`
2162 /// // and `runtime`.
2163 /// const_eval_select((), compiletime, runtime)
2168 /// const X: i32 = inconsistent();
2169 /// let x = inconsistent();
2170 /// if x != X { unsafe { unreachable_unchecked(); }}
2173 /// This code causes Undefined Behavior when being run, since the
2174 /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2175 /// which violates the principle that a `const fn` must behave the same at
2176 /// compile-time and at run-time. The unsafe code in crate B is fine.
2177 #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2178 pub fn const_eval_select<ARG: Tuple, F, G, RET>(
2184 G: FnOnce<ARG, Output = RET>,
2185 F: FnOnce<ARG, Output = RET>;
2188 // Some functions are defined here because they accidentally got made
2189 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
2190 // (`transmute` also falls into this category, but it cannot be wrapped due to the
2191 // check that `T` and `U` have the same size.)
2193 /// Check that the preconditions of an unsafe function are followed, if debug_assertions are on,
2194 /// and only at runtime.
2196 /// This macro should be called as `assert_unsafe_precondition!([Generics](name: Type) => Expression)`
2197 /// where the names specified will be moved into the macro as captured variables, and defines an item
2198 /// to call `const_eval_select` on. The tokens inside the square brackets are used to denote generics
2199 /// for the function declaractions and can be omitted if there is no generics.
2203 /// Invoking this macro is only sound if the following code is already UB when the passed
2204 /// expression evaluates to false.
2206 /// This macro expands to a check at runtime if debug_assertions is set. It has no effect at
2207 /// compile time, but the semantics of the contained `const_eval_select` must be the same at
2208 /// runtime and at compile time. Thus if the expression evaluates to false, this macro produces
2209 /// different behavior at compile time and at runtime, and invoking it is incorrect.
2211 /// So in a sense it is UB if this macro is useful, but we expect callers of `unsafe fn` to make
2212 /// the occasional mistake, and this check should help them figure things out.
2213 #[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn
2214 macro_rules! assert_unsafe_precondition {
2215 ($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr) => {
2216 if cfg!(debug_assertions) {
2217 // allow non_snake_case to allow capturing const generics
2218 #[allow(non_snake_case)]
2220 fn runtime$(<$($tt)*>)?($($i:$ty),*) {
2222 // don't unwind to reduce impact on code size
2223 ::core::panicking::panic_nounwind(
2224 concat!("unsafe precondition(s) violated: ", $name)
2228 #[allow(non_snake_case)]
2229 const fn comptime$(<$($tt)*>)?($(_:$ty),*) {}
2231 ::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
2235 pub(crate) use assert_unsafe_precondition;
2237 /// Checks whether `ptr` is properly aligned with respect to
2238 /// `align_of::<T>()`.
2239 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
2240 !ptr.is_null() && ptr.is_aligned()
2243 /// Checks whether an allocation of `len` instances of `T` exceeds
2244 /// the maximum allowed allocation size.
2245 pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool {
2246 let max_len = const {
2247 let size = crate::mem::size_of::<T>();
2248 if size == 0 { usize::MAX } else { isize::MAX as usize / size }
2253 /// Checks whether the regions of memory starting at `src` and `dst` of size
2254 /// `count * size_of::<T>()` do *not* overlap.
2255 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
2256 let src_usize = src.addr();
2257 let dst_usize = dst.addr();
2258 let size = mem::size_of::<T>().checked_mul(count).unwrap();
2259 let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
2260 // If the absolute distance between the ptrs is at least as big as the size of the buffer,
2261 // they do not overlap.
2265 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2266 /// and destination must *not* overlap.
2268 /// For regions of memory which might overlap, use [`copy`] instead.
2270 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
2271 /// with the argument order swapped.
2273 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2274 /// requirements of `T`. The initialization state is preserved exactly.
2276 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
2280 /// Behavior is undefined if any of the following conditions are violated:
2282 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2284 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2286 /// * Both `src` and `dst` must be properly aligned.
2288 /// * The region of memory beginning at `src` with a size of `count *
2289 /// size_of::<T>()` bytes must *not* overlap with the region of memory
2290 /// beginning at `dst` with the same size.
2292 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
2293 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
2294 /// in the region beginning at `*src` and the region beginning at `*dst` can
2295 /// [violate memory safety][read-ownership].
2297 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2298 /// `0`, the pointers must be non-null and properly aligned.
2300 /// [`read`]: crate::ptr::read
2301 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2302 /// [valid]: crate::ptr#safety
2306 /// Manually implement [`Vec::append`]:
2311 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
2312 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
2313 /// let src_len = src.len();
2314 /// let dst_len = dst.len();
2316 /// // Ensure that `dst` has enough capacity to hold all of `src`.
2317 /// dst.reserve(src_len);
2320 /// // The call to add is always safe because `Vec` will never
2321 /// // allocate more than `isize::MAX` bytes.
2322 /// let dst_ptr = dst.as_mut_ptr().add(dst_len);
2323 /// let src_ptr = src.as_ptr();
2325 /// // Truncate `src` without dropping its contents. We do this first,
2326 /// // to avoid problems in case something further down panics.
2329 /// // The two regions cannot overlap because mutable references do
2330 /// // not alias, and two different vectors cannot own the same
2332 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
2334 /// // Notify `dst` that it now holds the contents of `src`.
2335 /// dst.set_len(dst_len + src_len);
2339 /// let mut a = vec!['r'];
2340 /// let mut b = vec!['u', 's', 't'];
2342 /// append(&mut a, &mut b);
2344 /// assert_eq!(a, &['r', 'u', 's', 't']);
2345 /// assert!(b.is_empty());
2348 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
2349 #[doc(alias = "memcpy")]
2350 #[stable(feature = "rust1", since = "1.0.0")]
2351 #[rustc_allowed_through_unstable_modules]
2352 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2354 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2355 pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
2356 extern "rust-intrinsic" {
2357 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2358 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
2361 // SAFETY: the safety contract for `copy_nonoverlapping` must be
2362 // upheld by the caller.
2364 assert_unsafe_precondition!(
2365 "ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
2366 and the specified memory ranges do not overlap",
2367 [T](src: *const T, dst: *mut T, count: usize) =>
2368 is_aligned_and_not_null(src)
2369 && is_aligned_and_not_null(dst)
2370 && is_nonoverlapping(src, dst, count)
2372 copy_nonoverlapping(src, dst, count)
2376 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2377 /// and destination may overlap.
2379 /// If the source and destination will *never* overlap,
2380 /// [`copy_nonoverlapping`] can be used instead.
2382 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
2383 /// order swapped. Copying takes place as if the bytes were copied from `src`
2384 /// to a temporary array and then copied from the array to `dst`.
2386 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2387 /// requirements of `T`. The initialization state is preserved exactly.
2389 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
2393 /// Behavior is undefined if any of the following conditions are violated:
2395 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2397 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2399 /// * Both `src` and `dst` must be properly aligned.
2401 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
2402 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
2403 /// in the region beginning at `*src` and the region beginning at `*dst` can
2404 /// [violate memory safety][read-ownership].
2406 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2407 /// `0`, the pointers must be non-null and properly aligned.
2409 /// [`read`]: crate::ptr::read
2410 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2411 /// [valid]: crate::ptr#safety
2415 /// Efficiently create a Rust vector from an unsafe buffer:
2422 /// /// * `ptr` must be correctly aligned for its type and non-zero.
2423 /// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
2424 /// /// * Those elements must not be used after calling this function unless `T: Copy`.
2425 /// # #[allow(dead_code)]
2426 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
2427 /// let mut dst = Vec::with_capacity(elts);
2429 /// // SAFETY: Our precondition ensures the source is aligned and valid,
2430 /// // and `Vec::with_capacity` ensures that we have usable space to write them.
2431 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
2433 /// // SAFETY: We created it with this much capacity earlier,
2434 /// // and the previous `copy` has initialized these elements.
2435 /// dst.set_len(elts);
2439 #[doc(alias = "memmove")]
2440 #[stable(feature = "rust1", since = "1.0.0")]
2441 #[rustc_allowed_through_unstable_modules]
2442 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2444 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2445 pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
2446 extern "rust-intrinsic" {
2447 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2448 fn copy<T>(src: *const T, dst: *mut T, count: usize);
2451 // SAFETY: the safety contract for `copy` must be upheld by the caller.
2453 assert_unsafe_precondition!(
2454 "ptr::copy requires that both pointer arguments are aligned aligned and non-null",
2455 [T](src: *const T, dst: *mut T) =>
2456 is_aligned_and_not_null(src) && is_aligned_and_not_null(dst)
2458 copy(src, dst, count)
2462 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
2465 /// `write_bytes` is similar to C's [`memset`], but sets `count *
2466 /// size_of::<T>()` bytes to `val`.
2468 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
2472 /// Behavior is undefined if any of the following conditions are violated:
2474 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2476 /// * `dst` must be properly aligned.
2478 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2479 /// `0`, the pointer must be non-null and properly aligned.
2481 /// Additionally, note that changing `*dst` in this way can easily lead to undefined behavior (UB)
2482 /// later if the written bytes are not a valid representation of some `T`. For instance, the
2483 /// following is an **incorrect** use of this function:
2487 /// let mut value: u8 = 0;
2488 /// let ptr: *mut bool = &mut value as *mut u8 as *mut bool;
2489 /// let _bool = ptr.read(); // This is fine, `ptr` points to a valid `bool`.
2490 /// ptr.write_bytes(42u8, 1); // This function itself does not cause UB...
2491 /// let _bool = ptr.read(); // ...but it makes this operation UB! ⚠️
2495 /// [valid]: crate::ptr#safety
2504 /// let mut vec = vec![0u32; 4];
2506 /// let vec_ptr = vec.as_mut_ptr();
2507 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
2509 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
2511 #[doc(alias = "memset")]
2512 #[stable(feature = "rust1", since = "1.0.0")]
2513 #[rustc_allowed_through_unstable_modules]
2514 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2516 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2517 pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
2518 extern "rust-intrinsic" {
2519 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2520 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
2523 // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
2525 assert_unsafe_precondition!(
2526 "ptr::write_bytes requires that the destination pointer is aligned and non-null",
2527 [T](dst: *mut T) => is_aligned_and_not_null(dst)
2529 write_bytes(dst, val, count)