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)]
58 use crate::marker::Destruct;
59 use crate::marker::DiscriminantKind;
62 // These imports are used for simplifying intra-doc links
63 #[allow(unused_imports)]
64 #[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))]
65 use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering};
67 #[stable(feature = "drop_in_place", since = "1.8.0")]
68 #[rustc_allowed_through_unstable_modules]
69 #[deprecated(note = "no longer an intrinsic - use `ptr::drop_in_place` directly", since = "1.52.0")]
71 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
72 // SAFETY: see `ptr::drop_in_place`
73 unsafe { crate::ptr::drop_in_place(to_drop) }
76 extern "rust-intrinsic" {
77 // N.B., these intrinsics take raw pointers because they mutate aliased
78 // memory, which is not valid for either `&` or `&mut`.
80 /// Stores a value if the current value is the same as the `old` value.
82 /// The stabilized version of this intrinsic is available on the
83 /// [`atomic`] types via the `compare_exchange` method by passing
84 /// [`Ordering::Relaxed`] as both the success and failure parameters.
85 /// For example, [`AtomicBool::compare_exchange`].
86 pub fn atomic_cxchg_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
87 /// Stores a value if the current value is the same as the `old` value.
89 /// The stabilized version of this intrinsic is available on the
90 /// [`atomic`] types via the `compare_exchange` method by passing
91 /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
92 /// For example, [`AtomicBool::compare_exchange`].
93 pub fn atomic_cxchg_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
94 /// Stores a value if the current value is the same as the `old` value.
96 /// The stabilized version of this intrinsic is available on the
97 /// [`atomic`] types via the `compare_exchange` method by passing
98 /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
99 /// For example, [`AtomicBool::compare_exchange`].
100 pub fn atomic_cxchg_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
101 /// Stores a value if the current value is the same as the `old` value.
103 /// The stabilized version of this intrinsic is available on the
104 /// [`atomic`] types via the `compare_exchange` method by passing
105 /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
106 /// For example, [`AtomicBool::compare_exchange`].
107 pub fn atomic_cxchg_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
108 /// Stores a value if the current value is the same as the `old` value.
110 /// The stabilized version of this intrinsic is available on the
111 /// [`atomic`] types via the `compare_exchange` method by passing
112 /// [`Ordering::Acquire`] as both the success and failure parameters.
113 /// For example, [`AtomicBool::compare_exchange`].
114 pub fn atomic_cxchg_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
115 /// Stores a value if the current value is the same as the `old` value.
117 /// The stabilized version of this intrinsic is available on the
118 /// [`atomic`] types via the `compare_exchange` method by passing
119 /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
120 /// For example, [`AtomicBool::compare_exchange`].
121 pub fn atomic_cxchg_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
122 /// Stores a value if the current value is the same as the `old` value.
124 /// The stabilized version of this intrinsic is available on the
125 /// [`atomic`] types via the `compare_exchange` method by passing
126 /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
127 /// For example, [`AtomicBool::compare_exchange`].
128 pub fn atomic_cxchg_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
129 /// Stores a value if the current value is the same as the `old` value.
131 /// The stabilized version of this intrinsic is available on the
132 /// [`atomic`] types via the `compare_exchange` method by passing
133 /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
134 /// For example, [`AtomicBool::compare_exchange`].
135 pub fn atomic_cxchg_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
136 /// Stores a value if the current value is the same as the `old` value.
138 /// The stabilized version of this intrinsic is available on the
139 /// [`atomic`] types via the `compare_exchange` method by passing
140 /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
141 /// For example, [`AtomicBool::compare_exchange`].
142 pub fn atomic_cxchg_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
143 /// Stores a value if the current value is the same as the `old` value.
145 /// The stabilized version of this intrinsic is available on the
146 /// [`atomic`] types via the `compare_exchange` method by passing
147 /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
148 /// For example, [`AtomicBool::compare_exchange`].
149 pub fn atomic_cxchg_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
150 /// Stores a value if the current value is the same as the `old` value.
152 /// The stabilized version of this intrinsic is available on the
153 /// [`atomic`] types via the `compare_exchange` method by passing
154 /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
155 /// For example, [`AtomicBool::compare_exchange`].
156 pub fn atomic_cxchg_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
157 /// Stores a value if the current value is the same as the `old` value.
159 /// The stabilized version of this intrinsic is available on the
160 /// [`atomic`] types via the `compare_exchange` method by passing
161 /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
162 /// For example, [`AtomicBool::compare_exchange`].
163 pub fn atomic_cxchg_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
164 /// Stores a value if the current value is the same as the `old` value.
166 /// The stabilized version of this intrinsic is available on the
167 /// [`atomic`] types via the `compare_exchange` method by passing
168 /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
169 /// For example, [`AtomicBool::compare_exchange`].
170 pub fn atomic_cxchg_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
171 /// Stores a value if the current value is the same as the `old` value.
173 /// The stabilized version of this intrinsic is available on the
174 /// [`atomic`] types via the `compare_exchange` method by passing
175 /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
176 /// For example, [`AtomicBool::compare_exchange`].
177 pub fn atomic_cxchg_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
178 /// Stores a value if the current value is the same as the `old` value.
180 /// The stabilized version of this intrinsic is available on the
181 /// [`atomic`] types via the `compare_exchange` method by passing
182 /// [`Ordering::SeqCst`] as both the success and failure parameters.
183 /// For example, [`AtomicBool::compare_exchange`].
184 pub fn atomic_cxchg_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
186 /// Stores a value if the current value is the same as the `old` value.
188 /// The stabilized version of this intrinsic is available on the
189 /// [`atomic`] types via the `compare_exchange_weak` method by passing
190 /// [`Ordering::Relaxed`] as both the success and failure parameters.
191 /// For example, [`AtomicBool::compare_exchange_weak`].
192 pub fn atomic_cxchgweak_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
193 /// Stores a value if the current value is the same as the `old` value.
195 /// The stabilized version of this intrinsic is available on the
196 /// [`atomic`] types via the `compare_exchange_weak` method by passing
197 /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
198 /// For example, [`AtomicBool::compare_exchange_weak`].
199 pub fn atomic_cxchgweak_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
200 /// Stores a value if the current value is the same as the `old` value.
202 /// The stabilized version of this intrinsic is available on the
203 /// [`atomic`] types via the `compare_exchange_weak` method by passing
204 /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
205 /// For example, [`AtomicBool::compare_exchange_weak`].
206 pub fn atomic_cxchgweak_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
207 /// Stores a value if the current value is the same as the `old` value.
209 /// The stabilized version of this intrinsic is available on the
210 /// [`atomic`] types via the `compare_exchange_weak` method by passing
211 /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
212 /// For example, [`AtomicBool::compare_exchange_weak`].
213 pub fn atomic_cxchgweak_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
214 /// Stores a value if the current value is the same as the `old` value.
216 /// The stabilized version of this intrinsic is available on the
217 /// [`atomic`] types via the `compare_exchange_weak` method by passing
218 /// [`Ordering::Acquire`] as both the success and failure parameters.
219 /// For example, [`AtomicBool::compare_exchange_weak`].
220 pub fn atomic_cxchgweak_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
221 /// Stores a value if the current value is the same as the `old` value.
223 /// The stabilized version of this intrinsic is available on the
224 /// [`atomic`] types via the `compare_exchange_weak` method by passing
225 /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
226 /// For example, [`AtomicBool::compare_exchange_weak`].
227 pub fn atomic_cxchgweak_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
228 /// Stores a value if the current value is the same as the `old` value.
230 /// The stabilized version of this intrinsic is available on the
231 /// [`atomic`] types via the `compare_exchange_weak` method by passing
232 /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
233 /// For example, [`AtomicBool::compare_exchange_weak`].
234 pub fn atomic_cxchgweak_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
235 /// Stores a value if the current value is the same as the `old` value.
237 /// The stabilized version of this intrinsic is available on the
238 /// [`atomic`] types via the `compare_exchange_weak` method by passing
239 /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
240 /// For example, [`AtomicBool::compare_exchange_weak`].
241 pub fn atomic_cxchgweak_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
242 /// Stores a value if the current value is the same as the `old` value.
244 /// The stabilized version of this intrinsic is available on the
245 /// [`atomic`] types via the `compare_exchange_weak` method by passing
246 /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
247 /// For example, [`AtomicBool::compare_exchange_weak`].
248 pub fn atomic_cxchgweak_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
249 /// Stores a value if the current value is the same as the `old` value.
251 /// The stabilized version of this intrinsic is available on the
252 /// [`atomic`] types via the `compare_exchange_weak` method by passing
253 /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
254 /// For example, [`AtomicBool::compare_exchange_weak`].
255 pub fn atomic_cxchgweak_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
256 /// Stores a value if the current value is the same as the `old` value.
258 /// The stabilized version of this intrinsic is available on the
259 /// [`atomic`] types via the `compare_exchange_weak` method by passing
260 /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
261 /// For example, [`AtomicBool::compare_exchange_weak`].
262 pub fn atomic_cxchgweak_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
263 /// Stores a value if the current value is the same as the `old` value.
265 /// The stabilized version of this intrinsic is available on the
266 /// [`atomic`] types via the `compare_exchange_weak` method by passing
267 /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
268 /// For example, [`AtomicBool::compare_exchange_weak`].
269 pub fn atomic_cxchgweak_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
270 /// Stores a value if the current value is the same as the `old` value.
272 /// The stabilized version of this intrinsic is available on the
273 /// [`atomic`] types via the `compare_exchange_weak` method by passing
274 /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
275 /// For example, [`AtomicBool::compare_exchange_weak`].
276 pub fn atomic_cxchgweak_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
277 /// Stores a value if the current value is the same as the `old` value.
279 /// The stabilized version of this intrinsic is available on the
280 /// [`atomic`] types via the `compare_exchange_weak` method by passing
281 /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
282 /// For example, [`AtomicBool::compare_exchange_weak`].
283 pub fn atomic_cxchgweak_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
284 /// Stores a value if the current value is the same as the `old` value.
286 /// The stabilized version of this intrinsic is available on the
287 /// [`atomic`] types via the `compare_exchange_weak` method by passing
288 /// [`Ordering::SeqCst`] as both the success and failure parameters.
289 /// For example, [`AtomicBool::compare_exchange_weak`].
290 pub fn atomic_cxchgweak_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
292 /// Loads the current value of the pointer.
294 /// The stabilized version of this intrinsic is available on the
295 /// [`atomic`] types via the `load` method by passing
296 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`].
297 pub fn atomic_load_seqcst<T: Copy>(src: *const T) -> T;
298 /// Loads the current value of the pointer.
300 /// The stabilized version of this intrinsic is available on the
301 /// [`atomic`] types via the `load` method by passing
302 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`].
303 pub fn atomic_load_acquire<T: Copy>(src: *const T) -> T;
304 /// Loads the current value of the pointer.
306 /// The stabilized version of this intrinsic is available on the
307 /// [`atomic`] types via the `load` method by passing
308 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`].
309 pub fn atomic_load_relaxed<T: Copy>(src: *const T) -> T;
310 pub fn atomic_load_unordered<T: Copy>(src: *const T) -> T;
312 /// Stores the value at the specified memory location.
314 /// The stabilized version of this intrinsic is available on the
315 /// [`atomic`] types via the `store` method by passing
316 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`].
317 pub fn atomic_store_seqcst<T: Copy>(dst: *mut T, val: T);
318 /// Stores the value at the specified memory location.
320 /// The stabilized version of this intrinsic is available on the
321 /// [`atomic`] types via the `store` method by passing
322 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`].
323 pub fn atomic_store_release<T: Copy>(dst: *mut T, val: T);
324 /// Stores the value at the specified memory location.
326 /// The stabilized version of this intrinsic is available on the
327 /// [`atomic`] types via the `store` method by passing
328 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`].
329 pub fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T);
330 pub fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T);
332 /// Stores the value at the specified memory location, returning the old value.
334 /// The stabilized version of this intrinsic is available on the
335 /// [`atomic`] types via the `swap` method by passing
336 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`].
337 pub fn atomic_xchg_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
338 /// Stores the value at the specified memory location, returning the old value.
340 /// The stabilized version of this intrinsic is available on the
341 /// [`atomic`] types via the `swap` method by passing
342 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`].
343 pub fn atomic_xchg_acquire<T: Copy>(dst: *mut T, src: T) -> T;
344 /// Stores the value at the specified memory location, returning the old value.
346 /// The stabilized version of this intrinsic is available on the
347 /// [`atomic`] types via the `swap` method by passing
348 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`].
349 pub fn atomic_xchg_release<T: Copy>(dst: *mut T, src: T) -> T;
350 /// Stores the value at the specified memory location, returning the old value.
352 /// The stabilized version of this intrinsic is available on the
353 /// [`atomic`] types via the `swap` method by passing
354 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`].
355 pub fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
356 /// Stores the value at the specified memory location, returning the old value.
358 /// The stabilized version of this intrinsic is available on the
359 /// [`atomic`] types via the `swap` method by passing
360 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`].
361 pub fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
363 /// Adds to the current value, returning the previous value.
365 /// The stabilized version of this intrinsic is available on the
366 /// [`atomic`] types via the `fetch_add` method by passing
367 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`].
368 pub fn atomic_xadd_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
369 /// Adds to the current value, returning the previous value.
371 /// The stabilized version of this intrinsic is available on the
372 /// [`atomic`] types via the `fetch_add` method by passing
373 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`].
374 pub fn atomic_xadd_acquire<T: Copy>(dst: *mut T, src: T) -> T;
375 /// Adds to the current value, returning the previous value.
377 /// The stabilized version of this intrinsic is available on the
378 /// [`atomic`] types via the `fetch_add` method by passing
379 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`].
380 pub fn atomic_xadd_release<T: Copy>(dst: *mut T, src: T) -> T;
381 /// Adds to the current value, returning the previous value.
383 /// The stabilized version of this intrinsic is available on the
384 /// [`atomic`] types via the `fetch_add` method by passing
385 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`].
386 pub fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
387 /// Adds to the current value, returning the previous value.
389 /// The stabilized version of this intrinsic is available on the
390 /// [`atomic`] types via the `fetch_add` method by passing
391 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`].
392 pub fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
394 /// Subtract from the current value, returning the previous value.
396 /// The stabilized version of this intrinsic is available on the
397 /// [`atomic`] types via the `fetch_sub` method by passing
398 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
399 pub fn atomic_xsub_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
400 /// Subtract from the current value, returning the previous value.
402 /// The stabilized version of this intrinsic is available on the
403 /// [`atomic`] types via the `fetch_sub` method by passing
404 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
405 pub fn atomic_xsub_acquire<T: Copy>(dst: *mut T, src: T) -> T;
406 /// Subtract from the current value, returning the previous value.
408 /// The stabilized version of this intrinsic is available on the
409 /// [`atomic`] types via the `fetch_sub` method by passing
410 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
411 pub fn atomic_xsub_release<T: Copy>(dst: *mut T, src: T) -> T;
412 /// Subtract from the current value, returning the previous value.
414 /// The stabilized version of this intrinsic is available on the
415 /// [`atomic`] types via the `fetch_sub` method by passing
416 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
417 pub fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
418 /// Subtract from the current value, returning the previous value.
420 /// The stabilized version of this intrinsic is available on the
421 /// [`atomic`] types via the `fetch_sub` method by passing
422 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
423 pub fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
425 /// Bitwise and with the current value, returning the previous value.
427 /// The stabilized version of this intrinsic is available on the
428 /// [`atomic`] types via the `fetch_and` method by passing
429 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`].
430 pub fn atomic_and_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
431 /// Bitwise and with the current value, returning the previous value.
433 /// The stabilized version of this intrinsic is available on the
434 /// [`atomic`] types via the `fetch_and` method by passing
435 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`].
436 pub fn atomic_and_acquire<T: Copy>(dst: *mut T, src: T) -> T;
437 /// Bitwise and with the current value, returning the previous value.
439 /// The stabilized version of this intrinsic is available on the
440 /// [`atomic`] types via the `fetch_and` method by passing
441 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`].
442 pub fn atomic_and_release<T: Copy>(dst: *mut T, src: T) -> T;
443 /// Bitwise and with the current value, returning the previous value.
445 /// The stabilized version of this intrinsic is available on the
446 /// [`atomic`] types via the `fetch_and` method by passing
447 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`].
448 pub fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
449 /// Bitwise and with the current value, returning the previous value.
451 /// The stabilized version of this intrinsic is available on the
452 /// [`atomic`] types via the `fetch_and` method by passing
453 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`].
454 pub fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
456 /// Bitwise nand with the current value, returning the previous value.
458 /// The stabilized version of this intrinsic is available on the
459 /// [`AtomicBool`] type via the `fetch_nand` method by passing
460 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`].
461 pub fn atomic_nand_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
462 /// Bitwise nand with the current value, returning the previous value.
464 /// The stabilized version of this intrinsic is available on the
465 /// [`AtomicBool`] type via the `fetch_nand` method by passing
466 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`].
467 pub fn atomic_nand_acquire<T: Copy>(dst: *mut T, src: T) -> T;
468 /// Bitwise nand with the current value, returning the previous value.
470 /// The stabilized version of this intrinsic is available on the
471 /// [`AtomicBool`] type via the `fetch_nand` method by passing
472 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`].
473 pub fn atomic_nand_release<T: Copy>(dst: *mut T, src: T) -> T;
474 /// Bitwise nand with the current value, returning the previous value.
476 /// The stabilized version of this intrinsic is available on the
477 /// [`AtomicBool`] type via the `fetch_nand` method by passing
478 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`].
479 pub fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
480 /// Bitwise nand with the current value, returning the previous value.
482 /// The stabilized version of this intrinsic is available on the
483 /// [`AtomicBool`] type via the `fetch_nand` method by passing
484 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`].
485 pub fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
487 /// Bitwise or with the current value, returning the previous value.
489 /// The stabilized version of this intrinsic is available on the
490 /// [`atomic`] types via the `fetch_or` method by passing
491 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`].
492 pub fn atomic_or_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
493 /// Bitwise or with the current value, returning the previous value.
495 /// The stabilized version of this intrinsic is available on the
496 /// [`atomic`] types via the `fetch_or` method by passing
497 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`].
498 pub fn atomic_or_acquire<T: Copy>(dst: *mut T, src: T) -> T;
499 /// Bitwise or with the current value, returning the previous value.
501 /// The stabilized version of this intrinsic is available on the
502 /// [`atomic`] types via the `fetch_or` method by passing
503 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`].
504 pub fn atomic_or_release<T: Copy>(dst: *mut T, src: T) -> T;
505 /// Bitwise or with the current value, returning the previous value.
507 /// The stabilized version of this intrinsic is available on the
508 /// [`atomic`] types via the `fetch_or` method by passing
509 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`].
510 pub fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
511 /// Bitwise or with the current value, returning the previous value.
513 /// The stabilized version of this intrinsic is available on the
514 /// [`atomic`] types via the `fetch_or` method by passing
515 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`].
516 pub fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
518 /// Bitwise xor with the current value, returning the previous value.
520 /// The stabilized version of this intrinsic is available on the
521 /// [`atomic`] types via the `fetch_xor` method by passing
522 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`].
523 pub fn atomic_xor_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
524 /// Bitwise xor with the current value, returning the previous value.
526 /// The stabilized version of this intrinsic is available on the
527 /// [`atomic`] types via the `fetch_xor` method by passing
528 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`].
529 pub fn atomic_xor_acquire<T: Copy>(dst: *mut T, src: T) -> T;
530 /// Bitwise xor with the current value, returning the previous value.
532 /// The stabilized version of this intrinsic is available on the
533 /// [`atomic`] types via the `fetch_xor` method by passing
534 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`].
535 pub fn atomic_xor_release<T: Copy>(dst: *mut T, src: T) -> T;
536 /// Bitwise xor with the current value, returning the previous value.
538 /// The stabilized version of this intrinsic is available on the
539 /// [`atomic`] types via the `fetch_xor` method by passing
540 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`].
541 pub fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
542 /// Bitwise xor with the current value, returning the previous value.
544 /// The stabilized version of this intrinsic is available on the
545 /// [`atomic`] types via the `fetch_xor` method by passing
546 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`].
547 pub fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
549 /// Maximum with the current value using a signed comparison.
551 /// The stabilized version of this intrinsic is available on the
552 /// [`atomic`] signed integer types via the `fetch_max` method by passing
553 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`].
554 pub fn atomic_max_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
555 /// Maximum with the current value using a signed comparison.
557 /// The stabilized version of this intrinsic is available on the
558 /// [`atomic`] signed integer types via the `fetch_max` method by passing
559 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`].
560 pub fn atomic_max_acquire<T: Copy>(dst: *mut T, src: T) -> T;
561 /// Maximum with the current value using a signed comparison.
563 /// The stabilized version of this intrinsic is available on the
564 /// [`atomic`] signed integer types via the `fetch_max` method by passing
565 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`].
566 pub fn atomic_max_release<T: Copy>(dst: *mut T, src: T) -> T;
567 /// Maximum with the current value using a signed comparison.
569 /// The stabilized version of this intrinsic is available on the
570 /// [`atomic`] signed integer types via the `fetch_max` method by passing
571 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`].
572 pub fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
573 /// Maximum with the current value.
575 /// The stabilized version of this intrinsic is available on the
576 /// [`atomic`] signed integer types via the `fetch_max` method by passing
577 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`].
578 pub fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
580 /// Minimum with the current value using a signed comparison.
582 /// The stabilized version of this intrinsic is available on the
583 /// [`atomic`] signed integer types via the `fetch_min` method by passing
584 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`].
585 pub fn atomic_min_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
586 /// Minimum with the current value using a signed comparison.
588 /// The stabilized version of this intrinsic is available on the
589 /// [`atomic`] signed integer types via the `fetch_min` method by passing
590 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`].
591 pub fn atomic_min_acquire<T: Copy>(dst: *mut T, src: T) -> T;
592 /// Minimum with the current value using a signed comparison.
594 /// The stabilized version of this intrinsic is available on the
595 /// [`atomic`] signed integer types via the `fetch_min` method by passing
596 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`].
597 pub fn atomic_min_release<T: Copy>(dst: *mut T, src: T) -> T;
598 /// Minimum with the current value using a signed comparison.
600 /// The stabilized version of this intrinsic is available on the
601 /// [`atomic`] signed integer types via the `fetch_min` method by passing
602 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`].
603 pub fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
604 /// Minimum with the current value using a signed comparison.
606 /// The stabilized version of this intrinsic is available on the
607 /// [`atomic`] signed integer types via the `fetch_min` method by passing
608 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`].
609 pub fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
611 /// Minimum with the current value using an unsigned comparison.
613 /// The stabilized version of this intrinsic is available on the
614 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
615 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`].
616 pub fn atomic_umin_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
617 /// Minimum with the current value using an unsigned comparison.
619 /// The stabilized version of this intrinsic is available on the
620 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
621 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`].
622 pub fn atomic_umin_acquire<T: Copy>(dst: *mut T, src: T) -> T;
623 /// Minimum with the current value using an unsigned comparison.
625 /// The stabilized version of this intrinsic is available on the
626 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
627 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`].
628 pub fn atomic_umin_release<T: Copy>(dst: *mut T, src: T) -> T;
629 /// Minimum with the current value using an unsigned comparison.
631 /// The stabilized version of this intrinsic is available on the
632 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
633 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`].
634 pub fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
635 /// Minimum with the current value using an unsigned comparison.
637 /// The stabilized version of this intrinsic is available on the
638 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
639 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`].
640 pub fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
642 /// Maximum with the current value using an unsigned comparison.
644 /// The stabilized version of this intrinsic is available on the
645 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
646 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`].
647 pub fn atomic_umax_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
648 /// Maximum with the current value using an unsigned comparison.
650 /// The stabilized version of this intrinsic is available on the
651 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
652 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`].
653 pub fn atomic_umax_acquire<T: Copy>(dst: *mut T, src: T) -> T;
654 /// Maximum with the current value using an unsigned comparison.
656 /// The stabilized version of this intrinsic is available on the
657 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
658 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`].
659 pub fn atomic_umax_release<T: Copy>(dst: *mut T, src: T) -> T;
660 /// Maximum with the current value using an unsigned comparison.
662 /// The stabilized version of this intrinsic is available on the
663 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
664 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`].
665 pub fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
666 /// Maximum with the current value using an unsigned comparison.
668 /// The stabilized version of this intrinsic is available on the
669 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
670 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`].
671 pub fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
675 /// The stabilized version of this intrinsic is available in
676 /// [`atomic::fence`] by passing [`Ordering::SeqCst`]
678 pub fn atomic_fence_seqcst();
681 /// The stabilized version of this intrinsic is available in
682 /// [`atomic::fence`] by passing [`Ordering::Acquire`]
684 pub fn atomic_fence_acquire();
687 /// The stabilized version of this intrinsic is available in
688 /// [`atomic::fence`] by passing [`Ordering::Release`]
690 pub fn atomic_fence_release();
693 /// The stabilized version of this intrinsic is available in
694 /// [`atomic::fence`] by passing [`Ordering::AcqRel`]
696 pub fn atomic_fence_acqrel();
698 /// A compiler-only memory barrier.
700 /// Memory accesses will never be reordered across this barrier by the
701 /// compiler, but no instructions will be emitted for it. This is
702 /// appropriate for operations on the same thread that may be preempted,
703 /// such as when interacting with signal handlers.
705 /// The stabilized version of this intrinsic is available in
706 /// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`]
708 pub fn atomic_singlethreadfence_seqcst();
709 /// A compiler-only memory barrier.
711 /// Memory accesses will never be reordered across this barrier by the
712 /// compiler, but no instructions will be emitted for it. This is
713 /// appropriate for operations on the same thread that may be preempted,
714 /// such as when interacting with signal handlers.
716 /// The stabilized version of this intrinsic is available in
717 /// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`]
719 pub fn atomic_singlethreadfence_acquire();
720 /// A compiler-only memory barrier.
722 /// Memory accesses will never be reordered across this barrier by the
723 /// compiler, but no instructions will be emitted for it. This is
724 /// appropriate for operations on the same thread that may be preempted,
725 /// such as when interacting with signal handlers.
727 /// The stabilized version of this intrinsic is available in
728 /// [`atomic::compiler_fence`] by passing [`Ordering::Release`]
730 pub fn atomic_singlethreadfence_release();
731 /// A compiler-only memory barrier.
733 /// Memory accesses will never be reordered across this barrier by the
734 /// compiler, but no instructions will be emitted for it. This is
735 /// appropriate for operations on the same thread that may be preempted,
736 /// such as when interacting with signal handlers.
738 /// The stabilized version of this intrinsic is available in
739 /// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`]
741 pub fn atomic_singlethreadfence_acqrel();
743 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
744 /// if supported; otherwise, it is a no-op.
745 /// Prefetches have no effect on the behavior of the program but can change its performance
748 /// The `locality` argument must be a constant integer and is a temporal locality specifier
749 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
751 /// This intrinsic does not have a stable counterpart.
752 pub fn prefetch_read_data<T>(data: *const T, locality: i32);
753 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
754 /// if supported; otherwise, it is a no-op.
755 /// Prefetches have no effect on the behavior of the program but can change its performance
758 /// The `locality` argument must be a constant integer and is a temporal locality specifier
759 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
761 /// This intrinsic does not have a stable counterpart.
762 pub fn prefetch_write_data<T>(data: *const T, locality: i32);
763 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
764 /// if supported; otherwise, it is a no-op.
765 /// Prefetches have no effect on the behavior of the program but can change its performance
768 /// The `locality` argument must be a constant integer and is a temporal locality specifier
769 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
771 /// This intrinsic does not have a stable counterpart.
772 pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
773 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
774 /// if supported; otherwise, it is a no-op.
775 /// Prefetches have no effect on the behavior of the program but can change its performance
778 /// The `locality` argument must be a constant integer and is a temporal locality specifier
779 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
781 /// This intrinsic does not have a stable counterpart.
782 pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
784 /// Magic intrinsic that derives its meaning from attributes
785 /// attached to the function.
787 /// For example, dataflow uses this to inject static assertions so
788 /// that `rustc_peek(potentially_uninitialized)` would actually
789 /// double-check that dataflow did indeed compute that it is
790 /// uninitialized at that point in the control flow.
792 /// This intrinsic should not be used outside of the compiler.
793 pub fn rustc_peek<T>(_: T) -> T;
795 /// Aborts the execution of the process.
797 /// Note that, unlike most intrinsics, this is safe to call;
798 /// it does not require an `unsafe` block.
799 /// Therefore, implementations must not require the user to uphold
800 /// any safety invariants.
802 /// [`std::process::abort`](../../std/process/fn.abort.html) is to be preferred if possible,
803 /// as its behavior is more user-friendly and more stable.
805 /// The current implementation of `intrinsics::abort` is to invoke an invalid instruction,
806 /// on most platforms.
808 /// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or
809 /// `SIGBUS`. The precise behaviour is not guaranteed and not stable.
812 /// Informs the optimizer that this point in the code is not reachable,
813 /// enabling further optimizations.
815 /// N.B., this is very different from the `unreachable!()` macro: Unlike the
816 /// macro, which panics when it is executed, it is *undefined behavior* to
817 /// reach code marked with this function.
819 /// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`].
820 #[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
821 pub fn unreachable() -> !;
823 /// Informs the optimizer that a condition is always true.
824 /// If the condition is false, the behavior is undefined.
826 /// No code is generated for this intrinsic, but the optimizer will try
827 /// to preserve it (and its condition) between passes, which may interfere
828 /// with optimization of surrounding code and reduce performance. It should
829 /// not be used if the invariant can be discovered by the optimizer on its
830 /// own, or if it does not enable any significant optimizations.
832 /// This intrinsic does not have a stable counterpart.
833 #[rustc_const_unstable(feature = "const_assume", issue = "76972")]
834 pub fn assume(b: bool);
836 /// Hints to the compiler that branch condition is likely to be true.
837 /// Returns the value passed to it.
839 /// Any use other than with `if` statements will probably not have an effect.
841 /// Note that, unlike most intrinsics, this is safe to call;
842 /// it does not require an `unsafe` block.
843 /// Therefore, implementations must not require the user to uphold
844 /// any safety invariants.
846 /// This intrinsic does not have a stable counterpart.
847 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
848 pub fn likely(b: bool) -> bool;
850 /// Hints to the compiler that branch condition is likely to be false.
851 /// Returns the value passed to it.
853 /// Any use other than with `if` statements will probably not have an effect.
855 /// Note that, unlike most intrinsics, this is safe to call;
856 /// it does not require an `unsafe` block.
857 /// Therefore, implementations must not require the user to uphold
858 /// any safety invariants.
860 /// This intrinsic does not have a stable counterpart.
861 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
862 pub fn unlikely(b: bool) -> bool;
864 /// Executes a breakpoint trap, for inspection by a debugger.
866 /// This intrinsic does not have a stable counterpart.
869 /// The size of a type in bytes.
871 /// Note that, unlike most intrinsics, this is safe to call;
872 /// it does not require an `unsafe` block.
873 /// Therefore, implementations must not require the user to uphold
874 /// any safety invariants.
876 /// More specifically, this is the offset in bytes between successive
877 /// items of the same type, including alignment padding.
879 /// The stabilized version of this intrinsic is [`core::mem::size_of`].
880 #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
881 pub fn size_of<T>() -> usize;
883 /// The minimum alignment of a type.
885 /// Note that, unlike most intrinsics, this is safe to call;
886 /// it does not require an `unsafe` block.
887 /// Therefore, implementations must not require the user to uphold
888 /// any safety invariants.
890 /// The stabilized version of this intrinsic is [`core::mem::align_of`].
891 #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
892 pub fn min_align_of<T>() -> usize;
893 /// The preferred alignment of a type.
895 /// This intrinsic does not have a stable counterpart.
896 /// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
897 #[rustc_const_unstable(feature = "const_pref_align_of", issue = "91971")]
898 pub fn pref_align_of<T>() -> usize;
900 /// The size of the referenced value in bytes.
902 /// The stabilized version of this intrinsic is [`mem::size_of_val`].
903 #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
904 pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
905 /// The required alignment of the referenced value.
907 /// The stabilized version of this intrinsic is [`core::mem::align_of_val`].
908 #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
909 pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
911 /// Gets a static string slice containing the name of a type.
913 /// Note that, unlike most intrinsics, this is safe to call;
914 /// it does not require an `unsafe` block.
915 /// Therefore, implementations must not require the user to uphold
916 /// any safety invariants.
918 /// The stabilized version of this intrinsic is [`core::any::type_name`].
919 #[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
920 pub fn type_name<T: ?Sized>() -> &'static str;
922 /// Gets an identifier which is globally unique to the specified type. This
923 /// function will return the same value for a type regardless of whichever
924 /// crate it is invoked in.
926 /// Note that, unlike most intrinsics, this is safe to call;
927 /// it does not require an `unsafe` block.
928 /// Therefore, implementations must not require the user to uphold
929 /// any safety invariants.
931 /// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
932 #[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
933 pub fn type_id<T: ?Sized + 'static>() -> u64;
935 /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
936 /// This will statically either panic, or do nothing.
938 /// This intrinsic does not have a stable counterpart.
939 #[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
940 pub fn assert_inhabited<T>();
942 /// A guard for unsafe functions that cannot ever be executed if `T` does not permit
943 /// zero-initialization: This will statically either panic, or do nothing.
945 /// This intrinsic does not have a stable counterpart.
946 #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
947 pub fn assert_zero_valid<T>();
949 /// A guard for unsafe functions that cannot ever be executed if `T` has invalid
950 /// bit patterns: This will statically either panic, or do nothing.
952 /// This intrinsic does not have a stable counterpart.
953 #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
954 pub fn assert_uninit_valid<T>();
956 /// Gets a reference to a static `Location` indicating where it was called.
958 /// Note that, unlike most intrinsics, this is safe to call;
959 /// it does not require an `unsafe` block.
960 /// Therefore, implementations must not require the user to uphold
961 /// any safety invariants.
963 /// Consider using [`core::panic::Location::caller`] instead.
964 #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
965 pub fn caller_location() -> &'static crate::panic::Location<'static>;
967 /// Moves a value out of scope without running drop glue.
969 /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses
970 /// `ManuallyDrop` instead.
972 /// Note that, unlike most intrinsics, this is safe to call;
973 /// it does not require an `unsafe` block.
974 /// Therefore, implementations must not require the user to uphold
975 /// any safety invariants.
976 #[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")]
977 pub fn forget<T: ?Sized>(_: T);
979 /// Reinterprets the bits of a value of one type as another type.
981 /// Both types must have the same size. Compilation will fail if this is not guaranteed.
983 /// `transmute` is semantically equivalent to a bitwise move of one type
984 /// into another. It copies the bits from the source value into the
985 /// destination value, then forgets the original. Note that source and destination
986 /// are passed by-value, which means if `T` or `U` contain padding, that padding
987 /// is *not* guaranteed to be preserved by `transmute`.
989 /// Both the argument and the result must be [valid](../../nomicon/what-unsafe-does.html) at
990 /// their given type. Violating this condition leads to [undefined behavior][ub]. The compiler
991 /// will generate code *assuming that you, the programmer, ensure that there will never be
992 /// undefined behavior*. It is therefore your responsibility to guarantee that every value
993 /// passed to `transmute` is valid at both types `T` and `U`. Failing to uphold this condition
994 /// may lead to unexpected and unstable compilation results. This makes `transmute` **incredibly
995 /// unsafe**. `transmute` should be the absolute last resort.
997 /// Transmuting pointers to integers in a `const` context is [undefined behavior][ub].
998 /// Any attempt to use the resulting value for integer operations will abort const-evaluation.
999 /// (And even outside `const`, such transmutation is touching on many unspecified aspects of the
1000 /// Rust memory model and should be avoided. See below for alternatives.)
1002 /// Because `transmute` is a by-value operation, alignment of the *transmuted values
1003 /// themselves* is not a concern. As with any other function, the compiler already ensures
1004 /// both `T` and `U` are properly aligned. However, when transmuting values that *point
1005 /// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper
1006 /// alignment of the pointed-to values.
1008 /// The [nomicon](../../nomicon/transmutes.html) has additional documentation.
1010 /// [ub]: ../../reference/behavior-considered-undefined.html
1014 /// There are a few things that `transmute` is really useful for.
1016 /// Turning a pointer into a function pointer. This is *not* portable to
1017 /// machines where function pointers and data pointers have different sizes.
1020 /// fn foo() -> i32 {
1023 /// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer.
1024 /// // This avoids an integer-to-pointer `transmute`, which can be problematic.
1025 /// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine.
1026 /// let pointer = foo as *const ();
1027 /// let function = unsafe {
1028 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
1030 /// assert_eq!(function(), 0);
1033 /// Extending a lifetime, or shortening an invariant lifetime. This is
1034 /// advanced, very unsafe Rust!
1037 /// struct R<'a>(&'a i32);
1038 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
1039 /// std::mem::transmute::<R<'b>, R<'static>>(r)
1042 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
1043 /// -> &'b mut R<'c> {
1044 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
1050 /// Don't despair: many uses of `transmute` can be achieved through other means.
1051 /// Below are common applications of `transmute` which can be replaced with safer
1054 /// Turning raw bytes (`&[u8]`) into `u32`, `f64`, etc.:
1057 /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
1059 /// let num = unsafe {
1060 /// std::mem::transmute::<[u8; 4], u32>(raw_bytes)
1063 /// // use `u32::from_ne_bytes` instead
1064 /// let num = u32::from_ne_bytes(raw_bytes);
1065 /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
1066 /// let num = u32::from_le_bytes(raw_bytes);
1067 /// assert_eq!(num, 0x12345678);
1068 /// let num = u32::from_be_bytes(raw_bytes);
1069 /// assert_eq!(num, 0x78563412);
1072 /// Turning a pointer into a `usize`:
1076 /// let ptr_num_transmute = unsafe {
1077 /// std::mem::transmute::<&i32, usize>(ptr)
1080 /// // Use an `as` cast instead
1081 /// let ptr_num_cast = ptr as *const i32 as usize;
1084 /// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined
1085 /// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave
1086 /// as expected -- this is touching on many unspecified aspects of the Rust memory model.
1087 /// Depending on what the code is doing, the following alternatives are preferable to
1088 /// pointer-to-integer transmutation:
1089 /// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a
1090 /// type for that buffer, it can use [`MaybeUninit`][mem::MaybeUninit].
1091 /// - If the code actually wants to work on the address the pointer points to, it can use `as`
1092 /// casts or [`ptr.addr()`][pointer::addr].
1094 /// Turning a `*mut T` into an `&mut T`:
1097 /// let ptr: *mut i32 = &mut 0;
1098 /// let ref_transmuted = unsafe {
1099 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
1102 /// // Use a reborrow instead
1103 /// let ref_casted = unsafe { &mut *ptr };
1106 /// Turning an `&mut T` into an `&mut U`:
1109 /// let ptr = &mut 0;
1110 /// let val_transmuted = unsafe {
1111 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
1114 /// // Now, put together `as` and reborrowing - note the chaining of `as`
1115 /// // `as` is not transitive
1116 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
1119 /// Turning an `&str` into a `&[u8]`:
1122 /// // this is not a good way to do this.
1123 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
1124 /// assert_eq!(slice, &[82, 117, 115, 116]);
1126 /// // You could use `str::as_bytes`
1127 /// let slice = "Rust".as_bytes();
1128 /// assert_eq!(slice, &[82, 117, 115, 116]);
1130 /// // Or, just use a byte string, if you have control over the string
1132 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
1135 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`.
1137 /// To transmute the inner type of the contents of a container, you must make sure to not
1138 /// violate any of the container's invariants. For `Vec`, this means that both the size
1139 /// *and alignment* of the inner types have to match. Other containers might rely on the
1140 /// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't
1141 /// be possible at all without violating the container invariants.
1144 /// let store = [0, 1, 2, 3];
1145 /// let v_orig = store.iter().collect::<Vec<&i32>>();
1147 /// // clone the vector as we will reuse them later
1148 /// let v_clone = v_orig.clone();
1150 /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
1151 /// // bad idea and could cause Undefined Behavior.
1152 /// // However, it is no-copy.
1153 /// let v_transmuted = unsafe {
1154 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
1157 /// let v_clone = v_orig.clone();
1159 /// // This is the suggested, safe way.
1160 /// // It does copy the entire vector, though, into a new array.
1161 /// let v_collected = v_clone.into_iter()
1163 /// .collect::<Vec<Option<&i32>>>();
1165 /// let v_clone = v_orig.clone();
1167 /// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the
1168 /// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but
1169 /// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`),
1170 /// // this has all the same caveats. Besides the information provided above, also consult the
1171 /// // [`from_raw_parts`] documentation.
1172 /// let v_from_raw = unsafe {
1173 // FIXME Update this when vec_into_raw_parts is stabilized
1174 /// // Ensure the original vector is not dropped.
1175 /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
1176 /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
1178 /// v_clone.capacity())
1182 /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
1184 /// Implementing `split_at_mut`:
1187 /// use std::{slice, mem};
1189 /// // There are multiple ways to do this, and there are multiple problems
1190 /// // with the following (transmute) way.
1191 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
1192 /// -> (&mut [T], &mut [T]) {
1193 /// let len = slice.len();
1194 /// assert!(mid <= len);
1196 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
1197 /// // first: transmute is not type safe; all it checks is that T and
1198 /// // U are of the same size. Second, right here, you have two
1199 /// // mutable references pointing to the same memory.
1200 /// (&mut slice[0..mid], &mut slice2[mid..len])
1204 /// // This gets rid of the type safety problems; `&mut *` will *only* give
1205 /// // you an `&mut T` from an `&mut T` or `*mut T`.
1206 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
1207 /// -> (&mut [T], &mut [T]) {
1208 /// let len = slice.len();
1209 /// assert!(mid <= len);
1211 /// let slice2 = &mut *(slice as *mut [T]);
1212 /// // however, you still have two mutable references pointing to
1213 /// // the same memory.
1214 /// (&mut slice[0..mid], &mut slice2[mid..len])
1218 /// // This is how the standard library does it. This is the best method, if
1219 /// // you need to do something like this
1220 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
1221 /// -> (&mut [T], &mut [T]) {
1222 /// let len = slice.len();
1223 /// assert!(mid <= len);
1225 /// let ptr = slice.as_mut_ptr();
1226 /// // This now has three mutable references pointing at the same
1227 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
1228 /// // `slice` is never used after `let ptr = ...`, and so one can
1229 /// // treat it as "dead", and therefore, you only have two real
1230 /// // mutable slices.
1231 /// (slice::from_raw_parts_mut(ptr, mid),
1232 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
1236 #[stable(feature = "rust1", since = "1.0.0")]
1237 #[rustc_allowed_through_unstable_modules]
1238 #[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
1239 #[rustc_diagnostic_item = "transmute"]
1240 pub fn transmute<T, U>(e: T) -> U;
1242 /// Returns `true` if the actual type given as `T` requires drop
1243 /// glue; returns `false` if the actual type provided for `T`
1244 /// implements `Copy`.
1246 /// If the actual type neither requires drop glue nor implements
1247 /// `Copy`, then the return value of this function is unspecified.
1249 /// Note that, unlike most intrinsics, this is safe to call;
1250 /// it does not require an `unsafe` block.
1251 /// Therefore, implementations must not require the user to uphold
1252 /// any safety invariants.
1254 /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1255 #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1256 pub fn needs_drop<T: ?Sized>() -> bool;
1258 /// Calculates the offset from a pointer.
1260 /// This is implemented as an intrinsic to avoid converting to and from an
1261 /// integer, since the conversion would throw away aliasing information.
1265 /// Both the starting and resulting pointer must be either in bounds or one
1266 /// byte past the end of an allocated object. If either pointer is out of
1267 /// bounds or arithmetic overflow occurs then any further use of the
1268 /// returned value will result in undefined behavior.
1270 /// The stabilized version of this intrinsic is [`pointer::offset`].
1271 #[must_use = "returns a new pointer rather than modifying its argument"]
1272 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1273 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1275 /// Calculates the offset from a pointer, potentially wrapping.
1277 /// This is implemented as an intrinsic to avoid converting to and from an
1278 /// integer, since the conversion inhibits certain optimizations.
1282 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1283 /// resulting pointer to point into or one byte past the end of an allocated
1284 /// object, and it wraps with two's complement arithmetic. The resulting
1285 /// value is not necessarily valid to be used to actually access memory.
1287 /// The stabilized version of this intrinsic is [`pointer::wrapping_offset`].
1288 #[must_use = "returns a new pointer rather than modifying its argument"]
1289 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1290 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1292 /// Masks out bits of the pointer according to a mask.
1294 /// Note that, unlike most intrinsics, this is safe to call;
1295 /// it does not require an `unsafe` block.
1296 /// Therefore, implementations must not require the user to uphold
1297 /// any safety invariants.
1299 /// Consider using [`pointer::mask`] instead.
1300 #[cfg(not(bootstrap))]
1301 pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;
1303 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1304 /// a size of `count` * `size_of::<T>()` and an alignment of
1305 /// `min_align_of::<T>()`
1307 /// The volatile parameter is set to `true`, so it will not be optimized out
1308 /// unless size is equal to zero.
1310 /// This intrinsic does not have a stable counterpart.
1311 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1312 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1313 /// a size of `count * size_of::<T>()` and an alignment of
1314 /// `min_align_of::<T>()`
1316 /// The volatile parameter is set to `true`, so it will not be optimized out
1317 /// unless size is equal to zero.
1319 /// This intrinsic does not have a stable counterpart.
1320 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1321 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1322 /// size of `count * size_of::<T>()` and an alignment of
1323 /// `min_align_of::<T>()`.
1325 /// The volatile parameter is set to `true`, so it will not be optimized out
1326 /// unless size is equal to zero.
1328 /// This intrinsic does not have a stable counterpart.
1329 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1331 /// Performs a volatile load from the `src` pointer.
1333 /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`].
1334 pub fn volatile_load<T>(src: *const T) -> T;
1335 /// Performs a volatile store to the `dst` pointer.
1337 /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`].
1338 pub fn volatile_store<T>(dst: *mut T, val: T);
1340 /// Performs a volatile load from the `src` pointer
1341 /// The pointer is not required to be aligned.
1343 /// This intrinsic does not have a stable counterpart.
1344 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1345 /// Performs a volatile store to the `dst` pointer.
1346 /// The pointer is not required to be aligned.
1348 /// This intrinsic does not have a stable counterpart.
1349 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1351 /// Returns the square root of an `f32`
1353 /// The stabilized version of this intrinsic is
1354 /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1355 pub fn sqrtf32(x: f32) -> f32;
1356 /// Returns the square root of an `f64`
1358 /// The stabilized version of this intrinsic is
1359 /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1360 pub fn sqrtf64(x: f64) -> f64;
1362 /// Raises an `f32` to an integer power.
1364 /// The stabilized version of this intrinsic is
1365 /// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1366 pub fn powif32(a: f32, x: i32) -> f32;
1367 /// Raises an `f64` to an integer power.
1369 /// The stabilized version of this intrinsic is
1370 /// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1371 pub fn powif64(a: f64, x: i32) -> f64;
1373 /// Returns the sine of an `f32`.
1375 /// The stabilized version of this intrinsic is
1376 /// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1377 pub fn sinf32(x: f32) -> f32;
1378 /// Returns the sine of an `f64`.
1380 /// The stabilized version of this intrinsic is
1381 /// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1382 pub fn sinf64(x: f64) -> f64;
1384 /// Returns the cosine of an `f32`.
1386 /// The stabilized version of this intrinsic is
1387 /// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1388 pub fn cosf32(x: f32) -> f32;
1389 /// Returns the cosine of an `f64`.
1391 /// The stabilized version of this intrinsic is
1392 /// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1393 pub fn cosf64(x: f64) -> f64;
1395 /// Raises an `f32` to an `f32` power.
1397 /// The stabilized version of this intrinsic is
1398 /// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1399 pub fn powf32(a: f32, x: f32) -> f32;
1400 /// Raises an `f64` to an `f64` power.
1402 /// The stabilized version of this intrinsic is
1403 /// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1404 pub fn powf64(a: f64, x: f64) -> f64;
1406 /// Returns the exponential of an `f32`.
1408 /// The stabilized version of this intrinsic is
1409 /// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1410 pub fn expf32(x: f32) -> f32;
1411 /// Returns the exponential of an `f64`.
1413 /// The stabilized version of this intrinsic is
1414 /// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1415 pub fn expf64(x: f64) -> f64;
1417 /// Returns 2 raised to the power of an `f32`.
1419 /// The stabilized version of this intrinsic is
1420 /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1421 pub fn exp2f32(x: f32) -> f32;
1422 /// Returns 2 raised to the power of an `f64`.
1424 /// The stabilized version of this intrinsic is
1425 /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1426 pub fn exp2f64(x: f64) -> f64;
1428 /// Returns the natural logarithm of an `f32`.
1430 /// The stabilized version of this intrinsic is
1431 /// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1432 pub fn logf32(x: f32) -> f32;
1433 /// Returns the natural logarithm of an `f64`.
1435 /// The stabilized version of this intrinsic is
1436 /// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1437 pub fn logf64(x: f64) -> f64;
1439 /// Returns the base 10 logarithm of an `f32`.
1441 /// The stabilized version of this intrinsic is
1442 /// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1443 pub fn log10f32(x: f32) -> f32;
1444 /// Returns the base 10 logarithm of an `f64`.
1446 /// The stabilized version of this intrinsic is
1447 /// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1448 pub fn log10f64(x: f64) -> f64;
1450 /// Returns the base 2 logarithm of an `f32`.
1452 /// The stabilized version of this intrinsic is
1453 /// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1454 pub fn log2f32(x: f32) -> f32;
1455 /// Returns the base 2 logarithm of an `f64`.
1457 /// The stabilized version of this intrinsic is
1458 /// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1459 pub fn log2f64(x: f64) -> f64;
1461 /// Returns `a * b + c` for `f32` values.
1463 /// The stabilized version of this intrinsic is
1464 /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1465 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1466 /// Returns `a * b + c` for `f64` values.
1468 /// The stabilized version of this intrinsic is
1469 /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1470 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1472 /// Returns the absolute value of an `f32`.
1474 /// The stabilized version of this intrinsic is
1475 /// [`f32::abs`](../../std/primitive.f32.html#method.abs)
1476 pub fn fabsf32(x: f32) -> f32;
1477 /// Returns the absolute value of an `f64`.
1479 /// The stabilized version of this intrinsic is
1480 /// [`f64::abs`](../../std/primitive.f64.html#method.abs)
1481 pub fn fabsf64(x: f64) -> f64;
1483 /// Returns the minimum of two `f32` values.
1485 /// Note that, unlike most intrinsics, this is safe to call;
1486 /// it does not require an `unsafe` block.
1487 /// Therefore, implementations must not require the user to uphold
1488 /// any safety invariants.
1490 /// The stabilized version of this intrinsic is
1492 pub fn minnumf32(x: f32, y: f32) -> f32;
1493 /// Returns the minimum of two `f64` values.
1495 /// Note that, unlike most intrinsics, this is safe to call;
1496 /// it does not require an `unsafe` block.
1497 /// Therefore, implementations must not require the user to uphold
1498 /// any safety invariants.
1500 /// The stabilized version of this intrinsic is
1502 pub fn minnumf64(x: f64, y: f64) -> f64;
1503 /// Returns the maximum of two `f32` values.
1505 /// Note that, unlike most intrinsics, this is safe to call;
1506 /// it does not require an `unsafe` block.
1507 /// Therefore, implementations must not require the user to uphold
1508 /// any safety invariants.
1510 /// The stabilized version of this intrinsic is
1512 pub fn maxnumf32(x: f32, y: f32) -> f32;
1513 /// Returns the maximum of two `f64` values.
1515 /// Note that, unlike most intrinsics, this is safe to call;
1516 /// it does not require an `unsafe` block.
1517 /// Therefore, implementations must not require the user to uphold
1518 /// any safety invariants.
1520 /// The stabilized version of this intrinsic is
1522 pub fn maxnumf64(x: f64, y: f64) -> f64;
1524 /// Copies the sign from `y` to `x` for `f32` values.
1526 /// The stabilized version of this intrinsic is
1527 /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
1528 pub fn copysignf32(x: f32, y: f32) -> f32;
1529 /// Copies the sign from `y` to `x` for `f64` values.
1531 /// The stabilized version of this intrinsic is
1532 /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
1533 pub fn copysignf64(x: f64, y: f64) -> f64;
1535 /// Returns the largest integer less than or equal to an `f32`.
1537 /// The stabilized version of this intrinsic is
1538 /// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1539 pub fn floorf32(x: f32) -> f32;
1540 /// Returns the largest integer less than or equal to an `f64`.
1542 /// The stabilized version of this intrinsic is
1543 /// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1544 pub fn floorf64(x: f64) -> f64;
1546 /// Returns the smallest integer greater than or equal to an `f32`.
1548 /// The stabilized version of this intrinsic is
1549 /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1550 pub fn ceilf32(x: f32) -> f32;
1551 /// Returns the smallest integer greater than or equal to an `f64`.
1553 /// The stabilized version of this intrinsic is
1554 /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1555 pub fn ceilf64(x: f64) -> f64;
1557 /// Returns the integer part of an `f32`.
1559 /// The stabilized version of this intrinsic is
1560 /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1561 pub fn truncf32(x: f32) -> f32;
1562 /// Returns the integer part of an `f64`.
1564 /// The stabilized version of this intrinsic is
1565 /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1566 pub fn truncf64(x: f64) -> f64;
1568 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1569 /// if the argument is not an integer.
1570 pub fn rintf32(x: f32) -> f32;
1571 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1572 /// if the argument is not an integer.
1573 pub fn rintf64(x: f64) -> f64;
1575 /// Returns the nearest integer to an `f32`.
1577 /// This intrinsic does not have a stable counterpart.
1578 pub fn nearbyintf32(x: f32) -> f32;
1579 /// Returns the nearest integer to an `f64`.
1581 /// This intrinsic does not have a stable counterpart.
1582 pub fn nearbyintf64(x: f64) -> f64;
1584 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1586 /// The stabilized version of this intrinsic is
1587 /// [`f32::round`](../../std/primitive.f32.html#method.round)
1588 pub fn roundf32(x: f32) -> f32;
1589 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1591 /// The stabilized version of this intrinsic is
1592 /// [`f64::round`](../../std/primitive.f64.html#method.round)
1593 pub fn roundf64(x: f64) -> f64;
1595 /// Float addition that allows optimizations based on algebraic rules.
1596 /// May assume inputs are finite.
1598 /// This intrinsic does not have a stable counterpart.
1599 pub fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1601 /// Float subtraction that allows optimizations based on algebraic rules.
1602 /// May assume inputs are finite.
1604 /// This intrinsic does not have a stable counterpart.
1605 pub fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1607 /// Float multiplication that allows optimizations based on algebraic rules.
1608 /// May assume inputs are finite.
1610 /// This intrinsic does not have a stable counterpart.
1611 pub fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1613 /// Float division that allows optimizations based on algebraic rules.
1614 /// May assume inputs are finite.
1616 /// This intrinsic does not have a stable counterpart.
1617 pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1619 /// Float remainder that allows optimizations based on algebraic rules.
1620 /// May assume inputs are finite.
1622 /// This intrinsic does not have a stable counterpart.
1623 pub fn frem_fast<T: Copy>(a: T, b: T) -> T;
1625 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1626 /// (<https://github.com/rust-lang/rust/issues/10184>)
1628 /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1629 pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
1631 /// Returns the number of bits set in an integer type `T`
1633 /// Note that, unlike most intrinsics, this is safe to call;
1634 /// it does not require an `unsafe` block.
1635 /// Therefore, implementations must not require the user to uphold
1636 /// any safety invariants.
1638 /// The stabilized versions of this intrinsic are available on the integer
1639 /// primitives via the `count_ones` method. For example,
1640 /// [`u32::count_ones`]
1641 #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1642 pub fn ctpop<T: Copy>(x: T) -> T;
1644 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1646 /// Note that, unlike most intrinsics, this is safe to call;
1647 /// it does not require an `unsafe` block.
1648 /// Therefore, implementations must not require the user to uphold
1649 /// any safety invariants.
1651 /// The stabilized versions of this intrinsic are available on the integer
1652 /// primitives via the `leading_zeros` method. For example,
1653 /// [`u32::leading_zeros`]
1658 /// #![feature(core_intrinsics)]
1660 /// use std::intrinsics::ctlz;
1662 /// let x = 0b0001_1100_u8;
1663 /// let num_leading = ctlz(x);
1664 /// assert_eq!(num_leading, 3);
1667 /// An `x` with value `0` will return the bit width of `T`.
1670 /// #![feature(core_intrinsics)]
1672 /// use std::intrinsics::ctlz;
1675 /// let num_leading = ctlz(x);
1676 /// assert_eq!(num_leading, 16);
1678 #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1679 pub fn ctlz<T: Copy>(x: T) -> T;
1681 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1682 /// given an `x` with value `0`.
1684 /// This intrinsic does not have a stable counterpart.
1689 /// #![feature(core_intrinsics)]
1691 /// use std::intrinsics::ctlz_nonzero;
1693 /// let x = 0b0001_1100_u8;
1694 /// let num_leading = unsafe { ctlz_nonzero(x) };
1695 /// assert_eq!(num_leading, 3);
1697 #[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
1698 pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
1700 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1702 /// Note that, unlike most intrinsics, this is safe to call;
1703 /// it does not require an `unsafe` block.
1704 /// Therefore, implementations must not require the user to uphold
1705 /// any safety invariants.
1707 /// The stabilized versions of this intrinsic are available on the integer
1708 /// primitives via the `trailing_zeros` method. For example,
1709 /// [`u32::trailing_zeros`]
1714 /// #![feature(core_intrinsics)]
1716 /// use std::intrinsics::cttz;
1718 /// let x = 0b0011_1000_u8;
1719 /// let num_trailing = cttz(x);
1720 /// assert_eq!(num_trailing, 3);
1723 /// An `x` with value `0` will return the bit width of `T`:
1726 /// #![feature(core_intrinsics)]
1728 /// use std::intrinsics::cttz;
1731 /// let num_trailing = cttz(x);
1732 /// assert_eq!(num_trailing, 16);
1734 #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1735 pub fn cttz<T: Copy>(x: T) -> T;
1737 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1738 /// given an `x` with value `0`.
1740 /// This intrinsic does not have a stable counterpart.
1745 /// #![feature(core_intrinsics)]
1747 /// use std::intrinsics::cttz_nonzero;
1749 /// let x = 0b0011_1000_u8;
1750 /// let num_trailing = unsafe { cttz_nonzero(x) };
1751 /// assert_eq!(num_trailing, 3);
1753 #[rustc_const_stable(feature = "const_cttz_nonzero", since = "1.53.0")]
1754 pub fn cttz_nonzero<T: Copy>(x: T) -> T;
1756 /// Reverses the bytes in an integer type `T`.
1758 /// Note that, unlike most intrinsics, this is safe to call;
1759 /// it does not require an `unsafe` block.
1760 /// Therefore, implementations must not require the user to uphold
1761 /// any safety invariants.
1763 /// The stabilized versions of this intrinsic are available on the integer
1764 /// primitives via the `swap_bytes` method. For example,
1765 /// [`u32::swap_bytes`]
1766 #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1767 pub fn bswap<T: Copy>(x: T) -> T;
1769 /// Reverses the bits in an integer type `T`.
1771 /// Note that, unlike most intrinsics, this is safe to call;
1772 /// it does not require an `unsafe` block.
1773 /// Therefore, implementations must not require the user to uphold
1774 /// any safety invariants.
1776 /// The stabilized versions of this intrinsic are available on the integer
1777 /// primitives via the `reverse_bits` method. For example,
1778 /// [`u32::reverse_bits`]
1779 #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1780 pub fn bitreverse<T: Copy>(x: T) -> T;
1782 /// Performs checked integer addition.
1784 /// Note that, unlike most intrinsics, this is safe to call;
1785 /// it does not require an `unsafe` block.
1786 /// Therefore, implementations must not require the user to uphold
1787 /// any safety invariants.
1789 /// The stabilized versions of this intrinsic are available on the integer
1790 /// primitives via the `overflowing_add` method. For example,
1791 /// [`u32::overflowing_add`]
1792 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1793 pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1795 /// Performs checked integer subtraction
1797 /// Note that, unlike most intrinsics, this is safe to call;
1798 /// it does not require an `unsafe` block.
1799 /// Therefore, implementations must not require the user to uphold
1800 /// any safety invariants.
1802 /// The stabilized versions of this intrinsic are available on the integer
1803 /// primitives via the `overflowing_sub` method. For example,
1804 /// [`u32::overflowing_sub`]
1805 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1806 pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1808 /// Performs checked integer multiplication
1810 /// Note that, unlike most intrinsics, this is safe to call;
1811 /// it does not require an `unsafe` block.
1812 /// Therefore, implementations must not require the user to uphold
1813 /// any safety invariants.
1815 /// The stabilized versions of this intrinsic are available on the integer
1816 /// primitives via the `overflowing_mul` method. For example,
1817 /// [`u32::overflowing_mul`]
1818 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1819 pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1821 /// Performs an exact division, resulting in undefined behavior where
1822 /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1824 /// This intrinsic does not have a stable counterpart.
1825 pub fn exact_div<T: Copy>(x: T, y: T) -> T;
1827 /// Performs an unchecked division, resulting in undefined behavior
1828 /// where `y == 0` or `x == T::MIN && y == -1`
1830 /// Safe wrappers for this intrinsic are available on the integer
1831 /// primitives via the `checked_div` method. For example,
1832 /// [`u32::checked_div`]
1833 #[rustc_const_stable(feature = "const_int_unchecked_div", since = "1.52.0")]
1834 pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1835 /// Returns the remainder of an unchecked division, resulting in
1836 /// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
1838 /// Safe wrappers for this intrinsic are available on the integer
1839 /// primitives via the `checked_rem` method. For example,
1840 /// [`u32::checked_rem`]
1841 #[rustc_const_stable(feature = "const_int_unchecked_rem", since = "1.52.0")]
1842 pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1844 /// Performs an unchecked left shift, resulting in undefined behavior when
1845 /// `y < 0` or `y >= N`, where N is the width of T in bits.
1847 /// Safe wrappers for this intrinsic are available on the integer
1848 /// primitives via the `checked_shl` method. For example,
1849 /// [`u32::checked_shl`]
1850 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1851 pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
1852 /// Performs an unchecked right shift, resulting in undefined behavior when
1853 /// `y < 0` or `y >= N`, where N is the width of T in bits.
1855 /// Safe wrappers for this intrinsic are available on the integer
1856 /// primitives via the `checked_shr` method. For example,
1857 /// [`u32::checked_shr`]
1858 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1859 pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
1861 /// Returns the result of an unchecked addition, resulting in
1862 /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1864 /// This intrinsic does not have a stable counterpart.
1865 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1866 pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1868 /// Returns the result of an unchecked subtraction, resulting in
1869 /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1871 /// This intrinsic does not have a stable counterpart.
1872 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1873 pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1875 /// Returns the result of an unchecked multiplication, resulting in
1876 /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1878 /// This intrinsic does not have a stable counterpart.
1879 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1880 pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1882 /// Performs rotate left.
1884 /// Note that, unlike most intrinsics, this is safe to call;
1885 /// it does not require an `unsafe` block.
1886 /// Therefore, implementations must not require the user to uphold
1887 /// any safety invariants.
1889 /// The stabilized versions of this intrinsic are available on the integer
1890 /// primitives via the `rotate_left` method. For example,
1891 /// [`u32::rotate_left`]
1892 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1893 pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
1895 /// Performs rotate right.
1897 /// Note that, unlike most intrinsics, this is safe to call;
1898 /// it does not require an `unsafe` block.
1899 /// Therefore, implementations must not require the user to uphold
1900 /// any safety invariants.
1902 /// The stabilized versions of this intrinsic are available on the integer
1903 /// primitives via the `rotate_right` method. For example,
1904 /// [`u32::rotate_right`]
1905 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1906 pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
1908 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1910 /// Note that, unlike most intrinsics, this is safe to call;
1911 /// it does not require an `unsafe` block.
1912 /// Therefore, implementations must not require the user to uphold
1913 /// any safety invariants.
1915 /// The stabilized versions of this intrinsic are available on the integer
1916 /// primitives via the `wrapping_add` method. For example,
1917 /// [`u32::wrapping_add`]
1918 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1919 pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
1920 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1922 /// Note that, unlike most intrinsics, this is safe to call;
1923 /// it does not require an `unsafe` block.
1924 /// Therefore, implementations must not require the user to uphold
1925 /// any safety invariants.
1927 /// The stabilized versions of this intrinsic are available on the integer
1928 /// primitives via the `wrapping_sub` method. For example,
1929 /// [`u32::wrapping_sub`]
1930 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1931 pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
1932 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1934 /// Note that, unlike most intrinsics, this is safe to call;
1935 /// it does not require an `unsafe` block.
1936 /// Therefore, implementations must not require the user to uphold
1937 /// any safety invariants.
1939 /// The stabilized versions of this intrinsic are available on the integer
1940 /// primitives via the `wrapping_mul` method. For example,
1941 /// [`u32::wrapping_mul`]
1942 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1943 pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
1945 /// Computes `a + b`, saturating at numeric bounds.
1947 /// Note that, unlike most intrinsics, this is safe to call;
1948 /// it does not require an `unsafe` block.
1949 /// Therefore, implementations must not require the user to uphold
1950 /// any safety invariants.
1952 /// The stabilized versions of this intrinsic are available on the integer
1953 /// primitives via the `saturating_add` method. For example,
1954 /// [`u32::saturating_add`]
1955 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1956 pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
1957 /// Computes `a - b`, saturating at numeric bounds.
1959 /// Note that, unlike most intrinsics, this is safe to call;
1960 /// it does not require an `unsafe` block.
1961 /// Therefore, implementations must not require the user to uphold
1962 /// any safety invariants.
1964 /// The stabilized versions of this intrinsic are available on the integer
1965 /// primitives via the `saturating_sub` method. For example,
1966 /// [`u32::saturating_sub`]
1967 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1968 pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
1970 /// Returns the value of the discriminant for the variant in 'v';
1971 /// if `T` has no discriminant, returns `0`.
1973 /// Note that, unlike most intrinsics, this is safe to call;
1974 /// it does not require an `unsafe` block.
1975 /// Therefore, implementations must not require the user to uphold
1976 /// any safety invariants.
1978 /// The stabilized version of this intrinsic is [`core::mem::discriminant`].
1979 #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
1980 pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
1982 /// Returns the number of variants of the type `T` cast to a `usize`;
1983 /// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
1985 /// Note that, unlike most intrinsics, this is safe to call;
1986 /// it does not require an `unsafe` block.
1987 /// Therefore, implementations must not require the user to uphold
1988 /// any safety invariants.
1990 /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
1991 #[rustc_const_unstable(feature = "variant_count", issue = "73662")]
1992 pub fn variant_count<T>() -> usize;
1994 /// Rust's "try catch" construct which invokes the function pointer `try_fn`
1995 /// with the data pointer `data`.
1997 /// The third argument is a function called if a panic occurs. This function
1998 /// takes the data pointer and a pointer to the target-specific exception
1999 /// object that was caught. For more information see the compiler's
2000 /// source as well as std's catch implementation.
2001 pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
2003 /// Emits a `!nontemporal` store according to LLVM (see their docs).
2004 /// Probably will never become stable.
2005 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
2007 /// See documentation of `<*const T>::offset_from` for details.
2008 #[rustc_const_stable(feature = "const_ptr_offset_from", since = "1.65.0")]
2009 pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
2011 /// See documentation of `<*const T>::sub_ptr` for details.
2012 #[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
2013 pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
2015 /// See documentation of `<*const T>::guaranteed_eq` for details.
2017 /// Note that, unlike most intrinsics, this is safe to call;
2018 /// it does not require an `unsafe` block.
2019 /// Therefore, implementations must not require the user to uphold
2020 /// any safety invariants.
2021 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
2022 pub fn ptr_guaranteed_eq<T>(ptr: *const T, other: *const T) -> bool;
2024 /// See documentation of `<*const T>::guaranteed_ne` for details.
2026 /// Note that, unlike most intrinsics, this is safe to call;
2027 /// it does not require an `unsafe` block.
2028 /// Therefore, implementations must not require the user to uphold
2029 /// any safety invariants.
2030 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
2031 pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;
2033 /// Allocates a block of memory at compile time.
2034 /// At runtime, just returns a null pointer.
2038 /// - The `align` argument must be a power of two.
2039 /// - At compile time, a compile error occurs if this constraint is violated.
2040 /// - At runtime, it is not checked.
2041 #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2042 pub fn const_allocate(size: usize, align: usize) -> *mut u8;
2044 /// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
2045 /// At runtime, does nothing.
2049 /// - The `align` argument must be a power of two.
2050 /// - At compile time, a compile error occurs if this constraint is violated.
2051 /// - At runtime, it is not checked.
2052 /// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
2053 /// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
2054 #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2055 pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);
2057 /// Determines whether the raw bytes of the two values are equal.
2059 /// This is particularly handy for arrays, since it allows things like just
2060 /// comparing `i96`s instead of forcing `alloca`s for `[6 x i16]`.
2062 /// Above some backend-decided threshold this will emit calls to `memcmp`,
2063 /// like slice equality does, instead of causing massive code size.
2067 /// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized or carry a
2069 /// Note that this is a stricter criterion than just the *values* being
2070 /// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
2072 /// (The implementation is allowed to branch on the results of comparisons,
2073 /// which is UB if any of their inputs are `undef`.)
2074 #[rustc_const_unstable(feature = "const_intrinsic_raw_eq", issue = "none")]
2075 pub fn raw_eq<T>(a: &T, b: &T) -> bool;
2077 /// See documentation of [`std::hint::black_box`] for details.
2079 /// [`std::hint::black_box`]: crate::hint::black_box
2080 #[rustc_const_unstable(feature = "const_black_box", issue = "none")]
2081 pub fn black_box<T>(dummy: T) -> T;
2083 /// `ptr` must point to a vtable.
2084 /// The intrinsic will return the size stored in that vtable.
2085 pub fn vtable_size(ptr: *const ()) -> usize;
2087 /// `ptr` must point to a vtable.
2088 /// The intrinsic will return the alignment stored in that vtable.
2089 pub fn vtable_align(ptr: *const ()) -> usize;
2091 /// Selects which function to call depending on the context.
2093 /// If this function is evaluated at compile-time, then a call to this
2094 /// intrinsic will be replaced with a call to `called_in_const`. It gets
2095 /// replaced with a call to `called_at_rt` otherwise.
2097 /// # Type Requirements
2099 /// The two functions must be both function items. They cannot be function
2100 /// pointers or closures. The first function must be a `const fn`.
2102 /// `arg` will be the tupled arguments that will be passed to either one of
2103 /// the two functions, therefore, both functions must accept the same type of
2104 /// arguments. Both functions must return RET.
2108 /// The two functions must behave observably equivalent. Safe code in other
2109 /// crates may assume that calling a `const fn` at compile-time and at run-time
2110 /// produces the same result. A function that produces a different result when
2111 /// evaluated at run-time, or has any other observable side-effects, is
2114 /// Here is an example of how this could cause a problem:
2116 /// #![feature(const_eval_select)]
2117 /// #![feature(core_intrinsics)]
2118 /// use std::hint::unreachable_unchecked;
2119 /// use std::intrinsics::const_eval_select;
2122 /// pub const fn inconsistent() -> i32 {
2123 /// fn runtime() -> i32 { 1 }
2124 /// const fn compiletime() -> i32 { 2 }
2127 // // ⚠ This code violates the required equivalence of `compiletime`
2128 /// // and `runtime`.
2129 /// const_eval_select((), compiletime, runtime)
2134 /// const X: i32 = inconsistent();
2135 /// let x = inconsistent();
2136 /// if x != X { unsafe { unreachable_unchecked(); }}
2139 /// This code causes Undefined Behavior when being run, since the
2140 /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2141 /// which violates the principle that a `const fn` must behave the same at
2142 /// compile-time and at run-time. The unsafe code in crate B is fine.
2143 #[cfg(not(bootstrap))]
2144 #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2145 pub fn const_eval_select<ARG, F, G, RET>(arg: ARG, called_in_const: F, called_at_rt: G) -> RET
2147 G: FnOnce<ARG, Output = RET>,
2148 F: FnOnce<ARG, Output = RET>;
2151 // Some functions are defined here because they accidentally got made
2152 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
2153 // (`transmute` also falls into this category, but it cannot be wrapped due to the
2154 // check that `T` and `U` have the same size.)
2156 /// Check that the preconditions of an unsafe function are followed, if debug_assertions are on,
2157 /// and only at runtime.
2159 /// This macro should be called as `assert_unsafe_precondition!([Generics](name: Type) => Expression)`
2160 /// where the names specified will be moved into the macro as captured variables, and defines an item
2161 /// to call `const_eval_select` on. The tokens inside the square brackets are used to denote generics
2162 /// for the function declaractions and can be omitted if there is no generics.
2166 /// Invoking this macro is only sound if the following code is already UB when the passed
2167 /// expression evaluates to false.
2169 /// This macro expands to a check at runtime if debug_assertions is set. It has no effect at
2170 /// compile time, but the semantics of the contained `const_eval_select` must be the same at
2171 /// runtime and at compile time. Thus if the expression evaluates to false, this macro produces
2172 /// different behavior at compile time and at runtime, and invoking it is incorrect.
2174 /// So in a sense it is UB if this macro is useful, but we expect callers of `unsafe fn` to make
2175 /// the occasional mistake, and this check should help them figure things out.
2176 #[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn
2177 macro_rules! assert_unsafe_precondition {
2178 ($([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr) => {
2179 if cfg!(debug_assertions) {
2180 // allow non_snake_case to allow capturing const generics
2181 #[allow(non_snake_case)]
2183 fn runtime$(<$($tt)*>)?($($i:$ty),*) {
2185 // abort instead of panicking to reduce impact on code size
2186 ::core::intrinsics::abort();
2189 #[allow(non_snake_case)]
2190 const fn comptime$(<$($tt)*>)?($(_:$ty),*) {}
2192 ::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
2196 pub(crate) use assert_unsafe_precondition;
2198 /// Checks whether `ptr` is properly aligned with respect to
2199 /// `align_of::<T>()`.
2200 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
2201 !ptr.is_null() && ptr.is_aligned()
2204 /// Checks whether the regions of memory starting at `src` and `dst` of size
2205 /// `count * size_of::<T>()` do *not* overlap.
2206 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
2207 let src_usize = src.addr();
2208 let dst_usize = dst.addr();
2209 let size = mem::size_of::<T>().checked_mul(count).unwrap();
2210 let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
2211 // If the absolute distance between the ptrs is at least as big as the size of the buffer,
2212 // they do not overlap.
2216 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2217 /// and destination must *not* overlap.
2219 /// For regions of memory which might overlap, use [`copy`] instead.
2221 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
2222 /// with the argument order swapped.
2224 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2225 /// requirements of `T`. The initialization state is preserved exactly.
2227 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
2231 /// Behavior is undefined if any of the following conditions are violated:
2233 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2235 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2237 /// * Both `src` and `dst` must be properly aligned.
2239 /// * The region of memory beginning at `src` with a size of `count *
2240 /// size_of::<T>()` bytes must *not* overlap with the region of memory
2241 /// beginning at `dst` with the same size.
2243 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
2244 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
2245 /// in the region beginning at `*src` and the region beginning at `*dst` can
2246 /// [violate memory safety][read-ownership].
2248 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2249 /// `0`, the pointers must be non-null and properly aligned.
2251 /// [`read`]: crate::ptr::read
2252 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2253 /// [valid]: crate::ptr#safety
2257 /// Manually implement [`Vec::append`]:
2262 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
2263 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
2264 /// let src_len = src.len();
2265 /// let dst_len = dst.len();
2267 /// // Ensure that `dst` has enough capacity to hold all of `src`.
2268 /// dst.reserve(src_len);
2271 /// // The call to add is always safe because `Vec` will never
2272 /// // allocate more than `isize::MAX` bytes.
2273 /// let dst_ptr = dst.as_mut_ptr().add(dst_len);
2274 /// let src_ptr = src.as_ptr();
2276 /// // Truncate `src` without dropping its contents. We do this first,
2277 /// // to avoid problems in case something further down panics.
2280 /// // The two regions cannot overlap because mutable references do
2281 /// // not alias, and two different vectors cannot own the same
2283 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
2285 /// // Notify `dst` that it now holds the contents of `src`.
2286 /// dst.set_len(dst_len + src_len);
2290 /// let mut a = vec!['r'];
2291 /// let mut b = vec!['u', 's', 't'];
2293 /// append(&mut a, &mut b);
2295 /// assert_eq!(a, &['r', 'u', 's', 't']);
2296 /// assert!(b.is_empty());
2299 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
2300 #[doc(alias = "memcpy")]
2301 #[stable(feature = "rust1", since = "1.0.0")]
2302 #[rustc_allowed_through_unstable_modules]
2303 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2305 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2306 pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
2307 extern "rust-intrinsic" {
2308 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2309 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
2312 // SAFETY: the safety contract for `copy_nonoverlapping` must be
2313 // upheld by the caller.
2315 assert_unsafe_precondition!([T](src: *const T, dst: *mut T, count: usize) =>
2316 is_aligned_and_not_null(src)
2317 && is_aligned_and_not_null(dst)
2318 && is_nonoverlapping(src, dst, count)
2320 copy_nonoverlapping(src, dst, count)
2324 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2325 /// and destination may overlap.
2327 /// If the source and destination will *never* overlap,
2328 /// [`copy_nonoverlapping`] can be used instead.
2330 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
2331 /// order swapped. Copying takes place as if the bytes were copied from `src`
2332 /// to a temporary array and then copied from the array to `dst`.
2334 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2335 /// requirements of `T`. The initialization state is preserved exactly.
2337 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
2341 /// Behavior is undefined if any of the following conditions are violated:
2343 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2345 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2347 /// * Both `src` and `dst` must be properly aligned.
2349 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
2350 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
2351 /// in the region beginning at `*src` and the region beginning at `*dst` can
2352 /// [violate memory safety][read-ownership].
2354 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2355 /// `0`, the pointers must be non-null and properly aligned.
2357 /// [`read`]: crate::ptr::read
2358 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2359 /// [valid]: crate::ptr#safety
2363 /// Efficiently create a Rust vector from an unsafe buffer:
2370 /// /// * `ptr` must be correctly aligned for its type and non-zero.
2371 /// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
2372 /// /// * Those elements must not be used after calling this function unless `T: Copy`.
2373 /// # #[allow(dead_code)]
2374 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
2375 /// let mut dst = Vec::with_capacity(elts);
2377 /// // SAFETY: Our precondition ensures the source is aligned and valid,
2378 /// // and `Vec::with_capacity` ensures that we have usable space to write them.
2379 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
2381 /// // SAFETY: We created it with this much capacity earlier,
2382 /// // and the previous `copy` has initialized these elements.
2383 /// dst.set_len(elts);
2387 #[doc(alias = "memmove")]
2388 #[stable(feature = "rust1", since = "1.0.0")]
2389 #[rustc_allowed_through_unstable_modules]
2390 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2392 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2393 pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
2394 extern "rust-intrinsic" {
2395 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2396 fn copy<T>(src: *const T, dst: *mut T, count: usize);
2399 // SAFETY: the safety contract for `copy` must be upheld by the caller.
2401 assert_unsafe_precondition!([T](src: *const T, dst: *mut T) =>
2402 is_aligned_and_not_null(src) && is_aligned_and_not_null(dst));
2403 copy(src, dst, count)
2407 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
2410 /// `write_bytes` is similar to C's [`memset`], but sets `count *
2411 /// size_of::<T>()` bytes to `val`.
2413 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
2417 /// Behavior is undefined if any of the following conditions are violated:
2419 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2421 /// * `dst` must be properly aligned.
2423 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2424 /// `0`, the pointer must be non-null and properly aligned.
2426 /// Additionally, note that changing `*dst` in this way can easily lead to undefined behavior (UB)
2427 /// later if the written bytes are not a valid representation of some `T`. For instance, the
2428 /// following is an **incorrect** use of this function:
2432 /// let mut value: u8 = 0;
2433 /// let ptr: *mut bool = &mut value as *mut u8 as *mut bool;
2434 /// let _bool = ptr.read(); // This is fine, `ptr` points to a valid `bool`.
2435 /// ptr.write_bytes(42u8, 1); // This function itself does not cause UB...
2436 /// let _bool = ptr.read(); // ...but it makes this operation UB! ⚠️
2440 /// [valid]: crate::ptr#safety
2449 /// let mut vec = vec![0u32; 4];
2451 /// let vec_ptr = vec.as_mut_ptr();
2452 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
2454 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
2456 #[doc(alias = "memset")]
2457 #[stable(feature = "rust1", since = "1.0.0")]
2458 #[rustc_allowed_through_unstable_modules]
2459 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2461 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2462 pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
2463 extern "rust-intrinsic" {
2464 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2465 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
2468 // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
2470 assert_unsafe_precondition!([T](dst: *mut T) => is_aligned_and_not_null(dst));
2471 write_bytes(dst, val, count)
2477 feature = "const_eval_select",
2479 reason = "const_eval_select will never be stable"
2481 #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2482 #[lang = "const_eval_select"]
2483 #[rustc_do_not_const_check]
2485 pub const unsafe fn const_eval_select<ARG, F, G, RET>(
2487 _called_in_const: F,
2491 F: ~const FnOnce<ARG, Output = RET>,
2492 G: FnOnce<ARG, Output = RET> + ~const Destruct,
2494 called_at_rt.call_once(arg)
2499 feature = "const_eval_select",
2501 reason = "const_eval_select will never be stable"
2503 #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2504 #[lang = "const_eval_select_ct"]
2505 pub const unsafe fn const_eval_select_ct<ARG, F, G, RET>(
2511 F: ~const FnOnce<ARG, Output = RET>,
2512 G: FnOnce<ARG, Output = RET> + ~const Destruct,
2514 called_in_const.call_once(arg)