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