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