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