core/intrinsics/
mod.rs

1//! Compiler intrinsics.
2//!
3//! The functions in this module are implementation details of `core` and should
4//! not be used outside of the standard library. We generally provide access to
5//! intrinsics via stable wrapper functions. Use these instead.
6//!
7//! These are the imports making intrinsics available to Rust code. The actual implementations live in the compiler.
8//! Some of these intrinsics are lowered to MIR in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_transform/src/lower_intrinsics.rs>.
9//! The remaining intrinsics are implemented for the LLVM backend in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs>
10//! and <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>,
11//! and for const evaluation in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>.
12//!
13//! # Const intrinsics
14//!
15//! In order to make an intrinsic unstable usable at compile-time, copy the implementation from
16//! <https://github.com/rust-lang/miri/blob/master/src/intrinsics> to
17//! <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>
18//! and make the intrinsic declaration below a `const fn`. This should be done in coordination with
19//! wg-const-eval.
20//!
21//! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
22//! `#[rustc_intrinsic_const_stable_indirect]` needs to be added to the intrinsic. Such a change requires
23//! T-lang approval, because it may bake a feature into the language that cannot be replicated in
24//! user code without compiler support.
25//!
26//! # Volatiles
27//!
28//! The volatile intrinsics provide operations intended to act on I/O
29//! memory, which are guaranteed to not be reordered by the compiler
30//! across other volatile intrinsics. See [`read_volatile`][ptr::read_volatile]
31//! and [`write_volatile`][ptr::write_volatile].
32//!
33//! # Atomics
34//!
35//! The atomic intrinsics provide common atomic operations on machine
36//! words, with multiple possible memory orderings. See the
37//! [atomic types][atomic] docs for details.
38//!
39//! # Unwinding
40//!
41//! Rust intrinsics may, in general, unwind. If an intrinsic can never unwind, add the
42//! `#[rustc_nounwind]` attribute so that the compiler can make use of this fact.
43//!
44//! However, even for intrinsics that may unwind, rustc assumes that a Rust intrinsics will never
45//! initiate a foreign (non-Rust) unwind, and thus for panic=abort we can always assume that these
46//! intrinsics cannot unwind.
47
48#![unstable(
49    feature = "core_intrinsics",
50    reason = "intrinsics are unlikely to ever be stabilized, instead \
51                      they should be used through stabilized interfaces \
52                      in the rest of the standard library",
53    issue = "none"
54)]
55#![allow(missing_docs)]
56
57use crate::ffi::va_list::{VaArgSafe, VaListImpl};
58use crate::marker::{ConstParamTy, DiscriminantKind, PointeeSized, Tuple};
59use crate::ptr;
60
61mod bounds;
62pub mod fallback;
63pub mod mir;
64pub mod simd;
65
66// These imports are used for simplifying intra-doc links
67#[allow(unused_imports)]
68#[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))]
69use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering};
70
71/// A type for atomic ordering parameters for intrinsics. This is a separate type from
72/// `atomic::Ordering` so that we can make it `ConstParamTy` and fix the values used here without a
73/// risk of leaking that to stable code.
74#[derive(Debug, ConstParamTy, PartialEq, Eq)]
75pub enum AtomicOrdering {
76    // These values must match the compiler's `AtomicOrdering` defined in
77    // `rustc_middle/src/ty/consts/int.rs`!
78    Relaxed = 0,
79    Release = 1,
80    Acquire = 2,
81    AcqRel = 3,
82    SeqCst = 4,
83}
84
85// N.B., these intrinsics take raw pointers because they mutate aliased
86// memory, which is not valid for either `&` or `&mut`.
87
88/// Stores a value if the current value is the same as the `old` value.
89/// `T` must be an integer or pointer type.
90///
91/// The stabilized version of this intrinsic is available on the
92/// [`atomic`] types via the `compare_exchange` method.
93/// For example, [`AtomicBool::compare_exchange`].
94#[rustc_intrinsic]
95#[rustc_nounwind]
96pub unsafe fn atomic_cxchg<
97    T: Copy,
98    const ORD_SUCC: AtomicOrdering,
99    const ORD_FAIL: AtomicOrdering,
100>(
101    dst: *mut T,
102    old: T,
103    src: T,
104) -> (T, bool);
105
106/// Stores a value if the current value is the same as the `old` value.
107/// `T` must be an integer or pointer type. The comparison may spuriously fail.
108///
109/// The stabilized version of this intrinsic is available on the
110/// [`atomic`] types via the `compare_exchange_weak` method.
111/// For example, [`AtomicBool::compare_exchange_weak`].
112#[rustc_intrinsic]
113#[rustc_nounwind]
114pub unsafe fn atomic_cxchgweak<
115    T: Copy,
116    const ORD_SUCC: AtomicOrdering,
117    const ORD_FAIL: AtomicOrdering,
118>(
119    _dst: *mut T,
120    _old: T,
121    _src: T,
122) -> (T, bool);
123
124/// Loads the current value of the pointer.
125/// `T` must be an integer or pointer type.
126///
127/// The stabilized version of this intrinsic is available on the
128/// [`atomic`] types via the `load` method. For example, [`AtomicBool::load`].
129#[rustc_intrinsic]
130#[rustc_nounwind]
131pub unsafe fn atomic_load<T: Copy, const ORD: AtomicOrdering>(src: *const T) -> T;
132
133/// Stores the value at the specified memory location.
134/// `T` must be an integer or pointer type.
135///
136/// The stabilized version of this intrinsic is available on the
137/// [`atomic`] types via the `store` method. For example, [`AtomicBool::store`].
138#[rustc_intrinsic]
139#[rustc_nounwind]
140pub unsafe fn atomic_store<T: Copy, const ORD: AtomicOrdering>(dst: *mut T, val: T);
141
142/// Stores the value at the specified memory location, returning the old value.
143/// `T` must be an integer or pointer type.
144///
145/// The stabilized version of this intrinsic is available on the
146/// [`atomic`] types via the `swap` method. For example, [`AtomicBool::swap`].
147#[rustc_intrinsic]
148#[rustc_nounwind]
149pub unsafe fn atomic_xchg<T: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: T) -> T;
150
151/// Adds to the current value, returning the previous value.
152/// `T` must be an integer or pointer type.
153/// `U` must be the same as `T` if that is an integer type, or `usize` if `T` is a pointer type.
154///
155/// The stabilized version of this intrinsic is available on the
156/// [`atomic`] types via the `fetch_add` method. For example, [`AtomicIsize::fetch_add`].
157#[rustc_intrinsic]
158#[rustc_nounwind]
159pub unsafe fn atomic_xadd<T: Copy, U: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: U) -> T;
160
161/// Subtract from the current value, returning the previous value.
162/// `T` must be an integer or pointer type.
163/// `U` must be the same as `T` if that is an integer type, or `usize` if `T` is a pointer type.
164///
165/// The stabilized version of this intrinsic is available on the
166/// [`atomic`] types via the `fetch_sub` method. For example, [`AtomicIsize::fetch_sub`].
167#[rustc_intrinsic]
168#[rustc_nounwind]
169pub unsafe fn atomic_xsub<T: Copy, U: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: U) -> T;
170
171/// Bitwise and with the current value, returning the previous value.
172/// `T` must be an integer or pointer type.
173/// `U` must be the same as `T` if that is an integer type, or `usize` if `T` is a pointer type.
174///
175/// The stabilized version of this intrinsic is available on the
176/// [`atomic`] types via the `fetch_and` method. For example, [`AtomicBool::fetch_and`].
177#[rustc_intrinsic]
178#[rustc_nounwind]
179pub unsafe fn atomic_and<T: Copy, U: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: U) -> T;
180
181/// Bitwise nand with the current value, returning the previous value.
182/// `T` must be an integer or pointer type.
183/// `U` must be the same as `T` if that is an integer type, or `usize` if `T` is a pointer type.
184///
185/// The stabilized version of this intrinsic is available on the
186/// [`AtomicBool`] type via the `fetch_nand` method. For example, [`AtomicBool::fetch_nand`].
187#[rustc_intrinsic]
188#[rustc_nounwind]
189pub unsafe fn atomic_nand<T: Copy, U: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: U) -> T;
190
191/// Bitwise or with the current value, returning the previous value.
192/// `T` must be an integer or pointer type.
193/// `U` must be the same as `T` if that is an integer type, or `usize` if `T` is a pointer type.
194///
195/// The stabilized version of this intrinsic is available on the
196/// [`atomic`] types via the `fetch_or` method. For example, [`AtomicBool::fetch_or`].
197#[rustc_intrinsic]
198#[rustc_nounwind]
199pub unsafe fn atomic_or<T: Copy, U: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: U) -> T;
200
201/// Bitwise xor with the current value, returning the previous value.
202/// `T` must be an integer or pointer type.
203/// `U` must be the same as `T` if that is an integer type, or `usize` if `T` is a pointer type.
204///
205/// The stabilized version of this intrinsic is available on the
206/// [`atomic`] types via the `fetch_xor` method. For example, [`AtomicBool::fetch_xor`].
207#[rustc_intrinsic]
208#[rustc_nounwind]
209pub unsafe fn atomic_xor<T: Copy, U: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: U) -> T;
210
211/// Maximum with the current value using a signed comparison.
212/// `T` must be a signed integer type.
213///
214/// The stabilized version of this intrinsic is available on the
215/// [`atomic`] signed integer types via the `fetch_max` method. For example, [`AtomicI32::fetch_max`].
216#[rustc_intrinsic]
217#[rustc_nounwind]
218pub unsafe fn atomic_max<T: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: T) -> T;
219
220/// Minimum with the current value using a signed comparison.
221/// `T` must be a signed integer type.
222///
223/// The stabilized version of this intrinsic is available on the
224/// [`atomic`] signed integer types via the `fetch_min` method. For example, [`AtomicI32::fetch_min`].
225#[rustc_intrinsic]
226#[rustc_nounwind]
227pub unsafe fn atomic_min<T: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: T) -> T;
228
229/// Minimum with the current value using an unsigned comparison.
230/// `T` must be an unsigned integer type.
231///
232/// The stabilized version of this intrinsic is available on the
233/// [`atomic`] unsigned integer types via the `fetch_min` method. For example, [`AtomicU32::fetch_min`].
234#[rustc_intrinsic]
235#[rustc_nounwind]
236pub unsafe fn atomic_umin<T: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: T) -> T;
237
238/// Maximum with the current value using an unsigned comparison.
239/// `T` must be an unsigned integer type.
240///
241/// The stabilized version of this intrinsic is available on the
242/// [`atomic`] unsigned integer types via the `fetch_max` method. For example, [`AtomicU32::fetch_max`].
243#[rustc_intrinsic]
244#[rustc_nounwind]
245pub unsafe fn atomic_umax<T: Copy, const ORD: AtomicOrdering>(dst: *mut T, src: T) -> T;
246
247/// An atomic fence.
248///
249/// The stabilized version of this intrinsic is available in
250/// [`atomic::fence`].
251#[rustc_intrinsic]
252#[rustc_nounwind]
253pub unsafe fn atomic_fence<const ORD: AtomicOrdering>();
254
255/// An atomic fence for synchronization within a single thread.
256///
257/// The stabilized version of this intrinsic is available in
258/// [`atomic::compiler_fence`].
259#[rustc_intrinsic]
260#[rustc_nounwind]
261pub unsafe fn atomic_singlethreadfence<const ORD: AtomicOrdering>();
262
263/// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
264/// if supported; otherwise, it is a no-op.
265/// Prefetches have no effect on the behavior of the program but can change its performance
266/// characteristics.
267///
268/// The `locality` argument must be a constant integer and is a temporal locality specifier
269/// ranging from (0) - no locality, to (3) - extremely local keep in cache.
270///
271/// This intrinsic does not have a stable counterpart.
272#[rustc_intrinsic]
273#[rustc_nounwind]
274pub unsafe fn prefetch_read_data<T>(data: *const T, locality: i32);
275/// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
276/// if supported; otherwise, it is a no-op.
277/// Prefetches have no effect on the behavior of the program but can change its performance
278/// characteristics.
279///
280/// The `locality` argument must be a constant integer and is a temporal locality specifier
281/// ranging from (0) - no locality, to (3) - extremely local keep in cache.
282///
283/// This intrinsic does not have a stable counterpart.
284#[rustc_intrinsic]
285#[rustc_nounwind]
286pub unsafe fn prefetch_write_data<T>(data: *const T, locality: i32);
287/// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
288/// if supported; otherwise, it is a no-op.
289/// Prefetches have no effect on the behavior of the program but can change its performance
290/// characteristics.
291///
292/// The `locality` argument must be a constant integer and is a temporal locality specifier
293/// ranging from (0) - no locality, to (3) - extremely local keep in cache.
294///
295/// This intrinsic does not have a stable counterpart.
296#[rustc_intrinsic]
297#[rustc_nounwind]
298pub unsafe fn prefetch_read_instruction<T>(data: *const T, locality: i32);
299/// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
300/// if supported; otherwise, it is a no-op.
301/// Prefetches have no effect on the behavior of the program but can change its performance
302/// characteristics.
303///
304/// The `locality` argument must be a constant integer and is a temporal locality specifier
305/// ranging from (0) - no locality, to (3) - extremely local keep in cache.
306///
307/// This intrinsic does not have a stable counterpart.
308#[rustc_intrinsic]
309#[rustc_nounwind]
310pub unsafe fn prefetch_write_instruction<T>(data: *const T, locality: i32);
311
312/// Executes a breakpoint trap, for inspection by a debugger.
313///
314/// This intrinsic does not have a stable counterpart.
315#[rustc_intrinsic]
316#[rustc_nounwind]
317pub fn breakpoint();
318
319/// Magic intrinsic that derives its meaning from attributes
320/// attached to the function.
321///
322/// For example, dataflow uses this to inject static assertions so
323/// that `rustc_peek(potentially_uninitialized)` would actually
324/// double-check that dataflow did indeed compute that it is
325/// uninitialized at that point in the control flow.
326///
327/// This intrinsic should not be used outside of the compiler.
328#[rustc_nounwind]
329#[rustc_intrinsic]
330pub fn rustc_peek<T>(_: T) -> T;
331
332/// Aborts the execution of the process.
333///
334/// Note that, unlike most intrinsics, this is safe to call;
335/// it does not require an `unsafe` block.
336/// Therefore, implementations must not require the user to uphold
337/// any safety invariants.
338///
339/// [`std::process::abort`](../../std/process/fn.abort.html) is to be preferred if possible,
340/// as its behavior is more user-friendly and more stable.
341///
342/// The current implementation of `intrinsics::abort` is to invoke an invalid instruction,
343/// on most platforms.
344/// On Unix, the
345/// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or
346/// `SIGBUS`.  The precise behavior is not guaranteed and not stable.
347#[rustc_nounwind]
348#[rustc_intrinsic]
349pub fn abort() -> !;
350
351/// Informs the optimizer that this point in the code is not reachable,
352/// enabling further optimizations.
353///
354/// N.B., this is very different from the `unreachable!()` macro: Unlike the
355/// macro, which panics when it is executed, it is *undefined behavior* to
356/// reach code marked with this function.
357///
358/// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`].
359#[rustc_intrinsic_const_stable_indirect]
360#[rustc_nounwind]
361#[rustc_intrinsic]
362pub const unsafe fn unreachable() -> !;
363
364/// Informs the optimizer that a condition is always true.
365/// If the condition is false, the behavior is undefined.
366///
367/// No code is generated for this intrinsic, but the optimizer will try
368/// to preserve it (and its condition) between passes, which may interfere
369/// with optimization of surrounding code and reduce performance. It should
370/// not be used if the invariant can be discovered by the optimizer on its
371/// own, or if it does not enable any significant optimizations.
372///
373/// The stabilized version of this intrinsic is [`core::hint::assert_unchecked`].
374#[rustc_intrinsic_const_stable_indirect]
375#[rustc_nounwind]
376#[unstable(feature = "core_intrinsics", issue = "none")]
377#[rustc_intrinsic]
378pub const unsafe fn assume(b: bool) {
379    if !b {
380        // SAFETY: the caller must guarantee the argument is never `false`
381        unsafe { unreachable() }
382    }
383}
384
385/// Hints to the compiler that current code path is cold.
386///
387/// Note that, unlike most intrinsics, this is safe to call;
388/// it does not require an `unsafe` block.
389/// Therefore, implementations must not require the user to uphold
390/// any safety invariants.
391///
392/// This intrinsic does not have a stable counterpart.
393#[unstable(feature = "core_intrinsics", issue = "none")]
394#[rustc_intrinsic]
395#[rustc_nounwind]
396#[miri::intrinsic_fallback_is_spec]
397#[cold]
398pub const fn cold_path() {}
399
400/// Hints to the compiler that branch condition is likely to be true.
401/// Returns the value passed to it.
402///
403/// Any use other than with `if` statements will probably not have an effect.
404///
405/// Note that, unlike most intrinsics, this is safe to call;
406/// it does not require an `unsafe` block.
407/// Therefore, implementations must not require the user to uphold
408/// any safety invariants.
409///
410/// This intrinsic does not have a stable counterpart.
411#[unstable(feature = "core_intrinsics", issue = "none")]
412#[rustc_nounwind]
413#[inline(always)]
414pub const fn likely(b: bool) -> bool {
415    if b {
416        true
417    } else {
418        cold_path();
419        false
420    }
421}
422
423/// Hints to the compiler that branch condition is likely to be false.
424/// Returns the value passed to it.
425///
426/// Any use other than with `if` statements will probably not have an effect.
427///
428/// Note that, unlike most intrinsics, this is safe to call;
429/// it does not require an `unsafe` block.
430/// Therefore, implementations must not require the user to uphold
431/// any safety invariants.
432///
433/// This intrinsic does not have a stable counterpart.
434#[unstable(feature = "core_intrinsics", issue = "none")]
435#[rustc_nounwind]
436#[inline(always)]
437pub const fn unlikely(b: bool) -> bool {
438    if b {
439        cold_path();
440        true
441    } else {
442        false
443    }
444}
445
446/// Returns either `true_val` or `false_val` depending on condition `b` with a
447/// hint to the compiler that this condition is unlikely to be correctly
448/// predicted by a CPU's branch predictor (e.g. a binary search).
449///
450/// This is otherwise functionally equivalent to `if b { true_val } else { false_val }`.
451///
452/// Note that, unlike most intrinsics, this is safe to call;
453/// it does not require an `unsafe` block.
454/// Therefore, implementations must not require the user to uphold
455/// any safety invariants.
456///
457/// The public form of this intrinsic is [`core::hint::select_unpredictable`].
458/// However unlike the public form, the intrinsic will not drop the value that
459/// is not selected.
460#[unstable(feature = "core_intrinsics", issue = "none")]
461#[rustc_intrinsic]
462#[rustc_nounwind]
463#[miri::intrinsic_fallback_is_spec]
464#[inline]
465pub fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T {
466    if b { true_val } else { false_val }
467}
468
469/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
470/// This will statically either panic, or do nothing. It does not *guarantee* to ever panic,
471/// and should only be called if an assertion failure will imply language UB in the following code.
472///
473/// This intrinsic does not have a stable counterpart.
474#[rustc_intrinsic_const_stable_indirect]
475#[rustc_nounwind]
476#[rustc_intrinsic]
477pub const fn assert_inhabited<T>();
478
479/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
480/// zero-initialization: This will statically either panic, or do nothing. It does not *guarantee*
481/// to ever panic, and should only be called if an assertion failure will imply language UB in the
482/// following code.
483///
484/// This intrinsic does not have a stable counterpart.
485#[rustc_intrinsic_const_stable_indirect]
486#[rustc_nounwind]
487#[rustc_intrinsic]
488pub const fn assert_zero_valid<T>();
489
490/// A guard for `std::mem::uninitialized`. This will statically either panic, or do nothing. It does
491/// not *guarantee* to ever panic, and should only be called if an assertion failure will imply
492/// language UB in the following code.
493///
494/// This intrinsic does not have a stable counterpart.
495#[rustc_intrinsic_const_stable_indirect]
496#[rustc_nounwind]
497#[rustc_intrinsic]
498pub const fn assert_mem_uninitialized_valid<T>();
499
500/// Gets a reference to a static `Location` indicating where it was called.
501///
502/// Note that, unlike most intrinsics, this is safe to call;
503/// it does not require an `unsafe` block.
504/// Therefore, implementations must not require the user to uphold
505/// any safety invariants.
506///
507/// Consider using [`core::panic::Location::caller`] instead.
508#[rustc_intrinsic_const_stable_indirect]
509#[rustc_nounwind]
510#[rustc_intrinsic]
511pub const fn caller_location() -> &'static crate::panic::Location<'static>;
512
513/// Moves a value out of scope without running drop glue.
514///
515/// This exists solely for [`crate::mem::forget_unsized`]; normal `forget` uses
516/// `ManuallyDrop` instead.
517///
518/// Note that, unlike most intrinsics, this is safe to call;
519/// it does not require an `unsafe` block.
520/// Therefore, implementations must not require the user to uphold
521/// any safety invariants.
522#[rustc_intrinsic_const_stable_indirect]
523#[rustc_nounwind]
524#[rustc_intrinsic]
525pub const fn forget<T: ?Sized>(_: T);
526
527/// Reinterprets the bits of a value of one type as another type.
528///
529/// Both types must have the same size. Compilation will fail if this is not guaranteed.
530///
531/// `transmute` is semantically equivalent to a bitwise move of one type
532/// into another. It copies the bits from the source value into the
533/// destination value, then forgets the original. Note that source and destination
534/// are passed by-value, which means if `Src` or `Dst` contain padding, that padding
535/// is *not* guaranteed to be preserved by `transmute`.
536///
537/// Both the argument and the result must be [valid](../../nomicon/what-unsafe-does.html) at
538/// their given type. Violating this condition leads to [undefined behavior][ub]. The compiler
539/// will generate code *assuming that you, the programmer, ensure that there will never be
540/// undefined behavior*. It is therefore your responsibility to guarantee that every value
541/// passed to `transmute` is valid at both types `Src` and `Dst`. Failing to uphold this condition
542/// may lead to unexpected and unstable compilation results. This makes `transmute` **incredibly
543/// unsafe**. `transmute` should be the absolute last resort.
544///
545/// Because `transmute` is a by-value operation, alignment of the *transmuted values
546/// themselves* is not a concern. As with any other function, the compiler already ensures
547/// both `Src` and `Dst` are properly aligned. However, when transmuting values that *point
548/// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper
549/// alignment of the pointed-to values.
550///
551/// The [nomicon](../../nomicon/transmutes.html) has additional documentation.
552///
553/// [ub]: ../../reference/behavior-considered-undefined.html
554///
555/// # Transmutation between pointers and integers
556///
557/// Special care has to be taken when transmuting between pointers and integers, e.g.
558/// transmuting between `*const ()` and `usize`.
559///
560/// Transmuting *pointers to integers* in a `const` context is [undefined behavior][ub], unless
561/// the pointer was originally created *from* an integer. (That includes this function
562/// specifically, integer-to-pointer casts, and helpers like [`dangling`][crate::ptr::dangling],
563/// but also semantically-equivalent conversions such as punning through `repr(C)` union
564/// fields.) Any attempt to use the resulting value for integer operations will abort
565/// const-evaluation. (And even outside `const`, such transmutation is touching on many
566/// unspecified aspects of the Rust memory model and should be avoided. See below for
567/// alternatives.)
568///
569/// Transmuting *integers to pointers* is a largely unspecified operation. It is likely *not*
570/// equivalent to an `as` cast. Doing non-zero-sized memory accesses with a pointer constructed
571/// this way is currently considered undefined behavior.
572///
573/// All this also applies when the integer is nested inside an array, tuple, struct, or enum.
574/// However, `MaybeUninit<usize>` is not considered an integer type for the purpose of this
575/// section. Transmuting `*const ()` to `MaybeUninit<usize>` is fine---but then calling
576/// `assume_init()` on that result is considered as completing the pointer-to-integer transmute
577/// and thus runs into the issues discussed above.
578///
579/// In particular, doing a pointer-to-integer-to-pointer roundtrip via `transmute` is *not* a
580/// lossless process. If you want to round-trip a pointer through an integer in a way that you
581/// can get back the original pointer, you need to use `as` casts, or replace the integer type
582/// by `MaybeUninit<$int>` (and never call `assume_init()`). If you are looking for a way to
583/// store data of arbitrary type, also use `MaybeUninit<T>` (that will also handle uninitialized
584/// memory due to padding). If you specifically need to store something that is "either an
585/// integer or a pointer", use `*mut ()`: integers can be converted to pointers and back without
586/// any loss (via `as` casts or via `transmute`).
587///
588/// # Examples
589///
590/// There are a few things that `transmute` is really useful for.
591///
592/// Turning a pointer into a function pointer. This is *not* portable to
593/// machines where function pointers and data pointers have different sizes.
594///
595/// ```
596/// fn foo() -> i32 {
597///     0
598/// }
599/// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer.
600/// // This avoids an integer-to-pointer `transmute`, which can be problematic.
601/// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine.
602/// let pointer = foo as *const ();
603/// let function = unsafe {
604///     std::mem::transmute::<*const (), fn() -> i32>(pointer)
605/// };
606/// assert_eq!(function(), 0);
607/// ```
608///
609/// Extending a lifetime, or shortening an invariant lifetime. This is
610/// advanced, very unsafe Rust!
611///
612/// ```
613/// struct R<'a>(&'a i32);
614/// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
615///     unsafe { std::mem::transmute::<R<'b>, R<'static>>(r) }
616/// }
617///
618/// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
619///                                              -> &'b mut R<'c> {
620///     unsafe { std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r) }
621/// }
622/// ```
623///
624/// # Alternatives
625///
626/// Don't despair: many uses of `transmute` can be achieved through other means.
627/// Below are common applications of `transmute` which can be replaced with safer
628/// constructs.
629///
630/// Turning raw bytes (`[u8; SZ]`) into `u32`, `f64`, etc.:
631///
632/// ```
633/// # #![allow(unnecessary_transmutes)]
634/// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
635///
636/// let num = unsafe {
637///     std::mem::transmute::<[u8; 4], u32>(raw_bytes)
638/// };
639///
640/// // use `u32::from_ne_bytes` instead
641/// let num = u32::from_ne_bytes(raw_bytes);
642/// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
643/// let num = u32::from_le_bytes(raw_bytes);
644/// assert_eq!(num, 0x12345678);
645/// let num = u32::from_be_bytes(raw_bytes);
646/// assert_eq!(num, 0x78563412);
647/// ```
648///
649/// Turning a pointer into a `usize`:
650///
651/// ```no_run
652/// let ptr = &0;
653/// let ptr_num_transmute = unsafe {
654///     std::mem::transmute::<&i32, usize>(ptr)
655/// };
656///
657/// // Use an `as` cast instead
658/// let ptr_num_cast = ptr as *const i32 as usize;
659/// ```
660///
661/// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined
662/// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave
663/// as expected -- this is touching on many unspecified aspects of the Rust memory model.
664/// Depending on what the code is doing, the following alternatives are preferable to
665/// pointer-to-integer transmutation:
666/// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a
667///   type for that buffer, it can use [`MaybeUninit`][crate::mem::MaybeUninit].
668/// - If the code actually wants to work on the address the pointer points to, it can use `as`
669///   casts or [`ptr.addr()`][pointer::addr].
670///
671/// Turning a `*mut T` into a `&mut T`:
672///
673/// ```
674/// let ptr: *mut i32 = &mut 0;
675/// let ref_transmuted = unsafe {
676///     std::mem::transmute::<*mut i32, &mut i32>(ptr)
677/// };
678///
679/// // Use a reborrow instead
680/// let ref_casted = unsafe { &mut *ptr };
681/// ```
682///
683/// Turning a `&mut T` into a `&mut U`:
684///
685/// ```
686/// let ptr = &mut 0;
687/// let val_transmuted = unsafe {
688///     std::mem::transmute::<&mut i32, &mut u32>(ptr)
689/// };
690///
691/// // Now, put together `as` and reborrowing - note the chaining of `as`
692/// // `as` is not transitive
693/// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
694/// ```
695///
696/// Turning a `&str` into a `&[u8]`:
697///
698/// ```
699/// // this is not a good way to do this.
700/// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
701/// assert_eq!(slice, &[82, 117, 115, 116]);
702///
703/// // You could use `str::as_bytes`
704/// let slice = "Rust".as_bytes();
705/// assert_eq!(slice, &[82, 117, 115, 116]);
706///
707/// // Or, just use a byte string, if you have control over the string
708/// // literal
709/// assert_eq!(b"Rust", &[82, 117, 115, 116]);
710/// ```
711///
712/// Turning a `Vec<&T>` into a `Vec<Option<&T>>`.
713///
714/// To transmute the inner type of the contents of a container, you must make sure to not
715/// violate any of the container's invariants. For `Vec`, this means that both the size
716/// *and alignment* of the inner types have to match. Other containers might rely on the
717/// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't
718/// be possible at all without violating the container invariants.
719///
720/// ```
721/// let store = [0, 1, 2, 3];
722/// let v_orig = store.iter().collect::<Vec<&i32>>();
723///
724/// // clone the vector as we will reuse them later
725/// let v_clone = v_orig.clone();
726///
727/// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
728/// // bad idea and could cause Undefined Behavior.
729/// // However, it is no-copy.
730/// let v_transmuted = unsafe {
731///     std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
732/// };
733///
734/// let v_clone = v_orig.clone();
735///
736/// // This is the suggested, safe way.
737/// // It may copy the entire vector into a new one though, but also may not.
738/// let v_collected = v_clone.into_iter()
739///                          .map(Some)
740///                          .collect::<Vec<Option<&i32>>>();
741///
742/// let v_clone = v_orig.clone();
743///
744/// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the
745/// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but
746/// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`),
747/// // this has all the same caveats. Besides the information provided above, also consult the
748/// // [`from_raw_parts`] documentation.
749/// let v_from_raw = unsafe {
750// FIXME Update this when vec_into_raw_parts is stabilized
751///     // Ensure the original vector is not dropped.
752///     let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
753///     Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
754///                         v_clone.len(),
755///                         v_clone.capacity())
756/// };
757/// ```
758///
759/// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
760///
761/// Implementing `split_at_mut`:
762///
763/// ```
764/// use std::{slice, mem};
765///
766/// // There are multiple ways to do this, and there are multiple problems
767/// // with the following (transmute) way.
768/// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
769///                              -> (&mut [T], &mut [T]) {
770///     let len = slice.len();
771///     assert!(mid <= len);
772///     unsafe {
773///         let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
774///         // first: transmute is not type safe; all it checks is that T and
775///         // U are of the same size. Second, right here, you have two
776///         // mutable references pointing to the same memory.
777///         (&mut slice[0..mid], &mut slice2[mid..len])
778///     }
779/// }
780///
781/// // This gets rid of the type safety problems; `&mut *` will *only* give
782/// // you a `&mut T` from a `&mut T` or `*mut T`.
783/// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
784///                          -> (&mut [T], &mut [T]) {
785///     let len = slice.len();
786///     assert!(mid <= len);
787///     unsafe {
788///         let slice2 = &mut *(slice as *mut [T]);
789///         // however, you still have two mutable references pointing to
790///         // the same memory.
791///         (&mut slice[0..mid], &mut slice2[mid..len])
792///     }
793/// }
794///
795/// // This is how the standard library does it. This is the best method, if
796/// // you need to do something like this
797/// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
798///                       -> (&mut [T], &mut [T]) {
799///     let len = slice.len();
800///     assert!(mid <= len);
801///     unsafe {
802///         let ptr = slice.as_mut_ptr();
803///         // This now has three mutable references pointing at the same
804///         // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
805///         // `slice` is never used after `let ptr = ...`, and so one can
806///         // treat it as "dead", and therefore, you only have two real
807///         // mutable slices.
808///         (slice::from_raw_parts_mut(ptr, mid),
809///          slice::from_raw_parts_mut(ptr.add(mid), len - mid))
810///     }
811/// }
812/// ```
813#[stable(feature = "rust1", since = "1.0.0")]
814#[rustc_allowed_through_unstable_modules = "import this function via `std::mem` instead"]
815#[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
816#[rustc_diagnostic_item = "transmute"]
817#[rustc_nounwind]
818#[rustc_intrinsic]
819pub const unsafe fn transmute<Src, Dst>(src: Src) -> Dst;
820
821/// Like [`transmute`], but even less checked at compile-time: rather than
822/// giving an error for `size_of::<Src>() != size_of::<Dst>()`, it's
823/// **Undefined Behavior** at runtime.
824///
825/// Prefer normal `transmute` where possible, for the extra checking, since
826/// both do exactly the same thing at runtime, if they both compile.
827///
828/// This is not expected to ever be exposed directly to users, rather it
829/// may eventually be exposed through some more-constrained API.
830#[rustc_intrinsic_const_stable_indirect]
831#[rustc_nounwind]
832#[rustc_intrinsic]
833pub const unsafe fn transmute_unchecked<Src, Dst>(src: Src) -> Dst;
834
835/// Returns `true` if the actual type given as `T` requires drop
836/// glue; returns `false` if the actual type provided for `T`
837/// implements `Copy`.
838///
839/// If the actual type neither requires drop glue nor implements
840/// `Copy`, then the return value of this function is unspecified.
841///
842/// Note that, unlike most intrinsics, this can only be called at compile-time
843/// as backends do not have an implementation for it. The only caller (its
844/// stable counterpart) wraps this intrinsic call in a `const` block so that
845/// backends only see an evaluated constant.
846///
847/// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
848#[rustc_intrinsic_const_stable_indirect]
849#[rustc_nounwind]
850#[rustc_intrinsic]
851pub const fn needs_drop<T: ?Sized>() -> bool;
852
853/// Calculates the offset from a pointer.
854///
855/// This is implemented as an intrinsic to avoid converting to and from an
856/// integer, since the conversion would throw away aliasing information.
857///
858/// This can only be used with `Ptr` as a raw pointer type (`*mut` or `*const`)
859/// to a `Sized` pointee and with `Delta` as `usize` or `isize`.  Any other
860/// instantiations may arbitrarily misbehave, and that's *not* a compiler bug.
861///
862/// # Safety
863///
864/// If the computed offset is non-zero, then both the starting and resulting pointer must be
865/// either in bounds or at the end of an allocation. If either pointer is out
866/// of bounds or arithmetic overflow occurs then this operation is undefined behavior.
867///
868/// The stabilized version of this intrinsic is [`pointer::offset`].
869#[must_use = "returns a new pointer rather than modifying its argument"]
870#[rustc_intrinsic_const_stable_indirect]
871#[rustc_nounwind]
872#[rustc_intrinsic]
873pub const unsafe fn offset<Ptr: bounds::BuiltinDeref, Delta>(dst: Ptr, offset: Delta) -> Ptr;
874
875/// Calculates the offset from a pointer, potentially wrapping.
876///
877/// This is implemented as an intrinsic to avoid converting to and from an
878/// integer, since the conversion inhibits certain optimizations.
879///
880/// # Safety
881///
882/// Unlike the `offset` intrinsic, this intrinsic does not restrict the
883/// resulting pointer to point into or at the end of an allocated
884/// object, and it wraps with two's complement arithmetic. The resulting
885/// value is not necessarily valid to be used to actually access memory.
886///
887/// The stabilized version of this intrinsic is [`pointer::wrapping_offset`].
888#[must_use = "returns a new pointer rather than modifying its argument"]
889#[rustc_intrinsic_const_stable_indirect]
890#[rustc_nounwind]
891#[rustc_intrinsic]
892pub const unsafe fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
893
894/// Projects to the `index`-th element of `slice_ptr`, as the same kind of pointer
895/// as the slice was provided -- so `&mut [T] → &mut T`, `&[T] → &T`,
896/// `*mut [T] → *mut T`, or `*const [T] → *const T` -- without a bounds check.
897///
898/// This is exposed via `<usize as SliceIndex>::get(_unchecked)(_mut)`,
899/// and isn't intended to be used elsewhere.
900///
901/// Expands in MIR to `{&, &mut, &raw const, &raw mut} (*slice_ptr)[index]`,
902/// depending on the types involved, so no backend support is needed.
903///
904/// # Safety
905///
906/// - `index < PtrMetadata(slice_ptr)`, so the indexing is in-bounds for the slice
907/// - the resulting offsetting is in-bounds of the allocation, which is
908///   always the case for references, but needs to be upheld manually for pointers
909#[rustc_nounwind]
910#[rustc_intrinsic]
911pub const unsafe fn slice_get_unchecked<
912    ItemPtr: bounds::ChangePointee<[T], Pointee = T, Output = SlicePtr>,
913    SlicePtr,
914    T,
915>(
916    slice_ptr: SlicePtr,
917    index: usize,
918) -> ItemPtr;
919
920/// Masks out bits of the pointer according to a mask.
921///
922/// Note that, unlike most intrinsics, this is safe to call;
923/// it does not require an `unsafe` block.
924/// Therefore, implementations must not require the user to uphold
925/// any safety invariants.
926///
927/// Consider using [`pointer::mask`] instead.
928#[rustc_nounwind]
929#[rustc_intrinsic]
930pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;
931
932/// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
933/// a size of `count` * `size_of::<T>()` and an alignment of `align_of::<T>()`.
934///
935/// This intrinsic does not have a stable counterpart.
936/// # Safety
937///
938/// The safety requirements are consistent with [`copy_nonoverlapping`]
939/// while the read and write behaviors are volatile,
940/// which means it will not be optimized out unless `_count` or `size_of::<T>()` is equal to zero.
941///
942/// [`copy_nonoverlapping`]: ptr::copy_nonoverlapping
943#[rustc_intrinsic]
944#[rustc_nounwind]
945pub unsafe fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
946/// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
947/// a size of `count * size_of::<T>()` and an alignment of `align_of::<T>()`.
948///
949/// The volatile parameter is set to `true`, so it will not be optimized out
950/// unless size is equal to zero.
951///
952/// This intrinsic does not have a stable counterpart.
953#[rustc_intrinsic]
954#[rustc_nounwind]
955pub unsafe fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
956/// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
957/// size of `count * size_of::<T>()` and an alignment of `align_of::<T>()`.
958///
959/// This intrinsic does not have a stable counterpart.
960/// # Safety
961///
962/// The safety requirements are consistent with [`write_bytes`] while the write behavior is volatile,
963/// which means it will not be optimized out unless `_count` or `size_of::<T>()` is equal to zero.
964///
965/// [`write_bytes`]: ptr::write_bytes
966#[rustc_intrinsic]
967#[rustc_nounwind]
968pub unsafe fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
969
970/// Performs a volatile load from the `src` pointer.
971///
972/// The stabilized version of this intrinsic is [`core::ptr::read_volatile`].
973#[rustc_intrinsic]
974#[rustc_nounwind]
975pub unsafe fn volatile_load<T>(src: *const T) -> T;
976/// Performs a volatile store to the `dst` pointer.
977///
978/// The stabilized version of this intrinsic is [`core::ptr::write_volatile`].
979#[rustc_intrinsic]
980#[rustc_nounwind]
981pub unsafe fn volatile_store<T>(dst: *mut T, val: T);
982
983/// Performs a volatile load from the `src` pointer
984/// The pointer is not required to be aligned.
985///
986/// This intrinsic does not have a stable counterpart.
987#[rustc_intrinsic]
988#[rustc_nounwind]
989#[rustc_diagnostic_item = "intrinsics_unaligned_volatile_load"]
990pub unsafe fn unaligned_volatile_load<T>(src: *const T) -> T;
991/// Performs a volatile store to the `dst` pointer.
992/// The pointer is not required to be aligned.
993///
994/// This intrinsic does not have a stable counterpart.
995#[rustc_intrinsic]
996#[rustc_nounwind]
997#[rustc_diagnostic_item = "intrinsics_unaligned_volatile_store"]
998pub unsafe fn unaligned_volatile_store<T>(dst: *mut T, val: T);
999
1000/// Returns the square root of an `f16`
1001///
1002/// The stabilized version of this intrinsic is
1003/// [`f16::sqrt`](../../std/primitive.f16.html#method.sqrt)
1004#[rustc_intrinsic]
1005#[rustc_nounwind]
1006pub unsafe fn sqrtf16(x: f16) -> f16;
1007/// Returns the square root of an `f32`
1008///
1009/// The stabilized version of this intrinsic is
1010/// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1011#[rustc_intrinsic]
1012#[rustc_nounwind]
1013pub unsafe fn sqrtf32(x: f32) -> f32;
1014/// Returns the square root of an `f64`
1015///
1016/// The stabilized version of this intrinsic is
1017/// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1018#[rustc_intrinsic]
1019#[rustc_nounwind]
1020pub unsafe fn sqrtf64(x: f64) -> f64;
1021/// Returns the square root of an `f128`
1022///
1023/// The stabilized version of this intrinsic is
1024/// [`f128::sqrt`](../../std/primitive.f128.html#method.sqrt)
1025#[rustc_intrinsic]
1026#[rustc_nounwind]
1027pub unsafe fn sqrtf128(x: f128) -> f128;
1028
1029/// Raises an `f16` to an integer power.
1030///
1031/// The stabilized version of this intrinsic is
1032/// [`f16::powi`](../../std/primitive.f16.html#method.powi)
1033#[rustc_intrinsic]
1034#[rustc_nounwind]
1035pub unsafe fn powif16(a: f16, x: i32) -> f16;
1036/// Raises an `f32` to an integer power.
1037///
1038/// The stabilized version of this intrinsic is
1039/// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1040#[rustc_intrinsic]
1041#[rustc_nounwind]
1042pub unsafe fn powif32(a: f32, x: i32) -> f32;
1043/// Raises an `f64` to an integer power.
1044///
1045/// The stabilized version of this intrinsic is
1046/// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1047#[rustc_intrinsic]
1048#[rustc_nounwind]
1049pub unsafe fn powif64(a: f64, x: i32) -> f64;
1050/// Raises an `f128` to an integer power.
1051///
1052/// The stabilized version of this intrinsic is
1053/// [`f128::powi`](../../std/primitive.f128.html#method.powi)
1054#[rustc_intrinsic]
1055#[rustc_nounwind]
1056pub unsafe fn powif128(a: f128, x: i32) -> f128;
1057
1058/// Returns the sine of an `f16`.
1059///
1060/// The stabilized version of this intrinsic is
1061/// [`f16::sin`](../../std/primitive.f16.html#method.sin)
1062#[rustc_intrinsic]
1063#[rustc_nounwind]
1064pub unsafe fn sinf16(x: f16) -> f16;
1065/// Returns the sine of an `f32`.
1066///
1067/// The stabilized version of this intrinsic is
1068/// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1069#[rustc_intrinsic]
1070#[rustc_nounwind]
1071pub unsafe fn sinf32(x: f32) -> f32;
1072/// Returns the sine of an `f64`.
1073///
1074/// The stabilized version of this intrinsic is
1075/// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1076#[rustc_intrinsic]
1077#[rustc_nounwind]
1078pub unsafe fn sinf64(x: f64) -> f64;
1079/// Returns the sine of an `f128`.
1080///
1081/// The stabilized version of this intrinsic is
1082/// [`f128::sin`](../../std/primitive.f128.html#method.sin)
1083#[rustc_intrinsic]
1084#[rustc_nounwind]
1085pub unsafe fn sinf128(x: f128) -> f128;
1086
1087/// Returns the cosine of an `f16`.
1088///
1089/// The stabilized version of this intrinsic is
1090/// [`f16::cos`](../../std/primitive.f16.html#method.cos)
1091#[rustc_intrinsic]
1092#[rustc_nounwind]
1093pub unsafe fn cosf16(x: f16) -> f16;
1094/// Returns the cosine of an `f32`.
1095///
1096/// The stabilized version of this intrinsic is
1097/// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1098#[rustc_intrinsic]
1099#[rustc_nounwind]
1100pub unsafe fn cosf32(x: f32) -> f32;
1101/// Returns the cosine of an `f64`.
1102///
1103/// The stabilized version of this intrinsic is
1104/// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1105#[rustc_intrinsic]
1106#[rustc_nounwind]
1107pub unsafe fn cosf64(x: f64) -> f64;
1108/// Returns the cosine of an `f128`.
1109///
1110/// The stabilized version of this intrinsic is
1111/// [`f128::cos`](../../std/primitive.f128.html#method.cos)
1112#[rustc_intrinsic]
1113#[rustc_nounwind]
1114pub unsafe fn cosf128(x: f128) -> f128;
1115
1116/// Raises an `f16` to an `f16` power.
1117///
1118/// The stabilized version of this intrinsic is
1119/// [`f16::powf`](../../std/primitive.f16.html#method.powf)
1120#[rustc_intrinsic]
1121#[rustc_nounwind]
1122pub unsafe fn powf16(a: f16, x: f16) -> f16;
1123/// Raises an `f32` to an `f32` power.
1124///
1125/// The stabilized version of this intrinsic is
1126/// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1127#[rustc_intrinsic]
1128#[rustc_nounwind]
1129pub unsafe fn powf32(a: f32, x: f32) -> f32;
1130/// Raises an `f64` to an `f64` power.
1131///
1132/// The stabilized version of this intrinsic is
1133/// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1134#[rustc_intrinsic]
1135#[rustc_nounwind]
1136pub unsafe fn powf64(a: f64, x: f64) -> f64;
1137/// Raises an `f128` to an `f128` power.
1138///
1139/// The stabilized version of this intrinsic is
1140/// [`f128::powf`](../../std/primitive.f128.html#method.powf)
1141#[rustc_intrinsic]
1142#[rustc_nounwind]
1143pub unsafe fn powf128(a: f128, x: f128) -> f128;
1144
1145/// Returns the exponential of an `f16`.
1146///
1147/// The stabilized version of this intrinsic is
1148/// [`f16::exp`](../../std/primitive.f16.html#method.exp)
1149#[rustc_intrinsic]
1150#[rustc_nounwind]
1151pub unsafe fn expf16(x: f16) -> f16;
1152/// Returns the exponential of an `f32`.
1153///
1154/// The stabilized version of this intrinsic is
1155/// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1156#[rustc_intrinsic]
1157#[rustc_nounwind]
1158pub unsafe fn expf32(x: f32) -> f32;
1159/// Returns the exponential of an `f64`.
1160///
1161/// The stabilized version of this intrinsic is
1162/// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1163#[rustc_intrinsic]
1164#[rustc_nounwind]
1165pub unsafe fn expf64(x: f64) -> f64;
1166/// Returns the exponential of an `f128`.
1167///
1168/// The stabilized version of this intrinsic is
1169/// [`f128::exp`](../../std/primitive.f128.html#method.exp)
1170#[rustc_intrinsic]
1171#[rustc_nounwind]
1172pub unsafe fn expf128(x: f128) -> f128;
1173
1174/// Returns 2 raised to the power of an `f16`.
1175///
1176/// The stabilized version of this intrinsic is
1177/// [`f16::exp2`](../../std/primitive.f16.html#method.exp2)
1178#[rustc_intrinsic]
1179#[rustc_nounwind]
1180pub unsafe fn exp2f16(x: f16) -> f16;
1181/// Returns 2 raised to the power of an `f32`.
1182///
1183/// The stabilized version of this intrinsic is
1184/// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1185#[rustc_intrinsic]
1186#[rustc_nounwind]
1187pub unsafe fn exp2f32(x: f32) -> f32;
1188/// Returns 2 raised to the power of an `f64`.
1189///
1190/// The stabilized version of this intrinsic is
1191/// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1192#[rustc_intrinsic]
1193#[rustc_nounwind]
1194pub unsafe fn exp2f64(x: f64) -> f64;
1195/// Returns 2 raised to the power of an `f128`.
1196///
1197/// The stabilized version of this intrinsic is
1198/// [`f128::exp2`](../../std/primitive.f128.html#method.exp2)
1199#[rustc_intrinsic]
1200#[rustc_nounwind]
1201pub unsafe fn exp2f128(x: f128) -> f128;
1202
1203/// Returns the natural logarithm of an `f16`.
1204///
1205/// The stabilized version of this intrinsic is
1206/// [`f16::ln`](../../std/primitive.f16.html#method.ln)
1207#[rustc_intrinsic]
1208#[rustc_nounwind]
1209pub unsafe fn logf16(x: f16) -> f16;
1210/// Returns the natural logarithm of an `f32`.
1211///
1212/// The stabilized version of this intrinsic is
1213/// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1214#[rustc_intrinsic]
1215#[rustc_nounwind]
1216pub unsafe fn logf32(x: f32) -> f32;
1217/// Returns the natural logarithm of an `f64`.
1218///
1219/// The stabilized version of this intrinsic is
1220/// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1221#[rustc_intrinsic]
1222#[rustc_nounwind]
1223pub unsafe fn logf64(x: f64) -> f64;
1224/// Returns the natural logarithm of an `f128`.
1225///
1226/// The stabilized version of this intrinsic is
1227/// [`f128::ln`](../../std/primitive.f128.html#method.ln)
1228#[rustc_intrinsic]
1229#[rustc_nounwind]
1230pub unsafe fn logf128(x: f128) -> f128;
1231
1232/// Returns the base 10 logarithm of an `f16`.
1233///
1234/// The stabilized version of this intrinsic is
1235/// [`f16::log10`](../../std/primitive.f16.html#method.log10)
1236#[rustc_intrinsic]
1237#[rustc_nounwind]
1238pub unsafe fn log10f16(x: f16) -> f16;
1239/// Returns the base 10 logarithm of an `f32`.
1240///
1241/// The stabilized version of this intrinsic is
1242/// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1243#[rustc_intrinsic]
1244#[rustc_nounwind]
1245pub unsafe fn log10f32(x: f32) -> f32;
1246/// Returns the base 10 logarithm of an `f64`.
1247///
1248/// The stabilized version of this intrinsic is
1249/// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1250#[rustc_intrinsic]
1251#[rustc_nounwind]
1252pub unsafe fn log10f64(x: f64) -> f64;
1253/// Returns the base 10 logarithm of an `f128`.
1254///
1255/// The stabilized version of this intrinsic is
1256/// [`f128::log10`](../../std/primitive.f128.html#method.log10)
1257#[rustc_intrinsic]
1258#[rustc_nounwind]
1259pub unsafe fn log10f128(x: f128) -> f128;
1260
1261/// Returns the base 2 logarithm of an `f16`.
1262///
1263/// The stabilized version of this intrinsic is
1264/// [`f16::log2`](../../std/primitive.f16.html#method.log2)
1265#[rustc_intrinsic]
1266#[rustc_nounwind]
1267pub unsafe fn log2f16(x: f16) -> f16;
1268/// Returns the base 2 logarithm of an `f32`.
1269///
1270/// The stabilized version of this intrinsic is
1271/// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1272#[rustc_intrinsic]
1273#[rustc_nounwind]
1274pub unsafe fn log2f32(x: f32) -> f32;
1275/// Returns the base 2 logarithm of an `f64`.
1276///
1277/// The stabilized version of this intrinsic is
1278/// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1279#[rustc_intrinsic]
1280#[rustc_nounwind]
1281pub unsafe fn log2f64(x: f64) -> f64;
1282/// Returns the base 2 logarithm of an `f128`.
1283///
1284/// The stabilized version of this intrinsic is
1285/// [`f128::log2`](../../std/primitive.f128.html#method.log2)
1286#[rustc_intrinsic]
1287#[rustc_nounwind]
1288pub unsafe fn log2f128(x: f128) -> f128;
1289
1290/// Returns `a * b + c` for `f16` values.
1291///
1292/// The stabilized version of this intrinsic is
1293/// [`f16::mul_add`](../../std/primitive.f16.html#method.mul_add)
1294#[rustc_intrinsic]
1295#[rustc_nounwind]
1296pub unsafe fn fmaf16(a: f16, b: f16, c: f16) -> f16;
1297/// Returns `a * b + c` for `f32` values.
1298///
1299/// The stabilized version of this intrinsic is
1300/// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1301#[rustc_intrinsic]
1302#[rustc_nounwind]
1303pub unsafe fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1304/// Returns `a * b + c` for `f64` values.
1305///
1306/// The stabilized version of this intrinsic is
1307/// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1308#[rustc_intrinsic]
1309#[rustc_nounwind]
1310pub unsafe fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1311/// Returns `a * b + c` for `f128` values.
1312///
1313/// The stabilized version of this intrinsic is
1314/// [`f128::mul_add`](../../std/primitive.f128.html#method.mul_add)
1315#[rustc_intrinsic]
1316#[rustc_nounwind]
1317pub unsafe fn fmaf128(a: f128, b: f128, c: f128) -> f128;
1318
1319/// Returns `a * b + c` for `f16` values, non-deterministically executing
1320/// either a fused multiply-add or two operations with rounding of the
1321/// intermediate result.
1322///
1323/// The operation is fused if the code generator determines that target
1324/// instruction set has support for a fused operation, and that the fused
1325/// operation is more efficient than the equivalent, separate pair of mul
1326/// and add instructions. It is unspecified whether or not a fused operation
1327/// is selected, and that may depend on optimization level and context, for
1328/// example.
1329#[rustc_intrinsic]
1330#[rustc_nounwind]
1331pub unsafe fn fmuladdf16(a: f16, b: f16, c: f16) -> f16;
1332/// Returns `a * b + c` for `f32` values, non-deterministically executing
1333/// either a fused multiply-add or two operations with rounding of the
1334/// intermediate result.
1335///
1336/// The operation is fused if the code generator determines that target
1337/// instruction set has support for a fused operation, and that the fused
1338/// operation is more efficient than the equivalent, separate pair of mul
1339/// and add instructions. It is unspecified whether or not a fused operation
1340/// is selected, and that may depend on optimization level and context, for
1341/// example.
1342#[rustc_intrinsic]
1343#[rustc_nounwind]
1344pub unsafe fn fmuladdf32(a: f32, b: f32, c: f32) -> f32;
1345/// Returns `a * b + c` for `f64` values, non-deterministically executing
1346/// either a fused multiply-add or two operations with rounding of the
1347/// intermediate result.
1348///
1349/// The operation is fused if the code generator determines that target
1350/// instruction set has support for a fused operation, and that the fused
1351/// operation is more efficient than the equivalent, separate pair of mul
1352/// and add instructions. It is unspecified whether or not a fused operation
1353/// is selected, and that may depend on optimization level and context, for
1354/// example.
1355#[rustc_intrinsic]
1356#[rustc_nounwind]
1357pub unsafe fn fmuladdf64(a: f64, b: f64, c: f64) -> f64;
1358/// Returns `a * b + c` for `f128` values, non-deterministically executing
1359/// either a fused multiply-add or two operations with rounding of the
1360/// intermediate result.
1361///
1362/// The operation is fused if the code generator determines that target
1363/// instruction set has support for a fused operation, and that the fused
1364/// operation is more efficient than the equivalent, separate pair of mul
1365/// and add instructions. It is unspecified whether or not a fused operation
1366/// is selected, and that may depend on optimization level and context, for
1367/// example.
1368#[rustc_intrinsic]
1369#[rustc_nounwind]
1370pub unsafe fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
1371
1372/// Returns the largest integer less than or equal to an `f16`.
1373///
1374/// The stabilized version of this intrinsic is
1375/// [`f16::floor`](../../std/primitive.f16.html#method.floor)
1376#[rustc_intrinsic_const_stable_indirect]
1377#[rustc_intrinsic]
1378#[rustc_nounwind]
1379pub const unsafe fn floorf16(x: f16) -> f16;
1380/// Returns the largest integer less than or equal to an `f32`.
1381///
1382/// The stabilized version of this intrinsic is
1383/// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1384#[rustc_intrinsic_const_stable_indirect]
1385#[rustc_intrinsic]
1386#[rustc_nounwind]
1387pub const unsafe fn floorf32(x: f32) -> f32;
1388/// Returns the largest integer less than or equal to an `f64`.
1389///
1390/// The stabilized version of this intrinsic is
1391/// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1392#[rustc_intrinsic_const_stable_indirect]
1393#[rustc_intrinsic]
1394#[rustc_nounwind]
1395pub const unsafe fn floorf64(x: f64) -> f64;
1396/// Returns the largest integer less than or equal to an `f128`.
1397///
1398/// The stabilized version of this intrinsic is
1399/// [`f128::floor`](../../std/primitive.f128.html#method.floor)
1400#[rustc_intrinsic_const_stable_indirect]
1401#[rustc_intrinsic]
1402#[rustc_nounwind]
1403pub const unsafe fn floorf128(x: f128) -> f128;
1404
1405/// Returns the smallest integer greater than or equal to an `f16`.
1406///
1407/// The stabilized version of this intrinsic is
1408/// [`f16::ceil`](../../std/primitive.f16.html#method.ceil)
1409#[rustc_intrinsic_const_stable_indirect]
1410#[rustc_intrinsic]
1411#[rustc_nounwind]
1412pub const unsafe fn ceilf16(x: f16) -> f16;
1413/// Returns the smallest integer greater than or equal to an `f32`.
1414///
1415/// The stabilized version of this intrinsic is
1416/// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1417#[rustc_intrinsic_const_stable_indirect]
1418#[rustc_intrinsic]
1419#[rustc_nounwind]
1420pub const unsafe fn ceilf32(x: f32) -> f32;
1421/// Returns the smallest integer greater than or equal to an `f64`.
1422///
1423/// The stabilized version of this intrinsic is
1424/// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1425#[rustc_intrinsic_const_stable_indirect]
1426#[rustc_intrinsic]
1427#[rustc_nounwind]
1428pub const unsafe fn ceilf64(x: f64) -> f64;
1429/// Returns the smallest integer greater than or equal to an `f128`.
1430///
1431/// The stabilized version of this intrinsic is
1432/// [`f128::ceil`](../../std/primitive.f128.html#method.ceil)
1433#[rustc_intrinsic_const_stable_indirect]
1434#[rustc_intrinsic]
1435#[rustc_nounwind]
1436pub const unsafe fn ceilf128(x: f128) -> f128;
1437
1438/// Returns the integer part of an `f16`.
1439///
1440/// The stabilized version of this intrinsic is
1441/// [`f16::trunc`](../../std/primitive.f16.html#method.trunc)
1442#[rustc_intrinsic_const_stable_indirect]
1443#[rustc_intrinsic]
1444#[rustc_nounwind]
1445pub const unsafe fn truncf16(x: f16) -> f16;
1446/// Returns the integer part of an `f32`.
1447///
1448/// The stabilized version of this intrinsic is
1449/// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1450#[rustc_intrinsic_const_stable_indirect]
1451#[rustc_intrinsic]
1452#[rustc_nounwind]
1453pub const unsafe fn truncf32(x: f32) -> f32;
1454/// Returns the integer part of an `f64`.
1455///
1456/// The stabilized version of this intrinsic is
1457/// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1458#[rustc_intrinsic_const_stable_indirect]
1459#[rustc_intrinsic]
1460#[rustc_nounwind]
1461pub const unsafe fn truncf64(x: f64) -> f64;
1462/// Returns the integer part of an `f128`.
1463///
1464/// The stabilized version of this intrinsic is
1465/// [`f128::trunc`](../../std/primitive.f128.html#method.trunc)
1466#[rustc_intrinsic_const_stable_indirect]
1467#[rustc_intrinsic]
1468#[rustc_nounwind]
1469pub const unsafe fn truncf128(x: f128) -> f128;
1470
1471/// Returns the nearest integer to an `f16`. Rounds half-way cases to the number with an even
1472/// least significant digit.
1473///
1474/// The stabilized version of this intrinsic is
1475/// [`f16::round_ties_even`](../../std/primitive.f16.html#method.round_ties_even)
1476#[rustc_intrinsic_const_stable_indirect]
1477#[rustc_intrinsic]
1478#[rustc_nounwind]
1479pub const fn round_ties_even_f16(x: f16) -> f16;
1480
1481/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number with an even
1482/// least significant digit.
1483///
1484/// The stabilized version of this intrinsic is
1485/// [`f32::round_ties_even`](../../std/primitive.f32.html#method.round_ties_even)
1486#[rustc_intrinsic_const_stable_indirect]
1487#[rustc_intrinsic]
1488#[rustc_nounwind]
1489pub const fn round_ties_even_f32(x: f32) -> f32;
1490
1491/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number with an even
1492/// least significant digit.
1493///
1494/// The stabilized version of this intrinsic is
1495/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
1496#[rustc_intrinsic_const_stable_indirect]
1497#[rustc_intrinsic]
1498#[rustc_nounwind]
1499pub const fn round_ties_even_f64(x: f64) -> f64;
1500
1501/// Returns the nearest integer to an `f128`. Rounds half-way cases to the number with an even
1502/// least significant digit.
1503///
1504/// The stabilized version of this intrinsic is
1505/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
1506#[rustc_intrinsic_const_stable_indirect]
1507#[rustc_intrinsic]
1508#[rustc_nounwind]
1509pub const fn round_ties_even_f128(x: f128) -> f128;
1510
1511/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
1512///
1513/// The stabilized version of this intrinsic is
1514/// [`f16::round`](../../std/primitive.f16.html#method.round)
1515#[rustc_intrinsic_const_stable_indirect]
1516#[rustc_intrinsic]
1517#[rustc_nounwind]
1518pub const unsafe fn roundf16(x: f16) -> f16;
1519/// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1520///
1521/// The stabilized version of this intrinsic is
1522/// [`f32::round`](../../std/primitive.f32.html#method.round)
1523#[rustc_intrinsic_const_stable_indirect]
1524#[rustc_intrinsic]
1525#[rustc_nounwind]
1526pub const unsafe fn roundf32(x: f32) -> f32;
1527/// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1528///
1529/// The stabilized version of this intrinsic is
1530/// [`f64::round`](../../std/primitive.f64.html#method.round)
1531#[rustc_intrinsic_const_stable_indirect]
1532#[rustc_intrinsic]
1533#[rustc_nounwind]
1534pub const unsafe fn roundf64(x: f64) -> f64;
1535/// Returns the nearest integer to an `f128`. Rounds half-way cases away from zero.
1536///
1537/// The stabilized version of this intrinsic is
1538/// [`f128::round`](../../std/primitive.f128.html#method.round)
1539#[rustc_intrinsic_const_stable_indirect]
1540#[rustc_intrinsic]
1541#[rustc_nounwind]
1542pub const unsafe fn roundf128(x: f128) -> f128;
1543
1544/// Float addition that allows optimizations based on algebraic rules.
1545/// May assume inputs are finite.
1546///
1547/// This intrinsic does not have a stable counterpart.
1548#[rustc_intrinsic]
1549#[rustc_nounwind]
1550pub unsafe fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1551
1552/// Float subtraction that allows optimizations based on algebraic rules.
1553/// May assume inputs are finite.
1554///
1555/// This intrinsic does not have a stable counterpart.
1556#[rustc_intrinsic]
1557#[rustc_nounwind]
1558pub unsafe fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1559
1560/// Float multiplication that allows optimizations based on algebraic rules.
1561/// May assume inputs are finite.
1562///
1563/// This intrinsic does not have a stable counterpart.
1564#[rustc_intrinsic]
1565#[rustc_nounwind]
1566pub unsafe fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1567
1568/// Float division that allows optimizations based on algebraic rules.
1569/// May assume inputs are finite.
1570///
1571/// This intrinsic does not have a stable counterpart.
1572#[rustc_intrinsic]
1573#[rustc_nounwind]
1574pub unsafe fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1575
1576/// Float remainder that allows optimizations based on algebraic rules.
1577/// May assume inputs are finite.
1578///
1579/// This intrinsic does not have a stable counterpart.
1580#[rustc_intrinsic]
1581#[rustc_nounwind]
1582pub unsafe fn frem_fast<T: Copy>(a: T, b: T) -> T;
1583
1584/// Converts with LLVM’s fptoui/fptosi, which may return undef for values out of range
1585/// (<https://github.com/rust-lang/rust/issues/10184>)
1586///
1587/// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1588#[rustc_intrinsic]
1589#[rustc_nounwind]
1590pub unsafe fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
1591
1592/// Float addition that allows optimizations based on algebraic rules.
1593///
1594/// Stabilized as [`f16::algebraic_add`], [`f32::algebraic_add`], [`f64::algebraic_add`] and [`f128::algebraic_add`].
1595#[rustc_nounwind]
1596#[rustc_intrinsic]
1597pub const fn fadd_algebraic<T: Copy>(a: T, b: T) -> T;
1598
1599/// Float subtraction that allows optimizations based on algebraic rules.
1600///
1601/// Stabilized as [`f16::algebraic_sub`], [`f32::algebraic_sub`], [`f64::algebraic_sub`] and [`f128::algebraic_sub`].
1602#[rustc_nounwind]
1603#[rustc_intrinsic]
1604pub const fn fsub_algebraic<T: Copy>(a: T, b: T) -> T;
1605
1606/// Float multiplication that allows optimizations based on algebraic rules.
1607///
1608/// Stabilized as [`f16::algebraic_mul`], [`f32::algebraic_mul`], [`f64::algebraic_mul`] and [`f128::algebraic_mul`].
1609#[rustc_nounwind]
1610#[rustc_intrinsic]
1611pub const fn fmul_algebraic<T: Copy>(a: T, b: T) -> T;
1612
1613/// Float division that allows optimizations based on algebraic rules.
1614///
1615/// Stabilized as [`f16::algebraic_div`], [`f32::algebraic_div`], [`f64::algebraic_div`] and [`f128::algebraic_div`].
1616#[rustc_nounwind]
1617#[rustc_intrinsic]
1618pub const fn fdiv_algebraic<T: Copy>(a: T, b: T) -> T;
1619
1620/// Float remainder that allows optimizations based on algebraic rules.
1621///
1622/// Stabilized as [`f16::algebraic_rem`], [`f32::algebraic_rem`], [`f64::algebraic_rem`] and [`f128::algebraic_rem`].
1623#[rustc_nounwind]
1624#[rustc_intrinsic]
1625pub const fn frem_algebraic<T: Copy>(a: T, b: T) -> T;
1626
1627/// Returns the number of bits set in an integer type `T`
1628///
1629/// Note that, unlike most intrinsics, this is safe to call;
1630/// it does not require an `unsafe` block.
1631/// Therefore, implementations must not require the user to uphold
1632/// any safety invariants.
1633///
1634/// The stabilized versions of this intrinsic are available on the integer
1635/// primitives via the `count_ones` method. For example,
1636/// [`u32::count_ones`]
1637#[rustc_intrinsic_const_stable_indirect]
1638#[rustc_nounwind]
1639#[rustc_intrinsic]
1640pub const fn ctpop<T: Copy>(x: T) -> u32;
1641
1642/// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1643///
1644/// Note that, unlike most intrinsics, this is safe to call;
1645/// it does not require an `unsafe` block.
1646/// Therefore, implementations must not require the user to uphold
1647/// any safety invariants.
1648///
1649/// The stabilized versions of this intrinsic are available on the integer
1650/// primitives via the `leading_zeros` method. For example,
1651/// [`u32::leading_zeros`]
1652///
1653/// # Examples
1654///
1655/// ```
1656/// #![feature(core_intrinsics)]
1657/// # #![allow(internal_features)]
1658///
1659/// use std::intrinsics::ctlz;
1660///
1661/// let x = 0b0001_1100_u8;
1662/// let num_leading = ctlz(x);
1663/// assert_eq!(num_leading, 3);
1664/// ```
1665///
1666/// An `x` with value `0` will return the bit width of `T`.
1667///
1668/// ```
1669/// #![feature(core_intrinsics)]
1670/// # #![allow(internal_features)]
1671///
1672/// use std::intrinsics::ctlz;
1673///
1674/// let x = 0u16;
1675/// let num_leading = ctlz(x);
1676/// assert_eq!(num_leading, 16);
1677/// ```
1678#[rustc_intrinsic_const_stable_indirect]
1679#[rustc_nounwind]
1680#[rustc_intrinsic]
1681pub const fn ctlz<T: Copy>(x: T) -> u32;
1682
1683/// Like `ctlz`, but extra-unsafe as it returns `undef` when
1684/// given an `x` with value `0`.
1685///
1686/// This intrinsic does not have a stable counterpart.
1687///
1688/// # Examples
1689///
1690/// ```
1691/// #![feature(core_intrinsics)]
1692/// # #![allow(internal_features)]
1693///
1694/// use std::intrinsics::ctlz_nonzero;
1695///
1696/// let x = 0b0001_1100_u8;
1697/// let num_leading = unsafe { ctlz_nonzero(x) };
1698/// assert_eq!(num_leading, 3);
1699/// ```
1700#[rustc_intrinsic_const_stable_indirect]
1701#[rustc_nounwind]
1702#[rustc_intrinsic]
1703pub const unsafe fn ctlz_nonzero<T: Copy>(x: T) -> u32;
1704
1705/// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1706///
1707/// Note that, unlike most intrinsics, this is safe to call;
1708/// it does not require an `unsafe` block.
1709/// Therefore, implementations must not require the user to uphold
1710/// any safety invariants.
1711///
1712/// The stabilized versions of this intrinsic are available on the integer
1713/// primitives via the `trailing_zeros` method. For example,
1714/// [`u32::trailing_zeros`]
1715///
1716/// # Examples
1717///
1718/// ```
1719/// #![feature(core_intrinsics)]
1720/// # #![allow(internal_features)]
1721///
1722/// use std::intrinsics::cttz;
1723///
1724/// let x = 0b0011_1000_u8;
1725/// let num_trailing = cttz(x);
1726/// assert_eq!(num_trailing, 3);
1727/// ```
1728///
1729/// An `x` with value `0` will return the bit width of `T`:
1730///
1731/// ```
1732/// #![feature(core_intrinsics)]
1733/// # #![allow(internal_features)]
1734///
1735/// use std::intrinsics::cttz;
1736///
1737/// let x = 0u16;
1738/// let num_trailing = cttz(x);
1739/// assert_eq!(num_trailing, 16);
1740/// ```
1741#[rustc_intrinsic_const_stable_indirect]
1742#[rustc_nounwind]
1743#[rustc_intrinsic]
1744pub const fn cttz<T: Copy>(x: T) -> u32;
1745
1746/// Like `cttz`, but extra-unsafe as it returns `undef` when
1747/// given an `x` with value `0`.
1748///
1749/// This intrinsic does not have a stable counterpart.
1750///
1751/// # Examples
1752///
1753/// ```
1754/// #![feature(core_intrinsics)]
1755/// # #![allow(internal_features)]
1756///
1757/// use std::intrinsics::cttz_nonzero;
1758///
1759/// let x = 0b0011_1000_u8;
1760/// let num_trailing = unsafe { cttz_nonzero(x) };
1761/// assert_eq!(num_trailing, 3);
1762/// ```
1763#[rustc_intrinsic_const_stable_indirect]
1764#[rustc_nounwind]
1765#[rustc_intrinsic]
1766pub const unsafe fn cttz_nonzero<T: Copy>(x: T) -> u32;
1767
1768/// Reverses the bytes in an integer type `T`.
1769///
1770/// Note that, unlike most intrinsics, this is safe to call;
1771/// it does not require an `unsafe` block.
1772/// Therefore, implementations must not require the user to uphold
1773/// any safety invariants.
1774///
1775/// The stabilized versions of this intrinsic are available on the integer
1776/// primitives via the `swap_bytes` method. For example,
1777/// [`u32::swap_bytes`]
1778#[rustc_intrinsic_const_stable_indirect]
1779#[rustc_nounwind]
1780#[rustc_intrinsic]
1781pub const fn bswap<T: Copy>(x: T) -> T;
1782
1783/// Reverses the bits in an integer type `T`.
1784///
1785/// Note that, unlike most intrinsics, this is safe to call;
1786/// it does not require an `unsafe` block.
1787/// Therefore, implementations must not require the user to uphold
1788/// any safety invariants.
1789///
1790/// The stabilized versions of this intrinsic are available on the integer
1791/// primitives via the `reverse_bits` method. For example,
1792/// [`u32::reverse_bits`]
1793#[rustc_intrinsic_const_stable_indirect]
1794#[rustc_nounwind]
1795#[rustc_intrinsic]
1796pub const fn bitreverse<T: Copy>(x: T) -> T;
1797
1798/// Does a three-way comparison between the two arguments,
1799/// which must be of character or integer (signed or unsigned) type.
1800///
1801/// This was originally added because it greatly simplified the MIR in `cmp`
1802/// implementations, and then LLVM 20 added a backend intrinsic for it too.
1803///
1804/// The stabilized version of this intrinsic is [`Ord::cmp`].
1805#[rustc_intrinsic_const_stable_indirect]
1806#[rustc_nounwind]
1807#[rustc_intrinsic]
1808pub const fn three_way_compare<T: Copy>(lhs: T, rhss: T) -> crate::cmp::Ordering;
1809
1810/// Combine two values which have no bits in common.
1811///
1812/// This allows the backend to implement it as `a + b` *or* `a | b`,
1813/// depending which is easier to implement on a specific target.
1814///
1815/// # Safety
1816///
1817/// Requires that `(a & b) == 0`, or equivalently that `(a | b) == (a + b)`.
1818///
1819/// Otherwise it's immediate UB.
1820#[rustc_const_unstable(feature = "disjoint_bitor", issue = "135758")]
1821#[rustc_nounwind]
1822#[rustc_intrinsic]
1823#[track_caller]
1824#[miri::intrinsic_fallback_is_spec] // the fallbacks all `assume` to tell Miri
1825pub const unsafe fn disjoint_bitor<T: [const] fallback::DisjointBitOr>(a: T, b: T) -> T {
1826    // SAFETY: same preconditions as this function.
1827    unsafe { fallback::DisjointBitOr::disjoint_bitor(a, b) }
1828}
1829
1830/// Performs checked integer addition.
1831///
1832/// Note that, unlike most intrinsics, this is safe to call;
1833/// it does not require an `unsafe` block.
1834/// Therefore, implementations must not require the user to uphold
1835/// any safety invariants.
1836///
1837/// The stabilized versions of this intrinsic are available on the integer
1838/// primitives via the `overflowing_add` method. For example,
1839/// [`u32::overflowing_add`]
1840#[rustc_intrinsic_const_stable_indirect]
1841#[rustc_nounwind]
1842#[rustc_intrinsic]
1843pub const fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1844
1845/// Performs checked integer subtraction
1846///
1847/// Note that, unlike most intrinsics, this is safe to call;
1848/// it does not require an `unsafe` block.
1849/// Therefore, implementations must not require the user to uphold
1850/// any safety invariants.
1851///
1852/// The stabilized versions of this intrinsic are available on the integer
1853/// primitives via the `overflowing_sub` method. For example,
1854/// [`u32::overflowing_sub`]
1855#[rustc_intrinsic_const_stable_indirect]
1856#[rustc_nounwind]
1857#[rustc_intrinsic]
1858pub const fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1859
1860/// Performs checked integer multiplication
1861///
1862/// Note that, unlike most intrinsics, this is safe to call;
1863/// it does not require an `unsafe` block.
1864/// Therefore, implementations must not require the user to uphold
1865/// any safety invariants.
1866///
1867/// The stabilized versions of this intrinsic are available on the integer
1868/// primitives via the `overflowing_mul` method. For example,
1869/// [`u32::overflowing_mul`]
1870#[rustc_intrinsic_const_stable_indirect]
1871#[rustc_nounwind]
1872#[rustc_intrinsic]
1873pub const fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1874
1875/// Performs full-width multiplication and addition with a carry:
1876/// `multiplier * multiplicand + addend + carry`.
1877///
1878/// This is possible without any overflow.  For `uN`:
1879///    MAX * MAX + MAX + MAX
1880/// => (2ⁿ-1) × (2ⁿ-1) + (2ⁿ-1) + (2ⁿ-1)
1881/// => (2²ⁿ - 2ⁿ⁺¹ + 1) + (2ⁿ⁺¹ - 2)
1882/// => 2²ⁿ - 1
1883///
1884/// For `iN`, the upper bound is MIN * MIN + MAX + MAX => 2²ⁿ⁻² + 2ⁿ - 2,
1885/// and the lower bound is MAX * MIN + MIN + MIN => -2²ⁿ⁻² - 2ⁿ + 2ⁿ⁺¹.
1886///
1887/// This currently supports unsigned integers *only*, no signed ones.
1888/// The stabilized versions of this intrinsic are available on integers.
1889#[unstable(feature = "core_intrinsics", issue = "none")]
1890#[rustc_const_unstable(feature = "const_carrying_mul_add", issue = "85532")]
1891#[rustc_nounwind]
1892#[rustc_intrinsic]
1893#[miri::intrinsic_fallback_is_spec]
1894pub const fn carrying_mul_add<T: [const] fallback::CarryingMulAdd<Unsigned = U>, U>(
1895    multiplier: T,
1896    multiplicand: T,
1897    addend: T,
1898    carry: T,
1899) -> (U, T) {
1900    multiplier.carrying_mul_add(multiplicand, addend, carry)
1901}
1902
1903/// Performs an exact division, resulting in undefined behavior where
1904/// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1905///
1906/// This intrinsic does not have a stable counterpart.
1907#[rustc_intrinsic_const_stable_indirect]
1908#[rustc_nounwind]
1909#[rustc_intrinsic]
1910pub const unsafe fn exact_div<T: Copy>(x: T, y: T) -> T;
1911
1912/// Performs an unchecked division, resulting in undefined behavior
1913/// where `y == 0` or `x == T::MIN && y == -1`
1914///
1915/// Safe wrappers for this intrinsic are available on the integer
1916/// primitives via the `checked_div` method. For example,
1917/// [`u32::checked_div`]
1918#[rustc_intrinsic_const_stable_indirect]
1919#[rustc_nounwind]
1920#[rustc_intrinsic]
1921pub const unsafe fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1922/// Returns the remainder of an unchecked division, resulting in
1923/// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
1924///
1925/// Safe wrappers for this intrinsic are available on the integer
1926/// primitives via the `checked_rem` method. For example,
1927/// [`u32::checked_rem`]
1928#[rustc_intrinsic_const_stable_indirect]
1929#[rustc_nounwind]
1930#[rustc_intrinsic]
1931pub const unsafe fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1932
1933/// Performs an unchecked left shift, resulting in undefined behavior when
1934/// `y < 0` or `y >= N`, where N is the width of T in bits.
1935///
1936/// Safe wrappers for this intrinsic are available on the integer
1937/// primitives via the `checked_shl` method. For example,
1938/// [`u32::checked_shl`]
1939#[rustc_intrinsic_const_stable_indirect]
1940#[rustc_nounwind]
1941#[rustc_intrinsic]
1942pub const unsafe fn unchecked_shl<T: Copy, U: Copy>(x: T, y: U) -> T;
1943/// Performs an unchecked right shift, resulting in undefined behavior when
1944/// `y < 0` or `y >= N`, where N is the width of T in bits.
1945///
1946/// Safe wrappers for this intrinsic are available on the integer
1947/// primitives via the `checked_shr` method. For example,
1948/// [`u32::checked_shr`]
1949#[rustc_intrinsic_const_stable_indirect]
1950#[rustc_nounwind]
1951#[rustc_intrinsic]
1952pub const unsafe fn unchecked_shr<T: Copy, U: Copy>(x: T, y: U) -> T;
1953
1954/// Returns the result of an unchecked addition, resulting in
1955/// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1956///
1957/// The stable counterpart of this intrinsic is `unchecked_add` on the various
1958/// integer types, such as [`u16::unchecked_add`] and [`i64::unchecked_add`].
1959#[rustc_intrinsic_const_stable_indirect]
1960#[rustc_nounwind]
1961#[rustc_intrinsic]
1962pub const unsafe fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1963
1964/// Returns the result of an unchecked subtraction, resulting in
1965/// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1966///
1967/// The stable counterpart of this intrinsic is `unchecked_sub` on the various
1968/// integer types, such as [`u16::unchecked_sub`] and [`i64::unchecked_sub`].
1969#[rustc_intrinsic_const_stable_indirect]
1970#[rustc_nounwind]
1971#[rustc_intrinsic]
1972pub const unsafe fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1973
1974/// Returns the result of an unchecked multiplication, resulting in
1975/// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1976///
1977/// The stable counterpart of this intrinsic is `unchecked_mul` on the various
1978/// integer types, such as [`u16::unchecked_mul`] and [`i64::unchecked_mul`].
1979#[rustc_intrinsic_const_stable_indirect]
1980#[rustc_nounwind]
1981#[rustc_intrinsic]
1982pub const unsafe fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1983
1984/// Performs rotate left.
1985///
1986/// Note that, unlike most intrinsics, this is safe to call;
1987/// it does not require an `unsafe` block.
1988/// Therefore, implementations must not require the user to uphold
1989/// any safety invariants.
1990///
1991/// The stabilized versions of this intrinsic are available on the integer
1992/// primitives via the `rotate_left` method. For example,
1993/// [`u32::rotate_left`]
1994#[rustc_intrinsic_const_stable_indirect]
1995#[rustc_nounwind]
1996#[rustc_intrinsic]
1997pub const fn rotate_left<T: Copy>(x: T, shift: u32) -> T;
1998
1999/// Performs rotate right.
2000///
2001/// Note that, unlike most intrinsics, this is safe to call;
2002/// it does not require an `unsafe` block.
2003/// Therefore, implementations must not require the user to uphold
2004/// any safety invariants.
2005///
2006/// The stabilized versions of this intrinsic are available on the integer
2007/// primitives via the `rotate_right` method. For example,
2008/// [`u32::rotate_right`]
2009#[rustc_intrinsic_const_stable_indirect]
2010#[rustc_nounwind]
2011#[rustc_intrinsic]
2012pub const fn rotate_right<T: Copy>(x: T, shift: u32) -> T;
2013
2014/// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
2015///
2016/// Note that, unlike most intrinsics, this is safe to call;
2017/// it does not require an `unsafe` block.
2018/// Therefore, implementations must not require the user to uphold
2019/// any safety invariants.
2020///
2021/// The stabilized versions of this intrinsic are available on the integer
2022/// primitives via the `wrapping_add` method. For example,
2023/// [`u32::wrapping_add`]
2024#[rustc_intrinsic_const_stable_indirect]
2025#[rustc_nounwind]
2026#[rustc_intrinsic]
2027pub const fn wrapping_add<T: Copy>(a: T, b: T) -> T;
2028/// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
2029///
2030/// Note that, unlike most intrinsics, this is safe to call;
2031/// it does not require an `unsafe` block.
2032/// Therefore, implementations must not require the user to uphold
2033/// any safety invariants.
2034///
2035/// The stabilized versions of this intrinsic are available on the integer
2036/// primitives via the `wrapping_sub` method. For example,
2037/// [`u32::wrapping_sub`]
2038#[rustc_intrinsic_const_stable_indirect]
2039#[rustc_nounwind]
2040#[rustc_intrinsic]
2041pub const fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
2042/// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
2043///
2044/// Note that, unlike most intrinsics, this is safe to call;
2045/// it does not require an `unsafe` block.
2046/// Therefore, implementations must not require the user to uphold
2047/// any safety invariants.
2048///
2049/// The stabilized versions of this intrinsic are available on the integer
2050/// primitives via the `wrapping_mul` method. For example,
2051/// [`u32::wrapping_mul`]
2052#[rustc_intrinsic_const_stable_indirect]
2053#[rustc_nounwind]
2054#[rustc_intrinsic]
2055pub const fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
2056
2057/// Computes `a + b`, saturating at numeric bounds.
2058///
2059/// Note that, unlike most intrinsics, this is safe to call;
2060/// it does not require an `unsafe` block.
2061/// Therefore, implementations must not require the user to uphold
2062/// any safety invariants.
2063///
2064/// The stabilized versions of this intrinsic are available on the integer
2065/// primitives via the `saturating_add` method. For example,
2066/// [`u32::saturating_add`]
2067#[rustc_intrinsic_const_stable_indirect]
2068#[rustc_nounwind]
2069#[rustc_intrinsic]
2070pub const fn saturating_add<T: Copy>(a: T, b: T) -> T;
2071/// Computes `a - b`, saturating at numeric bounds.
2072///
2073/// Note that, unlike most intrinsics, this is safe to call;
2074/// it does not require an `unsafe` block.
2075/// Therefore, implementations must not require the user to uphold
2076/// any safety invariants.
2077///
2078/// The stabilized versions of this intrinsic are available on the integer
2079/// primitives via the `saturating_sub` method. For example,
2080/// [`u32::saturating_sub`]
2081#[rustc_intrinsic_const_stable_indirect]
2082#[rustc_nounwind]
2083#[rustc_intrinsic]
2084pub const fn saturating_sub<T: Copy>(a: T, b: T) -> T;
2085
2086/// This is an implementation detail of [`crate::ptr::read`] and should
2087/// not be used anywhere else.  See its comments for why this exists.
2088///
2089/// This intrinsic can *only* be called where the pointer is a local without
2090/// projections (`read_via_copy(ptr)`, not `read_via_copy(*ptr)`) so that it
2091/// trivially obeys runtime-MIR rules about derefs in operands.
2092#[rustc_intrinsic_const_stable_indirect]
2093#[rustc_nounwind]
2094#[rustc_intrinsic]
2095pub const unsafe fn read_via_copy<T>(ptr: *const T) -> T;
2096
2097/// This is an implementation detail of [`crate::ptr::write`] and should
2098/// not be used anywhere else.  See its comments for why this exists.
2099///
2100/// This intrinsic can *only* be called where the pointer is a local without
2101/// projections (`write_via_move(ptr, x)`, not `write_via_move(*ptr, x)`) so
2102/// that it trivially obeys runtime-MIR rules about derefs in operands.
2103#[rustc_intrinsic_const_stable_indirect]
2104#[rustc_nounwind]
2105#[rustc_intrinsic]
2106pub const unsafe fn write_via_move<T>(ptr: *mut T, value: T);
2107
2108/// Returns the value of the discriminant for the variant in 'v';
2109/// if `T` has no discriminant, returns `0`.
2110///
2111/// Note that, unlike most intrinsics, this is safe to call;
2112/// it does not require an `unsafe` block.
2113/// Therefore, implementations must not require the user to uphold
2114/// any safety invariants.
2115///
2116/// The stabilized version of this intrinsic is [`core::mem::discriminant`].
2117#[rustc_intrinsic_const_stable_indirect]
2118#[rustc_nounwind]
2119#[rustc_intrinsic]
2120pub const fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
2121
2122/// Rust's "try catch" construct for unwinding. Invokes the function pointer `try_fn` with the
2123/// data pointer `data`, and calls `catch_fn` if unwinding occurs while `try_fn` runs.
2124/// Returns `1` if unwinding occurred and `catch_fn` was called; returns `0` otherwise.
2125///
2126/// `catch_fn` must not unwind.
2127///
2128/// The third argument is a function called if an unwind occurs (both Rust `panic` and foreign
2129/// unwinds). This function takes the data pointer and a pointer to the target- and
2130/// runtime-specific exception object that was caught.
2131///
2132/// Note that in the case of a foreign unwinding operation, the exception object data may not be
2133/// safely usable from Rust, and should not be directly exposed via the standard library. To
2134/// prevent unsafe access, the library implementation may either abort the process or present an
2135/// opaque error type to the user.
2136///
2137/// For more information, see the compiler's source, as well as the documentation for the stable
2138/// version of this intrinsic, `std::panic::catch_unwind`.
2139#[rustc_intrinsic]
2140#[rustc_nounwind]
2141pub unsafe fn catch_unwind(
2142    _try_fn: fn(*mut u8),
2143    _data: *mut u8,
2144    _catch_fn: fn(*mut u8, *mut u8),
2145) -> i32;
2146
2147/// Emits a `nontemporal` store, which gives a hint to the CPU that the data should not be held
2148/// in cache. Except for performance, this is fully equivalent to `ptr.write(val)`.
2149///
2150/// Not all architectures provide such an operation. For instance, x86 does not: while `MOVNT`
2151/// exists, that operation is *not* equivalent to `ptr.write(val)` (`MOVNT` writes can be reordered
2152/// in ways that are not allowed for regular writes).
2153#[rustc_intrinsic]
2154#[rustc_nounwind]
2155pub unsafe fn nontemporal_store<T>(ptr: *mut T, val: T);
2156
2157/// See documentation of `<*const T>::offset_from` for details.
2158#[rustc_intrinsic_const_stable_indirect]
2159#[rustc_nounwind]
2160#[rustc_intrinsic]
2161pub const unsafe fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
2162
2163/// See documentation of `<*const T>::offset_from_unsigned` for details.
2164#[rustc_nounwind]
2165#[rustc_intrinsic]
2166#[rustc_intrinsic_const_stable_indirect]
2167pub const unsafe fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
2168
2169/// See documentation of `<*const T>::guaranteed_eq` for details.
2170/// Returns `2` if the result is unknown.
2171/// Returns `1` if the pointers are guaranteed equal.
2172/// Returns `0` if the pointers are guaranteed inequal.
2173#[rustc_intrinsic]
2174#[rustc_nounwind]
2175#[rustc_do_not_const_check]
2176#[inline]
2177#[miri::intrinsic_fallback_is_spec]
2178pub const fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8 {
2179    (ptr == other) as u8
2180}
2181
2182/// Determines whether the raw bytes of the two values are equal.
2183///
2184/// This is particularly handy for arrays, since it allows things like just
2185/// comparing `i96`s instead of forcing `alloca`s for `[6 x i16]`.
2186///
2187/// Above some backend-decided threshold this will emit calls to `memcmp`,
2188/// like slice equality does, instead of causing massive code size.
2189///
2190/// Since this works by comparing the underlying bytes, the actual `T` is
2191/// not particularly important.  It will be used for its size and alignment,
2192/// but any validity restrictions will be ignored, not enforced.
2193///
2194/// # Safety
2195///
2196/// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized.
2197/// Note that this is a stricter criterion than just the *values* being
2198/// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
2199///
2200/// At compile-time, it is furthermore UB to call this if any of the bytes
2201/// in `*a` or `*b` have provenance.
2202///
2203/// (The implementation is allowed to branch on the results of comparisons,
2204/// which is UB if any of their inputs are `undef`.)
2205#[rustc_nounwind]
2206#[rustc_intrinsic]
2207pub const unsafe fn raw_eq<T>(a: &T, b: &T) -> bool;
2208
2209/// Lexicographically compare `[left, left + bytes)` and `[right, right + bytes)`
2210/// as unsigned bytes, returning negative if `left` is less, zero if all the
2211/// bytes match, or positive if `left` is greater.
2212///
2213/// This underlies things like `<[u8]>::cmp`, and will usually lower to `memcmp`.
2214///
2215/// # Safety
2216///
2217/// `left` and `right` must each be [valid] for reads of `bytes` bytes.
2218///
2219/// Note that this applies to the whole range, not just until the first byte
2220/// that differs.  That allows optimizations that can read in large chunks.
2221///
2222/// [valid]: crate::ptr#safety
2223#[rustc_nounwind]
2224#[rustc_intrinsic]
2225#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2226pub const unsafe fn compare_bytes(left: *const u8, right: *const u8, bytes: usize) -> i32;
2227
2228/// See documentation of [`std::hint::black_box`] for details.
2229///
2230/// [`std::hint::black_box`]: crate::hint::black_box
2231#[rustc_nounwind]
2232#[rustc_intrinsic]
2233#[rustc_intrinsic_const_stable_indirect]
2234pub const fn black_box<T>(dummy: T) -> T;
2235
2236/// Selects which function to call depending on the context.
2237///
2238/// If this function is evaluated at compile-time, then a call to this
2239/// intrinsic will be replaced with a call to `called_in_const`. It gets
2240/// replaced with a call to `called_at_rt` otherwise.
2241///
2242/// This function is safe to call, but note the stability concerns below.
2243///
2244/// # Type Requirements
2245///
2246/// The two functions must be both function items. They cannot be function
2247/// pointers or closures. The first function must be a `const fn`.
2248///
2249/// `arg` will be the tupled arguments that will be passed to either one of
2250/// the two functions, therefore, both functions must accept the same type of
2251/// arguments. Both functions must return RET.
2252///
2253/// # Stability concerns
2254///
2255/// Rust has not yet decided that `const fn` are allowed to tell whether
2256/// they run at compile-time or at runtime. Therefore, when using this
2257/// intrinsic anywhere that can be reached from stable, it is crucial that
2258/// the end-to-end behavior of the stable `const fn` is the same for both
2259/// modes of execution. (Here, Undefined Behavior is considered "the same"
2260/// as any other behavior, so if the function exhibits UB at runtime then
2261/// it may do whatever it wants at compile-time.)
2262///
2263/// Here is an example of how this could cause a problem:
2264/// ```no_run
2265/// #![feature(const_eval_select)]
2266/// #![feature(core_intrinsics)]
2267/// # #![allow(internal_features)]
2268/// use std::intrinsics::const_eval_select;
2269///
2270/// // Standard library
2271/// pub const fn inconsistent() -> i32 {
2272///     fn runtime() -> i32 { 1 }
2273///     const fn compiletime() -> i32 { 2 }
2274///
2275///     // ⚠ This code violates the required equivalence of `compiletime`
2276///     // and `runtime`.
2277///     const_eval_select((), compiletime, runtime)
2278/// }
2279///
2280/// // User Crate
2281/// const X: i32 = inconsistent();
2282/// let x = inconsistent();
2283/// assert_eq!(x, X);
2284/// ```
2285///
2286/// Currently such an assertion would always succeed; until Rust decides
2287/// otherwise, that principle should not be violated.
2288#[rustc_const_unstable(feature = "const_eval_select", issue = "124625")]
2289#[rustc_intrinsic]
2290pub const fn const_eval_select<ARG: Tuple, F, G, RET>(
2291    _arg: ARG,
2292    _called_in_const: F,
2293    _called_at_rt: G,
2294) -> RET
2295where
2296    G: FnOnce<ARG, Output = RET>,
2297    F: const FnOnce<ARG, Output = RET>;
2298
2299/// A macro to make it easier to invoke const_eval_select. Use as follows:
2300/// ```rust,ignore (just a macro example)
2301/// const_eval_select!(
2302///     @capture { arg1: i32 = some_expr, arg2: T = other_expr } -> U:
2303///     if const #[attributes_for_const_arm] {
2304///         // Compile-time code goes here.
2305///     } else #[attributes_for_runtime_arm] {
2306///         // Run-time code goes here.
2307///     }
2308/// )
2309/// ```
2310/// The `@capture` block declares which surrounding variables / expressions can be
2311/// used inside the `if const`.
2312/// Note that the two arms of this `if` really each become their own function, which is why the
2313/// macro supports setting attributes for those functions. The runtime function is always
2314/// marked as `#[inline]`.
2315///
2316/// See [`const_eval_select()`] for the rules and requirements around that intrinsic.
2317pub(crate) macro const_eval_select {
2318    (
2319        @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
2320        if const
2321            $(#[$compiletime_attr:meta])* $compiletime:block
2322        else
2323            $(#[$runtime_attr:meta])* $runtime:block
2324    ) => {
2325        // Use the `noinline` arm, after adding explicit `inline` attributes
2326        $crate::intrinsics::const_eval_select!(
2327            @capture$([$($binders)*])? { $($arg : $ty = $val),* } $(-> $ret)? :
2328            #[noinline]
2329            if const
2330                #[inline] // prevent codegen on this function
2331                $(#[$compiletime_attr])*
2332                $compiletime
2333            else
2334                #[inline] // avoid the overhead of an extra fn call
2335                $(#[$runtime_attr])*
2336                $runtime
2337        )
2338    },
2339    // With a leading #[noinline], we don't add inline attributes
2340    (
2341        @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty = $val:expr),* $(,)? } $( -> $ret:ty )? :
2342        #[noinline]
2343        if const
2344            $(#[$compiletime_attr:meta])* $compiletime:block
2345        else
2346            $(#[$runtime_attr:meta])* $runtime:block
2347    ) => {{
2348        $(#[$runtime_attr])*
2349        fn runtime$(<$($binders)*>)?($($arg: $ty),*) $( -> $ret )? {
2350            $runtime
2351        }
2352
2353        $(#[$compiletime_attr])*
2354        const fn compiletime$(<$($binders)*>)?($($arg: $ty),*) $( -> $ret )? {
2355            // Don't warn if one of the arguments is unused.
2356            $(let _ = $arg;)*
2357
2358            $compiletime
2359        }
2360
2361        const_eval_select(($($val,)*), compiletime, runtime)
2362    }},
2363    // We support leaving away the `val` expressions for *all* arguments
2364    // (but not for *some* arguments, that's too tricky).
2365    (
2366        @capture$([$($binders:tt)*])? { $($arg:ident : $ty:ty),* $(,)? } $( -> $ret:ty )? :
2367        if const
2368            $(#[$compiletime_attr:meta])* $compiletime:block
2369        else
2370            $(#[$runtime_attr:meta])* $runtime:block
2371    ) => {
2372        $crate::intrinsics::const_eval_select!(
2373            @capture$([$($binders)*])? { $($arg : $ty = $arg),* } $(-> $ret)? :
2374            if const
2375                $(#[$compiletime_attr])* $compiletime
2376            else
2377                $(#[$runtime_attr])* $runtime
2378        )
2379    },
2380}
2381
2382/// Returns whether the argument's value is statically known at
2383/// compile-time.
2384///
2385/// This is useful when there is a way of writing the code that will
2386/// be *faster* when some variables have known values, but *slower*
2387/// in the general case: an `if is_val_statically_known(var)` can be used
2388/// to select between these two variants. The `if` will be optimized away
2389/// and only the desired branch remains.
2390///
2391/// Formally speaking, this function non-deterministically returns `true`
2392/// or `false`, and the caller has to ensure sound behavior for both cases.
2393/// In other words, the following code has *Undefined Behavior*:
2394///
2395/// ```no_run
2396/// #![feature(core_intrinsics)]
2397/// # #![allow(internal_features)]
2398/// use std::hint::unreachable_unchecked;
2399/// use std::intrinsics::is_val_statically_known;
2400///
2401/// if !is_val_statically_known(0) { unsafe { unreachable_unchecked(); } }
2402/// ```
2403///
2404/// This also means that the following code's behavior is unspecified; it
2405/// may panic, or it may not:
2406///
2407/// ```no_run
2408/// #![feature(core_intrinsics)]
2409/// # #![allow(internal_features)]
2410/// use std::intrinsics::is_val_statically_known;
2411///
2412/// assert_eq!(is_val_statically_known(0), is_val_statically_known(0));
2413/// ```
2414///
2415/// Unsafe code may not rely on `is_val_statically_known` returning any
2416/// particular value, ever. However, the compiler will generally make it
2417/// return `true` only if the value of the argument is actually known.
2418///
2419/// # Stability concerns
2420///
2421/// While it is safe to call, this intrinsic may behave differently in
2422/// a `const` context than otherwise. See the [`const_eval_select()`]
2423/// documentation for an explanation of the issues this can cause. Unlike
2424/// `const_eval_select`, this intrinsic isn't guaranteed to behave
2425/// deterministically even in a `const` context.
2426///
2427/// # Type Requirements
2428///
2429/// `T` must be either a `bool`, a `char`, a primitive numeric type (e.g. `f32`,
2430/// but not `NonZeroISize`), or any thin pointer (e.g. `*mut String`).
2431/// Any other argument types *may* cause a compiler error.
2432///
2433/// ## Pointers
2434///
2435/// When the input is a pointer, only the pointer itself is
2436/// ever considered. The pointee has no effect. Currently, these functions
2437/// behave identically:
2438///
2439/// ```
2440/// #![feature(core_intrinsics)]
2441/// # #![allow(internal_features)]
2442/// use std::intrinsics::is_val_statically_known;
2443///
2444/// fn foo(x: &i32) -> bool {
2445///     is_val_statically_known(x)
2446/// }
2447///
2448/// fn bar(x: &i32) -> bool {
2449///     is_val_statically_known(
2450///         (x as *const i32).addr()
2451///     )
2452/// }
2453/// # _ = foo(&5_i32);
2454/// # _ = bar(&5_i32);
2455/// ```
2456#[rustc_const_stable_indirect]
2457#[rustc_nounwind]
2458#[unstable(feature = "core_intrinsics", issue = "none")]
2459#[rustc_intrinsic]
2460pub const fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
2461    false
2462}
2463
2464/// Non-overlapping *typed* swap of a single value.
2465///
2466/// The codegen backends will replace this with a better implementation when
2467/// `T` is a simple type that can be loaded and stored as an immediate.
2468///
2469/// The stabilized form of this intrinsic is [`crate::mem::swap`].
2470///
2471/// # Safety
2472/// Behavior is undefined if any of the following conditions are violated:
2473///
2474/// * Both `x` and `y` must be [valid] for both reads and writes.
2475///
2476/// * Both `x` and `y` must be properly aligned.
2477///
2478/// * The region of memory beginning at `x` must *not* overlap with the region of memory
2479///   beginning at `y`.
2480///
2481/// * The memory pointed by `x` and `y` must both contain values of type `T`.
2482///
2483/// [valid]: crate::ptr#safety
2484#[rustc_nounwind]
2485#[inline]
2486#[rustc_intrinsic]
2487#[rustc_intrinsic_const_stable_indirect]
2488pub const unsafe fn typed_swap_nonoverlapping<T>(x: *mut T, y: *mut T) {
2489    // SAFETY: The caller provided single non-overlapping items behind
2490    // pointers, so swapping them with `count: 1` is fine.
2491    unsafe { ptr::swap_nonoverlapping(x, y, 1) };
2492}
2493
2494/// Returns whether we should perform some UB-checking at runtime. This eventually evaluates to
2495/// `cfg!(ub_checks)`, but behaves different from `cfg!` when mixing crates built with different
2496/// flags: if the crate has UB checks enabled or carries the `#[rustc_preserve_ub_checks]`
2497/// attribute, evaluation is delayed until monomorphization (or until the call gets inlined into
2498/// a crate that does not delay evaluation further); otherwise it can happen any time.
2499///
2500/// The common case here is a user program built with ub_checks linked against the distributed
2501/// sysroot which is built without ub_checks but with `#[rustc_preserve_ub_checks]`.
2502/// For code that gets monomorphized in the user crate (i.e., generic functions and functions with
2503/// `#[inline]`), gating assertions on `ub_checks()` rather than `cfg!(ub_checks)` means that
2504/// assertions are enabled whenever the *user crate* has UB checks enabled. However, if the
2505/// user has UB checks disabled, the checks will still get optimized out. This intrinsic is
2506/// primarily used by [`crate::ub_checks::assert_unsafe_precondition`].
2507#[rustc_intrinsic_const_stable_indirect] // just for UB checks
2508#[inline(always)]
2509#[rustc_intrinsic]
2510pub const fn ub_checks() -> bool {
2511    cfg!(ub_checks)
2512}
2513
2514/// Allocates a block of memory at compile time.
2515/// At runtime, just returns a null pointer.
2516///
2517/// # Safety
2518///
2519/// - The `align` argument must be a power of two.
2520///    - At compile time, a compile error occurs if this constraint is violated.
2521///    - At runtime, it is not checked.
2522#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2523#[rustc_nounwind]
2524#[rustc_intrinsic]
2525#[miri::intrinsic_fallback_is_spec]
2526pub const unsafe fn const_allocate(_size: usize, _align: usize) -> *mut u8 {
2527    // const eval overrides this function, but runtime code for now just returns null pointers.
2528    // See <https://github.com/rust-lang/rust/issues/93935>.
2529    crate::ptr::null_mut()
2530}
2531
2532/// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
2533/// At runtime, does nothing.
2534///
2535/// # Safety
2536///
2537/// - The `align` argument must be a power of two.
2538///    - At compile time, a compile error occurs if this constraint is violated.
2539///    - At runtime, it is not checked.
2540/// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
2541/// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
2542#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2543#[unstable(feature = "core_intrinsics", issue = "none")]
2544#[rustc_nounwind]
2545#[rustc_intrinsic]
2546#[miri::intrinsic_fallback_is_spec]
2547pub const unsafe fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {
2548    // Runtime NOP
2549}
2550
2551#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2552#[rustc_nounwind]
2553#[rustc_intrinsic]
2554#[miri::intrinsic_fallback_is_spec]
2555pub const unsafe fn const_make_global(ptr: *mut u8) -> *const u8 {
2556    // const eval overrides this function; at runtime, it is a NOP.
2557    ptr
2558}
2559
2560/// Returns whether we should perform contract-checking at runtime.
2561///
2562/// This is meant to be similar to the ub_checks intrinsic, in terms
2563/// of not prematurely committing at compile-time to whether contract
2564/// checking is turned on, so that we can specify contracts in libstd
2565/// and let an end user opt into turning them on.
2566#[rustc_const_unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
2567#[unstable(feature = "contracts_internals", issue = "128044" /* compiler-team#759 */)]
2568#[inline(always)]
2569#[rustc_intrinsic]
2570pub const fn contract_checks() -> bool {
2571    // FIXME: should this be `false` or `cfg!(contract_checks)`?
2572
2573    // cfg!(contract_checks)
2574    false
2575}
2576
2577/// Check if the pre-condition `cond` has been met.
2578///
2579/// By default, if `contract_checks` is enabled, this will panic with no unwind if the condition
2580/// returns false.
2581///
2582/// Note that this function is a no-op during constant evaluation.
2583#[unstable(feature = "contracts_internals", issue = "128044")]
2584// Calls to this function get inserted by an AST expansion pass, which uses the equivalent of
2585// `#[allow_internal_unstable]` to allow using `contracts_internals` functions. Const-checking
2586// doesn't honor `#[allow_internal_unstable]`, so for the const feature gate we use the user-facing
2587// `contracts` feature rather than the perma-unstable `contracts_internals`
2588#[rustc_const_unstable(feature = "contracts", issue = "128044")]
2589#[lang = "contract_check_requires"]
2590#[rustc_intrinsic]
2591pub const fn contract_check_requires<C: Fn() -> bool + Copy>(cond: C) {
2592    const_eval_select!(
2593        @capture[C: Fn() -> bool + Copy] { cond: C } :
2594        if const {
2595                // Do nothing
2596        } else {
2597            if contract_checks() && !cond() {
2598                // Emit no unwind panic in case this was a safety requirement.
2599                crate::panicking::panic_nounwind("failed requires check");
2600            }
2601        }
2602    )
2603}
2604
2605/// Check if the post-condition `cond` has been met.
2606///
2607/// By default, if `contract_checks` is enabled, this will panic with no unwind if the condition
2608/// returns false.
2609///
2610/// Note that this function is a no-op during constant evaluation.
2611#[unstable(feature = "contracts_internals", issue = "128044")]
2612// Similar to `contract_check_requires`, we need to use the user-facing
2613// `contracts` feature rather than the perma-unstable `contracts_internals`.
2614// Const-checking doesn't honor allow_internal_unstable logic used by contract expansion.
2615#[rustc_const_unstable(feature = "contracts", issue = "128044")]
2616#[lang = "contract_check_ensures"]
2617#[rustc_intrinsic]
2618pub const fn contract_check_ensures<C: Fn(&Ret) -> bool + Copy, Ret>(cond: C, ret: Ret) -> Ret {
2619    const_eval_select!(
2620        @capture[C: Fn(&Ret) -> bool + Copy, Ret] { cond: C, ret: Ret } -> Ret :
2621        if const {
2622            // Do nothing
2623            ret
2624        } else {
2625            if contract_checks() && !cond(&ret) {
2626                // Emit no unwind panic in case this was a safety requirement.
2627                crate::panicking::panic_nounwind("failed ensures check");
2628            }
2629            ret
2630        }
2631    )
2632}
2633
2634/// The intrinsic will return the size stored in that vtable.
2635///
2636/// # Safety
2637///
2638/// `ptr` must point to a vtable.
2639#[rustc_nounwind]
2640#[unstable(feature = "core_intrinsics", issue = "none")]
2641#[rustc_intrinsic]
2642pub unsafe fn vtable_size(ptr: *const ()) -> usize;
2643
2644/// The intrinsic will return the alignment stored in that vtable.
2645///
2646/// # Safety
2647///
2648/// `ptr` must point to a vtable.
2649#[rustc_nounwind]
2650#[unstable(feature = "core_intrinsics", issue = "none")]
2651#[rustc_intrinsic]
2652pub unsafe fn vtable_align(ptr: *const ()) -> usize;
2653
2654/// The size of a type in bytes.
2655///
2656/// Note that, unlike most intrinsics, this is safe to call;
2657/// it does not require an `unsafe` block.
2658/// Therefore, implementations must not require the user to uphold
2659/// any safety invariants.
2660///
2661/// More specifically, this is the offset in bytes between successive
2662/// items of the same type, including alignment padding.
2663///
2664/// The stabilized version of this intrinsic is [`core::mem::size_of`].
2665#[rustc_nounwind]
2666#[unstable(feature = "core_intrinsics", issue = "none")]
2667#[rustc_intrinsic_const_stable_indirect]
2668#[rustc_intrinsic]
2669pub const fn size_of<T>() -> usize;
2670
2671/// The minimum alignment of a type.
2672///
2673/// Note that, unlike most intrinsics, this is safe to call;
2674/// it does not require an `unsafe` block.
2675/// Therefore, implementations must not require the user to uphold
2676/// any safety invariants.
2677///
2678/// The stabilized version of this intrinsic is [`core::mem::align_of`].
2679#[rustc_nounwind]
2680#[unstable(feature = "core_intrinsics", issue = "none")]
2681#[rustc_intrinsic_const_stable_indirect]
2682#[rustc_intrinsic]
2683pub const fn align_of<T>() -> usize;
2684
2685/// Returns the number of variants of the type `T` cast to a `usize`;
2686/// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
2687///
2688/// Note that, unlike most intrinsics, this can only be called at compile-time
2689/// as backends do not have an implementation for it. The only caller (its
2690/// stable counterpart) wraps this intrinsic call in a `const` block so that
2691/// backends only see an evaluated constant.
2692///
2693/// The to-be-stabilized version of this intrinsic is [`crate::mem::variant_count`].
2694#[rustc_nounwind]
2695#[unstable(feature = "core_intrinsics", issue = "none")]
2696#[rustc_intrinsic]
2697pub const fn variant_count<T>() -> usize;
2698
2699/// The size of the referenced value in bytes.
2700///
2701/// The stabilized version of this intrinsic is [`core::mem::size_of_val`].
2702///
2703/// # Safety
2704///
2705/// See [`crate::mem::size_of_val_raw`] for safety conditions.
2706#[rustc_nounwind]
2707#[unstable(feature = "core_intrinsics", issue = "none")]
2708#[rustc_intrinsic]
2709#[rustc_intrinsic_const_stable_indirect]
2710pub const unsafe fn size_of_val<T: ?Sized>(ptr: *const T) -> usize;
2711
2712/// The required alignment of the referenced value.
2713///
2714/// The stabilized version of this intrinsic is [`core::mem::align_of_val`].
2715///
2716/// # Safety
2717///
2718/// See [`crate::mem::align_of_val_raw`] for safety conditions.
2719#[rustc_nounwind]
2720#[unstable(feature = "core_intrinsics", issue = "none")]
2721#[rustc_intrinsic]
2722#[rustc_intrinsic_const_stable_indirect]
2723pub const unsafe fn align_of_val<T: ?Sized>(ptr: *const T) -> usize;
2724
2725/// Gets a static string slice containing the name of a type.
2726///
2727/// Note that, unlike most intrinsics, this can only be called at compile-time
2728/// as backends do not have an implementation for it. The only caller (its
2729/// stable counterpart) wraps this intrinsic call in a `const` block so that
2730/// backends only see an evaluated constant.
2731///
2732/// The stabilized version of this intrinsic is [`core::any::type_name`].
2733#[rustc_nounwind]
2734#[unstable(feature = "core_intrinsics", issue = "none")]
2735#[rustc_intrinsic]
2736pub const fn type_name<T: ?Sized>() -> &'static str;
2737
2738/// Gets an identifier which is globally unique to the specified type. This
2739/// function will return the same value for a type regardless of whichever
2740/// crate it is invoked in.
2741///
2742/// Note that, unlike most intrinsics, this can only be called at compile-time
2743/// as backends do not have an implementation for it. The only caller (its
2744/// stable counterpart) wraps this intrinsic call in a `const` block so that
2745/// backends only see an evaluated constant.
2746///
2747/// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
2748#[rustc_nounwind]
2749#[unstable(feature = "core_intrinsics", issue = "none")]
2750#[rustc_intrinsic]
2751pub const fn type_id<T: ?Sized + 'static>() -> crate::any::TypeId;
2752
2753/// Tests (at compile-time) if two [`crate::any::TypeId`] instances identify the
2754/// same type. This is necessary because at const-eval time the actual discriminating
2755/// data is opaque and cannot be inspected directly.
2756///
2757/// The stabilized version of this intrinsic is the [PartialEq] impl for [`core::any::TypeId`].
2758#[rustc_nounwind]
2759#[unstable(feature = "core_intrinsics", issue = "none")]
2760#[rustc_intrinsic]
2761#[rustc_do_not_const_check]
2762pub const fn type_id_eq(a: crate::any::TypeId, b: crate::any::TypeId) -> bool {
2763    a.data == b.data
2764}
2765
2766/// Lowers in MIR to `Rvalue::Aggregate` with `AggregateKind::RawPtr`.
2767///
2768/// This is used to implement functions like `slice::from_raw_parts_mut` and
2769/// `ptr::from_raw_parts` in a way compatible with the compiler being able to
2770/// change the possible layouts of pointers.
2771#[rustc_nounwind]
2772#[unstable(feature = "core_intrinsics", issue = "none")]
2773#[rustc_intrinsic_const_stable_indirect]
2774#[rustc_intrinsic]
2775pub const fn aggregate_raw_ptr<P: bounds::BuiltinDeref, D, M>(data: D, meta: M) -> P
2776where
2777    <P as bounds::BuiltinDeref>::Pointee: ptr::Pointee<Metadata = M>;
2778
2779/// Lowers in MIR to `Rvalue::UnaryOp` with `UnOp::PtrMetadata`.
2780///
2781/// This is used to implement functions like `ptr::metadata`.
2782#[rustc_nounwind]
2783#[unstable(feature = "core_intrinsics", issue = "none")]
2784#[rustc_intrinsic_const_stable_indirect]
2785#[rustc_intrinsic]
2786pub const fn ptr_metadata<P: ptr::Pointee<Metadata = M> + PointeeSized, M>(ptr: *const P) -> M;
2787
2788/// This is an accidentally-stable alias to [`ptr::copy_nonoverlapping`]; use that instead.
2789// Note (intentionally not in the doc comment): `ptr::copy_nonoverlapping` adds some extra
2790// debug assertions; if you are writing compiler tests or code inside the standard library
2791// that wants to avoid those debug assertions, directly call this intrinsic instead.
2792#[stable(feature = "rust1", since = "1.0.0")]
2793#[rustc_allowed_through_unstable_modules = "import this function via `std::ptr` instead"]
2794#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
2795#[rustc_nounwind]
2796#[rustc_intrinsic]
2797pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
2798
2799/// This is an accidentally-stable alias to [`ptr::copy`]; use that instead.
2800// Note (intentionally not in the doc comment): `ptr::copy` adds some extra
2801// debug assertions; if you are writing compiler tests or code inside the standard library
2802// that wants to avoid those debug assertions, directly call this intrinsic instead.
2803#[stable(feature = "rust1", since = "1.0.0")]
2804#[rustc_allowed_through_unstable_modules = "import this function via `std::ptr` instead"]
2805#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
2806#[rustc_nounwind]
2807#[rustc_intrinsic]
2808pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize);
2809
2810/// This is an accidentally-stable alias to [`ptr::write_bytes`]; use that instead.
2811// Note (intentionally not in the doc comment): `ptr::write_bytes` adds some extra
2812// debug assertions; if you are writing compiler tests or code inside the standard library
2813// that wants to avoid those debug assertions, directly call this intrinsic instead.
2814#[stable(feature = "rust1", since = "1.0.0")]
2815#[rustc_allowed_through_unstable_modules = "import this function via `std::ptr` instead"]
2816#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.83.0")]
2817#[rustc_nounwind]
2818#[rustc_intrinsic]
2819pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
2820
2821/// Returns the minimum (IEEE 754-2008 minNum) of two `f16` values.
2822///
2823/// Note that, unlike most intrinsics, this is safe to call;
2824/// it does not require an `unsafe` block.
2825/// Therefore, implementations must not require the user to uphold
2826/// any safety invariants.
2827///
2828/// The stabilized version of this intrinsic is
2829/// [`f16::min`]
2830#[rustc_nounwind]
2831#[rustc_intrinsic]
2832pub const fn minnumf16(x: f16, y: f16) -> f16;
2833
2834/// Returns the minimum (IEEE 754-2008 minNum) of two `f32` values.
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 version of this intrinsic is
2842/// [`f32::min`]
2843#[rustc_nounwind]
2844#[rustc_intrinsic_const_stable_indirect]
2845#[rustc_intrinsic]
2846pub const fn minnumf32(x: f32, y: f32) -> f32;
2847
2848/// Returns the minimum (IEEE 754-2008 minNum) of two `f64` values.
2849///
2850/// Note that, unlike most intrinsics, this is safe to call;
2851/// it does not require an `unsafe` block.
2852/// Therefore, implementations must not require the user to uphold
2853/// any safety invariants.
2854///
2855/// The stabilized version of this intrinsic is
2856/// [`f64::min`]
2857#[rustc_nounwind]
2858#[rustc_intrinsic_const_stable_indirect]
2859#[rustc_intrinsic]
2860pub const fn minnumf64(x: f64, y: f64) -> f64;
2861
2862/// Returns the minimum (IEEE 754-2008 minNum) of two `f128` values.
2863///
2864/// Note that, unlike most intrinsics, this is safe to call;
2865/// it does not require an `unsafe` block.
2866/// Therefore, implementations must not require the user to uphold
2867/// any safety invariants.
2868///
2869/// The stabilized version of this intrinsic is
2870/// [`f128::min`]
2871#[rustc_nounwind]
2872#[rustc_intrinsic]
2873pub const fn minnumf128(x: f128, y: f128) -> f128;
2874
2875/// Returns the minimum (IEEE 754-2019 minimum) of two `f16` values.
2876///
2877/// Note that, unlike most intrinsics, this is safe to call;
2878/// it does not require an `unsafe` block.
2879/// Therefore, implementations must not require the user to uphold
2880/// any safety invariants.
2881#[rustc_nounwind]
2882#[rustc_intrinsic]
2883pub const fn minimumf16(x: f16, y: f16) -> f16 {
2884    if x < y {
2885        x
2886    } else if y < x {
2887        y
2888    } else if x == y {
2889        if x.is_sign_negative() && y.is_sign_positive() { x } else { y }
2890    } else {
2891        // At least one input is NaN. Use `+` to perform NaN propagation and quieting.
2892        x + y
2893    }
2894}
2895
2896/// Returns the minimum (IEEE 754-2019 minimum) of two `f32` values.
2897///
2898/// Note that, unlike most intrinsics, this is safe to call;
2899/// it does not require an `unsafe` block.
2900/// Therefore, implementations must not require the user to uphold
2901/// any safety invariants.
2902#[rustc_nounwind]
2903#[rustc_intrinsic]
2904pub const fn minimumf32(x: f32, y: f32) -> f32 {
2905    if x < y {
2906        x
2907    } else if y < x {
2908        y
2909    } else if x == y {
2910        if x.is_sign_negative() && y.is_sign_positive() { x } else { y }
2911    } else {
2912        // At least one input is NaN. Use `+` to perform NaN propagation and quieting.
2913        x + y
2914    }
2915}
2916
2917/// Returns the minimum (IEEE 754-2019 minimum) of two `f64` values.
2918///
2919/// Note that, unlike most intrinsics, this is safe to call;
2920/// it does not require an `unsafe` block.
2921/// Therefore, implementations must not require the user to uphold
2922/// any safety invariants.
2923#[rustc_nounwind]
2924#[rustc_intrinsic]
2925pub const fn minimumf64(x: f64, y: f64) -> f64 {
2926    if x < y {
2927        x
2928    } else if y < x {
2929        y
2930    } else if x == y {
2931        if x.is_sign_negative() && y.is_sign_positive() { x } else { y }
2932    } else {
2933        // At least one input is NaN. Use `+` to perform NaN propagation and quieting.
2934        x + y
2935    }
2936}
2937
2938/// Returns the minimum (IEEE 754-2019 minimum) of two `f128` values.
2939///
2940/// Note that, unlike most intrinsics, this is safe to call;
2941/// it does not require an `unsafe` block.
2942/// Therefore, implementations must not require the user to uphold
2943/// any safety invariants.
2944#[rustc_nounwind]
2945#[rustc_intrinsic]
2946pub const fn minimumf128(x: f128, y: f128) -> f128 {
2947    if x < y {
2948        x
2949    } else if y < x {
2950        y
2951    } else if x == y {
2952        if x.is_sign_negative() && y.is_sign_positive() { x } else { y }
2953    } else {
2954        // At least one input is NaN. Use `+` to perform NaN propagation and quieting.
2955        x + y
2956    }
2957}
2958
2959/// Returns the maximum (IEEE 754-2008 maxNum) of two `f16` values.
2960///
2961/// Note that, unlike most intrinsics, this is safe to call;
2962/// it does not require an `unsafe` block.
2963/// Therefore, implementations must not require the user to uphold
2964/// any safety invariants.
2965///
2966/// The stabilized version of this intrinsic is
2967/// [`f16::max`]
2968#[rustc_nounwind]
2969#[rustc_intrinsic]
2970pub const fn maxnumf16(x: f16, y: f16) -> f16;
2971
2972/// Returns the maximum (IEEE 754-2008 maxNum) of two `f32` values.
2973///
2974/// Note that, unlike most intrinsics, this is safe to call;
2975/// it does not require an `unsafe` block.
2976/// Therefore, implementations must not require the user to uphold
2977/// any safety invariants.
2978///
2979/// The stabilized version of this intrinsic is
2980/// [`f32::max`]
2981#[rustc_nounwind]
2982#[rustc_intrinsic_const_stable_indirect]
2983#[rustc_intrinsic]
2984pub const fn maxnumf32(x: f32, y: f32) -> f32;
2985
2986/// Returns the maximum (IEEE 754-2008 maxNum) of two `f64` values.
2987///
2988/// Note that, unlike most intrinsics, this is safe to call;
2989/// it does not require an `unsafe` block.
2990/// Therefore, implementations must not require the user to uphold
2991/// any safety invariants.
2992///
2993/// The stabilized version of this intrinsic is
2994/// [`f64::max`]
2995#[rustc_nounwind]
2996#[rustc_intrinsic_const_stable_indirect]
2997#[rustc_intrinsic]
2998pub const fn maxnumf64(x: f64, y: f64) -> f64;
2999
3000/// Returns the maximum (IEEE 754-2008 maxNum) of two `f128` values.
3001///
3002/// Note that, unlike most intrinsics, this is safe to call;
3003/// it does not require an `unsafe` block.
3004/// Therefore, implementations must not require the user to uphold
3005/// any safety invariants.
3006///
3007/// The stabilized version of this intrinsic is
3008/// [`f128::max`]
3009#[rustc_nounwind]
3010#[rustc_intrinsic]
3011pub const fn maxnumf128(x: f128, y: f128) -> f128;
3012
3013/// Returns the maximum (IEEE 754-2019 maximum) of two `f16` values.
3014///
3015/// Note that, unlike most intrinsics, this is safe to call;
3016/// it does not require an `unsafe` block.
3017/// Therefore, implementations must not require the user to uphold
3018/// any safety invariants.
3019#[rustc_nounwind]
3020#[rustc_intrinsic]
3021pub const fn maximumf16(x: f16, y: f16) -> f16 {
3022    if x > y {
3023        x
3024    } else if y > x {
3025        y
3026    } else if x == y {
3027        if x.is_sign_positive() && y.is_sign_negative() { x } else { y }
3028    } else {
3029        x + y
3030    }
3031}
3032
3033/// Returns the maximum (IEEE 754-2019 maximum) of two `f32` values.
3034///
3035/// Note that, unlike most intrinsics, this is safe to call;
3036/// it does not require an `unsafe` block.
3037/// Therefore, implementations must not require the user to uphold
3038/// any safety invariants.
3039#[rustc_nounwind]
3040#[rustc_intrinsic]
3041pub const fn maximumf32(x: f32, y: f32) -> f32 {
3042    if x > y {
3043        x
3044    } else if y > x {
3045        y
3046    } else if x == y {
3047        if x.is_sign_positive() && y.is_sign_negative() { x } else { y }
3048    } else {
3049        x + y
3050    }
3051}
3052
3053/// Returns the maximum (IEEE 754-2019 maximum) of two `f64` values.
3054///
3055/// Note that, unlike most intrinsics, this is safe to call;
3056/// it does not require an `unsafe` block.
3057/// Therefore, implementations must not require the user to uphold
3058/// any safety invariants.
3059#[rustc_nounwind]
3060#[rustc_intrinsic]
3061pub const fn maximumf64(x: f64, y: f64) -> f64 {
3062    if x > y {
3063        x
3064    } else if y > x {
3065        y
3066    } else if x == y {
3067        if x.is_sign_positive() && y.is_sign_negative() { x } else { y }
3068    } else {
3069        x + y
3070    }
3071}
3072
3073/// Returns the maximum (IEEE 754-2019 maximum) of two `f128` values.
3074///
3075/// Note that, unlike most intrinsics, this is safe to call;
3076/// it does not require an `unsafe` block.
3077/// Therefore, implementations must not require the user to uphold
3078/// any safety invariants.
3079#[rustc_nounwind]
3080#[rustc_intrinsic]
3081pub const fn maximumf128(x: f128, y: f128) -> f128 {
3082    if x > y {
3083        x
3084    } else if y > x {
3085        y
3086    } else if x == y {
3087        if x.is_sign_positive() && y.is_sign_negative() { x } else { y }
3088    } else {
3089        x + y
3090    }
3091}
3092
3093/// Returns the absolute value of an `f16`.
3094///
3095/// The stabilized version of this intrinsic is
3096/// [`f16::abs`](../../std/primitive.f16.html#method.abs)
3097#[rustc_nounwind]
3098#[rustc_intrinsic]
3099pub const unsafe fn fabsf16(x: f16) -> f16;
3100
3101/// Returns the absolute value of an `f32`.
3102///
3103/// The stabilized version of this intrinsic is
3104/// [`f32::abs`](../../std/primitive.f32.html#method.abs)
3105#[rustc_nounwind]
3106#[rustc_intrinsic_const_stable_indirect]
3107#[rustc_intrinsic]
3108pub const unsafe fn fabsf32(x: f32) -> f32;
3109
3110/// Returns the absolute value of an `f64`.
3111///
3112/// The stabilized version of this intrinsic is
3113/// [`f64::abs`](../../std/primitive.f64.html#method.abs)
3114#[rustc_nounwind]
3115#[rustc_intrinsic_const_stable_indirect]
3116#[rustc_intrinsic]
3117pub const unsafe fn fabsf64(x: f64) -> f64;
3118
3119/// Returns the absolute value of an `f128`.
3120///
3121/// The stabilized version of this intrinsic is
3122/// [`f128::abs`](../../std/primitive.f128.html#method.abs)
3123#[rustc_nounwind]
3124#[rustc_intrinsic]
3125pub const unsafe fn fabsf128(x: f128) -> f128;
3126
3127/// Copies the sign from `y` to `x` for `f16` values.
3128///
3129/// The stabilized version of this intrinsic is
3130/// [`f16::copysign`](../../std/primitive.f16.html#method.copysign)
3131#[rustc_nounwind]
3132#[rustc_intrinsic]
3133pub const unsafe fn copysignf16(x: f16, y: f16) -> f16;
3134
3135/// Copies the sign from `y` to `x` for `f32` values.
3136///
3137/// The stabilized version of this intrinsic is
3138/// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
3139#[rustc_nounwind]
3140#[rustc_intrinsic_const_stable_indirect]
3141#[rustc_intrinsic]
3142pub const unsafe fn copysignf32(x: f32, y: f32) -> f32;
3143/// Copies the sign from `y` to `x` for `f64` values.
3144///
3145/// The stabilized version of this intrinsic is
3146/// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
3147#[rustc_nounwind]
3148#[rustc_intrinsic_const_stable_indirect]
3149#[rustc_intrinsic]
3150pub const unsafe fn copysignf64(x: f64, y: f64) -> f64;
3151
3152/// Copies the sign from `y` to `x` for `f128` values.
3153///
3154/// The stabilized version of this intrinsic is
3155/// [`f128::copysign`](../../std/primitive.f128.html#method.copysign)
3156#[rustc_nounwind]
3157#[rustc_intrinsic]
3158pub const unsafe fn copysignf128(x: f128, y: f128) -> f128;
3159
3160/// Inform Miri that a given pointer definitely has a certain alignment.
3161#[cfg(miri)]
3162#[rustc_allow_const_fn_unstable(const_eval_select)]
3163pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize) {
3164    unsafe extern "Rust" {
3165        /// Miri-provided extern function to promise that a given pointer is properly aligned for
3166        /// "symbolic" alignment checks. Will fail if the pointer is not actually aligned or `align` is
3167        /// not a power of two. Has no effect when alignment checks are concrete (which is the default).
3168        fn miri_promise_symbolic_alignment(ptr: *const (), align: usize);
3169    }
3170
3171    const_eval_select!(
3172        @capture { ptr: *const (), align: usize}:
3173        if const {
3174            // Do nothing.
3175        } else {
3176            // SAFETY: this call is always safe.
3177            unsafe {
3178                miri_promise_symbolic_alignment(ptr, align);
3179            }
3180        }
3181    )
3182}
3183
3184/// Copies the current location of arglist `src` to the arglist `dst`.
3185///
3186/// FIXME: document safety requirements
3187#[rustc_intrinsic]
3188#[rustc_nounwind]
3189pub unsafe fn va_copy<'f>(dest: *mut VaListImpl<'f>, src: &VaListImpl<'f>);
3190
3191/// Loads an argument of type `T` from the `va_list` `ap` and increment the
3192/// argument `ap` points to.
3193///
3194/// FIXME: document safety requirements
3195#[rustc_intrinsic]
3196#[rustc_nounwind]
3197pub unsafe fn va_arg<T: VaArgSafe>(ap: &mut VaListImpl<'_>) -> T;
3198
3199/// Destroy the arglist `ap` after initialization with `va_start` or `va_copy`.
3200///
3201/// FIXME: document safety requirements
3202#[rustc_intrinsic]
3203#[rustc_nounwind]
3204pub unsafe fn va_end(ap: &mut VaListImpl<'_>);