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