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