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