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