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