Module intrinsics
Compiler intrinsics.
The functions in this module are implementation details of core and should
not be used outside of the standard library. We generally provide access to
intrinsics via stable wrapper functions. Use these instead.
These are the imports making intrinsics available to Rust code. The actual implementations live in the compiler. Some of these intrinsics are lowered to MIR in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_mir_transform/src/lower_intrinsics.rs. The remaining intrinsics are implemented for the LLVM backend in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_codegen_ssa/src/mir/intrinsic.rs and https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_codegen_llvm/src/intrinsic.rs, and for const evaluation in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_const_eval/src/interpret/intrinsics.rs.
Const intrinsics
In order to make an intrinsic unstable usable at compile-time, copy the implementation from
https://github.com/rust-lang/miri/blob/master/src/intrinsics to
https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_const_eval/src/interpret/intrinsics.rs
and make the intrinsic declaration below a const fn. This should be done in coordination with
wg-const-eval.
If an intrinsic is supposed to be used from a const fn with a rustc_const_stable attribute,
#[rustc_intrinsic_const_stable_indirect] needs to be added to the intrinsic. Such a change requires
T-lang approval, because it may bake a feature into the language that cannot be replicated in
user code without compiler support.
Volatiles
The volatile intrinsics provide operations intended to act on I/O
memory, which are guaranteed to not be reordered by the compiler
across other volatile intrinsics. See [read_volatile][ptr::read_volatile]
and [write_volatile][ptr::write_volatile].
Atomics
The atomic intrinsics provide common atomic operations on machine words, with multiple possible memory orderings. See the [atomic types][atomic] docs for details.
Unwinding
Rust intrinsics may, in general, unwind. If an intrinsic can never unwind, add the
#[rustc_nounwind] attribute so that the compiler can make use of this fact.
However, even for intrinsics that may unwind, rustc assumes that a Rust intrinsics will never initiate a foreign (non-Rust) unwind, and thus for panic=abort we can always assume that these intrinsics cannot unwind.
Modules
Enums
-
AtomicOrdering
A type for atomic ordering parameters for intrinsics. This is a separate type from
atomic::Orderingso that we can make itConstParamTyand fix the values used here without a risk of leaking that to stable code.
Functions
- abort Aborts the execution of the process.
- add_with_overflow Performs checked integer addition.
-
aggregate_raw_ptr
Lowers in MIR to
Rvalue::AggregatewithAggregateKind::RawPtr. - align_of The minimum alignment of a type.
- align_of_val The required alignment of the referenced value.
- arith_offset Calculates the offset from a pointer, potentially wrapping.
-
assert_inhabited
A guard for unsafe functions that cannot ever be executed if
Tis uninhabited: This will statically either panic, or do nothing. It does not guarantee to ever panic, and should only be called if an assertion failure will imply language UB in the following code. -
assert_mem_uninitialized_valid
A guard for
std::mem::uninitialized. This will statically either panic, or do nothing. It does not guarantee to ever panic, and should only be called if an assertion failure will imply language UB in the following code. -
assert_zero_valid
A guard for unsafe functions that cannot ever be executed if
Tdoes not permit zero-initialization: This will statically either panic, or do nothing. It does not guarantee to ever panic, and should only be called if an assertion failure will imply language UB in the following code. - assume Informs the optimizer that a condition is always true. If the condition is false, the behavior is undefined.
-
atomic_and
Bitwise and with the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. -
atomic_cxchg
Stores a value if the current value is the same as the
oldvalue.Tmust be an integer or pointer type. -
atomic_cxchgweak
Stores a value if the current value is the same as the
oldvalue.Tmust be an integer or pointer type. The comparison may spuriously fail. - atomic_fence An atomic fence.
-
atomic_load
Loads the current value of the pointer.
Tmust be an integer or pointer type. -
atomic_max
Maximum with the current value using a signed comparison.
Tmust be a signed integer type. -
atomic_min
Minimum with the current value using a signed comparison.
Tmust be a signed integer type. -
atomic_nand
Bitwise nand with the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. -
atomic_or
Bitwise or with the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. - atomic_singlethreadfence An atomic fence for synchronization within a single thread.
-
atomic_store
Stores the value at the specified memory location.
Tmust be an integer or pointer type. -
atomic_umax
Maximum with the current value using an unsigned comparison.
Tmust be an unsigned integer type. -
atomic_umin
Minimum with the current value using an unsigned comparison.
Tmust be an unsigned integer type. -
atomic_xadd
Adds to the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. -
atomic_xchg
Stores the value at the specified memory location, returning the old value.
Tmust be an integer or pointer type. -
atomic_xor
Bitwise xor with the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. -
atomic_xsub
Subtract from the current value, returning the previous value.
Tmust be an integer or pointer type.Umust be the same asTif that is an integer type, orusizeifTis a pointer type. -
autodiff
Generates the LLVM body for the automatic differentiation of
fusing Enzyme, withdfas the derivative function andargsas its arguments. -
bitreverse
Reverses the bits in an integer type
T. -
black_box
See documentation of
std::hint::black_boxfor details. - breakpoint Executes a breakpoint trap, for inspection by a debugger.
-
bswap
Reverses the bytes in an integer type
T. -
caller_location
Gets a reference to a static
Locationindicating where it was called. -
carrying_mul_add
Performs full-width multiplication and addition with a carry:
multiplier * multiplicand + addend + carry. -
catch_unwind
Rust's "try catch" construct for unwinding. Invokes the function pointer
try_fnwith the data pointerdata, and callscatch_fnif unwinding occurs whiletry_fnruns. Returns1if unwinding occurred andcatch_fnwas called; returns0otherwise. -
ceilf128
Returns the smallest integer greater than or equal to an
f128. -
ceilf16
Returns the smallest integer greater than or equal to an
f16. -
ceilf32
Returns the smallest integer greater than or equal to an
f32. -
ceilf64
Returns the smallest integer greater than or equal to an
f64. - cold_path Hints to the compiler that current code path is cold.
-
compare_bytes
Lexicographically compare
[left, left + bytes)and[right, right + bytes)as unsigned bytes, returning negative ifleftis less, zero if all the bytes match, or positive ifleftis greater. - const_allocate Allocates a block of memory at compile time. At runtime, just returns a null pointer.
-
const_deallocate
Deallocates a memory which allocated by
intrinsics::const_allocateat compile time. At runtime, does nothing. - const_eval_select Selects which function to call depending on the context.
- const_make_global
-
contract_check_ensures
Check if the post-condition
condhas been met. -
contract_check_requires
Check if the pre-condition
condhas been met. -
copy
This is an accidentally-stable alias to
ptr::copy; use that instead. -
copy_nonoverlapping
This is an accidentally-stable alias to
ptr::copy_nonoverlapping; use that instead. -
copysignf128
Copies the sign from
ytoxforf128values. -
copysignf16
Copies the sign from
ytoxforf16values. -
copysignf32
Copies the sign from
ytoxforf32values. -
copysignf64
Copies the sign from
ytoxforf64values. -
cosf128
Returns the cosine of an
f128. -
cosf16
Returns the cosine of an
f16. -
cosf32
Returns the cosine of an
f32. -
cosf64
Returns the cosine of an
f64. -
ctlz
Returns the number of leading unset bits (zeroes) in an integer type
T. -
ctlz_nonzero
Like
ctlz, but extra-unsafe as it returnsundefwhen given anxwith value0. -
ctpop
Returns the number of bits set in an integer type
T -
cttz
Returns the number of trailing unset bits (zeroes) in an integer type
T. -
cttz_nonzero
Like
cttz, but extra-unsafe as it returnsundefwhen given anxwith value0. -
discriminant_value
Returns the value of the discriminant for the variant in 'v';
if
Thas no discriminant, returns0. - disjoint_bitor Combine two values which have no bits in common.
-
exact_div
Performs an exact division, resulting in undefined behavior where
x % y != 0ory == 0orx == T::MIN && y == -1 -
exp2f128
Returns 2 raised to the power of an
f128. -
exp2f16
Returns 2 raised to the power of an
f16. -
exp2f32
Returns 2 raised to the power of an
f32. -
exp2f64
Returns 2 raised to the power of an
f64. -
expf128
Returns the exponential of an
f128. -
expf16
Returns the exponential of an
f16. -
expf32
Returns the exponential of an
f32. -
expf64
Returns the exponential of an
f64. -
fabsf128
Returns the absolute value of an
f128. -
fabsf16
Returns the absolute value of an
f16. -
fabsf32
Returns the absolute value of an
f32. -
fabsf64
Returns the absolute value of an
f64. - fadd_algebraic Float addition that allows optimizations based on algebraic rules.
- fadd_fast Float addition that allows optimizations based on algebraic rules. Requires that inputs and output of the operation are finite, causing UB otherwise.
- fdiv_algebraic Float division that allows optimizations based on algebraic rules.
- fdiv_fast Float division that allows optimizations based on algebraic rules. Requires that inputs and output of the operation are finite, causing UB otherwise.
- float_to_int_unchecked Converts with LLVM’s fptoui/fptosi, which may return undef for values out of range (https://github.com/rust-lang/rust/issues/10184)
-
floorf128
Returns the largest integer less than or equal to an
f128. -
floorf16
Returns the largest integer less than or equal to an
f16. -
floorf32
Returns the largest integer less than or equal to an
f32. -
floorf64
Returns the largest integer less than or equal to an
f64. -
fmaf128
Returns
a * b + cforf128values. -
fmaf16
Returns
a * b + cforf16values. -
fmaf32
Returns
a * b + cforf32values. -
fmaf64
Returns
a * b + cforf64values. - fmul_algebraic Float multiplication that allows optimizations based on algebraic rules.
- fmul_fast Float multiplication that allows optimizations based on algebraic rules. Requires that inputs and output of the operation are finite, causing UB otherwise.
-
fmuladdf128
Returns
a * b + cforf128values, non-deterministically executing either a fused multiply-add or two operations with rounding of the intermediate result. -
fmuladdf16
Returns
a * b + cforf16values, non-deterministically executing either a fused multiply-add or two operations with rounding of the intermediate result. -
fmuladdf32
Returns
a * b + cforf32values, non-deterministically executing either a fused multiply-add or two operations with rounding of the intermediate result. -
fmuladdf64
Returns
a * b + cforf64values, non-deterministically executing either a fused multiply-add or two operations with rounding of the intermediate result. - forget Moves a value out of scope without running drop glue.
- frem_algebraic Float remainder that allows optimizations based on algebraic rules.
- frem_fast Float remainder that allows optimizations based on algebraic rules. Requires that inputs and output of the operation are finite, causing UB otherwise.
- fsub_algebraic Float subtraction that allows optimizations based on algebraic rules.
- fsub_fast Float subtraction that allows optimizations based on algebraic rules. Requires that inputs and output of the operation are finite, causing UB otherwise.
- is_val_statically_known Returns whether the argument's value is statically known at compile-time.
- likely Hints to the compiler that branch condition is likely to be true. Returns the value passed to it.
-
log10f128
Returns the base 10 logarithm of an
f128. -
log10f16
Returns the base 10 logarithm of an
f16. -
log10f32
Returns the base 10 logarithm of an
f32. -
log10f64
Returns the base 10 logarithm of an
f64. -
log2f128
Returns the base 2 logarithm of an
f128. -
log2f16
Returns the base 2 logarithm of an
f16. -
log2f32
Returns the base 2 logarithm of an
f32. -
log2f64
Returns the base 2 logarithm of an
f64. -
logf128
Returns the natural logarithm of an
f128. -
logf16
Returns the natural logarithm of an
f16. -
logf32
Returns the natural logarithm of an
f32. -
logf64
Returns the natural logarithm of an
f64. -
maximumf128
Returns the maximum of two
f128values, propagating NaN. -
maximumf16
Returns the maximum of two
f16values, propagating NaN. -
maximumf32
Returns the maximum of two
f32values, propagating NaN. -
maximumf64
Returns the maximum of two
f64values, propagating NaN. -
maxnumf128
Returns the maximum of two
f128values, ignoring NaN. -
maxnumf16
Returns the maximum of two
f16values, ignoring NaN. -
maxnumf32
Returns the maximum of two
f32values, ignoring NaN. -
maxnumf64
Returns the maximum of two
f64values, ignoring NaN. -
minimumf128
Returns the minimum of two
f128values, propagating NaN. -
minimumf16
Returns the minimum of two
f16values, propagating NaN. -
minimumf32
Returns the minimum of two
f32values, propagating NaN. -
minimumf64
Returns the minimum of two
f64values, propagating NaN. -
minnumf128
Returns the minimum of two
f128values, ignoring NaN. -
minnumf16
Returns the minimum of two
f16values, ignoring NaN. -
minnumf32
Returns the minimum of two
f32values, ignoring NaN. -
minnumf64
Returns the minimum of two
f64values, ignoring NaN. - mul_with_overflow Performs checked integer multiplication
-
needs_drop
Returns
trueif the actual type given asTrequires drop glue; returnsfalseif the actual type provided forTimplementsCopy. -
nontemporal_store
Emits a
nontemporalstore, which gives a hint to the CPU that the data should not be held in cache. Except for performance, this is fully equivalent toptr.write(val). -
offload
Generates the LLVM body of a wrapper function to offload a kernel
f. - offset Calculates the offset from a pointer.
- offset_of The offset of a field inside a type.
-
overflow_checks
Returns whether we should perform some overflow-checking at runtime. This eventually evaluates to
cfg!(overflow_checks), but behaves different fromcfg!when mixing crates built with different flags: if the crate has overflow checks enabled or carries the#[rustc_inherit_overflow_checks]attribute, evaluation is delayed until monomorphization (or until the call gets inlined into a crate that does not delay evaluation further); otherwise it can happen any time. -
powf128
Raises an
f128to anf128power. -
powf16
Raises an
f16to anf16power. -
powf32
Raises an
f32to anf32power. -
powf64
Raises an
f64to anf64power. -
powif128
Raises an
f128to an integer power. -
powif16
Raises an
f16to an integer power. -
powif32
Raises an
f32to an integer power. -
powif64
Raises an
f64to an integer power. -
prefetch_read_data
The
prefetchintrinsic is a hint to the code generator to insert a prefetch instruction for the given address if supported; otherwise, it is a no-op. Prefetches have no effect on the behavior of the program but can change its performance characteristics. -
prefetch_read_instruction
The
prefetchintrinsic is a hint to the code generator to insert a prefetch instruction for the given address if supported; otherwise, it is a no-op. Prefetches have no effect on the behavior of the program but can change its performance characteristics. -
prefetch_write_data
The
prefetchintrinsic is a hint to the code generator to insert a prefetch instruction for the given address if supported; otherwise, it is a no-op. Prefetches have no effect on the behavior of the program but can change its performance characteristics. -
prefetch_write_instruction
The
prefetchintrinsic is a hint to the code generator to insert a prefetch instruction for the given address if supported; otherwise, it is a no-op. Prefetches have no effect on the behavior of the program but can change its performance characteristics. -
ptr_guaranteed_cmp
See documentation of
<*const T>::guaranteed_eqfor details. Returns2if the result is unknown. Returns1if the pointers are guaranteed equal. Returns0if the pointers are guaranteed inequal. - ptr_mask Masks out bits of the pointer according to a mask.
-
ptr_metadata
Lowers in MIR to
Rvalue::UnaryOpwithUnOp::PtrMetadata. -
ptr_offset_from
See documentation of
<*const T>::offset_fromfor details. -
ptr_offset_from_unsigned
See documentation of
<*const T>::offset_from_unsignedfor details. - raw_eq Determines whether the raw bytes of the two values are equal.
-
read_via_copy
This is an implementation detail of
crate::ptr::readand should not be used anywhere else. See its comments for why this exists. - rotate_left Performs rotate left.
- rotate_right Performs rotate right.
-
round_ties_even_f128
Returns the nearest integer to an
f128. Rounds half-way cases to the number with an even least significant digit. -
round_ties_even_f16
Returns the nearest integer to an
f16. Rounds half-way cases to the number with an even least significant digit. -
round_ties_even_f32
Returns the nearest integer to an
f32. Rounds half-way cases to the number with an even least significant digit. -
round_ties_even_f64
Returns the nearest integer to an
f64. Rounds half-way cases to the number with an even least significant digit. -
roundf128
Returns the nearest integer to an
f128. Rounds half-way cases away from zero. -
roundf16
Returns the nearest integer to an
f16. Rounds half-way cases away from zero. -
roundf32
Returns the nearest integer to an
f32. Rounds half-way cases away from zero. -
roundf64
Returns the nearest integer to an
f64. Rounds half-way cases away from zero. - rustc_peek Magic intrinsic that derives its meaning from attributes attached to the function.
-
saturating_add
Computes
a + b, saturating at numeric bounds. -
saturating_sub
Computes
a - b, saturating at numeric bounds. -
select_unpredictable
Returns either
true_valorfalse_valdepending on conditionbwith a hint to the compiler that this condition is unlikely to be correctly predicted by a CPU's branch predictor (e.g. a binary search). -
sinf128
Returns the sine of an
f128. -
sinf16
Returns the sine of an
f16. -
sinf32
Returns the sine of an
f32. -
sinf64
Returns the sine of an
f64. - size_of The size of a type in bytes.
- size_of_val The size of the referenced value in bytes.
-
slice_get_unchecked
Projects to the
index-th element ofslice_ptr, as the same kind of pointer as the slice was provided -- so&mut [T] → &mut T,&[T] → &T,*mut [T] → *mut T, or*const [T] → *const T-- without a bounds check. -
sqrtf128
Returns the square root of an
f128 -
sqrtf16
Returns the square root of an
f16 -
sqrtf32
Returns the square root of an
f32 -
sqrtf64
Returns the square root of an
f64 - sub_with_overflow Performs checked integer subtraction
- three_way_compare Does a three-way comparison between the two arguments, which must be of character or integer (signed or unsigned) type.
- transmute Reinterprets the bits of a value of one type as another type.
-
transmute_unchecked
Like
transmute, but even less checked at compile-time: rather than giving an error forsize_of::<Src>() != size_of::<Dst>(), it's Undefined Behavior at runtime. -
truncf128
Returns the integer part of an
f128. -
truncf16
Returns the integer part of an
f16. -
truncf32
Returns the integer part of an
f32. -
truncf64
Returns the integer part of an
f64. - type_id Gets an identifier which is globally unique to the specified type. This function will return the same value for a type regardless of whichever crate it is invoked in.
-
type_id_eq
Tests (at compile-time) if two
crate::any::TypeIdinstances identify the same type. This is necessary because at const-eval time the actual discriminating data is opaque and cannot be inspected directly. - type_name Gets a static string slice containing the name of a type.
- type_of Compute the type information of a concrete type. It can only be called at compile time, the backends do not implement it.
- typed_swap_nonoverlapping Non-overlapping typed swap of a single value.
-
ub_checks
Returns whether we should perform some UB-checking at runtime. This eventually evaluates to
cfg!(ub_checks), but behaves different fromcfg!when mixing crates built with different flags: if the crate has UB checks enabled or carries the#[rustc_preserve_ub_checks]attribute, evaluation is delayed until monomorphization (or until the call gets inlined into a crate that does not delay evaluation further); otherwise it can happen any time. -
unaligned_volatile_load
Performs a volatile load from the
srcpointer The pointer is not required to be aligned. -
unaligned_volatile_store
Performs a volatile store to the
dstpointer. The pointer is not required to be aligned. -
unchecked_add
Returns the result of an unchecked addition, resulting in
undefined behavior when
x + y > T::MAXorx + y < T::MIN. -
unchecked_div
Performs an unchecked division, resulting in undefined behavior
where
y == 0orx == T::MIN && y == -1 - unchecked_funnel_shl Funnel Shift left.
- unchecked_funnel_shr Funnel Shift right.
-
unchecked_mul
Returns the result of an unchecked multiplication, resulting in
undefined behavior when
x * y > T::MAXorx * y < T::MIN. -
unchecked_rem
Returns the remainder of an unchecked division, resulting in
undefined behavior when
y == 0orx == T::MIN && y == -1 -
unchecked_shl
Performs an unchecked left shift, resulting in undefined behavior when
y < 0ory >= N, where N is the width of T in bits. -
unchecked_shr
Performs an unchecked right shift, resulting in undefined behavior when
y < 0ory >= N, where N is the width of T in bits. -
unchecked_sub
Returns the result of an unchecked subtraction, resulting in
undefined behavior when
x - y > T::MAXorx - y < T::MIN. - unlikely Hints to the compiler that branch condition is likely to be false. Returns the value passed to it.
- unreachable Informs the optimizer that this point in the code is not reachable, enabling further optimizations.
-
va_arg
Loads an argument of type
Tfrom theva_listapand increment the argumentappoints to. -
va_copy
Copies the current location of arglist
srcto the arglistdst. -
va_end
Destroy the arglist
apafter initialization withva_startorva_copy. -
variant_count
Returns the number of variants of the type
Tcast to ausize; ifThas no variants, returns0. Uninhabited variants will be counted. -
volatile_copy_memory
Equivalent to the appropriate
llvm.memmove.p0i8.0i8.*intrinsic, with a size ofcount * size_of::<T>()and an alignment ofalign_of::<T>(). -
volatile_copy_nonoverlapping_memory
Equivalent to the appropriate
llvm.memcpy.p0i8.0i8.*intrinsic, with a size ofcount*size_of::<T>()and an alignment ofalign_of::<T>(). -
volatile_load
Performs a volatile load from the
srcpointer. -
volatile_set_memory
Equivalent to the appropriate
llvm.memset.p0i8.*intrinsic, with a size ofcount * size_of::<T>()and an alignment ofalign_of::<T>(). -
volatile_store
Performs a volatile store to the
dstpointer. - vtable_align The intrinsic will return the alignment stored in that vtable.
-
vtable_for
The intrinsic returns the
Uvtable forTifTcan be coerced to the trait object typeU. - vtable_size The intrinsic will return the size stored in that vtable.
- wrapping_add Returns (a + b) mod 2N, where N is the width of T in bits.
- wrapping_mul Returns (a * b) mod 2N, where N is the width of T in bits.
- wrapping_sub Returns (a - b) mod 2N, where N is the width of T in bits.
-
write_bytes
This is an accidentally-stable alias to
ptr::write_bytes; use that instead. -
write_via_move
This is an implementation detail of
crate::ptr::writeand should not be used anywhere else. See its comments for why this exists.