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