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