core/intrinsics/mod.rs
1//! Compiler intrinsics.
2//!
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>.
5//!
6//! # Const intrinsics
7//!
8//! In order to make an intrinsic unstable usable at compile-time, copy the implementation from
9//! <https://github.com/rust-lang/miri/blob/master/src/intrinsics> to
10//! <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>
11//! and make the intrinsic declaration below a `const fn`. This should be done in coordination with
12//! wg-const-eval.
13//!
14//! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
15//! `#[rustc_intrinsic_const_stable_indirect]` needs to be added to the intrinsic. Such a change requires
16//! T-lang approval, because it may bake a feature into the language that cannot be replicated in
17//! user code without compiler support.
18//!
19//! # Volatiles
20//!
21//! The volatile intrinsics provide operations intended to act on I/O
22//! memory, which are guaranteed to not be reordered by the compiler
23//! across other volatile intrinsics. See the LLVM documentation on
24//! [[volatile]].
25//!
26//! [volatile]: https://llvm.org/docs/LangRef.html#volatile-memory-accesses
27//!
28//! # Atomics
29//!
30//! The atomic intrinsics provide common atomic operations on machine
31//! words, with multiple possible memory orderings. They obey the same
32//! semantics as C++11. See the LLVM documentation on [[atomics]].
33//!
34//! [atomics]: https://llvm.org/docs/Atomics.html
35//!
36//! A quick refresher on memory ordering:
37//!
38//! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
39//! take place after the barrier.
40//! * Release - a barrier for releasing a lock. Preceding reads and writes
41//! take place before the barrier.
42//! * Sequentially consistent - sequentially consistent operations are
43//! guaranteed to happen in order. This is the standard mode for working
44//! with atomic types and is equivalent to Java's `volatile`.
45//!
46//! # Unwinding
47//!
48//! Rust intrinsics may, in general, unwind. If an intrinsic can never unwind, add the
49//! `#[rustc_nounwind]` attribute so that the compiler can make use of this fact.
50//!
51//! However, even for intrinsics that may unwind, rustc assumes that a Rust intrinsics will never
52//! initiate a foreign (non-Rust) unwind, and thus for panic=abort we can always assume that these
53//! intrinsics cannot unwind.
54
55#![unstable(
56 feature = "core_intrinsics",
57 reason = "intrinsics are unlikely to ever be stabilized, instead \
58 they should be used through stabilized interfaces \
59 in the rest of the standard library",
60 issue = "none"
61)]
62#![allow(missing_docs)]
63
64use crate::marker::{DiscriminantKind, Tuple};
65use crate::mem::SizedTypeProperties;
66use crate::{ptr, ub_checks};
67
68pub mod fallback;
69pub mod mir;
70pub mod simd;
71
72// These imports are used for simplifying intra-doc links
73#[allow(unused_imports)]
74#[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))]
75use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering};
76
77#[stable(feature = "drop_in_place", since = "1.8.0")]
78#[rustc_allowed_through_unstable_modules = "import this function via `std::ptr` instead"]
79#[deprecated(note = "no longer an intrinsic - use `ptr::drop_in_place` directly", since = "1.52.0")]
80#[inline]
81pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
82 // SAFETY: see `ptr::drop_in_place`
83 unsafe { crate::ptr::drop_in_place(to_drop) }
84}
85
86// N.B., these intrinsics take raw pointers because they mutate aliased
87// memory, which is not valid for either `&` or `&mut`.
88
89/// Stores a value if the current value is the same as the `old` value.
90/// `T` must be an integer or pointer type.
91///
92/// The stabilized version of this intrinsic is available on the
93/// [`atomic`] types via the `compare_exchange` method by passing
94/// [`Ordering::Relaxed`] as both the success and failure parameters.
95/// For example, [`AtomicBool::compare_exchange`].
96#[rustc_intrinsic]
97#[rustc_nounwind]
98pub unsafe fn atomic_cxchg_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
99/// Stores a value if the current value is the same as the `old` value.
100/// `T` must be an integer or pointer type.
101///
102/// The stabilized version of this intrinsic is available on the
103/// [`atomic`] types via the `compare_exchange` method by passing
104/// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
105/// For example, [`AtomicBool::compare_exchange`].
106#[rustc_intrinsic]
107#[rustc_nounwind]
108pub unsafe fn atomic_cxchg_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
109/// Stores a value if the current value is the same as the `old` value.
110/// `T` must be an integer or pointer type.
111///
112/// The stabilized version of this intrinsic is available on the
113/// [`atomic`] types via the `compare_exchange` method by passing
114/// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
115/// For example, [`AtomicBool::compare_exchange`].
116#[rustc_intrinsic]
117#[rustc_nounwind]
118pub unsafe fn atomic_cxchg_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
119/// Stores a value if the current value is the same as the `old` value.
120/// `T` must be an integer or pointer type.
121///
122/// The stabilized version of this intrinsic is available on the
123/// [`atomic`] types via the `compare_exchange` method by passing
124/// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
125/// For example, [`AtomicBool::compare_exchange`].
126#[rustc_intrinsic]
127#[rustc_nounwind]
128pub unsafe fn atomic_cxchg_acquire_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.
130/// `T` must be an integer or pointer type.
131///
132/// The stabilized version of this intrinsic is available on the
133/// [`atomic`] types via the `compare_exchange` method by passing
134/// [`Ordering::Acquire`] as both the success and failure parameters.
135/// For example, [`AtomicBool::compare_exchange`].
136#[rustc_intrinsic]
137#[rustc_nounwind]
138pub unsafe fn atomic_cxchg_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
139/// Stores a value if the current value is the same as the `old` value.
140/// `T` must be an integer or pointer type.
141///
142/// The stabilized version of this intrinsic is available on the
143/// [`atomic`] types via the `compare_exchange` method by passing
144/// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
145/// For example, [`AtomicBool::compare_exchange`].
146#[rustc_intrinsic]
147#[rustc_nounwind]
148pub unsafe fn atomic_cxchg_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
149/// Stores a value if the current value is the same as the `old` value.
150/// `T` must be an integer or pointer type.
151///
152/// The stabilized version of this intrinsic is available on the
153/// [`atomic`] types via the `compare_exchange` method by passing
154/// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
155/// For example, [`AtomicBool::compare_exchange`].
156#[rustc_intrinsic]
157#[rustc_nounwind]
158pub unsafe fn atomic_cxchg_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
159/// Stores a value if the current value is the same as the `old` value.
160/// `T` must be an integer or pointer type.
161///
162/// The stabilized version of this intrinsic is available on the
163/// [`atomic`] types via the `compare_exchange` method by passing
164/// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
165/// For example, [`AtomicBool::compare_exchange`].
166#[rustc_intrinsic]
167#[rustc_nounwind]
168pub unsafe fn atomic_cxchg_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
169/// Stores a value if the current value is the same as the `old` value.
170/// `T` must be an integer or pointer type.
171///
172/// The stabilized version of this intrinsic is available on the
173/// [`atomic`] types via the `compare_exchange` method by passing
174/// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
175/// For example, [`AtomicBool::compare_exchange`].
176#[rustc_intrinsic]
177#[rustc_nounwind]
178pub unsafe fn atomic_cxchg_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
179/// Stores a value if the current value is the same as the `old` value.
180/// `T` must be an integer or pointer type.
181///
182/// The stabilized version of this intrinsic is available on the
183/// [`atomic`] types via the `compare_exchange` method by passing
184/// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
185/// For example, [`AtomicBool::compare_exchange`].
186#[rustc_intrinsic]
187#[rustc_nounwind]
188pub unsafe fn atomic_cxchg_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
189/// Stores a value if the current value is the same as the `old` value.
190/// `T` must be an integer or pointer type.
191///
192/// The stabilized version of this intrinsic is available on the
193/// [`atomic`] types via the `compare_exchange` method by passing
194/// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
195/// For example, [`AtomicBool::compare_exchange`].
196#[rustc_intrinsic]
197#[rustc_nounwind]
198pub unsafe fn atomic_cxchg_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
199/// Stores a value if the current value is the same as the `old` value.
200/// `T` must be an integer or pointer type.
201///
202/// The stabilized version of this intrinsic is available on the
203/// [`atomic`] types via the `compare_exchange` method by passing
204/// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
205/// For example, [`AtomicBool::compare_exchange`].
206#[rustc_intrinsic]
207#[rustc_nounwind]
208pub unsafe fn atomic_cxchg_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
209/// Stores a value if the current value is the same as the `old` value.
210/// `T` must be an integer or pointer type.
211///
212/// The stabilized version of this intrinsic is available on the
213/// [`atomic`] types via the `compare_exchange` method by passing
214/// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
215/// For example, [`AtomicBool::compare_exchange`].
216#[rustc_intrinsic]
217#[rustc_nounwind]
218pub unsafe fn atomic_cxchg_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
219/// Stores a value if the current value is the same as the `old` value.
220/// `T` must be an integer or pointer type.
221///
222/// The stabilized version of this intrinsic is available on the
223/// [`atomic`] types via the `compare_exchange` method by passing
224/// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
225/// For example, [`AtomicBool::compare_exchange`].
226#[rustc_intrinsic]
227#[rustc_nounwind]
228pub unsafe fn atomic_cxchg_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
229/// Stores a value if the current value is the same as the `old` value.
230/// `T` must be an integer or pointer type.
231///
232/// The stabilized version of this intrinsic is available on the
233/// [`atomic`] types via the `compare_exchange` method by passing
234/// [`Ordering::SeqCst`] as both the success and failure parameters.
235/// For example, [`AtomicBool::compare_exchange`].
236#[rustc_intrinsic]
237#[rustc_nounwind]
238pub unsafe fn atomic_cxchg_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
239
240/// Stores a value if the current value is the same as the `old` value.
241/// `T` must be an integer or pointer type.
242///
243/// The stabilized version of this intrinsic is available on the
244/// [`atomic`] types via the `compare_exchange_weak` method by passing
245/// [`Ordering::Relaxed`] as both the success and failure parameters.
246/// For example, [`AtomicBool::compare_exchange_weak`].
247#[rustc_intrinsic]
248#[rustc_nounwind]
249pub unsafe fn atomic_cxchgweak_relaxed_relaxed<T: Copy>(
250 _dst: *mut T,
251 _old: T,
252 _src: T,
253) -> (T, bool);
254/// Stores a value if the current value is the same as the `old` value.
255/// `T` must be an integer or pointer type.
256///
257/// The stabilized version of this intrinsic is available on the
258/// [`atomic`] types via the `compare_exchange_weak` method by passing
259/// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
260/// For example, [`AtomicBool::compare_exchange_weak`].
261#[rustc_intrinsic]
262#[rustc_nounwind]
263pub unsafe fn atomic_cxchgweak_relaxed_acquire<T: Copy>(
264 _dst: *mut T,
265 _old: T,
266 _src: T,
267) -> (T, bool);
268/// Stores a value if the current value is the same as the `old` value.
269/// `T` must be an integer or pointer type.
270///
271/// The stabilized version of this intrinsic is available on the
272/// [`atomic`] types via the `compare_exchange_weak` method by passing
273/// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
274/// For example, [`AtomicBool::compare_exchange_weak`].
275#[rustc_intrinsic]
276#[rustc_nounwind]
277pub unsafe fn atomic_cxchgweak_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
278/// Stores a value if the current value is the same as the `old` value.
279/// `T` must be an integer or pointer type.
280///
281/// The stabilized version of this intrinsic is available on the
282/// [`atomic`] types via the `compare_exchange_weak` method by passing
283/// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
284/// For example, [`AtomicBool::compare_exchange_weak`].
285#[rustc_intrinsic]
286#[rustc_nounwind]
287pub unsafe fn atomic_cxchgweak_acquire_relaxed<T: Copy>(
288 _dst: *mut T,
289 _old: T,
290 _src: T,
291) -> (T, bool);
292/// Stores a value if the current value is the same as the `old` value.
293/// `T` must be an integer or pointer type.
294///
295/// The stabilized version of this intrinsic is available on the
296/// [`atomic`] types via the `compare_exchange_weak` method by passing
297/// [`Ordering::Acquire`] as both the success and failure parameters.
298/// For example, [`AtomicBool::compare_exchange_weak`].
299#[rustc_intrinsic]
300#[rustc_nounwind]
301pub unsafe fn atomic_cxchgweak_acquire_acquire<T: Copy>(
302 _dst: *mut T,
303 _old: T,
304 _src: T,
305) -> (T, bool);
306/// Stores a value if the current value is the same as the `old` value.
307/// `T` must be an integer or pointer type.
308///
309/// The stabilized version of this intrinsic is available on the
310/// [`atomic`] types via the `compare_exchange_weak` method by passing
311/// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
312/// For example, [`AtomicBool::compare_exchange_weak`].
313#[rustc_intrinsic]
314#[rustc_nounwind]
315pub unsafe fn atomic_cxchgweak_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
316/// Stores a value if the current value is the same as the `old` value.
317/// `T` must be an integer or pointer type.
318///
319/// The stabilized version of this intrinsic is available on the
320/// [`atomic`] types via the `compare_exchange_weak` method by passing
321/// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
322/// For example, [`AtomicBool::compare_exchange_weak`].
323#[rustc_intrinsic]
324#[rustc_nounwind]
325pub unsafe fn atomic_cxchgweak_release_relaxed<T: Copy>(
326 _dst: *mut T,
327 _old: T,
328 _src: T,
329) -> (T, bool);
330/// Stores a value if the current value is the same as the `old` value.
331/// `T` must be an integer or pointer type.
332///
333/// The stabilized version of this intrinsic is available on the
334/// [`atomic`] types via the `compare_exchange_weak` method by passing
335/// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
336/// For example, [`AtomicBool::compare_exchange_weak`].
337#[rustc_intrinsic]
338#[rustc_nounwind]
339pub unsafe fn atomic_cxchgweak_release_acquire<T: Copy>(
340 _dst: *mut T,
341 _old: T,
342 _src: T,
343) -> (T, bool);
344/// Stores a value if the current value is the same as the `old` value.
345/// `T` must be an integer or pointer type.
346///
347/// The stabilized version of this intrinsic is available on the
348/// [`atomic`] types via the `compare_exchange_weak` method by passing
349/// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
350/// For example, [`AtomicBool::compare_exchange_weak`].
351#[rustc_intrinsic]
352#[rustc_nounwind]
353pub unsafe fn atomic_cxchgweak_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
354/// Stores a value if the current value is the same as the `old` value.
355/// `T` must be an integer or pointer type.
356///
357/// The stabilized version of this intrinsic is available on the
358/// [`atomic`] types via the `compare_exchange_weak` method by passing
359/// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
360/// For example, [`AtomicBool::compare_exchange_weak`].
361#[rustc_intrinsic]
362#[rustc_nounwind]
363pub unsafe fn atomic_cxchgweak_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
364/// Stores a value if the current value is the same as the `old` value.
365/// `T` must be an integer or pointer type.
366///
367/// The stabilized version of this intrinsic is available on the
368/// [`atomic`] types via the `compare_exchange_weak` method by passing
369/// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
370/// For example, [`AtomicBool::compare_exchange_weak`].
371#[rustc_intrinsic]
372#[rustc_nounwind]
373pub unsafe fn atomic_cxchgweak_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
374/// Stores a value if the current value is the same as the `old` value.
375/// `T` must be an integer or pointer type.
376///
377/// The stabilized version of this intrinsic is available on the
378/// [`atomic`] types via the `compare_exchange_weak` method by passing
379/// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
380/// For example, [`AtomicBool::compare_exchange_weak`].
381#[rustc_intrinsic]
382#[rustc_nounwind]
383pub unsafe fn atomic_cxchgweak_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
384/// Stores a value if the current value is the same as the `old` value.
385/// `T` must be an integer or pointer type.
386///
387/// The stabilized version of this intrinsic is available on the
388/// [`atomic`] types via the `compare_exchange_weak` method by passing
389/// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
390/// For example, [`AtomicBool::compare_exchange_weak`].
391#[rustc_intrinsic]
392#[rustc_nounwind]
393pub unsafe fn atomic_cxchgweak_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
394/// Stores a value if the current value is the same as the `old` value.
395/// `T` must be an integer or pointer type.
396///
397/// The stabilized version of this intrinsic is available on the
398/// [`atomic`] types via the `compare_exchange_weak` method by passing
399/// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
400/// For example, [`AtomicBool::compare_exchange_weak`].
401#[rustc_intrinsic]
402#[rustc_nounwind]
403pub unsafe fn atomic_cxchgweak_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
404/// Stores a value if the current value is the same as the `old` value.
405/// `T` must be an integer or pointer type.
406///
407/// The stabilized version of this intrinsic is available on the
408/// [`atomic`] types via the `compare_exchange_weak` method by passing
409/// [`Ordering::SeqCst`] as both the success and failure parameters.
410/// For example, [`AtomicBool::compare_exchange_weak`].
411#[rustc_intrinsic]
412#[rustc_nounwind]
413pub unsafe fn atomic_cxchgweak_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
414
415/// Loads the current value of the pointer.
416/// `T` must be an integer or pointer type.
417///
418/// The stabilized version of this intrinsic is available on the
419/// [`atomic`] types via the `load` method by passing
420/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`].
421#[rustc_intrinsic]
422#[rustc_nounwind]
423pub unsafe fn atomic_load_seqcst<T: Copy>(src: *const T) -> T;
424/// Loads the current value of the pointer.
425/// `T` must be an integer or pointer type.
426///
427/// The stabilized version of this intrinsic is available on the
428/// [`atomic`] types via the `load` method by passing
429/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`].
430#[rustc_intrinsic]
431#[rustc_nounwind]
432pub unsafe fn atomic_load_acquire<T: Copy>(src: *const T) -> T;
433/// Loads the current value of the pointer.
434/// `T` must be an integer or pointer type.
435///
436/// The stabilized version of this intrinsic is available on the
437/// [`atomic`] types via the `load` method by passing
438/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`].
439#[rustc_intrinsic]
440#[rustc_nounwind]
441pub unsafe fn atomic_load_relaxed<T: Copy>(src: *const T) -> T;
442/// Do NOT use this intrinsic; "unordered" operations do not exist in our memory model!
443/// In terms of the Rust Abstract Machine, this operation is equivalent to `src.read()`,
444/// i.e., it performs a non-atomic read.
445#[rustc_intrinsic]
446#[rustc_nounwind]
447pub unsafe fn atomic_load_unordered<T: Copy>(src: *const T) -> T;
448
449/// Stores the value at the specified memory location.
450/// `T` must be an integer or pointer type.
451///
452/// The stabilized version of this intrinsic is available on the
453/// [`atomic`] types via the `store` method by passing
454/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`].
455#[rustc_intrinsic]
456#[rustc_nounwind]
457pub unsafe fn atomic_store_seqcst<T: Copy>(dst: *mut T, val: T);
458/// Stores the value at the specified memory location.
459/// `T` must be an integer or pointer type.
460///
461/// The stabilized version of this intrinsic is available on the
462/// [`atomic`] types via the `store` method by passing
463/// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`].
464#[rustc_intrinsic]
465#[rustc_nounwind]
466pub unsafe fn atomic_store_release<T: Copy>(dst: *mut T, val: T);
467/// Stores the value at the specified memory location.
468/// `T` must be an integer or pointer type.
469///
470/// The stabilized version of this intrinsic is available on the
471/// [`atomic`] types via the `store` method by passing
472/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`].
473#[rustc_intrinsic]
474#[rustc_nounwind]
475pub unsafe fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T);
476/// Do NOT use this intrinsic; "unordered" operations do not exist in our memory model!
477/// In terms of the Rust Abstract Machine, this operation is equivalent to `dst.write(val)`,
478/// i.e., it performs a non-atomic write.
479#[rustc_intrinsic]
480#[rustc_nounwind]
481pub unsafe fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T);
482
483/// Stores the value at the specified memory location, returning the old value.
484/// `T` must be an integer or pointer type.
485///
486/// The stabilized version of this intrinsic is available on the
487/// [`atomic`] types via the `swap` method by passing
488/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`].
489#[rustc_intrinsic]
490#[rustc_nounwind]
491pub unsafe fn atomic_xchg_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
492/// Stores the value at the specified memory location, returning the old value.
493/// `T` must be an integer or pointer type.
494///
495/// The stabilized version of this intrinsic is available on the
496/// [`atomic`] types via the `swap` method by passing
497/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`].
498#[rustc_intrinsic]
499#[rustc_nounwind]
500pub unsafe fn atomic_xchg_acquire<T: Copy>(dst: *mut T, src: T) -> T;
501/// Stores the value at the specified memory location, returning the old value.
502/// `T` must be an integer or pointer type.
503///
504/// The stabilized version of this intrinsic is available on the
505/// [`atomic`] types via the `swap` method by passing
506/// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`].
507#[rustc_intrinsic]
508#[rustc_nounwind]
509pub unsafe fn atomic_xchg_release<T: Copy>(dst: *mut T, src: T) -> T;
510/// Stores the value at the specified memory location, returning the old value.
511/// `T` must be an integer or pointer type.
512///
513/// The stabilized version of this intrinsic is available on the
514/// [`atomic`] types via the `swap` method by passing
515/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`].
516#[rustc_intrinsic]
517#[rustc_nounwind]
518pub unsafe fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
519/// Stores the value at the specified memory location, returning the old value.
520/// `T` must be an integer or pointer type.
521///
522/// The stabilized version of this intrinsic is available on the
523/// [`atomic`] types via the `swap` method by passing
524/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`].
525#[rustc_intrinsic]
526#[rustc_nounwind]
527pub unsafe fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
528
529/// Adds to the current value, returning the previous value.
530/// `T` must be an integer or pointer type.
531/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
532/// value stored at `*dst` will have the provenance of the old value stored there.
533///
534/// The stabilized version of this intrinsic is available on the
535/// [`atomic`] types via the `fetch_add` method by passing
536/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`].
537#[rustc_intrinsic]
538#[rustc_nounwind]
539pub unsafe fn atomic_xadd_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
540/// Adds to the current value, returning the previous value.
541/// `T` must be an integer or pointer type.
542/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
543/// value stored at `*dst` will have the provenance of the old value stored there.
544///
545/// The stabilized version of this intrinsic is available on the
546/// [`atomic`] types via the `fetch_add` method by passing
547/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`].
548#[rustc_intrinsic]
549#[rustc_nounwind]
550pub unsafe fn atomic_xadd_acquire<T: Copy>(dst: *mut T, src: T) -> T;
551/// Adds to the current value, returning the previous value.
552/// `T` must be an integer or pointer type.
553/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
554/// value stored at `*dst` will have the provenance of the old value stored there.
555///
556/// The stabilized version of this intrinsic is available on the
557/// [`atomic`] types via the `fetch_add` method by passing
558/// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`].
559#[rustc_intrinsic]
560#[rustc_nounwind]
561pub unsafe fn atomic_xadd_release<T: Copy>(dst: *mut T, src: T) -> T;
562/// Adds to the current value, returning the previous value.
563/// `T` must be an integer or pointer type.
564/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
565/// value stored at `*dst` will have the provenance of the old value stored there.
566///
567/// The stabilized version of this intrinsic is available on the
568/// [`atomic`] types via the `fetch_add` method by passing
569/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`].
570#[rustc_intrinsic]
571#[rustc_nounwind]
572pub unsafe fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
573/// Adds to the current value, returning the previous value.
574/// `T` must be an integer or pointer type.
575/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
576/// value stored at `*dst` will have the provenance of the old value stored there.
577///
578/// The stabilized version of this intrinsic is available on the
579/// [`atomic`] types via the `fetch_add` method by passing
580/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`].
581#[rustc_intrinsic]
582#[rustc_nounwind]
583pub unsafe fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
584
585/// Subtract from the current value, returning the previous value.
586/// `T` must be an integer or pointer type.
587/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
588/// value stored at `*dst` will have the provenance of the old value stored there.
589///
590/// The stabilized version of this intrinsic is available on the
591/// [`atomic`] types via the `fetch_sub` method by passing
592/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
593#[rustc_intrinsic]
594#[rustc_nounwind]
595pub unsafe fn atomic_xsub_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
596/// Subtract from the current value, returning the previous value.
597/// `T` must be an integer or pointer type.
598/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
599/// value stored at `*dst` will have the provenance of the old value stored there.
600///
601/// The stabilized version of this intrinsic is available on the
602/// [`atomic`] types via the `fetch_sub` method by passing
603/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
604#[rustc_intrinsic]
605#[rustc_nounwind]
606pub unsafe fn atomic_xsub_acquire<T: Copy>(dst: *mut T, src: T) -> T;
607/// Subtract from the current value, returning the previous value.
608/// `T` must be an integer or pointer type.
609/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
610/// value stored at `*dst` will have the provenance of the old value stored there.
611///
612/// The stabilized version of this intrinsic is available on the
613/// [`atomic`] types via the `fetch_sub` method by passing
614/// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
615#[rustc_intrinsic]
616#[rustc_nounwind]
617pub unsafe fn atomic_xsub_release<T: Copy>(dst: *mut T, src: T) -> T;
618/// Subtract from the current value, returning the previous value.
619/// `T` must be an integer or pointer type.
620/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
621/// value stored at `*dst` will have the provenance of the old value stored there.
622///
623/// The stabilized version of this intrinsic is available on the
624/// [`atomic`] types via the `fetch_sub` method by passing
625/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
626#[rustc_intrinsic]
627#[rustc_nounwind]
628pub unsafe fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
629/// Subtract from the current value, returning the previous value.
630/// `T` must be an integer or pointer type.
631/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
632/// value stored at `*dst` will have the provenance of the old value stored there.
633///
634/// The stabilized version of this intrinsic is available on the
635/// [`atomic`] types via the `fetch_sub` method by passing
636/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
637#[rustc_intrinsic]
638#[rustc_nounwind]
639pub unsafe fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
640
641/// Bitwise and with the current value, returning the previous value.
642/// `T` must be an integer or pointer type.
643/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
644/// value stored at `*dst` will have the provenance of the old value stored there.
645///
646/// The stabilized version of this intrinsic is available on the
647/// [`atomic`] types via the `fetch_and` method by passing
648/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`].
649#[rustc_intrinsic]
650#[rustc_nounwind]
651pub unsafe fn atomic_and_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
652/// Bitwise and with the current value, returning the previous value.
653/// `T` must be an integer or pointer type.
654/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
655/// value stored at `*dst` will have the provenance of the old value stored there.
656///
657/// The stabilized version of this intrinsic is available on the
658/// [`atomic`] types via the `fetch_and` method by passing
659/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`].
660#[rustc_intrinsic]
661#[rustc_nounwind]
662pub unsafe fn atomic_and_acquire<T: Copy>(dst: *mut T, src: T) -> T;
663/// Bitwise and with the current value, returning the previous value.
664/// `T` must be an integer or pointer type.
665/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
666/// value stored at `*dst` will have the provenance of the old value stored there.
667///
668/// The stabilized version of this intrinsic is available on the
669/// [`atomic`] types via the `fetch_and` method by passing
670/// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`].
671#[rustc_intrinsic]
672#[rustc_nounwind]
673pub unsafe fn atomic_and_release<T: Copy>(dst: *mut T, src: T) -> T;
674/// Bitwise and with the current value, returning the previous value.
675/// `T` must be an integer or pointer type.
676/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
677/// value stored at `*dst` will have the provenance of the old value stored there.
678///
679/// The stabilized version of this intrinsic is available on the
680/// [`atomic`] types via the `fetch_and` method by passing
681/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`].
682#[rustc_intrinsic]
683#[rustc_nounwind]
684pub unsafe fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
685/// Bitwise and with the current value, returning the previous value.
686/// `T` must be an integer or pointer type.
687/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
688/// value stored at `*dst` will have the provenance of the old value stored there.
689///
690/// The stabilized version of this intrinsic is available on the
691/// [`atomic`] types via the `fetch_and` method by passing
692/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`].
693#[rustc_intrinsic]
694#[rustc_nounwind]
695pub unsafe fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
696
697/// Bitwise nand with the current value, returning the previous value.
698/// `T` must be an integer or pointer type.
699/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
700/// value stored at `*dst` will have the provenance of the old value stored there.
701///
702/// The stabilized version of this intrinsic is available on the
703/// [`AtomicBool`] type via the `fetch_nand` method by passing
704/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`].
705#[rustc_intrinsic]
706#[rustc_nounwind]
707pub unsafe fn atomic_nand_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
708/// Bitwise nand with the current value, returning the previous value.
709/// `T` must be an integer or pointer type.
710/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
711/// value stored at `*dst` will have the provenance of the old value stored there.
712///
713/// The stabilized version of this intrinsic is available on the
714/// [`AtomicBool`] type via the `fetch_nand` method by passing
715/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`].
716#[rustc_intrinsic]
717#[rustc_nounwind]
718pub unsafe fn atomic_nand_acquire<T: Copy>(dst: *mut T, src: T) -> T;
719/// Bitwise nand with the current value, returning the previous value.
720/// `T` must be an integer or pointer type.
721/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
722/// value stored at `*dst` will have the provenance of the old value stored there.
723///
724/// The stabilized version of this intrinsic is available on the
725/// [`AtomicBool`] type via the `fetch_nand` method by passing
726/// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`].
727#[rustc_intrinsic]
728#[rustc_nounwind]
729pub unsafe fn atomic_nand_release<T: Copy>(dst: *mut T, src: T) -> T;
730/// Bitwise nand with the current value, returning the previous value.
731/// `T` must be an integer or pointer type.
732/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
733/// value stored at `*dst` will have the provenance of the old value stored there.
734///
735/// The stabilized version of this intrinsic is available on the
736/// [`AtomicBool`] type via the `fetch_nand` method by passing
737/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`].
738#[rustc_intrinsic]
739#[rustc_nounwind]
740pub unsafe fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
741/// Bitwise nand with the current value, returning the previous value.
742/// `T` must be an integer or pointer type.
743/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
744/// value stored at `*dst` will have the provenance of the old value stored there.
745///
746/// The stabilized version of this intrinsic is available on the
747/// [`AtomicBool`] type via the `fetch_nand` method by passing
748/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`].
749#[rustc_intrinsic]
750#[rustc_nounwind]
751pub unsafe fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
752
753/// Bitwise or with the current value, returning the previous value.
754/// `T` must be an integer or pointer type.
755/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
756/// value stored at `*dst` will have the provenance of the old value stored there.
757///
758/// The stabilized version of this intrinsic is available on the
759/// [`atomic`] types via the `fetch_or` method by passing
760/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`].
761#[rustc_intrinsic]
762#[rustc_nounwind]
763pub unsafe fn atomic_or_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
764/// Bitwise or with the current value, returning the previous value.
765/// `T` must be an integer or pointer type.
766/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
767/// value stored at `*dst` will have the provenance of the old value stored there.
768///
769/// The stabilized version of this intrinsic is available on the
770/// [`atomic`] types via the `fetch_or` method by passing
771/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`].
772#[rustc_intrinsic]
773#[rustc_nounwind]
774pub unsafe fn atomic_or_acquire<T: Copy>(dst: *mut T, src: T) -> T;
775/// Bitwise or with the current value, returning the previous value.
776/// `T` must be an integer or pointer type.
777/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
778/// value stored at `*dst` will have the provenance of the old value stored there.
779///
780/// The stabilized version of this intrinsic is available on the
781/// [`atomic`] types via the `fetch_or` method by passing
782/// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`].
783#[rustc_intrinsic]
784#[rustc_nounwind]
785pub unsafe fn atomic_or_release<T: Copy>(dst: *mut T, src: T) -> T;
786/// Bitwise or with the current value, returning the previous value.
787/// `T` must be an integer or pointer type.
788/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
789/// value stored at `*dst` will have the provenance of the old value stored there.
790///
791/// The stabilized version of this intrinsic is available on the
792/// [`atomic`] types via the `fetch_or` method by passing
793/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`].
794#[rustc_intrinsic]
795#[rustc_nounwind]
796pub unsafe fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
797/// Bitwise or with the current value, returning the previous value.
798/// `T` must be an integer or pointer type.
799/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
800/// value stored at `*dst` will have the provenance of the old value stored there.
801///
802/// The stabilized version of this intrinsic is available on the
803/// [`atomic`] types via the `fetch_or` method by passing
804/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`].
805#[rustc_intrinsic]
806#[rustc_nounwind]
807pub unsafe fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
808
809/// Bitwise xor with the current value, returning the previous value.
810/// `T` must be an integer or pointer type.
811/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
812/// value stored at `*dst` will have the provenance of the old value stored there.
813///
814/// The stabilized version of this intrinsic is available on the
815/// [`atomic`] types via the `fetch_xor` method by passing
816/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`].
817#[rustc_intrinsic]
818#[rustc_nounwind]
819pub unsafe fn atomic_xor_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
820/// Bitwise xor with the current value, returning the previous value.
821/// `T` must be an integer or pointer type.
822/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
823/// value stored at `*dst` will have the provenance of the old value stored there.
824///
825/// The stabilized version of this intrinsic is available on the
826/// [`atomic`] types via the `fetch_xor` method by passing
827/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`].
828#[rustc_intrinsic]
829#[rustc_nounwind]
830pub unsafe fn atomic_xor_acquire<T: Copy>(dst: *mut T, src: T) -> T;
831/// Bitwise xor with the current value, returning the previous value.
832/// `T` must be an integer or pointer type.
833/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
834/// value stored at `*dst` will have the provenance of the old value stored there.
835///
836/// The stabilized version of this intrinsic is available on the
837/// [`atomic`] types via the `fetch_xor` method by passing
838/// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`].
839#[rustc_intrinsic]
840#[rustc_nounwind]
841pub unsafe fn atomic_xor_release<T: Copy>(dst: *mut T, src: T) -> T;
842/// Bitwise xor with the current value, returning the previous value.
843/// `T` must be an integer or pointer type.
844/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
845/// value stored at `*dst` will have the provenance of the old value stored there.
846///
847/// The stabilized version of this intrinsic is available on the
848/// [`atomic`] types via the `fetch_xor` method by passing
849/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`].
850#[rustc_intrinsic]
851#[rustc_nounwind]
852pub unsafe fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
853/// Bitwise xor with the current value, returning the previous value.
854/// `T` must be an integer or pointer type.
855/// If `T` is a pointer type, the provenance of `src` is ignored: both the return value and the new
856/// value stored at `*dst` will have the provenance of the old value stored there.
857///
858/// The stabilized version of this intrinsic is available on the
859/// [`atomic`] types via the `fetch_xor` method by passing
860/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`].
861#[rustc_intrinsic]
862#[rustc_nounwind]
863pub unsafe fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
864
865/// Maximum with the current value using a signed comparison.
866/// `T` must be a signed integer type.
867///
868/// The stabilized version of this intrinsic is available on the
869/// [`atomic`] signed integer types via the `fetch_max` method by passing
870/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`].
871#[rustc_intrinsic]
872#[rustc_nounwind]
873pub unsafe fn atomic_max_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
874/// Maximum with the current value using a signed comparison.
875/// `T` must be a signed integer type.
876///
877/// The stabilized version of this intrinsic is available on the
878/// [`atomic`] signed integer types via the `fetch_max` method by passing
879/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`].
880#[rustc_intrinsic]
881#[rustc_nounwind]
882pub unsafe fn atomic_max_acquire<T: Copy>(dst: *mut T, src: T) -> T;
883/// Maximum with the current value using a signed comparison.
884/// `T` must be a signed integer type.
885///
886/// The stabilized version of this intrinsic is available on the
887/// [`atomic`] signed integer types via the `fetch_max` method by passing
888/// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`].
889#[rustc_intrinsic]
890#[rustc_nounwind]
891pub unsafe fn atomic_max_release<T: Copy>(dst: *mut T, src: T) -> T;
892/// Maximum with the current value using a signed comparison.
893/// `T` must be a signed integer type.
894///
895/// The stabilized version of this intrinsic is available on the
896/// [`atomic`] signed integer types via the `fetch_max` method by passing
897/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`].
898#[rustc_intrinsic]
899#[rustc_nounwind]
900pub unsafe fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
901/// Maximum with the current value using a signed comparison.
902/// `T` must be a signed integer type.
903///
904/// The stabilized version of this intrinsic is available on the
905/// [`atomic`] signed integer types via the `fetch_max` method by passing
906/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`].
907#[rustc_intrinsic]
908#[rustc_nounwind]
909pub unsafe fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
910
911/// Minimum with the current value using a signed comparison.
912/// `T` must be a signed integer type.
913///
914/// The stabilized version of this intrinsic is available on the
915/// [`atomic`] signed integer types via the `fetch_min` method by passing
916/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`].
917#[rustc_intrinsic]
918#[rustc_nounwind]
919pub unsafe fn atomic_min_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
920/// Minimum with the current value using a signed comparison.
921/// `T` must be a signed integer type.
922///
923/// The stabilized version of this intrinsic is available on the
924/// [`atomic`] signed integer types via the `fetch_min` method by passing
925/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`].
926#[rustc_intrinsic]
927#[rustc_nounwind]
928pub unsafe fn atomic_min_acquire<T: Copy>(dst: *mut T, src: T) -> T;
929/// Minimum with the current value using a signed comparison.
930/// `T` must be a signed integer type.
931///
932/// The stabilized version of this intrinsic is available on the
933/// [`atomic`] signed integer types via the `fetch_min` method by passing
934/// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`].
935#[rustc_intrinsic]
936#[rustc_nounwind]
937pub unsafe fn atomic_min_release<T: Copy>(dst: *mut T, src: T) -> T;
938/// Minimum with the current value using a signed comparison.
939/// `T` must be a signed integer type.
940///
941/// The stabilized version of this intrinsic is available on the
942/// [`atomic`] signed integer types via the `fetch_min` method by passing
943/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`].
944#[rustc_intrinsic]
945#[rustc_nounwind]
946pub unsafe fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
947/// Minimum with the current value using a signed comparison.
948/// `T` must be a signed integer type.
949///
950/// The stabilized version of this intrinsic is available on the
951/// [`atomic`] signed integer types via the `fetch_min` method by passing
952/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`].
953#[rustc_intrinsic]
954#[rustc_nounwind]
955pub unsafe fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
956
957/// Minimum with the current value using an unsigned comparison.
958/// `T` must be an unsigned integer type.
959///
960/// The stabilized version of this intrinsic is available on the
961/// [`atomic`] unsigned integer types via the `fetch_min` method by passing
962/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`].
963#[rustc_intrinsic]
964#[rustc_nounwind]
965pub unsafe fn atomic_umin_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
966/// Minimum with the current value using an unsigned comparison.
967/// `T` must be an unsigned integer type.
968///
969/// The stabilized version of this intrinsic is available on the
970/// [`atomic`] unsigned integer types via the `fetch_min` method by passing
971/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`].
972#[rustc_intrinsic]
973#[rustc_nounwind]
974pub unsafe fn atomic_umin_acquire<T: Copy>(dst: *mut T, src: T) -> T;
975/// Minimum with the current value using an unsigned comparison.
976/// `T` must be an unsigned integer type.
977///
978/// The stabilized version of this intrinsic is available on the
979/// [`atomic`] unsigned integer types via the `fetch_min` method by passing
980/// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`].
981#[rustc_intrinsic]
982#[rustc_nounwind]
983pub unsafe fn atomic_umin_release<T: Copy>(dst: *mut T, src: T) -> T;
984/// Minimum with the current value using an unsigned comparison.
985/// `T` must be an unsigned integer type.
986///
987/// The stabilized version of this intrinsic is available on the
988/// [`atomic`] unsigned integer types via the `fetch_min` method by passing
989/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`].
990#[rustc_intrinsic]
991#[rustc_nounwind]
992pub unsafe fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
993/// Minimum with the current value using an unsigned comparison.
994/// `T` must be an unsigned integer type.
995///
996/// The stabilized version of this intrinsic is available on the
997/// [`atomic`] unsigned integer types via the `fetch_min` method by passing
998/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`].
999#[rustc_intrinsic]
1000#[rustc_nounwind]
1001pub unsafe fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
1002
1003/// Maximum with the current value using an unsigned comparison.
1004/// `T` must be an unsigned integer type.
1005///
1006/// The stabilized version of this intrinsic is available on the
1007/// [`atomic`] unsigned integer types via the `fetch_max` method by passing
1008/// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`].
1009#[rustc_intrinsic]
1010#[rustc_nounwind]
1011pub unsafe fn atomic_umax_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
1012/// Maximum with the current value using an unsigned comparison.
1013/// `T` must be an unsigned integer type.
1014///
1015/// The stabilized version of this intrinsic is available on the
1016/// [`atomic`] unsigned integer types via the `fetch_max` method by passing
1017/// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`].
1018#[rustc_intrinsic]
1019#[rustc_nounwind]
1020pub unsafe fn atomic_umax_acquire<T: Copy>(dst: *mut T, src: T) -> T;
1021/// Maximum with the current value using an unsigned comparison.
1022/// `T` must be an unsigned integer type.
1023///
1024/// The stabilized version of this intrinsic is available on the
1025/// [`atomic`] unsigned integer types via the `fetch_max` method by passing
1026/// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`].
1027#[rustc_intrinsic]
1028#[rustc_nounwind]
1029pub unsafe fn atomic_umax_release<T: Copy>(dst: *mut T, src: T) -> T;
1030/// Maximum with the current value using an unsigned comparison.
1031/// `T` must be an unsigned integer type.
1032///
1033/// The stabilized version of this intrinsic is available on the
1034/// [`atomic`] unsigned integer types via the `fetch_max` method by passing
1035/// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`].
1036#[rustc_intrinsic]
1037#[rustc_nounwind]
1038pub unsafe fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
1039/// Maximum with the current value using an unsigned comparison.
1040/// `T` must be an unsigned integer type.
1041///
1042/// The stabilized version of this intrinsic is available on the
1043/// [`atomic`] unsigned integer types via the `fetch_max` method by passing
1044/// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`].
1045#[rustc_intrinsic]
1046#[rustc_nounwind]
1047pub unsafe fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
1048
1049/// An atomic fence.
1050///
1051/// The stabilized version of this intrinsic is available in
1052/// [`atomic::fence`] by passing [`Ordering::SeqCst`]
1053/// as the `order`.
1054#[rustc_intrinsic]
1055#[rustc_nounwind]
1056pub unsafe fn atomic_fence_seqcst();
1057/// An atomic fence.
1058///
1059/// The stabilized version of this intrinsic is available in
1060/// [`atomic::fence`] by passing [`Ordering::Acquire`]
1061/// as the `order`.
1062#[rustc_intrinsic]
1063#[rustc_nounwind]
1064pub unsafe fn atomic_fence_acquire();
1065/// An atomic fence.
1066///
1067/// The stabilized version of this intrinsic is available in
1068/// [`atomic::fence`] by passing [`Ordering::Release`]
1069/// as the `order`.
1070#[rustc_intrinsic]
1071#[rustc_nounwind]
1072pub unsafe fn atomic_fence_release();
1073/// An atomic fence.
1074///
1075/// The stabilized version of this intrinsic is available in
1076/// [`atomic::fence`] by passing [`Ordering::AcqRel`]
1077/// as the `order`.
1078#[rustc_intrinsic]
1079#[rustc_nounwind]
1080pub unsafe fn atomic_fence_acqrel();
1081
1082/// A compiler-only memory barrier.
1083///
1084/// Memory accesses will never be reordered across this barrier by the
1085/// compiler, but no instructions will be emitted for it. This is
1086/// appropriate for operations on the same thread that may be preempted,
1087/// such as when interacting with signal handlers.
1088///
1089/// The stabilized version of this intrinsic is available in
1090/// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`]
1091/// as the `order`.
1092#[rustc_intrinsic]
1093#[rustc_nounwind]
1094pub unsafe fn atomic_singlethreadfence_seqcst();
1095/// A compiler-only memory barrier.
1096///
1097/// Memory accesses will never be reordered across this barrier by the
1098/// compiler, but no instructions will be emitted for it. This is
1099/// appropriate for operations on the same thread that may be preempted,
1100/// such as when interacting with signal handlers.
1101///
1102/// The stabilized version of this intrinsic is available in
1103/// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`]
1104/// as the `order`.
1105#[rustc_intrinsic]
1106#[rustc_nounwind]
1107pub unsafe fn atomic_singlethreadfence_acquire();
1108/// A compiler-only memory barrier.
1109///
1110/// Memory accesses will never be reordered across this barrier by the
1111/// compiler, but no instructions will be emitted for it. This is
1112/// appropriate for operations on the same thread that may be preempted,
1113/// such as when interacting with signal handlers.
1114///
1115/// The stabilized version of this intrinsic is available in
1116/// [`atomic::compiler_fence`] by passing [`Ordering::Release`]
1117/// as the `order`.
1118#[rustc_intrinsic]
1119#[rustc_nounwind]
1120pub unsafe fn atomic_singlethreadfence_release();
1121/// A compiler-only memory barrier.
1122///
1123/// Memory accesses will never be reordered across this barrier by the
1124/// compiler, but no instructions will be emitted for it. This is
1125/// appropriate for operations on the same thread that may be preempted,
1126/// such as when interacting with signal handlers.
1127///
1128/// The stabilized version of this intrinsic is available in
1129/// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`]
1130/// as the `order`.
1131#[rustc_intrinsic]
1132#[rustc_nounwind]
1133pub unsafe fn atomic_singlethreadfence_acqrel();
1134
1135/// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
1136/// if supported; otherwise, it is a no-op.
1137/// Prefetches have no effect on the behavior of the program but can change its performance
1138/// characteristics.
1139///
1140/// The `locality` argument must be a constant integer and is a temporal locality specifier
1141/// ranging from (0) - no locality, to (3) - extremely local keep in cache.
1142///
1143/// This intrinsic does not have a stable counterpart.
1144#[rustc_intrinsic]
1145#[rustc_nounwind]
1146pub unsafe fn prefetch_read_data<T>(data: *const T, locality: i32);
1147/// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
1148/// if supported; otherwise, it is a no-op.
1149/// Prefetches have no effect on the behavior of the program but can change its performance
1150/// characteristics.
1151///
1152/// The `locality` argument must be a constant integer and is a temporal locality specifier
1153/// ranging from (0) - no locality, to (3) - extremely local keep in cache.
1154///
1155/// This intrinsic does not have a stable counterpart.
1156#[rustc_intrinsic]
1157#[rustc_nounwind]
1158pub unsafe fn prefetch_write_data<T>(data: *const T, locality: i32);
1159/// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
1160/// if supported; otherwise, it is a no-op.
1161/// Prefetches have no effect on the behavior of the program but can change its performance
1162/// characteristics.
1163///
1164/// The `locality` argument must be a constant integer and is a temporal locality specifier
1165/// ranging from (0) - no locality, to (3) - extremely local keep in cache.
1166///
1167/// This intrinsic does not have a stable counterpart.
1168#[rustc_intrinsic]
1169#[rustc_nounwind]
1170pub unsafe fn prefetch_read_instruction<T>(data: *const T, locality: i32);
1171/// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
1172/// if supported; otherwise, it is a no-op.
1173/// Prefetches have no effect on the behavior of the program but can change its performance
1174/// characteristics.
1175///
1176/// The `locality` argument must be a constant integer and is a temporal locality specifier
1177/// ranging from (0) - no locality, to (3) - extremely local keep in cache.
1178///
1179/// This intrinsic does not have a stable counterpart.
1180#[rustc_intrinsic]
1181#[rustc_nounwind]
1182pub unsafe fn prefetch_write_instruction<T>(data: *const T, locality: i32);
1183
1184/// Executes a breakpoint trap, for inspection by a debugger.
1185///
1186/// This intrinsic does not have a stable counterpart.
1187#[rustc_intrinsic]
1188#[rustc_nounwind]
1189pub fn breakpoint();
1190
1191/// Magic intrinsic that derives its meaning from attributes
1192/// attached to the function.
1193///
1194/// For example, dataflow uses this to inject static assertions so
1195/// that `rustc_peek(potentially_uninitialized)` would actually
1196/// double-check that dataflow did indeed compute that it is
1197/// uninitialized at that point in the control flow.
1198///
1199/// This intrinsic should not be used outside of the compiler.
1200#[rustc_nounwind]
1201#[rustc_intrinsic]
1202pub fn rustc_peek<T>(_: T) -> T;
1203
1204/// Aborts the execution of the process.
1205///
1206/// Note that, unlike most intrinsics, this is safe to call;
1207/// it does not require an `unsafe` block.
1208/// Therefore, implementations must not require the user to uphold
1209/// any safety invariants.
1210///
1211/// [`std::process::abort`](../../std/process/fn.abort.html) is to be preferred if possible,
1212/// as its behavior is more user-friendly and more stable.
1213///
1214/// The current implementation of `intrinsics::abort` is to invoke an invalid instruction,
1215/// on most platforms.
1216/// On Unix, the
1217/// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or
1218/// `SIGBUS`. The precise behavior is not guaranteed and not stable.
1219#[rustc_nounwind]
1220#[rustc_intrinsic]
1221pub fn abort() -> !;
1222
1223/// Informs the optimizer that this point in the code is not reachable,
1224/// enabling further optimizations.
1225///
1226/// N.B., this is very different from the `unreachable!()` macro: Unlike the
1227/// macro, which panics when it is executed, it is *undefined behavior* to
1228/// reach code marked with this function.
1229///
1230/// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`].
1231#[rustc_intrinsic_const_stable_indirect]
1232#[rustc_nounwind]
1233#[rustc_intrinsic]
1234pub const unsafe fn unreachable() -> !;
1235
1236/// Informs the optimizer that a condition is always true.
1237/// If the condition is false, the behavior is undefined.
1238///
1239/// No code is generated for this intrinsic, but the optimizer will try
1240/// to preserve it (and its condition) between passes, which may interfere
1241/// with optimization of surrounding code and reduce performance. It should
1242/// not be used if the invariant can be discovered by the optimizer on its
1243/// own, or if it does not enable any significant optimizations.
1244///
1245/// The stabilized version of this intrinsic is [`core::hint::assert_unchecked`].
1246#[rustc_intrinsic_const_stable_indirect]
1247#[rustc_nounwind]
1248#[unstable(feature = "core_intrinsics", issue = "none")]
1249#[rustc_intrinsic]
1250pub const unsafe fn assume(b: bool) {
1251 if !b {
1252 // SAFETY: the caller must guarantee the argument is never `false`
1253 unsafe { unreachable() }
1254 }
1255}
1256
1257/// Hints to the compiler that current code path is cold.
1258///
1259/// Note that, unlike most intrinsics, this is safe to call;
1260/// it does not require an `unsafe` block.
1261/// Therefore, implementations must not require the user to uphold
1262/// any safety invariants.
1263///
1264/// This intrinsic does not have a stable counterpart.
1265#[unstable(feature = "core_intrinsics", issue = "none")]
1266#[rustc_intrinsic]
1267#[rustc_nounwind]
1268#[miri::intrinsic_fallback_is_spec]
1269#[cold]
1270pub const fn cold_path() {}
1271
1272/// Hints to the compiler that branch condition is likely to be true.
1273/// Returns the value passed to it.
1274///
1275/// Any use other than with `if` statements will probably not have an effect.
1276///
1277/// Note that, unlike most intrinsics, this is safe to call;
1278/// it does not require an `unsafe` block.
1279/// Therefore, implementations must not require the user to uphold
1280/// any safety invariants.
1281///
1282/// This intrinsic does not have a stable counterpart.
1283#[unstable(feature = "core_intrinsics", issue = "none")]
1284#[rustc_nounwind]
1285#[inline(always)]
1286pub const fn likely(b: bool) -> bool {
1287 if b {
1288 true
1289 } else {
1290 cold_path();
1291 false
1292 }
1293}
1294
1295/// Hints to the compiler that branch condition is likely to be false.
1296/// Returns the value passed to it.
1297///
1298/// Any use other than with `if` statements will probably not have an effect.
1299///
1300/// Note that, unlike most intrinsics, this is safe to call;
1301/// it does not require an `unsafe` block.
1302/// Therefore, implementations must not require the user to uphold
1303/// any safety invariants.
1304///
1305/// This intrinsic does not have a stable counterpart.
1306#[unstable(feature = "core_intrinsics", issue = "none")]
1307#[rustc_nounwind]
1308#[inline(always)]
1309pub const fn unlikely(b: bool) -> bool {
1310 if b {
1311 cold_path();
1312 true
1313 } else {
1314 false
1315 }
1316}
1317
1318/// Returns either `true_val` or `false_val` depending on condition `b` with a
1319/// hint to the compiler that this condition is unlikely to be correctly
1320/// predicted by a CPU's branch predictor (e.g. a binary search).
1321///
1322/// This is otherwise functionally equivalent to `if b { true_val } else { false_val }`.
1323///
1324/// Note that, unlike most intrinsics, this is safe to call;
1325/// it does not require an `unsafe` block.
1326/// Therefore, implementations must not require the user to uphold
1327/// any safety invariants.
1328///
1329/// The public form of this instrinsic is [`core::hint::select_unpredictable`].
1330/// However unlike the public form, the intrinsic will not drop the value that
1331/// is not selected.
1332#[unstable(feature = "core_intrinsics", issue = "none")]
1333#[rustc_intrinsic]
1334#[rustc_nounwind]
1335#[miri::intrinsic_fallback_is_spec]
1336#[inline]
1337pub fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T {
1338 if b { true_val } else { false_val }
1339}
1340
1341/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
1342/// This will statically either panic, or do nothing.
1343///
1344/// This intrinsic does not have a stable counterpart.
1345#[rustc_intrinsic_const_stable_indirect]
1346#[rustc_nounwind]
1347#[rustc_intrinsic]
1348pub const fn assert_inhabited<T>();
1349
1350/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
1351/// zero-initialization: This will statically either panic, or do nothing.
1352///
1353/// This intrinsic does not have a stable counterpart.
1354#[rustc_intrinsic_const_stable_indirect]
1355#[rustc_nounwind]
1356#[rustc_intrinsic]
1357pub const fn assert_zero_valid<T>();
1358
1359/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing.
1360///
1361/// This intrinsic does not have a stable counterpart.
1362#[rustc_intrinsic_const_stable_indirect]
1363#[rustc_nounwind]
1364#[rustc_intrinsic]
1365pub const fn assert_mem_uninitialized_valid<T>();
1366
1367/// Gets a reference to a static `Location` indicating where it was called.
1368///
1369/// Note that, unlike most intrinsics, this is safe to call;
1370/// it does not require an `unsafe` block.
1371/// Therefore, implementations must not require the user to uphold
1372/// any safety invariants.
1373///
1374/// Consider using [`core::panic::Location::caller`] instead.
1375#[rustc_intrinsic_const_stable_indirect]
1376#[rustc_nounwind]
1377#[rustc_intrinsic]
1378pub const fn caller_location() -> &'static crate::panic::Location<'static>;
1379
1380/// Moves a value out of scope without running drop glue.
1381///
1382/// This exists solely for [`crate::mem::forget_unsized`]; normal `forget` uses
1383/// `ManuallyDrop` instead.
1384///
1385/// Note that, unlike most intrinsics, this is safe to call;
1386/// it does not require an `unsafe` block.
1387/// Therefore, implementations must not require the user to uphold
1388/// any safety invariants.
1389#[rustc_intrinsic_const_stable_indirect]
1390#[rustc_nounwind]
1391#[rustc_intrinsic]
1392pub const fn forget<T: ?Sized>(_: T);
1393
1394/// Reinterprets the bits of a value of one type as another type.
1395///
1396/// Both types must have the same size. Compilation will fail if this is not guaranteed.
1397///
1398/// `transmute` is semantically equivalent to a bitwise move of one type
1399/// into another. It copies the bits from the source value into the
1400/// destination value, then forgets the original. Note that source and destination
1401/// are passed by-value, which means if `Src` or `Dst` contain padding, that padding
1402/// is *not* guaranteed to be preserved by `transmute`.
1403///
1404/// Both the argument and the result must be [valid](../../nomicon/what-unsafe-does.html) at
1405/// their given type. Violating this condition leads to [undefined behavior][ub]. The compiler
1406/// will generate code *assuming that you, the programmer, ensure that there will never be
1407/// undefined behavior*. It is therefore your responsibility to guarantee that every value
1408/// passed to `transmute` is valid at both types `Src` and `Dst`. Failing to uphold this condition
1409/// may lead to unexpected and unstable compilation results. This makes `transmute` **incredibly
1410/// unsafe**. `transmute` should be the absolute last resort.
1411///
1412/// Because `transmute` is a by-value operation, alignment of the *transmuted values
1413/// themselves* is not a concern. As with any other function, the compiler already ensures
1414/// both `Src` and `Dst` are properly aligned. However, when transmuting values that *point
1415/// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper
1416/// alignment of the pointed-to values.
1417///
1418/// The [nomicon](../../nomicon/transmutes.html) has additional documentation.
1419///
1420/// [ub]: ../../reference/behavior-considered-undefined.html
1421///
1422/// # Transmutation between pointers and integers
1423///
1424/// Special care has to be taken when transmuting between pointers and integers, e.g.
1425/// transmuting between `*const ()` and `usize`.
1426///
1427/// Transmuting *pointers to integers* in a `const` context is [undefined behavior][ub], unless
1428/// the pointer was originally created *from* an integer. (That includes this function
1429/// specifically, integer-to-pointer casts, and helpers like [`dangling`][crate::ptr::dangling],
1430/// but also semantically-equivalent conversions such as punning through `repr(C)` union
1431/// fields.) Any attempt to use the resulting value for integer operations will abort
1432/// const-evaluation. (And even outside `const`, such transmutation is touching on many
1433/// unspecified aspects of the Rust memory model and should be avoided. See below for
1434/// alternatives.)
1435///
1436/// Transmuting *integers to pointers* is a largely unspecified operation. It is likely *not*
1437/// equivalent to an `as` cast. Doing non-zero-sized memory accesses with a pointer constructed
1438/// this way is currently considered undefined behavior.
1439///
1440/// All this also applies when the integer is nested inside an array, tuple, struct, or enum.
1441/// However, `MaybeUninit<usize>` is not considered an integer type for the purpose of this
1442/// section. Transmuting `*const ()` to `MaybeUninit<usize>` is fine---but then calling
1443/// `assume_init()` on that result is considered as completing the pointer-to-integer transmute
1444/// and thus runs into the issues discussed above.
1445///
1446/// In particular, doing a pointer-to-integer-to-pointer roundtrip via `transmute` is *not* a
1447/// lossless process. If you want to round-trip a pointer through an integer in a way that you
1448/// can get back the original pointer, you need to use `as` casts, or replace the integer type
1449/// by `MaybeUninit<$int>` (and never call `assume_init()`). If you are looking for a way to
1450/// store data of arbitrary type, also use `MaybeUninit<T>` (that will also handle uninitialized
1451/// memory due to padding). If you specifically need to store something that is "either an
1452/// integer or a pointer", use `*mut ()`: integers can be converted to pointers and back without
1453/// any loss (via `as` casts or via `transmute`).
1454///
1455/// # Examples
1456///
1457/// There are a few things that `transmute` is really useful for.
1458///
1459/// Turning a pointer into a function pointer. This is *not* portable to
1460/// machines where function pointers and data pointers have different sizes.
1461///
1462/// ```
1463/// fn foo() -> i32 {
1464/// 0
1465/// }
1466/// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer.
1467/// // This avoids an integer-to-pointer `transmute`, which can be problematic.
1468/// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine.
1469/// let pointer = foo as *const ();
1470/// let function = unsafe {
1471/// std::mem::transmute::<*const (), fn() -> i32>(pointer)
1472/// };
1473/// assert_eq!(function(), 0);
1474/// ```
1475///
1476/// Extending a lifetime, or shortening an invariant lifetime. This is
1477/// advanced, very unsafe Rust!
1478///
1479/// ```
1480/// struct R<'a>(&'a i32);
1481/// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
1482/// unsafe { std::mem::transmute::<R<'b>, R<'static>>(r) }
1483/// }
1484///
1485/// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
1486/// -> &'b mut R<'c> {
1487/// unsafe { std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r) }
1488/// }
1489/// ```
1490///
1491/// # Alternatives
1492///
1493/// Don't despair: many uses of `transmute` can be achieved through other means.
1494/// Below are common applications of `transmute` which can be replaced with safer
1495/// constructs.
1496///
1497/// Turning raw bytes (`[u8; SZ]`) into `u32`, `f64`, etc.:
1498///
1499/// ```
1500/// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
1501///
1502/// let num = unsafe {
1503/// std::mem::transmute::<[u8; 4], u32>(raw_bytes)
1504/// };
1505///
1506/// // use `u32::from_ne_bytes` instead
1507/// let num = u32::from_ne_bytes(raw_bytes);
1508/// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
1509/// let num = u32::from_le_bytes(raw_bytes);
1510/// assert_eq!(num, 0x12345678);
1511/// let num = u32::from_be_bytes(raw_bytes);
1512/// assert_eq!(num, 0x78563412);
1513/// ```
1514///
1515/// Turning a pointer into a `usize`:
1516///
1517/// ```no_run
1518/// let ptr = &0;
1519/// let ptr_num_transmute = unsafe {
1520/// std::mem::transmute::<&i32, usize>(ptr)
1521/// };
1522///
1523/// // Use an `as` cast instead
1524/// let ptr_num_cast = ptr as *const i32 as usize;
1525/// ```
1526///
1527/// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined
1528/// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave
1529/// as expected -- this is touching on many unspecified aspects of the Rust memory model.
1530/// Depending on what the code is doing, the following alternatives are preferable to
1531/// pointer-to-integer transmutation:
1532/// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a
1533/// type for that buffer, it can use [`MaybeUninit`][crate::mem::MaybeUninit].
1534/// - If the code actually wants to work on the address the pointer points to, it can use `as`
1535/// casts or [`ptr.addr()`][pointer::addr].
1536///
1537/// Turning a `*mut T` into a `&mut T`:
1538///
1539/// ```
1540/// let ptr: *mut i32 = &mut 0;
1541/// let ref_transmuted = unsafe {
1542/// std::mem::transmute::<*mut i32, &mut i32>(ptr)
1543/// };
1544///
1545/// // Use a reborrow instead
1546/// let ref_casted = unsafe { &mut *ptr };
1547/// ```
1548///
1549/// Turning a `&mut T` into a `&mut U`:
1550///
1551/// ```
1552/// let ptr = &mut 0;
1553/// let val_transmuted = unsafe {
1554/// std::mem::transmute::<&mut i32, &mut u32>(ptr)
1555/// };
1556///
1557/// // Now, put together `as` and reborrowing - note the chaining of `as`
1558/// // `as` is not transitive
1559/// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
1560/// ```
1561///
1562/// Turning a `&str` into a `&[u8]`:
1563///
1564/// ```
1565/// // this is not a good way to do this.
1566/// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
1567/// assert_eq!(slice, &[82, 117, 115, 116]);
1568///
1569/// // You could use `str::as_bytes`
1570/// let slice = "Rust".as_bytes();
1571/// assert_eq!(slice, &[82, 117, 115, 116]);
1572///
1573/// // Or, just use a byte string, if you have control over the string
1574/// // literal
1575/// assert_eq!(b"Rust", &[82, 117, 115, 116]);
1576/// ```
1577///
1578/// Turning a `Vec<&T>` into a `Vec<Option<&T>>`.
1579///
1580/// To transmute the inner type of the contents of a container, you must make sure to not
1581/// violate any of the container's invariants. For `Vec`, this means that both the size
1582/// *and alignment* of the inner types have to match. Other containers might rely on the
1583/// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't
1584/// be possible at all without violating the container invariants.
1585///
1586/// ```
1587/// let store = [0, 1, 2, 3];
1588/// let v_orig = store.iter().collect::<Vec<&i32>>();
1589///
1590/// // clone the vector as we will reuse them later
1591/// let v_clone = v_orig.clone();
1592///
1593/// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
1594/// // bad idea and could cause Undefined Behavior.
1595/// // However, it is no-copy.
1596/// let v_transmuted = unsafe {
1597/// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
1598/// };
1599///
1600/// let v_clone = v_orig.clone();
1601///
1602/// // This is the suggested, safe way.
1603/// // It may copy the entire vector into a new one though, but also may not.
1604/// let v_collected = v_clone.into_iter()
1605/// .map(Some)
1606/// .collect::<Vec<Option<&i32>>>();
1607///
1608/// let v_clone = v_orig.clone();
1609///
1610/// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the
1611/// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but
1612/// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`),
1613/// // this has all the same caveats. Besides the information provided above, also consult the
1614/// // [`from_raw_parts`] documentation.
1615/// let v_from_raw = unsafe {
1616// FIXME Update this when vec_into_raw_parts is stabilized
1617/// // Ensure the original vector is not dropped.
1618/// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
1619/// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
1620/// v_clone.len(),
1621/// v_clone.capacity())
1622/// };
1623/// ```
1624///
1625/// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
1626///
1627/// Implementing `split_at_mut`:
1628///
1629/// ```
1630/// use std::{slice, mem};
1631///
1632/// // There are multiple ways to do this, and there are multiple problems
1633/// // with the following (transmute) way.
1634/// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
1635/// -> (&mut [T], &mut [T]) {
1636/// let len = slice.len();
1637/// assert!(mid <= len);
1638/// unsafe {
1639/// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
1640/// // first: transmute is not type safe; all it checks is that T and
1641/// // U are of the same size. Second, right here, you have two
1642/// // mutable references pointing to the same memory.
1643/// (&mut slice[0..mid], &mut slice2[mid..len])
1644/// }
1645/// }
1646///
1647/// // This gets rid of the type safety problems; `&mut *` will *only* give
1648/// // you a `&mut T` from a `&mut T` or `*mut T`.
1649/// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
1650/// -> (&mut [T], &mut [T]) {
1651/// let len = slice.len();
1652/// assert!(mid <= len);
1653/// unsafe {
1654/// let slice2 = &mut *(slice as *mut [T]);
1655/// // however, you still have two mutable references pointing to
1656/// // the same memory.
1657/// (&mut slice[0..mid], &mut slice2[mid..len])
1658/// }
1659/// }
1660///
1661/// // This is how the standard library does it. This is the best method, if
1662/// // you need to do something like this
1663/// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
1664/// -> (&mut [T], &mut [T]) {
1665/// let len = slice.len();
1666/// assert!(mid <= len);
1667/// unsafe {
1668/// let ptr = slice.as_mut_ptr();
1669/// // This now has three mutable references pointing at the same
1670/// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
1671/// // `slice` is never used after `let ptr = ...`, and so one can
1672/// // treat it as "dead", and therefore, you only have two real
1673/// // mutable slices.
1674/// (slice::from_raw_parts_mut(ptr, mid),
1675/// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
1676/// }
1677/// }
1678/// ```
1679#[stable(feature = "rust1", since = "1.0.0")]
1680#[rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead"]
1681#[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
1682#[rustc_diagnostic_item = "transmute"]
1683#[rustc_nounwind]
1684#[rustc_intrinsic]
1685pub const unsafe fn transmute<Src, Dst>(src: Src) -> Dst;
1686
1687/// Like [`transmute`], but even less checked at compile-time: rather than
1688/// giving an error for `size_of::<Src>() != size_of::<Dst>()`, it's
1689/// **Undefined Behavior** at runtime.
1690///
1691/// Prefer normal `transmute` where possible, for the extra checking, since
1692/// both do exactly the same thing at runtime, if they both compile.
1693///
1694/// This is not expected to ever be exposed directly to users, rather it
1695/// may eventually be exposed through some more-constrained API.
1696#[rustc_intrinsic_const_stable_indirect]
1697#[rustc_nounwind]
1698#[rustc_intrinsic]
1699pub const unsafe fn transmute_unchecked<Src, Dst>(src: Src) -> Dst;
1700
1701/// Returns `true` if the actual type given as `T` requires drop
1702/// glue; returns `false` if the actual type provided for `T`
1703/// implements `Copy`.
1704///
1705/// If the actual type neither requires drop glue nor implements
1706/// `Copy`, then the return value of this function is unspecified.
1707///
1708/// Note that, unlike most intrinsics, this is safe to call;
1709/// it does not require an `unsafe` block.
1710/// Therefore, implementations must not require the user to uphold
1711/// any safety invariants.
1712///
1713/// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1714#[rustc_intrinsic_const_stable_indirect]
1715#[rustc_nounwind]
1716#[rustc_intrinsic]
1717pub const fn needs_drop<T: ?Sized>() -> bool;
1718
1719/// Calculates the offset from a pointer.
1720///
1721/// This is implemented as an intrinsic to avoid converting to and from an
1722/// integer, since the conversion would throw away aliasing information.
1723///
1724/// This can only be used with `Ptr` as a raw pointer type (`*mut` or `*const`)
1725/// to a `Sized` pointee and with `Delta` as `usize` or `isize`. Any other
1726/// instantiations may arbitrarily misbehave, and that's *not* a compiler bug.
1727///
1728/// # Safety
1729///
1730/// If the computed offset is non-zero, then both the starting and resulting pointer must be
1731/// either in bounds or at the end of an allocated object. If either pointer is out
1732/// of bounds or arithmetic overflow occurs then this operation is undefined behavior.
1733///
1734/// The stabilized version of this intrinsic is [`pointer::offset`].
1735#[must_use = "returns a new pointer rather than modifying its argument"]
1736#[rustc_intrinsic_const_stable_indirect]
1737#[rustc_nounwind]
1738#[rustc_intrinsic]
1739pub const unsafe fn offset<Ptr, Delta>(dst: Ptr, offset: Delta) -> Ptr;
1740
1741/// Calculates the offset from a pointer, potentially wrapping.
1742///
1743/// This is implemented as an intrinsic to avoid converting to and from an
1744/// integer, since the conversion inhibits certain optimizations.
1745///
1746/// # Safety
1747///
1748/// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1749/// resulting pointer to point into or at the end of an allocated
1750/// object, and it wraps with two's complement arithmetic. The resulting
1751/// value is not necessarily valid to be used to actually access memory.
1752///
1753/// The stabilized version of this intrinsic is [`pointer::wrapping_offset`].
1754#[must_use = "returns a new pointer rather than modifying its argument"]
1755#[rustc_intrinsic_const_stable_indirect]
1756#[rustc_nounwind]
1757#[rustc_intrinsic]
1758pub const unsafe fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1759
1760/// Masks out bits of the pointer according to a mask.
1761///
1762/// Note that, unlike most intrinsics, this is safe to call;
1763/// it does not require an `unsafe` block.
1764/// Therefore, implementations must not require the user to uphold
1765/// any safety invariants.
1766///
1767/// Consider using [`pointer::mask`] instead.
1768#[rustc_nounwind]
1769#[rustc_intrinsic]
1770pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;
1771
1772/// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1773/// a size of `count` * `size_of::<T>()` and an alignment of
1774/// `min_align_of::<T>()`
1775///
1776/// This intrinsic does not have a stable counterpart.
1777/// # Safety
1778///
1779/// The safety requirements are consistent with [`copy_nonoverlapping`]
1780/// while the read and write behaviors are volatile,
1781/// which means it will not be optimized out unless `_count` or `size_of::<T>()` is equal to zero.
1782///
1783/// [`copy_nonoverlapping`]: ptr::copy_nonoverlapping
1784#[rustc_intrinsic]
1785#[rustc_nounwind]
1786pub unsafe fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1787/// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1788/// a size of `count * size_of::<T>()` and an alignment of
1789/// `min_align_of::<T>()`
1790///
1791/// The volatile parameter is set to `true`, so it will not be optimized out
1792/// unless size is equal to zero.
1793///
1794/// This intrinsic does not have a stable counterpart.
1795#[rustc_intrinsic]
1796#[rustc_nounwind]
1797pub unsafe fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1798/// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1799/// size of `count * size_of::<T>()` and an alignment of
1800/// `min_align_of::<T>()`.
1801///
1802/// This intrinsic does not have a stable counterpart.
1803/// # Safety
1804///
1805/// The safety requirements are consistent with [`write_bytes`] while the write behavior is volatile,
1806/// which means it will not be optimized out unless `_count` or `size_of::<T>()` is equal to zero.
1807///
1808/// [`write_bytes`]: ptr::write_bytes
1809#[rustc_intrinsic]
1810#[rustc_nounwind]
1811pub unsafe fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1812
1813/// Performs a volatile load from the `src` pointer.
1814///
1815/// The stabilized version of this intrinsic is [`core::ptr::read_volatile`].
1816#[rustc_intrinsic]
1817#[rustc_nounwind]
1818pub unsafe fn volatile_load<T>(src: *const T) -> T;
1819/// Performs a volatile store to the `dst` pointer.
1820///
1821/// The stabilized version of this intrinsic is [`core::ptr::write_volatile`].
1822#[rustc_intrinsic]
1823#[rustc_nounwind]
1824pub unsafe fn volatile_store<T>(dst: *mut T, val: T);
1825
1826/// Performs a volatile load from the `src` pointer
1827/// The pointer is not required to be aligned.
1828///
1829/// This intrinsic does not have a stable counterpart.
1830#[rustc_intrinsic]
1831#[rustc_nounwind]
1832#[rustc_diagnostic_item = "intrinsics_unaligned_volatile_load"]
1833pub unsafe fn unaligned_volatile_load<T>(src: *const T) -> T;
1834/// Performs a volatile store to the `dst` pointer.
1835/// The pointer is not required to be aligned.
1836///
1837/// This intrinsic does not have a stable counterpart.
1838#[rustc_intrinsic]
1839#[rustc_nounwind]
1840#[rustc_diagnostic_item = "intrinsics_unaligned_volatile_store"]
1841pub unsafe fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1842
1843/// Returns the square root of an `f16`
1844///
1845/// The stabilized version of this intrinsic is
1846/// [`f16::sqrt`](../../std/primitive.f16.html#method.sqrt)
1847#[rustc_intrinsic]
1848#[rustc_nounwind]
1849pub unsafe fn sqrtf16(x: f16) -> f16;
1850/// Returns the square root of an `f32`
1851///
1852/// The stabilized version of this intrinsic is
1853/// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1854#[rustc_intrinsic]
1855#[rustc_nounwind]
1856pub unsafe fn sqrtf32(x: f32) -> f32;
1857/// Returns the square root of an `f64`
1858///
1859/// The stabilized version of this intrinsic is
1860/// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1861#[rustc_intrinsic]
1862#[rustc_nounwind]
1863pub unsafe fn sqrtf64(x: f64) -> f64;
1864/// Returns the square root of an `f128`
1865///
1866/// The stabilized version of this intrinsic is
1867/// [`f128::sqrt`](../../std/primitive.f128.html#method.sqrt)
1868#[rustc_intrinsic]
1869#[rustc_nounwind]
1870pub unsafe fn sqrtf128(x: f128) -> f128;
1871
1872/// Raises an `f16` to an integer power.
1873///
1874/// The stabilized version of this intrinsic is
1875/// [`f16::powi`](../../std/primitive.f16.html#method.powi)
1876#[rustc_intrinsic]
1877#[rustc_nounwind]
1878pub unsafe fn powif16(a: f16, x: i32) -> f16;
1879/// Raises an `f32` to an integer power.
1880///
1881/// The stabilized version of this intrinsic is
1882/// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1883#[rustc_intrinsic]
1884#[rustc_nounwind]
1885pub unsafe fn powif32(a: f32, x: i32) -> f32;
1886/// Raises an `f64` to an integer power.
1887///
1888/// The stabilized version of this intrinsic is
1889/// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1890#[rustc_intrinsic]
1891#[rustc_nounwind]
1892pub unsafe fn powif64(a: f64, x: i32) -> f64;
1893/// Raises an `f128` to an integer power.
1894///
1895/// The stabilized version of this intrinsic is
1896/// [`f128::powi`](../../std/primitive.f128.html#method.powi)
1897#[rustc_intrinsic]
1898#[rustc_nounwind]
1899pub unsafe fn powif128(a: f128, x: i32) -> f128;
1900
1901/// Returns the sine of an `f16`.
1902///
1903/// The stabilized version of this intrinsic is
1904/// [`f16::sin`](../../std/primitive.f16.html#method.sin)
1905#[rustc_intrinsic]
1906#[rustc_nounwind]
1907pub unsafe fn sinf16(x: f16) -> f16;
1908/// Returns the sine of an `f32`.
1909///
1910/// The stabilized version of this intrinsic is
1911/// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1912#[rustc_intrinsic]
1913#[rustc_nounwind]
1914pub unsafe fn sinf32(x: f32) -> f32;
1915/// Returns the sine of an `f64`.
1916///
1917/// The stabilized version of this intrinsic is
1918/// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1919#[rustc_intrinsic]
1920#[rustc_nounwind]
1921pub unsafe fn sinf64(x: f64) -> f64;
1922/// Returns the sine of an `f128`.
1923///
1924/// The stabilized version of this intrinsic is
1925/// [`f128::sin`](../../std/primitive.f128.html#method.sin)
1926#[rustc_intrinsic]
1927#[rustc_nounwind]
1928pub unsafe fn sinf128(x: f128) -> f128;
1929
1930/// Returns the cosine of an `f16`.
1931///
1932/// The stabilized version of this intrinsic is
1933/// [`f16::cos`](../../std/primitive.f16.html#method.cos)
1934#[rustc_intrinsic]
1935#[rustc_nounwind]
1936pub unsafe fn cosf16(x: f16) -> f16;
1937/// Returns the cosine of an `f32`.
1938///
1939/// The stabilized version of this intrinsic is
1940/// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1941#[rustc_intrinsic]
1942#[rustc_nounwind]
1943pub unsafe fn cosf32(x: f32) -> f32;
1944/// Returns the cosine of an `f64`.
1945///
1946/// The stabilized version of this intrinsic is
1947/// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1948#[rustc_intrinsic]
1949#[rustc_nounwind]
1950pub unsafe fn cosf64(x: f64) -> f64;
1951/// Returns the cosine of an `f128`.
1952///
1953/// The stabilized version of this intrinsic is
1954/// [`f128::cos`](../../std/primitive.f128.html#method.cos)
1955#[rustc_intrinsic]
1956#[rustc_nounwind]
1957pub unsafe fn cosf128(x: f128) -> f128;
1958
1959/// Raises an `f16` to an `f16` power.
1960///
1961/// The stabilized version of this intrinsic is
1962/// [`f16::powf`](../../std/primitive.f16.html#method.powf)
1963#[rustc_intrinsic]
1964#[rustc_nounwind]
1965pub unsafe fn powf16(a: f16, x: f16) -> f16;
1966/// Raises an `f32` to an `f32` power.
1967///
1968/// The stabilized version of this intrinsic is
1969/// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1970#[rustc_intrinsic]
1971#[rustc_nounwind]
1972pub unsafe fn powf32(a: f32, x: f32) -> f32;
1973/// Raises an `f64` to an `f64` power.
1974///
1975/// The stabilized version of this intrinsic is
1976/// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1977#[rustc_intrinsic]
1978#[rustc_nounwind]
1979pub unsafe fn powf64(a: f64, x: f64) -> f64;
1980/// Raises an `f128` to an `f128` power.
1981///
1982/// The stabilized version of this intrinsic is
1983/// [`f128::powf`](../../std/primitive.f128.html#method.powf)
1984#[rustc_intrinsic]
1985#[rustc_nounwind]
1986pub unsafe fn powf128(a: f128, x: f128) -> f128;
1987
1988/// Returns the exponential of an `f16`.
1989///
1990/// The stabilized version of this intrinsic is
1991/// [`f16::exp`](../../std/primitive.f16.html#method.exp)
1992#[rustc_intrinsic]
1993#[rustc_nounwind]
1994pub unsafe fn expf16(x: f16) -> f16;
1995/// Returns the exponential of an `f32`.
1996///
1997/// The stabilized version of this intrinsic is
1998/// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1999#[rustc_intrinsic]
2000#[rustc_nounwind]
2001pub unsafe fn expf32(x: f32) -> f32;
2002/// Returns the exponential of an `f64`.
2003///
2004/// The stabilized version of this intrinsic is
2005/// [`f64::exp`](../../std/primitive.f64.html#method.exp)
2006#[rustc_intrinsic]
2007#[rustc_nounwind]
2008pub unsafe fn expf64(x: f64) -> f64;
2009/// Returns the exponential of an `f128`.
2010///
2011/// The stabilized version of this intrinsic is
2012/// [`f128::exp`](../../std/primitive.f128.html#method.exp)
2013#[rustc_intrinsic]
2014#[rustc_nounwind]
2015pub unsafe fn expf128(x: f128) -> f128;
2016
2017/// Returns 2 raised to the power of an `f16`.
2018///
2019/// The stabilized version of this intrinsic is
2020/// [`f16::exp2`](../../std/primitive.f16.html#method.exp2)
2021#[rustc_intrinsic]
2022#[rustc_nounwind]
2023pub unsafe fn exp2f16(x: f16) -> f16;
2024/// Returns 2 raised to the power of an `f32`.
2025///
2026/// The stabilized version of this intrinsic is
2027/// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
2028#[rustc_intrinsic]
2029#[rustc_nounwind]
2030pub unsafe fn exp2f32(x: f32) -> f32;
2031/// Returns 2 raised to the power of an `f64`.
2032///
2033/// The stabilized version of this intrinsic is
2034/// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
2035#[rustc_intrinsic]
2036#[rustc_nounwind]
2037pub unsafe fn exp2f64(x: f64) -> f64;
2038/// Returns 2 raised to the power of an `f128`.
2039///
2040/// The stabilized version of this intrinsic is
2041/// [`f128::exp2`](../../std/primitive.f128.html#method.exp2)
2042#[rustc_intrinsic]
2043#[rustc_nounwind]
2044pub unsafe fn exp2f128(x: f128) -> f128;
2045
2046/// Returns the natural logarithm of an `f16`.
2047///
2048/// The stabilized version of this intrinsic is
2049/// [`f16::ln`](../../std/primitive.f16.html#method.ln)
2050#[rustc_intrinsic]
2051#[rustc_nounwind]
2052pub unsafe fn logf16(x: f16) -> f16;
2053/// Returns the natural logarithm of an `f32`.
2054///
2055/// The stabilized version of this intrinsic is
2056/// [`f32::ln`](../../std/primitive.f32.html#method.ln)
2057#[rustc_intrinsic]
2058#[rustc_nounwind]
2059pub unsafe fn logf32(x: f32) -> f32;
2060/// Returns the natural logarithm of an `f64`.
2061///
2062/// The stabilized version of this intrinsic is
2063/// [`f64::ln`](../../std/primitive.f64.html#method.ln)
2064#[rustc_intrinsic]
2065#[rustc_nounwind]
2066pub unsafe fn logf64(x: f64) -> f64;
2067/// Returns the natural logarithm of an `f128`.
2068///
2069/// The stabilized version of this intrinsic is
2070/// [`f128::ln`](../../std/primitive.f128.html#method.ln)
2071#[rustc_intrinsic]
2072#[rustc_nounwind]
2073pub unsafe fn logf128(x: f128) -> f128;
2074
2075/// Returns the base 10 logarithm of an `f16`.
2076///
2077/// The stabilized version of this intrinsic is
2078/// [`f16::log10`](../../std/primitive.f16.html#method.log10)
2079#[rustc_intrinsic]
2080#[rustc_nounwind]
2081pub unsafe fn log10f16(x: f16) -> f16;
2082/// Returns the base 10 logarithm of an `f32`.
2083///
2084/// The stabilized version of this intrinsic is
2085/// [`f32::log10`](../../std/primitive.f32.html#method.log10)
2086#[rustc_intrinsic]
2087#[rustc_nounwind]
2088pub unsafe fn log10f32(x: f32) -> f32;
2089/// Returns the base 10 logarithm of an `f64`.
2090///
2091/// The stabilized version of this intrinsic is
2092/// [`f64::log10`](../../std/primitive.f64.html#method.log10)
2093#[rustc_intrinsic]
2094#[rustc_nounwind]
2095pub unsafe fn log10f64(x: f64) -> f64;
2096/// Returns the base 10 logarithm of an `f128`.
2097///
2098/// The stabilized version of this intrinsic is
2099/// [`f128::log10`](../../std/primitive.f128.html#method.log10)
2100#[rustc_intrinsic]
2101#[rustc_nounwind]
2102pub unsafe fn log10f128(x: f128) -> f128;
2103
2104/// Returns the base 2 logarithm of an `f16`.
2105///
2106/// The stabilized version of this intrinsic is
2107/// [`f16::log2`](../../std/primitive.f16.html#method.log2)
2108#[rustc_intrinsic]
2109#[rustc_nounwind]
2110pub unsafe fn log2f16(x: f16) -> f16;
2111/// Returns the base 2 logarithm of an `f32`.
2112///
2113/// The stabilized version of this intrinsic is
2114/// [`f32::log2`](../../std/primitive.f32.html#method.log2)
2115#[rustc_intrinsic]
2116#[rustc_nounwind]
2117pub unsafe fn log2f32(x: f32) -> f32;
2118/// Returns the base 2 logarithm of an `f64`.
2119///
2120/// The stabilized version of this intrinsic is
2121/// [`f64::log2`](../../std/primitive.f64.html#method.log2)
2122#[rustc_intrinsic]
2123#[rustc_nounwind]
2124pub unsafe fn log2f64(x: f64) -> f64;
2125/// Returns the base 2 logarithm of an `f128`.
2126///
2127/// The stabilized version of this intrinsic is
2128/// [`f128::log2`](../../std/primitive.f128.html#method.log2)
2129#[rustc_intrinsic]
2130#[rustc_nounwind]
2131pub unsafe fn log2f128(x: f128) -> f128;
2132
2133/// Returns `a * b + c` for `f16` values.
2134///
2135/// The stabilized version of this intrinsic is
2136/// [`f16::mul_add`](../../std/primitive.f16.html#method.mul_add)
2137#[rustc_intrinsic]
2138#[rustc_nounwind]
2139pub unsafe fn fmaf16(a: f16, b: f16, c: f16) -> f16;
2140/// Returns `a * b + c` for `f32` values.
2141///
2142/// The stabilized version of this intrinsic is
2143/// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
2144#[rustc_intrinsic]
2145#[rustc_nounwind]
2146pub unsafe fn fmaf32(a: f32, b: f32, c: f32) -> f32;
2147/// Returns `a * b + c` for `f64` values.
2148///
2149/// The stabilized version of this intrinsic is
2150/// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
2151#[rustc_intrinsic]
2152#[rustc_nounwind]
2153pub unsafe fn fmaf64(a: f64, b: f64, c: f64) -> f64;
2154/// Returns `a * b + c` for `f128` values.
2155///
2156/// The stabilized version of this intrinsic is
2157/// [`f128::mul_add`](../../std/primitive.f128.html#method.mul_add)
2158#[rustc_intrinsic]
2159#[rustc_nounwind]
2160pub unsafe fn fmaf128(a: f128, b: f128, c: f128) -> f128;
2161
2162/// Returns `a * b + c` for `f16` values, non-deterministically executing
2163/// either a fused multiply-add or two operations with rounding of the
2164/// intermediate result.
2165///
2166/// The operation is fused if the code generator determines that target
2167/// instruction set has support for a fused operation, and that the fused
2168/// operation is more efficient than the equivalent, separate pair of mul
2169/// and add instructions. It is unspecified whether or not a fused operation
2170/// is selected, and that may depend on optimization level and context, for
2171/// example.
2172#[rustc_intrinsic]
2173#[rustc_nounwind]
2174pub unsafe fn fmuladdf16(a: f16, b: f16, c: f16) -> f16;
2175/// Returns `a * b + c` for `f32` values, non-deterministically executing
2176/// either a fused multiply-add or two operations with rounding of the
2177/// intermediate result.
2178///
2179/// The operation is fused if the code generator determines that target
2180/// instruction set has support for a fused operation, and that the fused
2181/// operation is more efficient than the equivalent, separate pair of mul
2182/// and add instructions. It is unspecified whether or not a fused operation
2183/// is selected, and that may depend on optimization level and context, for
2184/// example.
2185#[rustc_intrinsic]
2186#[rustc_nounwind]
2187pub unsafe fn fmuladdf32(a: f32, b: f32, c: f32) -> f32;
2188/// Returns `a * b + c` for `f64` values, non-deterministically executing
2189/// either a fused multiply-add or two operations with rounding of the
2190/// intermediate result.
2191///
2192/// The operation is fused if the code generator determines that target
2193/// instruction set has support for a fused operation, and that the fused
2194/// operation is more efficient than the equivalent, separate pair of mul
2195/// and add instructions. It is unspecified whether or not a fused operation
2196/// is selected, and that may depend on optimization level and context, for
2197/// example.
2198#[rustc_intrinsic]
2199#[rustc_nounwind]
2200pub unsafe fn fmuladdf64(a: f64, b: f64, c: f64) -> f64;
2201/// Returns `a * b + c` for `f128` values, non-deterministically executing
2202/// either a fused multiply-add or two operations with rounding of the
2203/// intermediate result.
2204///
2205/// The operation is fused if the code generator determines that target
2206/// instruction set has support for a fused operation, and that the fused
2207/// operation is more efficient than the equivalent, separate pair of mul
2208/// and add instructions. It is unspecified whether or not a fused operation
2209/// is selected, and that may depend on optimization level and context, for
2210/// example.
2211#[rustc_intrinsic]
2212#[rustc_nounwind]
2213pub unsafe fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
2214
2215/// Returns the largest integer less than or equal to an `f16`.
2216///
2217/// The stabilized version of this intrinsic is
2218/// [`f16::floor`](../../std/primitive.f16.html#method.floor)
2219#[rustc_intrinsic]
2220#[rustc_nounwind]
2221pub unsafe fn floorf16(x: f16) -> f16;
2222/// Returns the largest integer less than or equal to an `f32`.
2223///
2224/// The stabilized version of this intrinsic is
2225/// [`f32::floor`](../../std/primitive.f32.html#method.floor)
2226#[rustc_intrinsic]
2227#[rustc_nounwind]
2228pub unsafe fn floorf32(x: f32) -> f32;
2229/// Returns the largest integer less than or equal to an `f64`.
2230///
2231/// The stabilized version of this intrinsic is
2232/// [`f64::floor`](../../std/primitive.f64.html#method.floor)
2233#[rustc_intrinsic]
2234#[rustc_nounwind]
2235pub unsafe fn floorf64(x: f64) -> f64;
2236/// Returns the largest integer less than or equal to an `f128`.
2237///
2238/// The stabilized version of this intrinsic is
2239/// [`f128::floor`](../../std/primitive.f128.html#method.floor)
2240#[rustc_intrinsic]
2241#[rustc_nounwind]
2242pub unsafe fn floorf128(x: f128) -> f128;
2243
2244/// Returns the smallest integer greater than or equal to an `f16`.
2245///
2246/// The stabilized version of this intrinsic is
2247/// [`f16::ceil`](../../std/primitive.f16.html#method.ceil)
2248#[rustc_intrinsic]
2249#[rustc_nounwind]
2250pub unsafe fn ceilf16(x: f16) -> f16;
2251/// Returns the smallest integer greater than or equal to an `f32`.
2252///
2253/// The stabilized version of this intrinsic is
2254/// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
2255#[rustc_intrinsic]
2256#[rustc_nounwind]
2257pub unsafe fn ceilf32(x: f32) -> f32;
2258/// Returns the smallest integer greater than or equal to an `f64`.
2259///
2260/// The stabilized version of this intrinsic is
2261/// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
2262#[rustc_intrinsic]
2263#[rustc_nounwind]
2264pub unsafe fn ceilf64(x: f64) -> f64;
2265/// Returns the smallest integer greater than or equal to an `f128`.
2266///
2267/// The stabilized version of this intrinsic is
2268/// [`f128::ceil`](../../std/primitive.f128.html#method.ceil)
2269#[rustc_intrinsic]
2270#[rustc_nounwind]
2271pub unsafe fn ceilf128(x: f128) -> f128;
2272
2273/// Returns the integer part of an `f16`.
2274///
2275/// The stabilized version of this intrinsic is
2276/// [`f16::trunc`](../../std/primitive.f16.html#method.trunc)
2277#[rustc_intrinsic]
2278#[rustc_nounwind]
2279pub unsafe fn truncf16(x: f16) -> f16;
2280/// Returns the integer part of an `f32`.
2281///
2282/// The stabilized version of this intrinsic is
2283/// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
2284#[rustc_intrinsic]
2285#[rustc_nounwind]
2286pub unsafe fn truncf32(x: f32) -> f32;
2287/// Returns the integer part of an `f64`.
2288///
2289/// The stabilized version of this intrinsic is
2290/// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
2291#[rustc_intrinsic]
2292#[rustc_nounwind]
2293pub unsafe fn truncf64(x: f64) -> f64;
2294/// Returns the integer part of an `f128`.
2295///
2296/// The stabilized version of this intrinsic is
2297/// [`f128::trunc`](../../std/primitive.f128.html#method.trunc)
2298#[rustc_intrinsic]
2299#[rustc_nounwind]
2300pub unsafe fn truncf128(x: f128) -> f128;
2301
2302/// Returns the nearest integer to an `f16`. Rounds half-way cases to the number with an even
2303/// least significant digit.
2304///
2305/// The stabilized version of this intrinsic is
2306/// [`f16::round_ties_even`](../../std/primitive.f16.html#method.round_ties_even)
2307#[rustc_intrinsic]
2308#[rustc_nounwind]
2309pub fn round_ties_even_f16(x: f16) -> f16;
2310
2311/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number with an even
2312/// least significant digit.
2313///
2314/// The stabilized version of this intrinsic is
2315/// [`f32::round_ties_even`](../../std/primitive.f32.html#method.round_ties_even)
2316#[rustc_intrinsic]
2317#[rustc_nounwind]
2318pub fn round_ties_even_f32(x: f32) -> f32;
2319
2320/// Provided for compatibility with stdarch. DO NOT USE.
2321#[inline(always)]
2322pub unsafe fn rintf32(x: f32) -> f32 {
2323 round_ties_even_f32(x)
2324}
2325
2326/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number with an even
2327/// least significant digit.
2328///
2329/// The stabilized version of this intrinsic is
2330/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
2331#[rustc_intrinsic]
2332#[rustc_nounwind]
2333pub fn round_ties_even_f64(x: f64) -> f64;
2334
2335/// Provided for compatibility with stdarch. DO NOT USE.
2336#[inline(always)]
2337pub unsafe fn rintf64(x: f64) -> f64 {
2338 round_ties_even_f64(x)
2339}
2340
2341/// Returns the nearest integer to an `f128`. Rounds half-way cases to the number with an even
2342/// least significant digit.
2343///
2344/// The stabilized version of this intrinsic is
2345/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
2346#[rustc_intrinsic]
2347#[rustc_nounwind]
2348pub fn round_ties_even_f128(x: f128) -> f128;
2349
2350/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
2351///
2352/// The stabilized version of this intrinsic is
2353/// [`f16::round`](../../std/primitive.f16.html#method.round)
2354#[rustc_intrinsic]
2355#[rustc_nounwind]
2356pub unsafe fn roundf16(x: f16) -> f16;
2357/// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
2358///
2359/// The stabilized version of this intrinsic is
2360/// [`f32::round`](../../std/primitive.f32.html#method.round)
2361#[rustc_intrinsic]
2362#[rustc_nounwind]
2363pub unsafe fn roundf32(x: f32) -> f32;
2364/// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
2365///
2366/// The stabilized version of this intrinsic is
2367/// [`f64::round`](../../std/primitive.f64.html#method.round)
2368#[rustc_intrinsic]
2369#[rustc_nounwind]
2370pub unsafe fn roundf64(x: f64) -> f64;
2371/// Returns the nearest integer to an `f128`. Rounds half-way cases away from zero.
2372///
2373/// The stabilized version of this intrinsic is
2374/// [`f128::round`](../../std/primitive.f128.html#method.round)
2375#[rustc_intrinsic]
2376#[rustc_nounwind]
2377pub unsafe fn roundf128(x: f128) -> f128;
2378
2379/// Float addition that allows optimizations based on algebraic rules.
2380/// May assume inputs are finite.
2381///
2382/// This intrinsic does not have a stable counterpart.
2383#[rustc_intrinsic]
2384#[rustc_nounwind]
2385pub unsafe fn fadd_fast<T: Copy>(a: T, b: T) -> T;
2386
2387/// Float subtraction that allows optimizations based on algebraic rules.
2388/// May assume inputs are finite.
2389///
2390/// This intrinsic does not have a stable counterpart.
2391#[rustc_intrinsic]
2392#[rustc_nounwind]
2393pub unsafe fn fsub_fast<T: Copy>(a: T, b: T) -> T;
2394
2395/// Float multiplication that allows optimizations based on algebraic rules.
2396/// May assume inputs are finite.
2397///
2398/// This intrinsic does not have a stable counterpart.
2399#[rustc_intrinsic]
2400#[rustc_nounwind]
2401pub unsafe fn fmul_fast<T: Copy>(a: T, b: T) -> T;
2402
2403/// Float division that allows optimizations based on algebraic rules.
2404/// May assume inputs are finite.
2405///
2406/// This intrinsic does not have a stable counterpart.
2407#[rustc_intrinsic]
2408#[rustc_nounwind]
2409pub unsafe fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
2410
2411/// Float remainder that allows optimizations based on algebraic rules.
2412/// May assume inputs are finite.
2413///
2414/// This intrinsic does not have a stable counterpart.
2415#[rustc_intrinsic]
2416#[rustc_nounwind]
2417pub unsafe fn frem_fast<T: Copy>(a: T, b: T) -> T;
2418
2419/// Converts with LLVM’s fptoui/fptosi, which may return undef for values out of range
2420/// (<https://github.com/rust-lang/rust/issues/10184>)
2421///
2422/// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
2423#[rustc_intrinsic]
2424#[rustc_nounwind]
2425pub unsafe fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
2426
2427/// Float addition that allows optimizations based on algebraic rules.
2428///
2429/// Stabilized as [`f16::algebraic_add`], [`f32::algebraic_add`], [`f64::algebraic_add`] and [`f128::algebraic_add`].
2430#[rustc_nounwind]
2431#[rustc_intrinsic]
2432pub fn fadd_algebraic<T: Copy>(a: T, b: T) -> T;
2433
2434/// Float subtraction that allows optimizations based on algebraic rules.
2435///
2436/// Stabilized as [`f16::algebraic_sub`], [`f32::algebraic_sub`], [`f64::algebraic_sub`] and [`f128::algebraic_sub`].
2437#[rustc_nounwind]
2438#[rustc_intrinsic]
2439pub fn fsub_algebraic<T: Copy>(a: T, b: T) -> T;
2440
2441/// Float multiplication that allows optimizations based on algebraic rules.
2442///
2443/// Stabilized as [`f16::algebraic_mul`], [`f32::algebraic_mul`], [`f64::algebraic_mul`] and [`f128::algebraic_mul`].
2444#[rustc_nounwind]
2445#[rustc_intrinsic]
2446pub fn fmul_algebraic<T: Copy>(a: T, b: T) -> T;
2447
2448/// Float division that allows optimizations based on algebraic rules.
2449///
2450/// Stabilized as [`f16::algebraic_div`], [`f32::algebraic_div`], [`f64::algebraic_div`] and [`f128::algebraic_div`].
2451#[rustc_nounwind]
2452#[rustc_intrinsic]
2453pub fn fdiv_algebraic<T: Copy>(a: T, b: T) -> T;
2454
2455/// Float remainder that allows optimizations based on algebraic rules.
2456///
2457/// Stabilized as [`f16::algebraic_rem`], [`f32::algebraic_rem`], [`f64::algebraic_rem`] and [`f128::algebraic_rem`].
2458#[rustc_nounwind]
2459#[rustc_intrinsic]
2460pub fn frem_algebraic<T: Copy>(a: T, b: T) -> T;
2461
2462/// Returns the number of bits set in an integer type `T`
2463///
2464/// Note that, unlike most intrinsics, this is safe to call;
2465/// it does not require an `unsafe` block.
2466/// Therefore, implementations must not require the user to uphold
2467/// any safety invariants.
2468///
2469/// The stabilized versions of this intrinsic are available on the integer
2470/// primitives via the `count_ones` method. For example,
2471/// [`u32::count_ones`]
2472#[rustc_intrinsic_const_stable_indirect]
2473#[rustc_nounwind]
2474#[rustc_intrinsic]
2475pub const fn ctpop<T: Copy>(x: T) -> u32;
2476
2477/// Returns the number of leading unset bits (zeroes) in an integer type `T`.
2478///
2479/// Note that, unlike most intrinsics, this is safe to call;
2480/// it does not require an `unsafe` block.
2481/// Therefore, implementations must not require the user to uphold
2482/// any safety invariants.
2483///
2484/// The stabilized versions of this intrinsic are available on the integer
2485/// primitives via the `leading_zeros` method. For example,
2486/// [`u32::leading_zeros`]
2487///
2488/// # Examples
2489///
2490/// ```
2491/// #![feature(core_intrinsics)]
2492/// # #![allow(internal_features)]
2493///
2494/// use std::intrinsics::ctlz;
2495///
2496/// let x = 0b0001_1100_u8;
2497/// let num_leading = ctlz(x);
2498/// assert_eq!(num_leading, 3);
2499/// ```
2500///
2501/// An `x` with value `0` will return the bit width of `T`.
2502///
2503/// ```
2504/// #![feature(core_intrinsics)]
2505/// # #![allow(internal_features)]
2506///
2507/// use std::intrinsics::ctlz;
2508///
2509/// let x = 0u16;
2510/// let num_leading = ctlz(x);
2511/// assert_eq!(num_leading, 16);
2512/// ```
2513#[rustc_intrinsic_const_stable_indirect]
2514#[rustc_nounwind]
2515#[rustc_intrinsic]
2516pub const fn ctlz<T: Copy>(x: T) -> u32;
2517
2518/// Like `ctlz`, but extra-unsafe as it returns `undef` when
2519/// given an `x` with value `0`.
2520///
2521/// This intrinsic does not have a stable counterpart.
2522///
2523/// # Examples
2524///
2525/// ```
2526/// #![feature(core_intrinsics)]
2527/// # #![allow(internal_features)]
2528///
2529/// use std::intrinsics::ctlz_nonzero;
2530///
2531/// let x = 0b0001_1100_u8;
2532/// let num_leading = unsafe { ctlz_nonzero(x) };
2533/// assert_eq!(num_leading, 3);
2534/// ```
2535#[rustc_intrinsic_const_stable_indirect]
2536#[rustc_nounwind]
2537#[rustc_intrinsic]
2538pub const unsafe fn ctlz_nonzero<T: Copy>(x: T) -> u32;
2539
2540/// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
2541///
2542/// Note that, unlike most intrinsics, this is safe to call;
2543/// it does not require an `unsafe` block.
2544/// Therefore, implementations must not require the user to uphold
2545/// any safety invariants.
2546///
2547/// The stabilized versions of this intrinsic are available on the integer
2548/// primitives via the `trailing_zeros` method. For example,
2549/// [`u32::trailing_zeros`]
2550///
2551/// # Examples
2552///
2553/// ```
2554/// #![feature(core_intrinsics)]
2555/// # #![allow(internal_features)]
2556///
2557/// use std::intrinsics::cttz;
2558///
2559/// let x = 0b0011_1000_u8;
2560/// let num_trailing = cttz(x);
2561/// assert_eq!(num_trailing, 3);
2562/// ```
2563///
2564/// An `x` with value `0` will return the bit width of `T`:
2565///
2566/// ```
2567/// #![feature(core_intrinsics)]
2568/// # #![allow(internal_features)]
2569///
2570/// use std::intrinsics::cttz;
2571///
2572/// let x = 0u16;
2573/// let num_trailing = cttz(x);
2574/// assert_eq!(num_trailing, 16);
2575/// ```
2576#[rustc_intrinsic_const_stable_indirect]
2577#[rustc_nounwind]
2578#[rustc_intrinsic]
2579pub const fn cttz<T: Copy>(x: T) -> u32;
2580
2581/// Like `cttz`, but extra-unsafe as it returns `undef` when
2582/// given an `x` with value `0`.
2583///
2584/// This intrinsic does not have a stable counterpart.
2585///
2586/// # Examples
2587///
2588/// ```
2589/// #![feature(core_intrinsics)]
2590/// # #![allow(internal_features)]
2591///
2592/// use std::intrinsics::cttz_nonzero;
2593///
2594/// let x = 0b0011_1000_u8;
2595/// let num_trailing = unsafe { cttz_nonzero(x) };
2596/// assert_eq!(num_trailing, 3);
2597/// ```
2598#[rustc_intrinsic_const_stable_indirect]
2599#[rustc_nounwind]
2600#[rustc_intrinsic]
2601pub const unsafe fn cttz_nonzero<T: Copy>(x: T) -> u32;
2602
2603/// Reverses the bytes in an integer type `T`.
2604///
2605/// Note that, unlike most intrinsics, this is safe to call;
2606/// it does not require an `unsafe` block.
2607/// Therefore, implementations must not require the user to uphold
2608/// any safety invariants.
2609///
2610/// The stabilized versions of this intrinsic are available on the integer
2611/// primitives via the `swap_bytes` method. For example,
2612/// [`u32::swap_bytes`]
2613#[rustc_intrinsic_const_stable_indirect]
2614#[rustc_nounwind]
2615#[rustc_intrinsic]
2616pub const fn bswap<T: Copy>(x: T) -> T;
2617
2618/// Reverses the bits in an integer type `T`.
2619///
2620/// Note that, unlike most intrinsics, this is safe to call;
2621/// it does not require an `unsafe` block.
2622/// Therefore, implementations must not require the user to uphold
2623/// any safety invariants.
2624///
2625/// The stabilized versions of this intrinsic are available on the integer
2626/// primitives via the `reverse_bits` method. For example,
2627/// [`u32::reverse_bits`]
2628#[rustc_intrinsic_const_stable_indirect]
2629#[rustc_nounwind]
2630#[rustc_intrinsic]
2631pub const fn bitreverse<T: Copy>(x: T) -> T;
2632
2633/// Does a three-way comparison between the two arguments,
2634/// which must be of character or integer (signed or unsigned) type.
2635///
2636/// This was originally added because it greatly simplified the MIR in `cmp`
2637/// implementations, and then LLVM 20 added a backend intrinsic for it too.
2638///
2639/// The stabilized version of this intrinsic is [`Ord::cmp`].
2640#[rustc_intrinsic_const_stable_indirect]
2641#[rustc_nounwind]
2642#[rustc_intrinsic]
2643pub const fn three_way_compare<T: Copy>(lhs: T, rhss: T) -> crate::cmp::Ordering;
2644
2645/// Combine two values which have no bits in common.
2646///
2647/// This allows the backend to implement it as `a + b` *or* `a | b`,
2648/// depending which is easier to implement on a specific target.
2649///
2650/// # Safety
2651///
2652/// Requires that `(a & b) == 0`, or equivalently that `(a | b) == (a + b)`.
2653///
2654/// Otherwise it's immediate UB.
2655#[rustc_const_unstable(feature = "disjoint_bitor", issue = "135758")]
2656#[rustc_nounwind]
2657#[rustc_intrinsic]
2658#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2659#[miri::intrinsic_fallback_is_spec] // the fallbacks all `assume` to tell Miri
2660pub const unsafe fn disjoint_bitor<T: ~const fallback::DisjointBitOr>(a: T, b: T) -> T {
2661 // SAFETY: same preconditions as this function.
2662 unsafe { fallback::DisjointBitOr::disjoint_bitor(a, b) }
2663}
2664
2665/// Performs checked integer addition.
2666///
2667/// Note that, unlike most intrinsics, this is safe to call;
2668/// it does not require an `unsafe` block.
2669/// Therefore, implementations must not require the user to uphold
2670/// any safety invariants.
2671///
2672/// The stabilized versions of this intrinsic are available on the integer
2673/// primitives via the `overflowing_add` method. For example,
2674/// [`u32::overflowing_add`]
2675#[rustc_intrinsic_const_stable_indirect]
2676#[rustc_nounwind]
2677#[rustc_intrinsic]
2678pub const fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
2679
2680/// Performs checked integer subtraction
2681///
2682/// Note that, unlike most intrinsics, this is safe to call;
2683/// it does not require an `unsafe` block.
2684/// Therefore, implementations must not require the user to uphold
2685/// any safety invariants.
2686///
2687/// The stabilized versions of this intrinsic are available on the integer
2688/// primitives via the `overflowing_sub` method. For example,
2689/// [`u32::overflowing_sub`]
2690#[rustc_intrinsic_const_stable_indirect]
2691#[rustc_nounwind]
2692#[rustc_intrinsic]
2693pub const fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
2694
2695/// Performs checked integer multiplication
2696///
2697/// Note that, unlike most intrinsics, this is safe to call;
2698/// it does not require an `unsafe` block.
2699/// Therefore, implementations must not require the user to uphold
2700/// any safety invariants.
2701///
2702/// The stabilized versions of this intrinsic are available on the integer
2703/// primitives via the `overflowing_mul` method. For example,
2704/// [`u32::overflowing_mul`]
2705#[rustc_intrinsic_const_stable_indirect]
2706#[rustc_nounwind]
2707#[rustc_intrinsic]
2708pub const fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
2709
2710/// Performs full-width multiplication and addition with a carry:
2711/// `multiplier * multiplicand + addend + carry`.
2712///
2713/// This is possible without any overflow. For `uN`:
2714/// MAX * MAX + MAX + MAX
2715/// => (2ⁿ-1) × (2ⁿ-1) + (2ⁿ-1) + (2ⁿ-1)
2716/// => (2²ⁿ - 2ⁿ⁺¹ + 1) + (2ⁿ⁺¹ - 2)
2717/// => 2²ⁿ - 1
2718///
2719/// For `iN`, the upper bound is MIN * MIN + MAX + MAX => 2²ⁿ⁻² + 2ⁿ - 2,
2720/// and the lower bound is MAX * MIN + MIN + MIN => -2²ⁿ⁻² - 2ⁿ + 2ⁿ⁺¹.
2721///
2722/// This currently supports unsigned integers *only*, no signed ones.
2723/// The stabilized versions of this intrinsic are available on integers.
2724#[unstable(feature = "core_intrinsics", issue = "none")]
2725#[rustc_const_unstable(feature = "const_carrying_mul_add", issue = "85532")]
2726#[rustc_nounwind]
2727#[rustc_intrinsic]
2728#[miri::intrinsic_fallback_is_spec]
2729pub const fn carrying_mul_add<T: ~const fallback::CarryingMulAdd<Unsigned = U>, U>(
2730 multiplier: T,
2731 multiplicand: T,
2732 addend: T,
2733 carry: T,
2734) -> (U, T) {
2735 multiplier.carrying_mul_add(multiplicand, addend, carry)
2736}
2737
2738/// Performs an exact division, resulting in undefined behavior where
2739/// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
2740///
2741/// This intrinsic does not have a stable counterpart.
2742#[rustc_intrinsic_const_stable_indirect]
2743#[rustc_nounwind]
2744#[rustc_intrinsic]
2745pub const unsafe fn exact_div<T: Copy>(x: T, y: T) -> T;
2746
2747/// Performs an unchecked division, resulting in undefined behavior
2748/// where `y == 0` or `x == T::MIN && y == -1`
2749///
2750/// Safe wrappers for this intrinsic are available on the integer
2751/// primitives via the `checked_div` method. For example,
2752/// [`u32::checked_div`]
2753#[rustc_intrinsic_const_stable_indirect]
2754#[rustc_nounwind]
2755#[rustc_intrinsic]
2756pub const unsafe fn unchecked_div<T: Copy>(x: T, y: T) -> T;
2757/// Returns the remainder of an unchecked division, resulting in
2758/// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
2759///
2760/// Safe wrappers for this intrinsic are available on the integer
2761/// primitives via the `checked_rem` method. For example,
2762/// [`u32::checked_rem`]
2763#[rustc_intrinsic_const_stable_indirect]
2764#[rustc_nounwind]
2765#[rustc_intrinsic]
2766pub const unsafe fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
2767
2768/// Performs an unchecked left shift, resulting in undefined behavior when
2769/// `y < 0` or `y >= N`, where N is the width of T in bits.
2770///
2771/// Safe wrappers for this intrinsic are available on the integer
2772/// primitives via the `checked_shl` method. For example,
2773/// [`u32::checked_shl`]
2774#[rustc_intrinsic_const_stable_indirect]
2775#[rustc_nounwind]
2776#[rustc_intrinsic]
2777pub const unsafe fn unchecked_shl<T: Copy, U: Copy>(x: T, y: U) -> T;
2778/// Performs an unchecked right shift, resulting in undefined behavior when
2779/// `y < 0` or `y >= N`, where N is the width of T in bits.
2780///
2781/// Safe wrappers for this intrinsic are available on the integer
2782/// primitives via the `checked_shr` method. For example,
2783/// [`u32::checked_shr`]
2784#[rustc_intrinsic_const_stable_indirect]
2785#[rustc_nounwind]
2786#[rustc_intrinsic]
2787pub const unsafe fn unchecked_shr<T: Copy, U: Copy>(x: T, y: U) -> T;
2788
2789/// Returns the result of an unchecked addition, resulting in
2790/// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
2791///
2792/// The stable counterpart of this intrinsic is `unchecked_add` on the various
2793/// integer types, such as [`u16::unchecked_add`] and [`i64::unchecked_add`].
2794#[rustc_intrinsic_const_stable_indirect]
2795#[rustc_nounwind]
2796#[rustc_intrinsic]
2797pub const unsafe fn unchecked_add<T: Copy>(x: T, y: T) -> T;
2798
2799/// Returns the result of an unchecked subtraction, resulting in
2800/// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
2801///
2802/// The stable counterpart of this intrinsic is `unchecked_sub` on the various
2803/// integer types, such as [`u16::unchecked_sub`] and [`i64::unchecked_sub`].
2804#[rustc_intrinsic_const_stable_indirect]
2805#[rustc_nounwind]
2806#[rustc_intrinsic]
2807pub const unsafe fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
2808
2809/// Returns the result of an unchecked multiplication, resulting in
2810/// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
2811///
2812/// The stable counterpart of this intrinsic is `unchecked_mul` on the various
2813/// integer types, such as [`u16::unchecked_mul`] and [`i64::unchecked_mul`].
2814#[rustc_intrinsic_const_stable_indirect]
2815#[rustc_nounwind]
2816#[rustc_intrinsic]
2817pub const unsafe fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
2818
2819/// Performs rotate left.
2820///
2821/// Note that, unlike most intrinsics, this is safe to call;
2822/// it does not require an `unsafe` block.
2823/// Therefore, implementations must not require the user to uphold
2824/// any safety invariants.
2825///
2826/// The stabilized versions of this intrinsic are available on the integer
2827/// primitives via the `rotate_left` method. For example,
2828/// [`u32::rotate_left`]
2829#[rustc_intrinsic_const_stable_indirect]
2830#[rustc_nounwind]
2831#[rustc_intrinsic]
2832pub const fn rotate_left<T: Copy>(x: T, shift: u32) -> T;
2833
2834/// Performs rotate right.
2835///
2836/// Note that, unlike most intrinsics, this is safe to call;
2837/// it does not require an `unsafe` block.
2838/// Therefore, implementations must not require the user to uphold
2839/// any safety invariants.
2840///
2841/// The stabilized versions of this intrinsic are available on the integer
2842/// primitives via the `rotate_right` method. For example,
2843/// [`u32::rotate_right`]
2844#[rustc_intrinsic_const_stable_indirect]
2845#[rustc_nounwind]
2846#[rustc_intrinsic]
2847pub const fn rotate_right<T: Copy>(x: T, shift: u32) -> T;
2848
2849/// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
2850///
2851/// Note that, unlike most intrinsics, this is safe to call;
2852/// it does not require an `unsafe` block.
2853/// Therefore, implementations must not require the user to uphold
2854/// any safety invariants.
2855///
2856/// The stabilized versions of this intrinsic are available on the integer
2857/// primitives via the `wrapping_add` method. For example,
2858/// [`u32::wrapping_add`]
2859#[rustc_intrinsic_const_stable_indirect]
2860#[rustc_nounwind]
2861#[rustc_intrinsic]
2862pub const fn wrapping_add<T: Copy>(a: T, b: T) -> T;
2863/// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
2864///
2865/// Note that, unlike most intrinsics, this is safe to call;
2866/// it does not require an `unsafe` block.
2867/// Therefore, implementations must not require the user to uphold
2868/// any safety invariants.
2869///
2870/// The stabilized versions of this intrinsic are available on the integer
2871/// primitives via the `wrapping_sub` method. For example,
2872/// [`u32::wrapping_sub`]
2873#[rustc_intrinsic_const_stable_indirect]
2874#[rustc_nounwind]
2875#[rustc_intrinsic]
2876pub const fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
2877/// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
2878///
2879/// Note that, unlike most intrinsics, this is safe to call;
2880/// it does not require an `unsafe` block.
2881/// Therefore, implementations must not require the user to uphold
2882/// any safety invariants.
2883///
2884/// The stabilized versions of this intrinsic are available on the integer
2885/// primitives via the `wrapping_mul` method. For example,
2886/// [`u32::wrapping_mul`]
2887#[rustc_intrinsic_const_stable_indirect]
2888#[rustc_nounwind]
2889#[rustc_intrinsic]
2890pub const fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
2891
2892/// Computes `a + b`, saturating at numeric bounds.
2893///
2894/// Note that, unlike most intrinsics, this is safe to call;
2895/// it does not require an `unsafe` block.
2896/// Therefore, implementations must not require the user to uphold
2897/// any safety invariants.
2898///
2899/// The stabilized versions of this intrinsic are available on the integer
2900/// primitives via the `saturating_add` method. For example,
2901/// [`u32::saturating_add`]
2902#[rustc_intrinsic_const_stable_indirect]
2903#[rustc_nounwind]
2904#[rustc_intrinsic]
2905pub const fn saturating_add<T: Copy>(a: T, b: T) -> T;
2906/// Computes `a - b`, saturating at numeric bounds.
2907///
2908/// Note that, unlike most intrinsics, this is safe to call;
2909/// it does not require an `unsafe` block.
2910/// Therefore, implementations must not require the user to uphold
2911/// any safety invariants.
2912///
2913/// The stabilized versions of this intrinsic are available on the integer
2914/// primitives via the `saturating_sub` method. For example,
2915/// [`u32::saturating_sub`]
2916#[rustc_intrinsic_const_stable_indirect]
2917#[rustc_nounwind]
2918#[rustc_intrinsic]
2919pub const fn saturating_sub<T: Copy>(a: T, b: T) -> T;
2920
2921/// This is an implementation detail of [`crate::ptr::read`] and should
2922/// not be used anywhere else. See its comments for why this exists.
2923///
2924/// This intrinsic can *only* be called where the pointer is a local without
2925/// projections (`read_via_copy(ptr)`, not `read_via_copy(*ptr)`) so that it
2926/// trivially obeys runtime-MIR rules about derefs in operands.
2927#[rustc_intrinsic_const_stable_indirect]
2928#[rustc_nounwind]
2929#[rustc_intrinsic]
2930pub const unsafe fn read_via_copy<T>(ptr: *const T) -> T;
2931
2932/// This is an implementation detail of [`crate::ptr::write`] and should
2933/// not be used anywhere else. See its comments for why this exists.
2934///
2935/// This intrinsic can *only* be called where the pointer is a local without
2936/// projections (`write_via_move(ptr, x)`, not `write_via_move(*ptr, x)`) so
2937/// that it trivially obeys runtime-MIR rules about derefs in operands.
2938#[rustc_intrinsic_const_stable_indirect]
2939#[rustc_nounwind]
2940#[rustc_intrinsic]
2941pub const unsafe fn write_via_move<T>(ptr: *mut T, value: T);
2942
2943/// Returns the value of the discriminant for the variant in 'v';
2944/// if `T` has no discriminant, returns `0`.
2945///
2946/// Note that, unlike most intrinsics, this is safe to call;
2947/// it does not require an `unsafe` block.
2948/// Therefore, implementations must not require the user to uphold
2949/// any safety invariants.
2950///
2951/// The stabilized version of this intrinsic is [`core::mem::discriminant`].
2952#[rustc_intrinsic_const_stable_indirect]
2953#[rustc_nounwind]
2954#[rustc_intrinsic]
2955pub const fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
2956
2957/// Rust's "try catch" construct for unwinding. Invokes the function pointer `try_fn` with the
2958/// data pointer `data`, and calls `catch_fn` if unwinding occurs while `try_fn` runs.
2959/// Returns `1` if unwinding occurred and `catch_fn` was called; returns `0` otherwise.
2960///
2961/// `catch_fn` must not unwind.
2962///
2963/// The third argument is a function called if an unwind occurs (both Rust `panic` and foreign
2964/// unwinds). This function takes the data pointer and a pointer to the target- and
2965/// runtime-specific exception object that was caught.
2966///
2967/// Note that in the case of a foreign unwinding operation, the exception object data may not be
2968/// safely usable from Rust, and should not be directly exposed via the standard library. To
2969/// prevent unsafe access, the library implementation may either abort the process or present an
2970/// opaque error type to the user.
2971///
2972/// For more information, see the compiler's source, as well as the documentation for the stable
2973/// version of this intrinsic, `std::panic::catch_unwind`.
2974#[rustc_intrinsic]
2975#[rustc_nounwind]
2976pub unsafe fn catch_unwind(
2977 _try_fn: fn(*mut u8),
2978 _data: *mut u8,
2979 _catch_fn: fn(*mut u8, *mut u8),
2980) -> i32;
2981
2982/// Emits a `nontemporal` store, which gives a hint to the CPU that the data should not be held
2983/// in cache. Except for performance, this is fully equivalent to `ptr.write(val)`.
2984///
2985/// Not all architectures provide such an operation. For instance, x86 does not: while `MOVNT`
2986/// exists, that operation is *not* equivalent to `ptr.write(val)` (`MOVNT` writes can be reordered
2987/// in ways that are not allowed for regular writes).
2988#[rustc_intrinsic]
2989#[rustc_nounwind]
2990pub unsafe fn nontemporal_store<T>(ptr: *mut T, val: T);
2991
2992/// See documentation of `<*const T>::offset_from` for details.
2993#[rustc_intrinsic_const_stable_indirect]
2994#[rustc_nounwind]
2995#[rustc_intrinsic]
2996pub const unsafe fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
2997
2998/// See documentation of `<*const T>::sub_ptr` for details.
2999#[rustc_nounwind]
3000#[rustc_intrinsic]
3001#[rustc_intrinsic_const_stable_indirect]
3002pub const unsafe fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
3003
3004/// See documentation of `<*const T>::guaranteed_eq` for details.
3005/// Returns `2` if the result is unknown.
3006/// Returns `1` if the pointers are guaranteed equal.
3007/// Returns `0` if the pointers are guaranteed inequal.
3008#[rustc_intrinsic]
3009#[rustc_nounwind]
3010#[rustc_do_not_const_check]
3011#[inline]
3012#[miri::intrinsic_fallback_is_spec]
3013pub const fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8 {
3014 (ptr == other) as u8
3015}
3016
3017/// Determines whether the raw bytes of the two values are equal.
3018///
3019/// This is particularly handy for arrays, since it allows things like just
3020/// comparing `i96`s instead of forcing `alloca`s for `[6 x i16]`.
3021///
3022/// Above some backend-decided threshold this will emit calls to `memcmp`,
3023/// like slice equality does, instead of causing massive code size.
3024///
3025/// Since this works by comparing the underlying bytes, the actual `T` is
3026/// not particularly important. It will be used for its size and alignment,
3027/// but any validity restrictions will be ignored, not enforced.
3028///
3029/// # Safety
3030///
3031/// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized.
3032/// Note that this is a stricter criterion than just the *values* being
3033/// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
3034///
3035/// At compile-time, it is furthermore UB to call this if any of the bytes
3036/// in `*a` or `*b` have provenance.
3037///
3038/// (The implementation is allowed to branch on the results of comparisons,
3039/// which is UB if any of their inputs are `undef`.)
3040#[rustc_nounwind]
3041#[rustc_intrinsic]
3042pub const unsafe fn raw_eq<T>(a: &T, b: &T) -> bool;
3043
3044/// Lexicographically compare `[left, left + bytes)` and `[right, right + bytes)`
3045/// as unsigned bytes, returning negative if `left` is less, zero if all the
3046/// bytes match, or positive if `left` is greater.
3047///
3048/// This underlies things like `<[u8]>::cmp`, and will usually lower to `memcmp`.
3049///
3050/// # Safety
3051///
3052/// `left` and `right` must each be [valid] for reads of `bytes` bytes.
3053///
3054/// Note that this applies to the whole range, not just until the first byte
3055/// that differs. That allows optimizations that can read in large chunks.
3056///
3057/// [valid]: crate::ptr#safety
3058#[rustc_nounwind]
3059#[rustc_intrinsic]
3060pub const unsafe fn compare_bytes(left: *const u8, right: *const u8, bytes: usize) -> i32;
3061
3062/// See documentation of [`std::hint::black_box`] for details.
3063///
3064/// [`std::hint::black_box`]: crate::hint::black_box
3065#[rustc_nounwind]
3066#[rustc_intrinsic]
3067#[rustc_intrinsic_const_stable_indirect]
3068pub const fn black_box<T>(dummy: T) -> T;
3069
3070/// Selects which function to call depending on the context.
3071///
3072/// If this function is evaluated at compile-time, then a call to this
3073/// intrinsic will be replaced with a call to `called_in_const`. It gets
3074/// replaced with a call to `called_at_rt` otherwise.
3075///
3076/// This function is safe to call, but note the stability concerns below.
3077///
3078/// # Type Requirements
3079///
3080/// The two functions must be both function items. They cannot be function
3081/// pointers or closures. The first function must be a `const fn`.
3082///
3083/// `arg` will be the tupled arguments that will be passed to either one of
3084/// the two functions, therefore, both functions must accept the same type of
3085/// arguments. Both functions must return RET.
3086///
3087/// # Stability concerns
3088///
3089/// Rust has not yet decided that `const fn` are allowed to tell whether
3090/// they run at compile-time or at runtime. Therefore, when using this
3091/// intrinsic anywhere that can be reached from stable, it is crucial that
3092/// the end-to-end behavior of the stable `const fn` is the same for both
3093/// modes of execution. (Here, Undefined Behavior is considered "the same"
3094/// as any other behavior, so if the function exhibits UB at runtime then
3095/// it may do whatever it wants at compile-time.)
3096///
3097/// Here is an example of how this could cause a problem:
3098/// ```no_run
3099/// #![feature(const_eval_select)]
3100/// #![feature(core_intrinsics)]
3101/// # #![allow(internal_features)]
3102/// use std::intrinsics::const_eval_select;
3103///
3104/// // Standard library
3105/// pub const fn inconsistent() -> i32 {
3106/// fn runtime() -> i32 { 1 }
3107/// const fn compiletime() -> i32 { 2 }
3108///
3109/// // ⚠ This code violates the required equivalence of `compiletime`
3110/// // and `runtime`.
3111/// const_eval_select((), compiletime, runtime)
3112/// }
3113///
3114/// // User Crate
3115/// const X: i32 = inconsistent();
3116/// let x = inconsistent();
3117/// assert_eq!(x, X);
3118/// ```
3119///
3120/// Currently such an assertion would always succeed; until Rust decides
3121/// otherwise, that principle should not be violated.
3122#[rustc_const_unstable(feature = "const_eval_select", issue = "124625")]
3123#[rustc_intrinsic]
3124pub const fn const_eval_select<ARG: Tuple, F, G, RET>(
3125 _arg: ARG,
3126 _called_in_const: F,
3127 _called_at_rt: G,
3128) -> RET
3129where
3130 G: FnOnce<ARG, Output = RET>,
3131 F: FnOnce<ARG, Output = RET>;
3132
3133/// A macro to make it easier to invoke const_eval_select. Use as follows:
3134/// ```rust,ignore (just a macro example)
3135/// const_eval_select!(
3136/// @capture { arg1: i32 = some_expr, arg2: T = other_expr } -> U:
3137/// if const #[attributes_for_const_arm] {
3138/// // Compile-time code goes here.
3139/// } else #[attributes_for_runtime_arm] {
3140/// // Run-time code goes here.
3141/// }
3142/// )
3143/// ```
3144/// The `@capture` block declares which surrounding variables / expressions can be
3145/// used inside the `if const`.
3146/// Note that the two arms of this `if` really each become their own function, which is why the
3147/// macro supports setting attributes for those functions. The runtime function is always
3148/// markes as `#[inline]`.
3149///
3150/// See [`const_eval_select()`] for the rules and requirements around that intrinsic.
3151pub(crate) macro const_eval_select {
3152 (
3153 @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
3154 if const
3155 $(#[$compiletime_attr:meta])* $compiletime:block
3156 else
3157 $(#[$runtime_attr:meta])* $runtime:block
3158 ) => {
3159 // Use the `noinline` arm, after adding explicit `inline` attributes
3160 $crate::intrinsics::const_eval_select!(
3161 @capture$([$($binders)*])? { $($arg : $ty = $val),* } $(-> $ret)? :
3162 #[noinline]
3163 if const
3164 #[inline] // prevent codegen on this function
3165 $(#[$compiletime_attr])*
3166 $compiletime
3167 else
3168 #[inline] // avoid the overhead of an extra fn call
3169 $(#[$runtime_attr])*
3170 $runtime
3171 )
3172 },
3173 // With a leading #[noinline], we don't add inline attributes
3174 (
3175 @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
3176 #[noinline]
3177 if const
3178 $(#[$compiletime_attr:meta])* $compiletime:block
3179 else
3180 $(#[$runtime_attr:meta])* $runtime:block
3181 ) => {{
3182 $(#[$runtime_attr])*
3183 fn runtime$(<$($binders)*>)?($($arg: $ty),*) $( -> $ret )? {
3184 $runtime
3185 }
3186
3187 $(#[$compiletime_attr])*
3188 const fn compiletime$(<$($binders)*>)?($($arg: $ty),*) $( -> $ret )? {
3189 // Don't warn if one of the arguments is unused.
3190 $(let _ = $arg;)*
3191
3192 $compiletime
3193 }
3194
3195 const_eval_select(($($val,)*), compiletime, runtime)
3196 }},
3197 // We support leaving away the `val` expressions for *all* arguments
3198 // (but not for *some* arguments, that's too tricky).
3199 (
3200 @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty),* $(,)? } $( -> $ret:ty )? :
3201 if const
3202 $(#[$compiletime_attr:meta])* $compiletime:block
3203 else
3204 $(#[$runtime_attr:meta])* $runtime:block
3205 ) => {
3206 $crate::intrinsics::const_eval_select!(
3207 @capture$([$($binders)*])? { $($arg : $ty = $arg),* } $(-> $ret)? :
3208 if const
3209 $(#[$compiletime_attr])* $compiletime
3210 else
3211 $(#[$runtime_attr])* $runtime
3212 )
3213 },
3214}
3215
3216/// Returns whether the argument's value is statically known at
3217/// compile-time.
3218///
3219/// This is useful when there is a way of writing the code that will
3220/// be *faster* when some variables have known values, but *slower*
3221/// in the general case: an `if is_val_statically_known(var)` can be used
3222/// to select between these two variants. The `if` will be optimized away
3223/// and only the desired branch remains.
3224///
3225/// Formally speaking, this function non-deterministically returns `true`
3226/// or `false`, and the caller has to ensure sound behavior for both cases.
3227/// In other words, the following code has *Undefined Behavior*:
3228///
3229/// ```no_run
3230/// #![feature(core_intrinsics)]
3231/// # #![allow(internal_features)]
3232/// use std::hint::unreachable_unchecked;
3233/// use std::intrinsics::is_val_statically_known;
3234///
3235/// if !is_val_statically_known(0) { unsafe { unreachable_unchecked(); } }
3236/// ```
3237///
3238/// This also means that the following code's behavior is unspecified; it
3239/// may panic, or it may not:
3240///
3241/// ```no_run
3242/// #![feature(core_intrinsics)]
3243/// # #![allow(internal_features)]
3244/// use std::intrinsics::is_val_statically_known;
3245///
3246/// assert_eq!(is_val_statically_known(0), is_val_statically_known(0));
3247/// ```
3248///
3249/// Unsafe code may not rely on `is_val_statically_known` returning any
3250/// particular value, ever. However, the compiler will generally make it
3251/// return `true` only if the value of the argument is actually known.
3252///
3253/// # Stability concerns
3254///
3255/// While it is safe to call, this intrinsic may behave differently in
3256/// a `const` context than otherwise. See the [`const_eval_select()`]
3257/// documentation for an explanation of the issues this can cause. Unlike
3258/// `const_eval_select`, this intrinsic isn't guaranteed to behave
3259/// deterministically even in a `const` context.
3260///
3261/// # Type Requirements
3262///
3263/// `T` must be either a `bool`, a `char`, a primitive numeric type (e.g. `f32`,
3264/// but not `NonZeroISize`), or any thin pointer (e.g. `*mut String`).
3265/// Any other argument types *may* cause a compiler error.
3266///
3267/// ## Pointers
3268///
3269/// When the input is a pointer, only the pointer itself is
3270/// ever considered. The pointee has no effect. Currently, these functions
3271/// behave identically:
3272///
3273/// ```
3274/// #![feature(core_intrinsics)]
3275/// # #![allow(internal_features)]
3276/// use std::intrinsics::is_val_statically_known;
3277///
3278/// fn foo(x: &i32) -> bool {
3279/// is_val_statically_known(x)
3280/// }
3281///
3282/// fn bar(x: &i32) -> bool {
3283/// is_val_statically_known(
3284/// (x as *const i32).addr()
3285/// )
3286/// }
3287/// # _ = foo(&5_i32);
3288/// # _ = bar(&5_i32);
3289/// ```
3290#[rustc_const_stable_indirect]
3291#[rustc_nounwind]
3292#[unstable(feature = "core_intrinsics", issue = "none")]
3293#[rustc_intrinsic]
3294pub const fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
3295 false
3296}
3297
3298/// Non-overlapping *typed* swap of a single value.
3299///
3300/// The codegen backends will replace this with a better implementation when
3301/// `T` is a simple type that can be loaded and stored as an immediate.
3302///
3303/// The stabilized form of this intrinsic is [`crate::mem::swap`].
3304///
3305/// # Safety
3306/// Behavior is undefined if any of the following conditions are violated:
3307///
3308/// * Both `x` and `y` must be [valid] for both reads and writes.
3309///
3310/// * Both `x` and `y` must be properly aligned.
3311///
3312/// * The region of memory beginning at `x` must *not* overlap with the region of memory
3313/// beginning at `y`.
3314///
3315/// * The memory pointed by `x` and `y` must both contain values of type `T`.
3316///
3317/// [valid]: crate::ptr#safety
3318#[rustc_nounwind]
3319#[inline]
3320#[rustc_intrinsic]
3321#[rustc_intrinsic_const_stable_indirect]
3322#[rustc_allow_const_fn_unstable(const_swap_nonoverlapping)] // this is anyway not called since CTFE implements the intrinsic
3323pub const unsafe fn typed_swap_nonoverlapping<T>(x: *mut T, y: *mut T) {
3324 // SAFETY: The caller provided single non-overlapping items behind
3325 // pointers, so swapping them with `count: 1` is fine.
3326 unsafe { ptr::swap_nonoverlapping(x, y, 1) };
3327}
3328
3329/// Returns whether we should perform some UB-checking at runtime. This eventually evaluates to
3330/// `cfg!(ub_checks)`, but behaves different from `cfg!` when mixing crates built with different
3331/// flags: if the crate has UB checks enabled or carries the `#[rustc_preserve_ub_checks]`
3332/// attribute, evaluation is delayed until monomorphization (or until the call gets inlined into
3333/// a crate that does not delay evaluation further); otherwise it can happen any time.
3334///
3335/// The common case here is a user program built with ub_checks linked against the distributed
3336/// sysroot which is built without ub_checks but with `#[rustc_preserve_ub_checks]`.
3337/// For code that gets monomorphized in the user crate (i.e., generic functions and functions with
3338/// `#[inline]`), gating assertions on `ub_checks()` rather than `cfg!(ub_checks)` means that
3339/// assertions are enabled whenever the *user crate* has UB checks enabled. However, if the
3340/// user has UB checks disabled, the checks will still get optimized out. This intrinsic is
3341/// primarily used by [`ub_checks::assert_unsafe_precondition`].
3342#[rustc_intrinsic_const_stable_indirect] // just for UB checks
3343#[inline(always)]
3344#[rustc_intrinsic]
3345pub const fn ub_checks() -> bool {
3346 cfg!(ub_checks)
3347}
3348
3349/// Allocates a block of memory at compile time.
3350/// At runtime, just returns a null pointer.
3351///
3352/// # Safety
3353///
3354/// - The `align` argument must be a power of two.
3355/// - At compile time, a compile error occurs if this constraint is violated.
3356/// - At runtime, it is not checked.
3357#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
3358#[rustc_nounwind]
3359#[rustc_intrinsic]
3360#[miri::intrinsic_fallback_is_spec]
3361pub const unsafe fn const_allocate(_size: usize, _align: usize) -> *mut u8 {
3362 // const eval overrides this function, but runtime code for now just returns null pointers.
3363 // See <https://github.com/rust-lang/rust/issues/93935>.
3364 crate::ptr::null_mut()
3365}
3366
3367/// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
3368/// At runtime, does nothing.
3369///
3370/// # Safety
3371///
3372/// - The `align` argument must be a power of two.
3373/// - At compile time, a compile error occurs if this constraint is violated.
3374/// - At runtime, it is not checked.
3375/// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
3376/// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
3377#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
3378#[unstable(feature = "core_intrinsics", issue = "none")]
3379#[rustc_nounwind]
3380#[rustc_intrinsic]
3381#[miri::intrinsic_fallback_is_spec]
3382pub const unsafe fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {
3383 // Runtime NOP
3384}
3385
3386/// Returns whether we should perform contract-checking at runtime.
3387///
3388/// This is meant to be similar to the ub_checks intrinsic, in terms
3389/// of not prematurely commiting at compile-time to whether contract
3390/// checking is turned on, so that we can specify contracts in libstd
3391/// and let an end user opt into turning them on.
3392#[rustc_const_unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
3393#[unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
3394#[inline(always)]
3395#[rustc_intrinsic]
3396pub const fn contract_checks() -> bool {
3397 // FIXME: should this be `false` or `cfg!(contract_checks)`?
3398
3399 // cfg!(contract_checks)
3400 false
3401}
3402
3403/// Check if the pre-condition `cond` has been met.
3404///
3405/// By default, if `contract_checks` is enabled, this will panic with no unwind if the condition
3406/// returns false.
3407///
3408/// Note that this function is a no-op during constant evaluation.
3409#[unstable(feature = "contracts_internals", issue = "128044")]
3410// Calls to this function get inserted by an AST expansion pass, which uses the equivalent of
3411// `#[allow_internal_unstable]` to allow using `contracts_internals` functions. Const-checking
3412// doesn't honor `#[allow_internal_unstable]`, so for the const feature gate we use the user-facing
3413// `contracts` feature rather than the perma-unstable `contracts_internals`
3414#[rustc_const_unstable(feature = "contracts", issue = "128044")]
3415#[lang = "contract_check_requires"]
3416#[rustc_intrinsic]
3417pub const fn contract_check_requires<C: Fn() -> bool + Copy>(cond: C) {
3418 const_eval_select!(
3419 @capture[C: Fn() -> bool + Copy] { cond: C } :
3420 if const {
3421 // Do nothing
3422 } else {
3423 if contract_checks() && !cond() {
3424 // Emit no unwind panic in case this was a safety requirement.
3425 crate::panicking::panic_nounwind("failed requires check");
3426 }
3427 }
3428 )
3429}
3430
3431/// Check if the post-condition `cond` has been met.
3432///
3433/// By default, if `contract_checks` is enabled, this will panic with no unwind if the condition
3434/// returns false.
3435///
3436/// Note that this function is a no-op during constant evaluation.
3437#[cfg(not(bootstrap))]
3438#[unstable(feature = "contracts_internals", issue = "128044")]
3439// Similar to `contract_check_requires`, we need to use the user-facing
3440// `contracts` feature rather than the perma-unstable `contracts_internals`.
3441// Const-checking doesn't honor allow_internal_unstable logic used by contract expansion.
3442#[rustc_const_unstable(feature = "contracts", issue = "128044")]
3443#[lang = "contract_check_ensures"]
3444#[rustc_intrinsic]
3445pub const fn contract_check_ensures<C: Fn(&Ret) -> bool + Copy, Ret>(cond: C, ret: Ret) -> Ret {
3446 const_eval_select!(
3447 @capture[C: Fn(&Ret) -> bool + Copy, Ret] { cond: C, ret: Ret } -> Ret :
3448 if const {
3449 // Do nothing
3450 ret
3451 } else {
3452 if contract_checks() && !cond(&ret) {
3453 // Emit no unwind panic in case this was a safety requirement.
3454 crate::panicking::panic_nounwind("failed ensures check");
3455 }
3456 ret
3457 }
3458 )
3459}
3460
3461/// This is the old version of contract_check_ensures kept here for bootstrap only.
3462#[cfg(bootstrap)]
3463#[unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
3464#[rustc_intrinsic]
3465pub fn contract_check_ensures<'a, Ret, C: Fn(&'a Ret) -> bool>(ret: &'a Ret, cond: C) {
3466 if contract_checks() && !cond(ret) {
3467 crate::panicking::panic_nounwind("failed ensures check");
3468 }
3469}
3470
3471/// The intrinsic will return the size stored in that vtable.
3472///
3473/// # Safety
3474///
3475/// `ptr` must point to a vtable.
3476#[rustc_nounwind]
3477#[unstable(feature = "core_intrinsics", issue = "none")]
3478#[rustc_intrinsic]
3479pub unsafe fn vtable_size(ptr: *const ()) -> usize;
3480
3481/// The intrinsic will return the alignment stored in that vtable.
3482///
3483/// # Safety
3484///
3485/// `ptr` must point to a vtable.
3486#[rustc_nounwind]
3487#[unstable(feature = "core_intrinsics", issue = "none")]
3488#[rustc_intrinsic]
3489pub unsafe fn vtable_align(ptr: *const ()) -> usize;
3490
3491/// The size of a type in bytes.
3492///
3493/// Note that, unlike most intrinsics, this is safe to call;
3494/// it does not require an `unsafe` block.
3495/// Therefore, implementations must not require the user to uphold
3496/// any safety invariants.
3497///
3498/// More specifically, this is the offset in bytes between successive
3499/// items of the same type, including alignment padding.
3500///
3501/// The stabilized version of this intrinsic is [`size_of`].
3502#[rustc_nounwind]
3503#[unstable(feature = "core_intrinsics", issue = "none")]
3504#[rustc_intrinsic_const_stable_indirect]
3505#[rustc_intrinsic]
3506pub const fn size_of<T>() -> usize;
3507
3508/// The minimum alignment of a type.
3509///
3510/// Note that, unlike most intrinsics, this is safe to call;
3511/// it does not require an `unsafe` block.
3512/// Therefore, implementations must not require the user to uphold
3513/// any safety invariants.
3514///
3515/// The stabilized version of this intrinsic is [`align_of`].
3516#[rustc_nounwind]
3517#[unstable(feature = "core_intrinsics", issue = "none")]
3518#[rustc_intrinsic_const_stable_indirect]
3519#[rustc_intrinsic]
3520pub const fn min_align_of<T>() -> usize;
3521
3522/// The preferred alignment of a type.
3523///
3524/// This intrinsic does not have a stable counterpart.
3525/// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
3526#[rustc_nounwind]
3527#[unstable(feature = "core_intrinsics", issue = "none")]
3528#[rustc_intrinsic]
3529pub const unsafe fn pref_align_of<T>() -> usize;
3530
3531/// Returns the number of variants of the type `T` cast to a `usize`;
3532/// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
3533///
3534/// Note that, unlike most intrinsics, this is safe to call;
3535/// it does not require an `unsafe` block.
3536/// Therefore, implementations must not require the user to uphold
3537/// any safety invariants.
3538///
3539/// The to-be-stabilized version of this intrinsic is [`crate::mem::variant_count`].
3540#[rustc_nounwind]
3541#[unstable(feature = "core_intrinsics", issue = "none")]
3542#[rustc_intrinsic]
3543pub const fn variant_count<T>() -> usize;
3544
3545/// The size of the referenced value in bytes.
3546///
3547/// The stabilized version of this intrinsic is [`size_of_val`].
3548///
3549/// # Safety
3550///
3551/// See [`crate::mem::size_of_val_raw`] for safety conditions.
3552#[rustc_nounwind]
3553#[unstable(feature = "core_intrinsics", issue = "none")]
3554#[rustc_intrinsic]
3555#[rustc_intrinsic_const_stable_indirect]
3556pub const unsafe fn size_of_val<T: ?Sized>(ptr: *const T) -> usize;
3557
3558/// The required alignment of the referenced value.
3559///
3560/// The stabilized version of this intrinsic is [`align_of_val`].
3561///
3562/// # Safety
3563///
3564/// See [`crate::mem::align_of_val_raw`] for safety conditions.
3565#[rustc_nounwind]
3566#[unstable(feature = "core_intrinsics", issue = "none")]
3567#[rustc_intrinsic]
3568#[rustc_intrinsic_const_stable_indirect]
3569pub const unsafe fn min_align_of_val<T: ?Sized>(ptr: *const T) -> usize;
3570
3571/// Gets a static string slice containing the name of a type.
3572///
3573/// Note that, unlike most intrinsics, this is safe to call;
3574/// it does not require an `unsafe` block.
3575/// Therefore, implementations must not require the user to uphold
3576/// any safety invariants.
3577///
3578/// The stabilized version of this intrinsic is [`core::any::type_name`].
3579#[rustc_nounwind]
3580#[unstable(feature = "core_intrinsics", issue = "none")]
3581#[rustc_intrinsic]
3582pub const fn type_name<T: ?Sized>() -> &'static str;
3583
3584/// Gets an identifier which is globally unique to the specified type. This
3585/// function will return the same value for a type regardless of whichever
3586/// crate it is invoked in.
3587///
3588/// Note that, unlike most intrinsics, this is safe to call;
3589/// it does not require an `unsafe` block.
3590/// Therefore, implementations must not require the user to uphold
3591/// any safety invariants.
3592///
3593/// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
3594#[rustc_nounwind]
3595#[unstable(feature = "core_intrinsics", issue = "none")]
3596#[rustc_intrinsic]
3597pub const fn type_id<T: ?Sized + 'static>() -> u128;
3598
3599/// Lowers in MIR to `Rvalue::Aggregate` with `AggregateKind::RawPtr`.
3600///
3601/// This is used to implement functions like `slice::from_raw_parts_mut` and
3602/// `ptr::from_raw_parts` in a way compatible with the compiler being able to
3603/// change the possible layouts of pointers.
3604#[rustc_nounwind]
3605#[unstable(feature = "core_intrinsics", issue = "none")]
3606#[rustc_intrinsic_const_stable_indirect]
3607#[rustc_intrinsic]
3608pub const fn aggregate_raw_ptr<P: AggregateRawPtr<D, Metadata = M>, D, M>(data: D, meta: M) -> P;
3609
3610#[unstable(feature = "core_intrinsics", issue = "none")]
3611pub trait AggregateRawPtr<D> {
3612 type Metadata: Copy;
3613}
3614impl<P: ?Sized, T: ptr::Thin> AggregateRawPtr<*const T> for *const P {
3615 type Metadata = <P as ptr::Pointee>::Metadata;
3616}
3617impl<P: ?Sized, T: ptr::Thin> AggregateRawPtr<*mut T> for *mut P {
3618 type Metadata = <P as ptr::Pointee>::Metadata;
3619}
3620
3621/// Lowers in MIR to `Rvalue::UnaryOp` with `UnOp::PtrMetadata`.
3622///
3623/// This is used to implement functions like `ptr::metadata`.
3624#[rustc_nounwind]
3625#[unstable(feature = "core_intrinsics", issue = "none")]
3626#[rustc_intrinsic_const_stable_indirect]
3627#[rustc_intrinsic]
3628pub const fn ptr_metadata<P: ptr::Pointee<Metadata = M> + ?Sized, M>(ptr: *const P) -> M;
3629
3630// Some functions are defined here because they accidentally got made
3631// available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
3632// (`transmute` also falls into this category, but it cannot be wrapped due to the
3633// check that `T` and `U` have the same size.)
3634
3635/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
3636/// and destination must *not* overlap.
3637///
3638/// For regions of memory which might overlap, use [`copy`] instead.
3639///
3640/// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
3641/// with the source and destination arguments swapped,
3642/// and `count` counting the number of `T`s instead of bytes.
3643///
3644/// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
3645/// requirements of `T`. The initialization state is preserved exactly.
3646///
3647/// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
3648///
3649/// # Safety
3650///
3651/// Behavior is undefined if any of the following conditions are violated:
3652///
3653/// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
3654///
3655/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
3656///
3657/// * Both `src` and `dst` must be properly aligned.
3658///
3659/// * The region of memory beginning at `src` with a size of `count *
3660/// size_of::<T>()` bytes must *not* overlap with the region of memory
3661/// beginning at `dst` with the same size.
3662///
3663/// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
3664/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
3665/// in the region beginning at `*src` and the region beginning at `*dst` can
3666/// [violate memory safety][read-ownership].
3667///
3668/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
3669/// `0`, the pointers must be properly aligned.
3670///
3671/// [`read`]: crate::ptr::read
3672/// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
3673/// [valid]: crate::ptr#safety
3674///
3675/// # Examples
3676///
3677/// Manually implement [`Vec::append`]:
3678///
3679/// ```
3680/// use std::ptr;
3681///
3682/// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
3683/// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
3684/// let src_len = src.len();
3685/// let dst_len = dst.len();
3686///
3687/// // Ensure that `dst` has enough capacity to hold all of `src`.
3688/// dst.reserve(src_len);
3689///
3690/// unsafe {
3691/// // The call to add is always safe because `Vec` will never
3692/// // allocate more than `isize::MAX` bytes.
3693/// let dst_ptr = dst.as_mut_ptr().add(dst_len);
3694/// let src_ptr = src.as_ptr();
3695///
3696/// // Truncate `src` without dropping its contents. We do this first,
3697/// // to avoid problems in case something further down panics.
3698/// src.set_len(0);
3699///
3700/// // The two regions cannot overlap because mutable references do
3701/// // not alias, and two different vectors cannot own the same
3702/// // memory.
3703/// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
3704///
3705/// // Notify `dst` that it now holds the contents of `src`.
3706/// dst.set_len(dst_len + src_len);
3707/// }
3708/// }
3709///
3710/// let mut a = vec!['r'];
3711/// let mut b = vec!['u', 's', 't'];
3712///
3713/// append(&mut a, &mut b);
3714///
3715/// assert_eq!(a, &['r', 'u', 's', 't']);
3716/// assert!(b.is_empty());
3717/// ```
3718///
3719/// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
3720#[doc(alias = "memcpy")]
3721#[stable(feature = "rust1", since = "1.0.0")]
3722#[rustc_allowed_through_unstable_modules = "import this function via `std::ptr` instead"]
3723#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
3724#[inline(always)]
3725#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
3726#[rustc_diagnostic_item = "ptr_copy_nonoverlapping"]
3727pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
3728 #[rustc_intrinsic_const_stable_indirect]
3729 #[rustc_nounwind]
3730 #[rustc_intrinsic]
3731 const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
3732
3733 ub_checks::assert_unsafe_precondition!(
3734 check_language_ub,
3735 "ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
3736 and the specified memory ranges do not overlap",
3737 (
3738 src: *const () = src as *const (),
3739 dst: *mut () = dst as *mut (),
3740 size: usize = size_of::<T>(),
3741 align: usize = align_of::<T>(),
3742 count: usize = count,
3743 ) => {
3744 let zero_size = count == 0 || size == 0;
3745 ub_checks::maybe_is_aligned_and_not_null(src, align, zero_size)
3746 && ub_checks::maybe_is_aligned_and_not_null(dst, align, zero_size)
3747 && ub_checks::maybe_is_nonoverlapping(src, dst, size, count)
3748 }
3749 );
3750
3751 // SAFETY: the safety contract for `copy_nonoverlapping` must be
3752 // upheld by the caller.
3753 unsafe { copy_nonoverlapping(src, dst, count) }
3754}
3755
3756/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
3757/// and destination may overlap.
3758///
3759/// If the source and destination will *never* overlap,
3760/// [`copy_nonoverlapping`] can be used instead.
3761///
3762/// `copy` is semantically equivalent to C's [`memmove`], but
3763/// with the source and destination arguments swapped,
3764/// and `count` counting the number of `T`s instead of bytes.
3765/// Copying takes place as if the bytes were copied from `src`
3766/// to a temporary array and then copied from the array to `dst`.
3767///
3768/// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
3769/// requirements of `T`. The initialization state is preserved exactly.
3770///
3771/// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
3772///
3773/// # Safety
3774///
3775/// Behavior is undefined if any of the following conditions are violated:
3776///
3777/// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
3778///
3779/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes, and must remain valid even
3780/// when `src` is read for `count * size_of::<T>()` bytes. (This means if the memory ranges
3781/// overlap, the `dst` pointer must not be invalidated by `src` reads.)
3782///
3783/// * Both `src` and `dst` must be properly aligned.
3784///
3785/// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
3786/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
3787/// in the region beginning at `*src` and the region beginning at `*dst` can
3788/// [violate memory safety][read-ownership].
3789///
3790/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
3791/// `0`, the pointers must be properly aligned.
3792///
3793/// [`read`]: crate::ptr::read
3794/// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
3795/// [valid]: crate::ptr#safety
3796///
3797/// # Examples
3798///
3799/// Efficiently create a Rust vector from an unsafe buffer:
3800///
3801/// ```
3802/// use std::ptr;
3803///
3804/// /// # Safety
3805/// ///
3806/// /// * `ptr` must be correctly aligned for its type and non-zero.
3807/// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
3808/// /// * Those elements must not be used after calling this function unless `T: Copy`.
3809/// # #[allow(dead_code)]
3810/// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
3811/// let mut dst = Vec::with_capacity(elts);
3812///
3813/// // SAFETY: Our precondition ensures the source is aligned and valid,
3814/// // and `Vec::with_capacity` ensures that we have usable space to write them.
3815/// unsafe { ptr::copy(ptr, dst.as_mut_ptr(), elts); }
3816///
3817/// // SAFETY: We created it with this much capacity earlier,
3818/// // and the previous `copy` has initialized these elements.
3819/// unsafe { dst.set_len(elts); }
3820/// dst
3821/// }
3822/// ```
3823#[doc(alias = "memmove")]
3824#[stable(feature = "rust1", since = "1.0.0")]
3825#[rustc_allowed_through_unstable_modules = "import this function via `std::ptr` instead"]
3826#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
3827#[inline(always)]
3828#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
3829#[rustc_diagnostic_item = "ptr_copy"]
3830pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
3831 #[rustc_intrinsic_const_stable_indirect]
3832 #[rustc_nounwind]
3833 #[rustc_intrinsic]
3834 const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize);
3835
3836 // SAFETY: the safety contract for `copy` must be upheld by the caller.
3837 unsafe {
3838 ub_checks::assert_unsafe_precondition!(
3839 check_language_ub,
3840 "ptr::copy requires that both pointer arguments are aligned and non-null",
3841 (
3842 src: *const () = src as *const (),
3843 dst: *mut () = dst as *mut (),
3844 align: usize = align_of::<T>(),
3845 zero_size: bool = T::IS_ZST || count == 0,
3846 ) =>
3847 ub_checks::maybe_is_aligned_and_not_null(src, align, zero_size)
3848 && ub_checks::maybe_is_aligned_and_not_null(dst, align, zero_size)
3849 );
3850 copy(src, dst, count)
3851 }
3852}
3853
3854/// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
3855/// `val`.
3856///
3857/// `write_bytes` is similar to C's [`memset`], but sets `count *
3858/// size_of::<T>()` bytes to `val`.
3859///
3860/// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
3861///
3862/// # Safety
3863///
3864/// Behavior is undefined if any of the following conditions are violated:
3865///
3866/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
3867///
3868/// * `dst` must be properly aligned.
3869///
3870/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
3871/// `0`, the pointer must be properly aligned.
3872///
3873/// Additionally, note that changing `*dst` in this way can easily lead to undefined behavior (UB)
3874/// later if the written bytes are not a valid representation of some `T`. For instance, the
3875/// following is an **incorrect** use of this function:
3876///
3877/// ```rust,no_run
3878/// unsafe {
3879/// let mut value: u8 = 0;
3880/// let ptr: *mut bool = &mut value as *mut u8 as *mut bool;
3881/// let _bool = ptr.read(); // This is fine, `ptr` points to a valid `bool`.
3882/// ptr.write_bytes(42u8, 1); // This function itself does not cause UB...
3883/// let _bool = ptr.read(); // ...but it makes this operation UB! ⚠️
3884/// }
3885/// ```
3886///
3887/// [valid]: crate::ptr#safety
3888///
3889/// # Examples
3890///
3891/// Basic usage:
3892///
3893/// ```
3894/// use std::ptr;
3895///
3896/// let mut vec = vec![0u32; 4];
3897/// unsafe {
3898/// let vec_ptr = vec.as_mut_ptr();
3899/// ptr::write_bytes(vec_ptr, 0xfe, 2);
3900/// }
3901/// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
3902/// ```
3903#[doc(alias = "memset")]
3904#[stable(feature = "rust1", since = "1.0.0")]
3905#[rustc_allowed_through_unstable_modules = "import this function via `std::ptr` instead"]
3906#[rustc_const_stable(feature = "const_ptr_write", since = "1.83.0")]
3907#[inline(always)]
3908#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
3909#[rustc_diagnostic_item = "ptr_write_bytes"]
3910pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
3911 #[rustc_intrinsic_const_stable_indirect]
3912 #[rustc_nounwind]
3913 #[rustc_intrinsic]
3914 const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
3915
3916 // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
3917 unsafe {
3918 ub_checks::assert_unsafe_precondition!(
3919 check_language_ub,
3920 "ptr::write_bytes requires that the destination pointer is aligned and non-null",
3921 (
3922 addr: *const () = dst as *const (),
3923 align: usize = align_of::<T>(),
3924 zero_size: bool = T::IS_ZST || count == 0,
3925 ) => ub_checks::maybe_is_aligned_and_not_null(addr, align, zero_size)
3926 );
3927 write_bytes(dst, val, count)
3928 }
3929}
3930
3931/// Returns the minimum of two `f16` values.
3932///
3933/// Note that, unlike most intrinsics, this is safe to call;
3934/// it does not require an `unsafe` block.
3935/// Therefore, implementations must not require the user to uphold
3936/// any safety invariants.
3937///
3938/// The stabilized version of this intrinsic is
3939/// [`f16::min`]
3940#[rustc_nounwind]
3941#[rustc_intrinsic]
3942pub const fn minnumf16(x: f16, y: f16) -> f16;
3943
3944/// Returns the minimum of two `f32` values.
3945///
3946/// Note that, unlike most intrinsics, this is safe to call;
3947/// it does not require an `unsafe` block.
3948/// Therefore, implementations must not require the user to uphold
3949/// any safety invariants.
3950///
3951/// The stabilized version of this intrinsic is
3952/// [`f32::min`]
3953#[rustc_nounwind]
3954#[rustc_intrinsic_const_stable_indirect]
3955#[rustc_intrinsic]
3956pub const fn minnumf32(x: f32, y: f32) -> f32;
3957
3958/// Returns the minimum of two `f64` values.
3959///
3960/// Note that, unlike most intrinsics, this is safe to call;
3961/// it does not require an `unsafe` block.
3962/// Therefore, implementations must not require the user to uphold
3963/// any safety invariants.
3964///
3965/// The stabilized version of this intrinsic is
3966/// [`f64::min`]
3967#[rustc_nounwind]
3968#[rustc_intrinsic_const_stable_indirect]
3969#[rustc_intrinsic]
3970pub const fn minnumf64(x: f64, y: f64) -> f64;
3971
3972/// Returns the minimum of two `f128` values.
3973///
3974/// Note that, unlike most intrinsics, this is safe to call;
3975/// it does not require an `unsafe` block.
3976/// Therefore, implementations must not require the user to uphold
3977/// any safety invariants.
3978///
3979/// The stabilized version of this intrinsic is
3980/// [`f128::min`]
3981#[rustc_nounwind]
3982#[rustc_intrinsic]
3983pub const fn minnumf128(x: f128, y: f128) -> f128;
3984
3985/// Returns the maximum of two `f16` values.
3986///
3987/// Note that, unlike most intrinsics, this is safe to call;
3988/// it does not require an `unsafe` block.
3989/// Therefore, implementations must not require the user to uphold
3990/// any safety invariants.
3991///
3992/// The stabilized version of this intrinsic is
3993/// [`f16::max`]
3994#[rustc_nounwind]
3995#[rustc_intrinsic]
3996pub const fn maxnumf16(x: f16, y: f16) -> f16;
3997
3998/// Returns the maximum of two `f32` values.
3999///
4000/// Note that, unlike most intrinsics, this is safe to call;
4001/// it does not require an `unsafe` block.
4002/// Therefore, implementations must not require the user to uphold
4003/// any safety invariants.
4004///
4005/// The stabilized version of this intrinsic is
4006/// [`f32::max`]
4007#[rustc_nounwind]
4008#[rustc_intrinsic_const_stable_indirect]
4009#[rustc_intrinsic]
4010pub const fn maxnumf32(x: f32, y: f32) -> f32;
4011
4012/// Returns the maximum of two `f64` values.
4013///
4014/// Note that, unlike most intrinsics, this is safe to call;
4015/// it does not require an `unsafe` block.
4016/// Therefore, implementations must not require the user to uphold
4017/// any safety invariants.
4018///
4019/// The stabilized version of this intrinsic is
4020/// [`f64::max`]
4021#[rustc_nounwind]
4022#[rustc_intrinsic_const_stable_indirect]
4023#[rustc_intrinsic]
4024pub const fn maxnumf64(x: f64, y: f64) -> f64;
4025
4026/// Returns the maximum of two `f128` values.
4027///
4028/// Note that, unlike most intrinsics, this is safe to call;
4029/// it does not require an `unsafe` block.
4030/// Therefore, implementations must not require the user to uphold
4031/// any safety invariants.
4032///
4033/// The stabilized version of this intrinsic is
4034/// [`f128::max`]
4035#[rustc_nounwind]
4036#[rustc_intrinsic]
4037pub const fn maxnumf128(x: f128, y: f128) -> f128;
4038
4039/// Returns the absolute value of an `f16`.
4040///
4041/// The stabilized version of this intrinsic is
4042/// [`f16::abs`](../../std/primitive.f16.html#method.abs)
4043#[rustc_nounwind]
4044#[rustc_intrinsic]
4045pub const unsafe fn fabsf16(x: f16) -> f16;
4046
4047/// Returns the absolute value of an `f32`.
4048///
4049/// The stabilized version of this intrinsic is
4050/// [`f32::abs`](../../std/primitive.f32.html#method.abs)
4051#[rustc_nounwind]
4052#[rustc_intrinsic_const_stable_indirect]
4053#[rustc_intrinsic]
4054pub const unsafe fn fabsf32(x: f32) -> f32;
4055
4056/// Returns the absolute value of an `f64`.
4057///
4058/// The stabilized version of this intrinsic is
4059/// [`f64::abs`](../../std/primitive.f64.html#method.abs)
4060#[rustc_nounwind]
4061#[rustc_intrinsic_const_stable_indirect]
4062#[rustc_intrinsic]
4063pub const unsafe fn fabsf64(x: f64) -> f64;
4064
4065/// Returns the absolute value of an `f128`.
4066///
4067/// The stabilized version of this intrinsic is
4068/// [`f128::abs`](../../std/primitive.f128.html#method.abs)
4069#[rustc_nounwind]
4070#[rustc_intrinsic]
4071pub const unsafe fn fabsf128(x: f128) -> f128;
4072
4073/// Copies the sign from `y` to `x` for `f16` values.
4074///
4075/// The stabilized version of this intrinsic is
4076/// [`f16::copysign`](../../std/primitive.f16.html#method.copysign)
4077#[rustc_nounwind]
4078#[rustc_intrinsic]
4079pub const unsafe fn copysignf16(x: f16, y: f16) -> f16;
4080
4081/// Copies the sign from `y` to `x` for `f32` values.
4082///
4083/// The stabilized version of this intrinsic is
4084/// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
4085#[rustc_nounwind]
4086#[rustc_intrinsic_const_stable_indirect]
4087#[rustc_intrinsic]
4088pub const unsafe fn copysignf32(x: f32, y: f32) -> f32;
4089/// Copies the sign from `y` to `x` for `f64` values.
4090///
4091/// The stabilized version of this intrinsic is
4092/// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
4093#[rustc_nounwind]
4094#[rustc_intrinsic_const_stable_indirect]
4095#[rustc_intrinsic]
4096pub const unsafe fn copysignf64(x: f64, y: f64) -> f64;
4097
4098/// Copies the sign from `y` to `x` for `f128` values.
4099///
4100/// The stabilized version of this intrinsic is
4101/// [`f128::copysign`](../../std/primitive.f128.html#method.copysign)
4102#[rustc_nounwind]
4103#[rustc_intrinsic]
4104pub const unsafe fn copysignf128(x: f128, y: f128) -> f128;
4105
4106/// Inform Miri that a given pointer definitely has a certain alignment.
4107#[cfg(miri)]
4108#[rustc_allow_const_fn_unstable(const_eval_select)]
4109pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize) {
4110 unsafe extern "Rust" {
4111 /// Miri-provided extern function to promise that a given pointer is properly aligned for
4112 /// "symbolic" alignment checks. Will fail if the pointer is not actually aligned or `align` is
4113 /// not a power of two. Has no effect when alignment checks are concrete (which is the default).
4114 fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
4115 }
4116
4117 const_eval_select!(
4118 @capture { ptr: *const (), align: usize}:
4119 if const {
4120 // Do nothing.
4121 } else {
4122 // SAFETY: this call is always safe.
4123 unsafe {
4124 miri_promise_symbolic_alignment(ptr, align);
4125 }
4126 }
4127 )
4128}