]> git.lizzy.rs Git - rust.git/blob - library/core/src/intrinsics.rs
Rename `assert_uninit_valid` intrinsic
[rust.git] / library / core / src / intrinsics.rs
1 //! Compiler intrinsics.
2 //!
3 //! The corresponding definitions are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/intrinsic.rs>.
4 //! The corresponding const implementations are in <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs>.
5 //!
6 //! # Const intrinsics
7 //!
8 //! Note: any changes to the constness of intrinsics should be discussed with the language team.
9 //! This includes changes in the stability of the constness.
10 //!
11 //! In order to make an intrinsic usable at compile-time, one needs to copy the implementation
12 //! from <https://github.com/rust-lang/miri/blob/master/src/shims/intrinsics.rs> to
13 //! <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs> and add a
14 //! `#[rustc_const_unstable(feature = "const_such_and_such", issue = "01234")]` to the intrinsic declaration.
15 //!
16 //! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
17 //! the intrinsic's attribute must be `rustc_const_stable`, too. Such a change should not be done
18 //! without T-lang consultation, because it bakes a feature into the language that cannot be
19 //! replicated in user code without compiler support.
20 //!
21 //! # Volatiles
22 //!
23 //! The volatile intrinsics provide operations intended to act on I/O
24 //! memory, which are guaranteed to not be reordered by the compiler
25 //! across other volatile intrinsics. See the LLVM documentation on
26 //! [[volatile]].
27 //!
28 //! [volatile]: https://llvm.org/docs/LangRef.html#volatile-memory-accesses
29 //!
30 //! # Atomics
31 //!
32 //! The atomic intrinsics provide common atomic operations on machine
33 //! words, with multiple possible memory orderings. They obey the same
34 //! semantics as C++11. See the LLVM documentation on [[atomics]].
35 //!
36 //! [atomics]: https://llvm.org/docs/Atomics.html
37 //!
38 //! A quick refresher on memory ordering:
39 //!
40 //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
41 //!   take place after the barrier.
42 //! * Release - a barrier for releasing a lock. Preceding reads and writes
43 //!   take place before the barrier.
44 //! * Sequentially consistent - sequentially consistent operations are
45 //!   guaranteed to happen in order. This is the standard mode for working
46 //!   with atomic types and is equivalent to Java's `volatile`.
47
48 #![unstable(
49     feature = "core_intrinsics",
50     reason = "intrinsics are unlikely to ever be stabilized, instead \
51                       they should be used through stabilized interfaces \
52                       in the rest of the standard library",
53     issue = "none"
54 )]
55 #![allow(missing_docs)]
56
57 use crate::marker::DiscriminantKind;
58 #[cfg(not(bootstrap))]
59 use crate::marker::Tuple;
60 use crate::mem;
61
62 #[cfg(not(bootstrap))]
63 pub mod mir;
64
65 // These imports are used for simplifying intra-doc links
66 #[allow(unused_imports)]
67 #[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))]
68 use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering};
69
70 #[stable(feature = "drop_in_place", since = "1.8.0")]
71 #[rustc_allowed_through_unstable_modules]
72 #[deprecated(note = "no longer an intrinsic - use `ptr::drop_in_place` directly", since = "1.52.0")]
73 #[inline]
74 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
75     // SAFETY: see `ptr::drop_in_place`
76     unsafe { crate::ptr::drop_in_place(to_drop) }
77 }
78
79 extern "rust-intrinsic" {
80     // N.B., these intrinsics take raw pointers because they mutate aliased
81     // memory, which is not valid for either `&` or `&mut`.
82
83     /// Stores a value if the current value is the same as the `old` value.
84     ///
85     /// The stabilized version of this intrinsic is available on the
86     /// [`atomic`] types via the `compare_exchange` method by passing
87     /// [`Ordering::Relaxed`] as both the success and failure parameters.
88     /// For example, [`AtomicBool::compare_exchange`].
89     pub fn atomic_cxchg_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
90     /// Stores a value if the current value is the same as the `old` value.
91     ///
92     /// The stabilized version of this intrinsic is available on the
93     /// [`atomic`] types via the `compare_exchange` method by passing
94     /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
95     /// For example, [`AtomicBool::compare_exchange`].
96     pub fn atomic_cxchg_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
97     /// Stores a value if the current value is the same as the `old` value.
98     ///
99     /// The stabilized version of this intrinsic is available on the
100     /// [`atomic`] types via the `compare_exchange` method by passing
101     /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
102     /// For example, [`AtomicBool::compare_exchange`].
103     pub fn atomic_cxchg_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
104     /// Stores a value if the current value is the same as the `old` value.
105     ///
106     /// The stabilized version of this intrinsic is available on the
107     /// [`atomic`] types via the `compare_exchange` method by passing
108     /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
109     /// For example, [`AtomicBool::compare_exchange`].
110     pub fn atomic_cxchg_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
111     /// Stores a value if the current value is the same as the `old` value.
112     ///
113     /// The stabilized version of this intrinsic is available on the
114     /// [`atomic`] types via the `compare_exchange` method by passing
115     /// [`Ordering::Acquire`] as both the success and failure parameters.
116     /// For example, [`AtomicBool::compare_exchange`].
117     pub fn atomic_cxchg_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
118     /// Stores a value if the current value is the same as the `old` value.
119     ///
120     /// The stabilized version of this intrinsic is available on the
121     /// [`atomic`] types via the `compare_exchange` method by passing
122     /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
123     /// For example, [`AtomicBool::compare_exchange`].
124     pub fn atomic_cxchg_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
125     /// Stores a value if the current value is the same as the `old` value.
126     ///
127     /// The stabilized version of this intrinsic is available on the
128     /// [`atomic`] types via the `compare_exchange` method by passing
129     /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
130     /// For example, [`AtomicBool::compare_exchange`].
131     pub fn atomic_cxchg_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
132     /// Stores a value if the current value is the same as the `old` value.
133     ///
134     /// The stabilized version of this intrinsic is available on the
135     /// [`atomic`] types via the `compare_exchange` method by passing
136     /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
137     /// For example, [`AtomicBool::compare_exchange`].
138     pub fn atomic_cxchg_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
139     /// Stores a value if the current value is the same as the `old` value.
140     ///
141     /// The stabilized version of this intrinsic is available on the
142     /// [`atomic`] types via the `compare_exchange` method by passing
143     /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
144     /// For example, [`AtomicBool::compare_exchange`].
145     pub fn atomic_cxchg_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
146     /// Stores a value if the current value is the same as the `old` value.
147     ///
148     /// The stabilized version of this intrinsic is available on the
149     /// [`atomic`] types via the `compare_exchange` method by passing
150     /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
151     /// For example, [`AtomicBool::compare_exchange`].
152     pub fn atomic_cxchg_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
153     /// Stores a value if the current value is the same as the `old` value.
154     ///
155     /// The stabilized version of this intrinsic is available on the
156     /// [`atomic`] types via the `compare_exchange` method by passing
157     /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
158     /// For example, [`AtomicBool::compare_exchange`].
159     pub fn atomic_cxchg_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
160     /// Stores a value if the current value is the same as the `old` value.
161     ///
162     /// The stabilized version of this intrinsic is available on the
163     /// [`atomic`] types via the `compare_exchange` method by passing
164     /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
165     /// For example, [`AtomicBool::compare_exchange`].
166     pub fn atomic_cxchg_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
167     /// Stores a value if the current value is the same as the `old` value.
168     ///
169     /// The stabilized version of this intrinsic is available on the
170     /// [`atomic`] types via the `compare_exchange` method by passing
171     /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
172     /// For example, [`AtomicBool::compare_exchange`].
173     pub fn atomic_cxchg_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
174     /// Stores a value if the current value is the same as the `old` value.
175     ///
176     /// The stabilized version of this intrinsic is available on the
177     /// [`atomic`] types via the `compare_exchange` method by passing
178     /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
179     /// For example, [`AtomicBool::compare_exchange`].
180     pub fn atomic_cxchg_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
181     /// Stores a value if the current value is the same as the `old` value.
182     ///
183     /// The stabilized version of this intrinsic is available on the
184     /// [`atomic`] types via the `compare_exchange` method by passing
185     /// [`Ordering::SeqCst`] as both the success and failure parameters.
186     /// For example, [`AtomicBool::compare_exchange`].
187     pub fn atomic_cxchg_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
188
189     /// Stores a value if the current value is the same as the `old` value.
190     ///
191     /// The stabilized version of this intrinsic is available on the
192     /// [`atomic`] types via the `compare_exchange_weak` method by passing
193     /// [`Ordering::Relaxed`] as both the success and failure parameters.
194     /// For example, [`AtomicBool::compare_exchange_weak`].
195     pub fn atomic_cxchgweak_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
196     /// Stores a value if the current value is the same as the `old` value.
197     ///
198     /// The stabilized version of this intrinsic is available on the
199     /// [`atomic`] types via the `compare_exchange_weak` method by passing
200     /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
201     /// For example, [`AtomicBool::compare_exchange_weak`].
202     pub fn atomic_cxchgweak_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
203     /// Stores a value if the current value is the same as the `old` value.
204     ///
205     /// The stabilized version of this intrinsic is available on the
206     /// [`atomic`] types via the `compare_exchange_weak` method by passing
207     /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
208     /// For example, [`AtomicBool::compare_exchange_weak`].
209     pub fn atomic_cxchgweak_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
210     /// Stores a value if the current value is the same as the `old` value.
211     ///
212     /// The stabilized version of this intrinsic is available on the
213     /// [`atomic`] types via the `compare_exchange_weak` method by passing
214     /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
215     /// For example, [`AtomicBool::compare_exchange_weak`].
216     pub fn atomic_cxchgweak_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
217     /// Stores a value if the current value is the same as the `old` value.
218     ///
219     /// The stabilized version of this intrinsic is available on the
220     /// [`atomic`] types via the `compare_exchange_weak` method by passing
221     /// [`Ordering::Acquire`] as both the success and failure parameters.
222     /// For example, [`AtomicBool::compare_exchange_weak`].
223     pub fn atomic_cxchgweak_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
224     /// Stores a value if the current value is the same as the `old` value.
225     ///
226     /// The stabilized version of this intrinsic is available on the
227     /// [`atomic`] types via the `compare_exchange_weak` method by passing
228     /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
229     /// For example, [`AtomicBool::compare_exchange_weak`].
230     pub fn atomic_cxchgweak_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
231     /// Stores a value if the current value is the same as the `old` value.
232     ///
233     /// The stabilized version of this intrinsic is available on the
234     /// [`atomic`] types via the `compare_exchange_weak` method by passing
235     /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
236     /// For example, [`AtomicBool::compare_exchange_weak`].
237     pub fn atomic_cxchgweak_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
238     /// Stores a value if the current value is the same as the `old` value.
239     ///
240     /// The stabilized version of this intrinsic is available on the
241     /// [`atomic`] types via the `compare_exchange_weak` method by passing
242     /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
243     /// For example, [`AtomicBool::compare_exchange_weak`].
244     pub fn atomic_cxchgweak_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
245     /// Stores a value if the current value is the same as the `old` value.
246     ///
247     /// The stabilized version of this intrinsic is available on the
248     /// [`atomic`] types via the `compare_exchange_weak` method by passing
249     /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
250     /// For example, [`AtomicBool::compare_exchange_weak`].
251     pub fn atomic_cxchgweak_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
252     /// Stores a value if the current value is the same as the `old` value.
253     ///
254     /// The stabilized version of this intrinsic is available on the
255     /// [`atomic`] types via the `compare_exchange_weak` method by passing
256     /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
257     /// For example, [`AtomicBool::compare_exchange_weak`].
258     pub fn atomic_cxchgweak_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
259     /// Stores a value if the current value is the same as the `old` value.
260     ///
261     /// The stabilized version of this intrinsic is available on the
262     /// [`atomic`] types via the `compare_exchange_weak` method by passing
263     /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
264     /// For example, [`AtomicBool::compare_exchange_weak`].
265     pub fn atomic_cxchgweak_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
266     /// Stores a value if the current value is the same as the `old` value.
267     ///
268     /// The stabilized version of this intrinsic is available on the
269     /// [`atomic`] types via the `compare_exchange_weak` method by passing
270     /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
271     /// For example, [`AtomicBool::compare_exchange_weak`].
272     pub fn atomic_cxchgweak_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
273     /// Stores a value if the current value is the same as the `old` value.
274     ///
275     /// The stabilized version of this intrinsic is available on the
276     /// [`atomic`] types via the `compare_exchange_weak` method by passing
277     /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
278     /// For example, [`AtomicBool::compare_exchange_weak`].
279     pub fn atomic_cxchgweak_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
280     /// Stores a value if the current value is the same as the `old` value.
281     ///
282     /// The stabilized version of this intrinsic is available on the
283     /// [`atomic`] types via the `compare_exchange_weak` method by passing
284     /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
285     /// For example, [`AtomicBool::compare_exchange_weak`].
286     pub fn atomic_cxchgweak_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
287     /// Stores a value if the current value is the same as the `old` value.
288     ///
289     /// The stabilized version of this intrinsic is available on the
290     /// [`atomic`] types via the `compare_exchange_weak` method by passing
291     /// [`Ordering::SeqCst`] as both the success and failure parameters.
292     /// For example, [`AtomicBool::compare_exchange_weak`].
293     pub fn atomic_cxchgweak_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
294
295     /// Loads the current value of the pointer.
296     ///
297     /// The stabilized version of this intrinsic is available on the
298     /// [`atomic`] types via the `load` method by passing
299     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`].
300     pub fn atomic_load_seqcst<T: Copy>(src: *const T) -> T;
301     /// Loads the current value of the pointer.
302     ///
303     /// The stabilized version of this intrinsic is available on the
304     /// [`atomic`] types via the `load` method by passing
305     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`].
306     pub fn atomic_load_acquire<T: Copy>(src: *const T) -> T;
307     /// Loads the current value of the pointer.
308     ///
309     /// The stabilized version of this intrinsic is available on the
310     /// [`atomic`] types via the `load` method by passing
311     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`].
312     pub fn atomic_load_relaxed<T: Copy>(src: *const T) -> T;
313     pub fn atomic_load_unordered<T: Copy>(src: *const T) -> T;
314
315     /// Stores the value at the specified memory location.
316     ///
317     /// The stabilized version of this intrinsic is available on the
318     /// [`atomic`] types via the `store` method by passing
319     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`].
320     pub fn atomic_store_seqcst<T: Copy>(dst: *mut T, val: T);
321     /// Stores the value at the specified memory location.
322     ///
323     /// The stabilized version of this intrinsic is available on the
324     /// [`atomic`] types via the `store` method by passing
325     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`].
326     pub fn atomic_store_release<T: Copy>(dst: *mut T, val: T);
327     /// Stores the value at the specified memory location.
328     ///
329     /// The stabilized version of this intrinsic is available on the
330     /// [`atomic`] types via the `store` method by passing
331     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`].
332     pub fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T);
333     pub fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T);
334
335     /// Stores the value at the specified memory location, returning the old value.
336     ///
337     /// The stabilized version of this intrinsic is available on the
338     /// [`atomic`] types via the `swap` method by passing
339     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`].
340     pub fn atomic_xchg_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
341     /// Stores the value at the specified memory location, returning the old value.
342     ///
343     /// The stabilized version of this intrinsic is available on the
344     /// [`atomic`] types via the `swap` method by passing
345     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`].
346     pub fn atomic_xchg_acquire<T: Copy>(dst: *mut T, src: T) -> T;
347     /// Stores the value at the specified memory location, returning the old value.
348     ///
349     /// The stabilized version of this intrinsic is available on the
350     /// [`atomic`] types via the `swap` method by passing
351     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`].
352     pub fn atomic_xchg_release<T: Copy>(dst: *mut T, src: T) -> T;
353     /// Stores the value at the specified memory location, returning the old value.
354     ///
355     /// The stabilized version of this intrinsic is available on the
356     /// [`atomic`] types via the `swap` method by passing
357     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`].
358     pub fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
359     /// Stores the value at the specified memory location, returning the old value.
360     ///
361     /// The stabilized version of this intrinsic is available on the
362     /// [`atomic`] types via the `swap` method by passing
363     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`].
364     pub fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
365
366     /// Adds to the current value, returning the previous value.
367     ///
368     /// The stabilized version of this intrinsic is available on the
369     /// [`atomic`] types via the `fetch_add` method by passing
370     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`].
371     pub fn atomic_xadd_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
372     /// Adds to the current value, returning the previous value.
373     ///
374     /// The stabilized version of this intrinsic is available on the
375     /// [`atomic`] types via the `fetch_add` method by passing
376     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`].
377     pub fn atomic_xadd_acquire<T: Copy>(dst: *mut T, src: T) -> T;
378     /// Adds to the current value, returning the previous value.
379     ///
380     /// The stabilized version of this intrinsic is available on the
381     /// [`atomic`] types via the `fetch_add` method by passing
382     /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`].
383     pub fn atomic_xadd_release<T: Copy>(dst: *mut T, src: T) -> T;
384     /// Adds to the current value, returning the previous value.
385     ///
386     /// The stabilized version of this intrinsic is available on the
387     /// [`atomic`] types via the `fetch_add` method by passing
388     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`].
389     pub fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
390     /// Adds to the current value, returning the previous value.
391     ///
392     /// The stabilized version of this intrinsic is available on the
393     /// [`atomic`] types via the `fetch_add` method by passing
394     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`].
395     pub fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
396
397     /// Subtract from the current value, returning the previous value.
398     ///
399     /// The stabilized version of this intrinsic is available on the
400     /// [`atomic`] types via the `fetch_sub` method by passing
401     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
402     pub fn atomic_xsub_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
403     /// Subtract from the current value, returning the previous value.
404     ///
405     /// The stabilized version of this intrinsic is available on the
406     /// [`atomic`] types via the `fetch_sub` method by passing
407     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
408     pub fn atomic_xsub_acquire<T: Copy>(dst: *mut T, src: T) -> T;
409     /// Subtract from the current value, returning the previous value.
410     ///
411     /// The stabilized version of this intrinsic is available on the
412     /// [`atomic`] types via the `fetch_sub` method by passing
413     /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
414     pub fn atomic_xsub_release<T: Copy>(dst: *mut T, src: T) -> T;
415     /// Subtract from the current value, returning the previous value.
416     ///
417     /// The stabilized version of this intrinsic is available on the
418     /// [`atomic`] types via the `fetch_sub` method by passing
419     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
420     pub fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
421     /// Subtract from the current value, returning the previous value.
422     ///
423     /// The stabilized version of this intrinsic is available on the
424     /// [`atomic`] types via the `fetch_sub` method by passing
425     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
426     pub fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
427
428     /// Bitwise and with the current value, returning the previous value.
429     ///
430     /// The stabilized version of this intrinsic is available on the
431     /// [`atomic`] types via the `fetch_and` method by passing
432     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`].
433     pub fn atomic_and_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
434     /// Bitwise and with the current value, returning the previous value.
435     ///
436     /// The stabilized version of this intrinsic is available on the
437     /// [`atomic`] types via the `fetch_and` method by passing
438     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`].
439     pub fn atomic_and_acquire<T: Copy>(dst: *mut T, src: T) -> T;
440     /// Bitwise and with the current value, returning the previous value.
441     ///
442     /// The stabilized version of this intrinsic is available on the
443     /// [`atomic`] types via the `fetch_and` method by passing
444     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`].
445     pub fn atomic_and_release<T: Copy>(dst: *mut T, src: T) -> T;
446     /// Bitwise and with the current value, returning the previous value.
447     ///
448     /// The stabilized version of this intrinsic is available on the
449     /// [`atomic`] types via the `fetch_and` method by passing
450     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`].
451     pub fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
452     /// Bitwise and with the current value, returning the previous value.
453     ///
454     /// The stabilized version of this intrinsic is available on the
455     /// [`atomic`] types via the `fetch_and` method by passing
456     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`].
457     pub fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
458
459     /// Bitwise nand with the current value, returning the previous value.
460     ///
461     /// The stabilized version of this intrinsic is available on the
462     /// [`AtomicBool`] type via the `fetch_nand` method by passing
463     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`].
464     pub fn atomic_nand_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
465     /// Bitwise nand with the current value, returning the previous value.
466     ///
467     /// The stabilized version of this intrinsic is available on the
468     /// [`AtomicBool`] type via the `fetch_nand` method by passing
469     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`].
470     pub fn atomic_nand_acquire<T: Copy>(dst: *mut T, src: T) -> T;
471     /// Bitwise nand with the current value, returning the previous value.
472     ///
473     /// The stabilized version of this intrinsic is available on the
474     /// [`AtomicBool`] type via the `fetch_nand` method by passing
475     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`].
476     pub fn atomic_nand_release<T: Copy>(dst: *mut T, src: T) -> T;
477     /// Bitwise nand with the current value, returning the previous value.
478     ///
479     /// The stabilized version of this intrinsic is available on the
480     /// [`AtomicBool`] type via the `fetch_nand` method by passing
481     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`].
482     pub fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
483     /// Bitwise nand with the current value, returning the previous value.
484     ///
485     /// The stabilized version of this intrinsic is available on the
486     /// [`AtomicBool`] type via the `fetch_nand` method by passing
487     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`].
488     pub fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
489
490     /// Bitwise or with the current value, returning the previous value.
491     ///
492     /// The stabilized version of this intrinsic is available on the
493     /// [`atomic`] types via the `fetch_or` method by passing
494     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`].
495     pub fn atomic_or_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
496     /// Bitwise or with the current value, returning the previous value.
497     ///
498     /// The stabilized version of this intrinsic is available on the
499     /// [`atomic`] types via the `fetch_or` method by passing
500     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`].
501     pub fn atomic_or_acquire<T: Copy>(dst: *mut T, src: T) -> T;
502     /// Bitwise or with the current value, returning the previous value.
503     ///
504     /// The stabilized version of this intrinsic is available on the
505     /// [`atomic`] types via the `fetch_or` method by passing
506     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`].
507     pub fn atomic_or_release<T: Copy>(dst: *mut T, src: T) -> T;
508     /// Bitwise or with the current value, returning the previous value.
509     ///
510     /// The stabilized version of this intrinsic is available on the
511     /// [`atomic`] types via the `fetch_or` method by passing
512     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`].
513     pub fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
514     /// Bitwise or with the current value, returning the previous value.
515     ///
516     /// The stabilized version of this intrinsic is available on the
517     /// [`atomic`] types via the `fetch_or` method by passing
518     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`].
519     pub fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
520
521     /// Bitwise xor with the current value, returning the previous value.
522     ///
523     /// The stabilized version of this intrinsic is available on the
524     /// [`atomic`] types via the `fetch_xor` method by passing
525     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`].
526     pub fn atomic_xor_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
527     /// Bitwise xor with the current value, returning the previous value.
528     ///
529     /// The stabilized version of this intrinsic is available on the
530     /// [`atomic`] types via the `fetch_xor` method by passing
531     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`].
532     pub fn atomic_xor_acquire<T: Copy>(dst: *mut T, src: T) -> T;
533     /// Bitwise xor with the current value, returning the previous value.
534     ///
535     /// The stabilized version of this intrinsic is available on the
536     /// [`atomic`] types via the `fetch_xor` method by passing
537     /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`].
538     pub fn atomic_xor_release<T: Copy>(dst: *mut T, src: T) -> T;
539     /// Bitwise xor with the current value, returning the previous value.
540     ///
541     /// The stabilized version of this intrinsic is available on the
542     /// [`atomic`] types via the `fetch_xor` method by passing
543     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`].
544     pub fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
545     /// Bitwise xor with the current value, returning the previous value.
546     ///
547     /// The stabilized version of this intrinsic is available on the
548     /// [`atomic`] types via the `fetch_xor` method by passing
549     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`].
550     pub fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
551
552     /// Maximum with the current value using a signed comparison.
553     ///
554     /// The stabilized version of this intrinsic is available on the
555     /// [`atomic`] signed integer types via the `fetch_max` method by passing
556     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`].
557     pub fn atomic_max_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
558     /// Maximum with the current value using a signed comparison.
559     ///
560     /// The stabilized version of this intrinsic is available on the
561     /// [`atomic`] signed integer types via the `fetch_max` method by passing
562     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`].
563     pub fn atomic_max_acquire<T: Copy>(dst: *mut T, src: T) -> T;
564     /// Maximum with the current value using a signed comparison.
565     ///
566     /// The stabilized version of this intrinsic is available on the
567     /// [`atomic`] signed integer types via the `fetch_max` method by passing
568     /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`].
569     pub fn atomic_max_release<T: Copy>(dst: *mut T, src: T) -> T;
570     /// Maximum with the current value using a signed comparison.
571     ///
572     /// The stabilized version of this intrinsic is available on the
573     /// [`atomic`] signed integer types via the `fetch_max` method by passing
574     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`].
575     pub fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
576     /// Maximum with the current value.
577     ///
578     /// The stabilized version of this intrinsic is available on the
579     /// [`atomic`] signed integer types via the `fetch_max` method by passing
580     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`].
581     pub fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
582
583     /// Minimum with the current value using a signed comparison.
584     ///
585     /// The stabilized version of this intrinsic is available on the
586     /// [`atomic`] signed integer types via the `fetch_min` method by passing
587     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`].
588     pub fn atomic_min_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
589     /// Minimum with the current value using a signed comparison.
590     ///
591     /// The stabilized version of this intrinsic is available on the
592     /// [`atomic`] signed integer types via the `fetch_min` method by passing
593     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`].
594     pub fn atomic_min_acquire<T: Copy>(dst: *mut T, src: T) -> T;
595     /// Minimum with the current value using a signed comparison.
596     ///
597     /// The stabilized version of this intrinsic is available on the
598     /// [`atomic`] signed integer types via the `fetch_min` method by passing
599     /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`].
600     pub fn atomic_min_release<T: Copy>(dst: *mut T, src: T) -> T;
601     /// Minimum with the current value using a signed comparison.
602     ///
603     /// The stabilized version of this intrinsic is available on the
604     /// [`atomic`] signed integer types via the `fetch_min` method by passing
605     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`].
606     pub fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
607     /// Minimum with the current value using a signed comparison.
608     ///
609     /// The stabilized version of this intrinsic is available on the
610     /// [`atomic`] signed integer types via the `fetch_min` method by passing
611     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`].
612     pub fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
613
614     /// Minimum with the current value using an unsigned comparison.
615     ///
616     /// The stabilized version of this intrinsic is available on the
617     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
618     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`].
619     pub fn atomic_umin_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
620     /// Minimum with the current value using an unsigned comparison.
621     ///
622     /// The stabilized version of this intrinsic is available on the
623     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
624     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`].
625     pub fn atomic_umin_acquire<T: Copy>(dst: *mut T, src: T) -> T;
626     /// Minimum with the current value using an unsigned comparison.
627     ///
628     /// The stabilized version of this intrinsic is available on the
629     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
630     /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`].
631     pub fn atomic_umin_release<T: Copy>(dst: *mut T, src: T) -> T;
632     /// Minimum with the current value using an unsigned comparison.
633     ///
634     /// The stabilized version of this intrinsic is available on the
635     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
636     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`].
637     pub fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
638     /// Minimum with the current value using an unsigned comparison.
639     ///
640     /// The stabilized version of this intrinsic is available on the
641     /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
642     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`].
643     pub fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
644
645     /// Maximum with the current value using an unsigned comparison.
646     ///
647     /// The stabilized version of this intrinsic is available on the
648     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
649     /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`].
650     pub fn atomic_umax_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
651     /// Maximum with the current value using an unsigned comparison.
652     ///
653     /// The stabilized version of this intrinsic is available on the
654     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
655     /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`].
656     pub fn atomic_umax_acquire<T: Copy>(dst: *mut T, src: T) -> T;
657     /// Maximum with the current value using an unsigned comparison.
658     ///
659     /// The stabilized version of this intrinsic is available on the
660     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
661     /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`].
662     pub fn atomic_umax_release<T: Copy>(dst: *mut T, src: T) -> T;
663     /// Maximum with the current value using an unsigned comparison.
664     ///
665     /// The stabilized version of this intrinsic is available on the
666     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
667     /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`].
668     pub fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
669     /// Maximum with the current value using an unsigned comparison.
670     ///
671     /// The stabilized version of this intrinsic is available on the
672     /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
673     /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`].
674     pub fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
675
676     /// An atomic fence.
677     ///
678     /// The stabilized version of this intrinsic is available in
679     /// [`atomic::fence`] by passing [`Ordering::SeqCst`]
680     /// as the `order`.
681     pub fn atomic_fence_seqcst();
682     /// An atomic fence.
683     ///
684     /// The stabilized version of this intrinsic is available in
685     /// [`atomic::fence`] by passing [`Ordering::Acquire`]
686     /// as the `order`.
687     pub fn atomic_fence_acquire();
688     /// An atomic fence.
689     ///
690     /// The stabilized version of this intrinsic is available in
691     /// [`atomic::fence`] by passing [`Ordering::Release`]
692     /// as the `order`.
693     pub fn atomic_fence_release();
694     /// An atomic fence.
695     ///
696     /// The stabilized version of this intrinsic is available in
697     /// [`atomic::fence`] by passing [`Ordering::AcqRel`]
698     /// as the `order`.
699     pub fn atomic_fence_acqrel();
700
701     /// A compiler-only memory barrier.
702     ///
703     /// Memory accesses will never be reordered across this barrier by the
704     /// compiler, but no instructions will be emitted for it. This is
705     /// appropriate for operations on the same thread that may be preempted,
706     /// such as when interacting with signal handlers.
707     ///
708     /// The stabilized version of this intrinsic is available in
709     /// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`]
710     /// as the `order`.
711     pub fn atomic_singlethreadfence_seqcst();
712     /// A compiler-only memory barrier.
713     ///
714     /// Memory accesses will never be reordered across this barrier by the
715     /// compiler, but no instructions will be emitted for it. This is
716     /// appropriate for operations on the same thread that may be preempted,
717     /// such as when interacting with signal handlers.
718     ///
719     /// The stabilized version of this intrinsic is available in
720     /// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`]
721     /// as the `order`.
722     pub fn atomic_singlethreadfence_acquire();
723     /// A compiler-only memory barrier.
724     ///
725     /// Memory accesses will never be reordered across this barrier by the
726     /// compiler, but no instructions will be emitted for it. This is
727     /// appropriate for operations on the same thread that may be preempted,
728     /// such as when interacting with signal handlers.
729     ///
730     /// The stabilized version of this intrinsic is available in
731     /// [`atomic::compiler_fence`] by passing [`Ordering::Release`]
732     /// as the `order`.
733     pub fn atomic_singlethreadfence_release();
734     /// A compiler-only memory barrier.
735     ///
736     /// Memory accesses will never be reordered across this barrier by the
737     /// compiler, but no instructions will be emitted for it. This is
738     /// appropriate for operations on the same thread that may be preempted,
739     /// such as when interacting with signal handlers.
740     ///
741     /// The stabilized version of this intrinsic is available in
742     /// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`]
743     /// as the `order`.
744     pub fn atomic_singlethreadfence_acqrel();
745
746     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
747     /// if supported; otherwise, it is a no-op.
748     /// Prefetches have no effect on the behavior of the program but can change its performance
749     /// characteristics.
750     ///
751     /// The `locality` argument must be a constant integer and is a temporal locality specifier
752     /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
753     ///
754     /// This intrinsic does not have a stable counterpart.
755     pub fn prefetch_read_data<T>(data: *const T, locality: i32);
756     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
757     /// if supported; otherwise, it is a no-op.
758     /// Prefetches have no effect on the behavior of the program but can change its performance
759     /// characteristics.
760     ///
761     /// The `locality` argument must be a constant integer and is a temporal locality specifier
762     /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
763     ///
764     /// This intrinsic does not have a stable counterpart.
765     pub fn prefetch_write_data<T>(data: *const T, locality: i32);
766     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
767     /// if supported; otherwise, it is a no-op.
768     /// Prefetches have no effect on the behavior of the program but can change its performance
769     /// characteristics.
770     ///
771     /// The `locality` argument must be a constant integer and is a temporal locality specifier
772     /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
773     ///
774     /// This intrinsic does not have a stable counterpart.
775     pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
776     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
777     /// if supported; otherwise, it is a no-op.
778     /// Prefetches have no effect on the behavior of the program but can change its performance
779     /// characteristics.
780     ///
781     /// The `locality` argument must be a constant integer and is a temporal locality specifier
782     /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
783     ///
784     /// This intrinsic does not have a stable counterpart.
785     pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
786
787     /// Magic intrinsic that derives its meaning from attributes
788     /// attached to the function.
789     ///
790     /// For example, dataflow uses this to inject static assertions so
791     /// that `rustc_peek(potentially_uninitialized)` would actually
792     /// double-check that dataflow did indeed compute that it is
793     /// uninitialized at that point in the control flow.
794     ///
795     /// This intrinsic should not be used outside of the compiler.
796     #[rustc_safe_intrinsic]
797     pub fn rustc_peek<T>(_: T) -> T;
798
799     /// Aborts the execution of the process.
800     ///
801     /// Note that, unlike most intrinsics, this is safe to call;
802     /// it does not require an `unsafe` block.
803     /// Therefore, implementations must not require the user to uphold
804     /// any safety invariants.
805     ///
806     /// [`std::process::abort`](../../std/process/fn.abort.html) is to be preferred if possible,
807     /// as its behavior is more user-friendly and more stable.
808     ///
809     /// The current implementation of `intrinsics::abort` is to invoke an invalid instruction,
810     /// on most platforms.
811     /// On Unix, the
812     /// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or
813     /// `SIGBUS`.  The precise behaviour is not guaranteed and not stable.
814     #[rustc_safe_intrinsic]
815     pub fn abort() -> !;
816
817     /// Informs the optimizer that this point in the code is not reachable,
818     /// enabling further optimizations.
819     ///
820     /// N.B., this is very different from the `unreachable!()` macro: Unlike the
821     /// macro, which panics when it is executed, it is *undefined behavior* to
822     /// reach code marked with this function.
823     ///
824     /// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`].
825     #[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
826     pub fn unreachable() -> !;
827
828     /// Informs the optimizer that a condition is always true.
829     /// If the condition is false, the behavior is undefined.
830     ///
831     /// No code is generated for this intrinsic, but the optimizer will try
832     /// to preserve it (and its condition) between passes, which may interfere
833     /// with optimization of surrounding code and reduce performance. It should
834     /// not be used if the invariant can be discovered by the optimizer on its
835     /// own, or if it does not enable any significant optimizations.
836     ///
837     /// This intrinsic does not have a stable counterpart.
838     #[rustc_const_unstable(feature = "const_assume", issue = "76972")]
839     pub fn assume(b: bool);
840
841     /// Hints to the compiler that branch condition is likely to be true.
842     /// Returns the value passed to it.
843     ///
844     /// Any use other than with `if` statements will probably not have an effect.
845     ///
846     /// Note that, unlike most intrinsics, this is safe to call;
847     /// it does not require an `unsafe` block.
848     /// Therefore, implementations must not require the user to uphold
849     /// any safety invariants.
850     ///
851     /// This intrinsic does not have a stable counterpart.
852     #[rustc_const_unstable(feature = "const_likely", issue = "none")]
853     #[rustc_safe_intrinsic]
854     pub fn likely(b: bool) -> bool;
855
856     /// Hints to the compiler that branch condition is likely to be false.
857     /// Returns the value passed to it.
858     ///
859     /// Any use other than with `if` statements will probably not have an effect.
860     ///
861     /// Note that, unlike most intrinsics, this is safe to call;
862     /// it does not require an `unsafe` block.
863     /// Therefore, implementations must not require the user to uphold
864     /// any safety invariants.
865     ///
866     /// This intrinsic does not have a stable counterpart.
867     #[rustc_const_unstable(feature = "const_likely", issue = "none")]
868     #[rustc_safe_intrinsic]
869     pub fn unlikely(b: bool) -> bool;
870
871     /// Executes a breakpoint trap, for inspection by a debugger.
872     ///
873     /// This intrinsic does not have a stable counterpart.
874     pub fn breakpoint();
875
876     /// The size of a type in bytes.
877     ///
878     /// Note that, unlike most intrinsics, this is safe to call;
879     /// it does not require an `unsafe` block.
880     /// Therefore, implementations must not require the user to uphold
881     /// any safety invariants.
882     ///
883     /// More specifically, this is the offset in bytes between successive
884     /// items of the same type, including alignment padding.
885     ///
886     /// The stabilized version of this intrinsic is [`core::mem::size_of`].
887     #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
888     #[rustc_safe_intrinsic]
889     pub fn size_of<T>() -> usize;
890
891     /// The minimum alignment of a type.
892     ///
893     /// Note that, unlike most intrinsics, this is safe to call;
894     /// it does not require an `unsafe` block.
895     /// Therefore, implementations must not require the user to uphold
896     /// any safety invariants.
897     ///
898     /// The stabilized version of this intrinsic is [`core::mem::align_of`].
899     #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
900     #[rustc_safe_intrinsic]
901     pub fn min_align_of<T>() -> usize;
902     /// The preferred alignment of a type.
903     ///
904     /// This intrinsic does not have a stable counterpart.
905     /// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
906     #[rustc_const_unstable(feature = "const_pref_align_of", issue = "91971")]
907     pub fn pref_align_of<T>() -> usize;
908
909     /// The size of the referenced value in bytes.
910     ///
911     /// The stabilized version of this intrinsic is [`mem::size_of_val`].
912     #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
913     pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
914     /// The required alignment of the referenced value.
915     ///
916     /// The stabilized version of this intrinsic is [`core::mem::align_of_val`].
917     #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
918     pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
919
920     /// Gets a static string slice containing the name of a type.
921     ///
922     /// Note that, unlike most intrinsics, this is safe to call;
923     /// it does not require an `unsafe` block.
924     /// Therefore, implementations must not require the user to uphold
925     /// any safety invariants.
926     ///
927     /// The stabilized version of this intrinsic is [`core::any::type_name`].
928     #[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
929     #[rustc_safe_intrinsic]
930     pub fn type_name<T: ?Sized>() -> &'static str;
931
932     /// Gets an identifier which is globally unique to the specified type. This
933     /// function will return the same value for a type regardless of whichever
934     /// crate it is invoked in.
935     ///
936     /// Note that, unlike most intrinsics, this is safe to call;
937     /// it does not require an `unsafe` block.
938     /// Therefore, implementations must not require the user to uphold
939     /// any safety invariants.
940     ///
941     /// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
942     #[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
943     #[rustc_safe_intrinsic]
944     pub fn type_id<T: ?Sized + 'static>() -> u64;
945
946     /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
947     /// This will statically either panic, or do nothing.
948     ///
949     /// This intrinsic does not have a stable counterpart.
950     #[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
951     #[rustc_safe_intrinsic]
952     pub fn assert_inhabited<T>();
953
954     /// A guard for unsafe functions that cannot ever be executed if `T` does not permit
955     /// zero-initialization: This will statically either panic, or do nothing.
956     ///
957     /// This intrinsic does not have a stable counterpart.
958     #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
959     #[rustc_safe_intrinsic]
960     pub fn assert_zero_valid<T>();
961
962     /// A guard for `std::mem::uninitialized`. Checks whether a repeated bit pattern `0x01`
963     /// is legal for `T`: This will statically either panic, or do nothing.
964     ///
965     /// This intrinsic does not have a stable counterpart.
966     #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
967     #[rustc_safe_intrinsic]
968     #[cfg(not(bootstrap))]
969     pub fn assert_mem_uninitialized_valid<T>();
970
971     /// Gets a reference to a static `Location` indicating where it was called.
972     ///
973     /// Note that, unlike most intrinsics, this is safe to call;
974     /// it does not require an `unsafe` block.
975     /// Therefore, implementations must not require the user to uphold
976     /// any safety invariants.
977     ///
978     /// Consider using [`core::panic::Location::caller`] instead.
979     #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
980     #[rustc_safe_intrinsic]
981     pub fn caller_location() -> &'static crate::panic::Location<'static>;
982
983     /// Moves a value out of scope without running drop glue.
984     ///
985     /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses
986     /// `ManuallyDrop` instead.
987     ///
988     /// Note that, unlike most intrinsics, this is safe to call;
989     /// it does not require an `unsafe` block.
990     /// Therefore, implementations must not require the user to uphold
991     /// any safety invariants.
992     #[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")]
993     #[rustc_safe_intrinsic]
994     pub fn forget<T: ?Sized>(_: T);
995
996     /// Reinterprets the bits of a value of one type as another type.
997     ///
998     /// Both types must have the same size. Compilation will fail if this is not guaranteed.
999     ///
1000     /// `transmute` is semantically equivalent to a bitwise move of one type
1001     /// into another. It copies the bits from the source value into the
1002     /// destination value, then forgets the original. Note that source and destination
1003     /// are passed by-value, which means if `Src` or `Dst` contain padding, that padding
1004     /// is *not* guaranteed to be preserved by `transmute`.
1005     ///
1006     /// Both the argument and the result must be [valid](../../nomicon/what-unsafe-does.html) at
1007     /// their given type. Violating this condition leads to [undefined behavior][ub]. The compiler
1008     /// will generate code *assuming that you, the programmer, ensure that there will never be
1009     /// undefined behavior*. It is therefore your responsibility to guarantee that every value
1010     /// passed to `transmute` is valid at both types `Src` and `Dst`. Failing to uphold this condition
1011     /// may lead to unexpected and unstable compilation results. This makes `transmute` **incredibly
1012     /// unsafe**. `transmute` should be the absolute last resort.
1013     ///
1014     /// Transmuting pointers to integers in a `const` context is [undefined behavior][ub].
1015     /// Any attempt to use the resulting value for integer operations will abort const-evaluation.
1016     /// (And even outside `const`, such transmutation is touching on many unspecified aspects of the
1017     /// Rust memory model and should be avoided. See below for alternatives.)
1018     ///
1019     /// Because `transmute` is a by-value operation, alignment of the *transmuted values
1020     /// themselves* is not a concern. As with any other function, the compiler already ensures
1021     /// both `Src` and `Dst` are properly aligned. However, when transmuting values that *point
1022     /// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper
1023     /// alignment of the pointed-to values.
1024     ///
1025     /// The [nomicon](../../nomicon/transmutes.html) has additional documentation.
1026     ///
1027     /// [ub]: ../../reference/behavior-considered-undefined.html
1028     ///
1029     /// # Examples
1030     ///
1031     /// There are a few things that `transmute` is really useful for.
1032     ///
1033     /// Turning a pointer into a function pointer. This is *not* portable to
1034     /// machines where function pointers and data pointers have different sizes.
1035     ///
1036     /// ```
1037     /// fn foo() -> i32 {
1038     ///     0
1039     /// }
1040     /// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer.
1041     /// // This avoids an integer-to-pointer `transmute`, which can be problematic.
1042     /// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine.
1043     /// let pointer = foo as *const ();
1044     /// let function = unsafe {
1045     ///     std::mem::transmute::<*const (), fn() -> i32>(pointer)
1046     /// };
1047     /// assert_eq!(function(), 0);
1048     /// ```
1049     ///
1050     /// Extending a lifetime, or shortening an invariant lifetime. This is
1051     /// advanced, very unsafe Rust!
1052     ///
1053     /// ```
1054     /// struct R<'a>(&'a i32);
1055     /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
1056     ///     std::mem::transmute::<R<'b>, R<'static>>(r)
1057     /// }
1058     ///
1059     /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
1060     ///                                              -> &'b mut R<'c> {
1061     ///     std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
1062     /// }
1063     /// ```
1064     ///
1065     /// # Alternatives
1066     ///
1067     /// Don't despair: many uses of `transmute` can be achieved through other means.
1068     /// Below are common applications of `transmute` which can be replaced with safer
1069     /// constructs.
1070     ///
1071     /// Turning raw bytes (`&[u8]`) into `u32`, `f64`, etc.:
1072     ///
1073     /// ```
1074     /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
1075     ///
1076     /// let num = unsafe {
1077     ///     std::mem::transmute::<[u8; 4], u32>(raw_bytes)
1078     /// };
1079     ///
1080     /// // use `u32::from_ne_bytes` instead
1081     /// let num = u32::from_ne_bytes(raw_bytes);
1082     /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
1083     /// let num = u32::from_le_bytes(raw_bytes);
1084     /// assert_eq!(num, 0x12345678);
1085     /// let num = u32::from_be_bytes(raw_bytes);
1086     /// assert_eq!(num, 0x78563412);
1087     /// ```
1088     ///
1089     /// Turning a pointer into a `usize`:
1090     ///
1091     /// ```no_run
1092     /// let ptr = &0;
1093     /// let ptr_num_transmute = unsafe {
1094     ///     std::mem::transmute::<&i32, usize>(ptr)
1095     /// };
1096     ///
1097     /// // Use an `as` cast instead
1098     /// let ptr_num_cast = ptr as *const i32 as usize;
1099     /// ```
1100     ///
1101     /// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined
1102     /// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave
1103     /// as expected -- this is touching on many unspecified aspects of the Rust memory model.
1104     /// Depending on what the code is doing, the following alternatives are preferable to
1105     /// pointer-to-integer transmutation:
1106     /// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a
1107     ///   type for that buffer, it can use [`MaybeUninit`][mem::MaybeUninit].
1108     /// - If the code actually wants to work on the address the pointer points to, it can use `as`
1109     ///   casts or [`ptr.addr()`][pointer::addr].
1110     ///
1111     /// Turning a `*mut T` into an `&mut T`:
1112     ///
1113     /// ```
1114     /// let ptr: *mut i32 = &mut 0;
1115     /// let ref_transmuted = unsafe {
1116     ///     std::mem::transmute::<*mut i32, &mut i32>(ptr)
1117     /// };
1118     ///
1119     /// // Use a reborrow instead
1120     /// let ref_casted = unsafe { &mut *ptr };
1121     /// ```
1122     ///
1123     /// Turning an `&mut T` into an `&mut U`:
1124     ///
1125     /// ```
1126     /// let ptr = &mut 0;
1127     /// let val_transmuted = unsafe {
1128     ///     std::mem::transmute::<&mut i32, &mut u32>(ptr)
1129     /// };
1130     ///
1131     /// // Now, put together `as` and reborrowing - note the chaining of `as`
1132     /// // `as` is not transitive
1133     /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
1134     /// ```
1135     ///
1136     /// Turning an `&str` into a `&[u8]`:
1137     ///
1138     /// ```
1139     /// // this is not a good way to do this.
1140     /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
1141     /// assert_eq!(slice, &[82, 117, 115, 116]);
1142     ///
1143     /// // You could use `str::as_bytes`
1144     /// let slice = "Rust".as_bytes();
1145     /// assert_eq!(slice, &[82, 117, 115, 116]);
1146     ///
1147     /// // Or, just use a byte string, if you have control over the string
1148     /// // literal
1149     /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
1150     /// ```
1151     ///
1152     /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`.
1153     ///
1154     /// To transmute the inner type of the contents of a container, you must make sure to not
1155     /// violate any of the container's invariants. For `Vec`, this means that both the size
1156     /// *and alignment* of the inner types have to match. Other containers might rely on the
1157     /// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't
1158     /// be possible at all without violating the container invariants.
1159     ///
1160     /// ```
1161     /// let store = [0, 1, 2, 3];
1162     /// let v_orig = store.iter().collect::<Vec<&i32>>();
1163     ///
1164     /// // clone the vector as we will reuse them later
1165     /// let v_clone = v_orig.clone();
1166     ///
1167     /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
1168     /// // bad idea and could cause Undefined Behavior.
1169     /// // However, it is no-copy.
1170     /// let v_transmuted = unsafe {
1171     ///     std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
1172     /// };
1173     ///
1174     /// let v_clone = v_orig.clone();
1175     ///
1176     /// // This is the suggested, safe way.
1177     /// // It does copy the entire vector, though, into a new array.
1178     /// let v_collected = v_clone.into_iter()
1179     ///                          .map(Some)
1180     ///                          .collect::<Vec<Option<&i32>>>();
1181     ///
1182     /// let v_clone = v_orig.clone();
1183     ///
1184     /// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the
1185     /// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but
1186     /// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`),
1187     /// // this has all the same caveats. Besides the information provided above, also consult the
1188     /// // [`from_raw_parts`] documentation.
1189     /// let v_from_raw = unsafe {
1190     // FIXME Update this when vec_into_raw_parts is stabilized
1191     ///     // Ensure the original vector is not dropped.
1192     ///     let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
1193     ///     Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
1194     ///                         v_clone.len(),
1195     ///                         v_clone.capacity())
1196     /// };
1197     /// ```
1198     ///
1199     /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
1200     ///
1201     /// Implementing `split_at_mut`:
1202     ///
1203     /// ```
1204     /// use std::{slice, mem};
1205     ///
1206     /// // There are multiple ways to do this, and there are multiple problems
1207     /// // with the following (transmute) way.
1208     /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
1209     ///                              -> (&mut [T], &mut [T]) {
1210     ///     let len = slice.len();
1211     ///     assert!(mid <= len);
1212     ///     unsafe {
1213     ///         let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
1214     ///         // first: transmute is not type safe; all it checks is that T and
1215     ///         // U are of the same size. Second, right here, you have two
1216     ///         // mutable references pointing to the same memory.
1217     ///         (&mut slice[0..mid], &mut slice2[mid..len])
1218     ///     }
1219     /// }
1220     ///
1221     /// // This gets rid of the type safety problems; `&mut *` will *only* give
1222     /// // you an `&mut T` from an `&mut T` or `*mut T`.
1223     /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
1224     ///                          -> (&mut [T], &mut [T]) {
1225     ///     let len = slice.len();
1226     ///     assert!(mid <= len);
1227     ///     unsafe {
1228     ///         let slice2 = &mut *(slice as *mut [T]);
1229     ///         // however, you still have two mutable references pointing to
1230     ///         // the same memory.
1231     ///         (&mut slice[0..mid], &mut slice2[mid..len])
1232     ///     }
1233     /// }
1234     ///
1235     /// // This is how the standard library does it. This is the best method, if
1236     /// // you need to do something like this
1237     /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
1238     ///                       -> (&mut [T], &mut [T]) {
1239     ///     let len = slice.len();
1240     ///     assert!(mid <= len);
1241     ///     unsafe {
1242     ///         let ptr = slice.as_mut_ptr();
1243     ///         // This now has three mutable references pointing at the same
1244     ///         // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
1245     ///         // `slice` is never used after `let ptr = ...`, and so one can
1246     ///         // treat it as "dead", and therefore, you only have two real
1247     ///         // mutable slices.
1248     ///         (slice::from_raw_parts_mut(ptr, mid),
1249     ///          slice::from_raw_parts_mut(ptr.add(mid), len - mid))
1250     ///     }
1251     /// }
1252     /// ```
1253     #[stable(feature = "rust1", since = "1.0.0")]
1254     #[rustc_allowed_through_unstable_modules]
1255     #[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
1256     #[rustc_diagnostic_item = "transmute"]
1257     pub fn transmute<Src, Dst>(src: Src) -> Dst;
1258
1259     /// Returns `true` if the actual type given as `T` requires drop
1260     /// glue; returns `false` if the actual type provided for `T`
1261     /// implements `Copy`.
1262     ///
1263     /// If the actual type neither requires drop glue nor implements
1264     /// `Copy`, then the return value of this function is unspecified.
1265     ///
1266     /// Note that, unlike most intrinsics, this is safe to call;
1267     /// it does not require an `unsafe` block.
1268     /// Therefore, implementations must not require the user to uphold
1269     /// any safety invariants.
1270     ///
1271     /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1272     #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1273     #[rustc_safe_intrinsic]
1274     pub fn needs_drop<T: ?Sized>() -> bool;
1275
1276     /// Calculates the offset from a pointer.
1277     ///
1278     /// This is implemented as an intrinsic to avoid converting to and from an
1279     /// integer, since the conversion would throw away aliasing information.
1280     ///
1281     /// # Safety
1282     ///
1283     /// Both the starting and resulting pointer must be either in bounds or one
1284     /// byte past the end of an allocated object. If either pointer is out of
1285     /// bounds or arithmetic overflow occurs then any further use of the
1286     /// returned value will result in undefined behavior.
1287     ///
1288     /// The stabilized version of this intrinsic is [`pointer::offset`].
1289     #[must_use = "returns a new pointer rather than modifying its argument"]
1290     #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1291     pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1292
1293     /// Calculates the offset from a pointer, potentially wrapping.
1294     ///
1295     /// This is implemented as an intrinsic to avoid converting to and from an
1296     /// integer, since the conversion inhibits certain optimizations.
1297     ///
1298     /// # Safety
1299     ///
1300     /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1301     /// resulting pointer to point into or one byte past the end of an allocated
1302     /// object, and it wraps with two's complement arithmetic. The resulting
1303     /// value is not necessarily valid to be used to actually access memory.
1304     ///
1305     /// The stabilized version of this intrinsic is [`pointer::wrapping_offset`].
1306     #[must_use = "returns a new pointer rather than modifying its argument"]
1307     #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1308     pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1309
1310     /// Masks out bits of the pointer according to a mask.
1311     ///
1312     /// Note that, unlike most intrinsics, this is safe to call;
1313     /// it does not require an `unsafe` block.
1314     /// Therefore, implementations must not require the user to uphold
1315     /// any safety invariants.
1316     ///
1317     /// Consider using [`pointer::mask`] instead.
1318     #[rustc_safe_intrinsic]
1319     pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;
1320
1321     /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1322     /// a size of `count` * `size_of::<T>()` and an alignment of
1323     /// `min_align_of::<T>()`
1324     ///
1325     /// The volatile parameter is set to `true`, so it will not be optimized out
1326     /// unless size is equal to zero.
1327     ///
1328     /// This intrinsic does not have a stable counterpart.
1329     pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1330     /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1331     /// a size of `count * size_of::<T>()` and an alignment of
1332     /// `min_align_of::<T>()`
1333     ///
1334     /// The volatile parameter is set to `true`, so it will not be optimized out
1335     /// unless size is equal to zero.
1336     ///
1337     /// This intrinsic does not have a stable counterpart.
1338     pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1339     /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1340     /// size of `count * size_of::<T>()` and an alignment of
1341     /// `min_align_of::<T>()`.
1342     ///
1343     /// The volatile parameter is set to `true`, so it will not be optimized out
1344     /// unless size is equal to zero.
1345     ///
1346     /// This intrinsic does not have a stable counterpart.
1347     pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1348
1349     /// Performs a volatile load from the `src` pointer.
1350     ///
1351     /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`].
1352     pub fn volatile_load<T>(src: *const T) -> T;
1353     /// Performs a volatile store to the `dst` pointer.
1354     ///
1355     /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`].
1356     pub fn volatile_store<T>(dst: *mut T, val: T);
1357
1358     /// Performs a volatile load from the `src` pointer
1359     /// The pointer is not required to be aligned.
1360     ///
1361     /// This intrinsic does not have a stable counterpart.
1362     pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1363     /// Performs a volatile store to the `dst` pointer.
1364     /// The pointer is not required to be aligned.
1365     ///
1366     /// This intrinsic does not have a stable counterpart.
1367     pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1368
1369     /// Returns the square root of an `f32`
1370     ///
1371     /// The stabilized version of this intrinsic is
1372     /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1373     pub fn sqrtf32(x: f32) -> f32;
1374     /// Returns the square root of an `f64`
1375     ///
1376     /// The stabilized version of this intrinsic is
1377     /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1378     pub fn sqrtf64(x: f64) -> f64;
1379
1380     /// Raises an `f32` to an integer power.
1381     ///
1382     /// The stabilized version of this intrinsic is
1383     /// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1384     pub fn powif32(a: f32, x: i32) -> f32;
1385     /// Raises an `f64` to an integer power.
1386     ///
1387     /// The stabilized version of this intrinsic is
1388     /// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1389     pub fn powif64(a: f64, x: i32) -> f64;
1390
1391     /// Returns the sine of an `f32`.
1392     ///
1393     /// The stabilized version of this intrinsic is
1394     /// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1395     pub fn sinf32(x: f32) -> f32;
1396     /// Returns the sine of an `f64`.
1397     ///
1398     /// The stabilized version of this intrinsic is
1399     /// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1400     pub fn sinf64(x: f64) -> f64;
1401
1402     /// Returns the cosine of an `f32`.
1403     ///
1404     /// The stabilized version of this intrinsic is
1405     /// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1406     pub fn cosf32(x: f32) -> f32;
1407     /// Returns the cosine of an `f64`.
1408     ///
1409     /// The stabilized version of this intrinsic is
1410     /// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1411     pub fn cosf64(x: f64) -> f64;
1412
1413     /// Raises an `f32` to an `f32` power.
1414     ///
1415     /// The stabilized version of this intrinsic is
1416     /// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1417     pub fn powf32(a: f32, x: f32) -> f32;
1418     /// Raises an `f64` to an `f64` power.
1419     ///
1420     /// The stabilized version of this intrinsic is
1421     /// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1422     pub fn powf64(a: f64, x: f64) -> f64;
1423
1424     /// Returns the exponential of an `f32`.
1425     ///
1426     /// The stabilized version of this intrinsic is
1427     /// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1428     pub fn expf32(x: f32) -> f32;
1429     /// Returns the exponential of an `f64`.
1430     ///
1431     /// The stabilized version of this intrinsic is
1432     /// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1433     pub fn expf64(x: f64) -> f64;
1434
1435     /// Returns 2 raised to the power of an `f32`.
1436     ///
1437     /// The stabilized version of this intrinsic is
1438     /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1439     pub fn exp2f32(x: f32) -> f32;
1440     /// Returns 2 raised to the power of an `f64`.
1441     ///
1442     /// The stabilized version of this intrinsic is
1443     /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1444     pub fn exp2f64(x: f64) -> f64;
1445
1446     /// Returns the natural logarithm of an `f32`.
1447     ///
1448     /// The stabilized version of this intrinsic is
1449     /// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1450     pub fn logf32(x: f32) -> f32;
1451     /// Returns the natural logarithm of an `f64`.
1452     ///
1453     /// The stabilized version of this intrinsic is
1454     /// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1455     pub fn logf64(x: f64) -> f64;
1456
1457     /// Returns the base 10 logarithm of an `f32`.
1458     ///
1459     /// The stabilized version of this intrinsic is
1460     /// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1461     pub fn log10f32(x: f32) -> f32;
1462     /// Returns the base 10 logarithm of an `f64`.
1463     ///
1464     /// The stabilized version of this intrinsic is
1465     /// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1466     pub fn log10f64(x: f64) -> f64;
1467
1468     /// Returns the base 2 logarithm of an `f32`.
1469     ///
1470     /// The stabilized version of this intrinsic is
1471     /// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1472     pub fn log2f32(x: f32) -> f32;
1473     /// Returns the base 2 logarithm of an `f64`.
1474     ///
1475     /// The stabilized version of this intrinsic is
1476     /// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1477     pub fn log2f64(x: f64) -> f64;
1478
1479     /// Returns `a * b + c` for `f32` values.
1480     ///
1481     /// The stabilized version of this intrinsic is
1482     /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1483     pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1484     /// Returns `a * b + c` for `f64` values.
1485     ///
1486     /// The stabilized version of this intrinsic is
1487     /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1488     pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1489
1490     /// Returns the absolute value of an `f32`.
1491     ///
1492     /// The stabilized version of this intrinsic is
1493     /// [`f32::abs`](../../std/primitive.f32.html#method.abs)
1494     pub fn fabsf32(x: f32) -> f32;
1495     /// Returns the absolute value of an `f64`.
1496     ///
1497     /// The stabilized version of this intrinsic is
1498     /// [`f64::abs`](../../std/primitive.f64.html#method.abs)
1499     pub fn fabsf64(x: f64) -> f64;
1500
1501     /// Returns the minimum of two `f32` values.
1502     ///
1503     /// Note that, unlike most intrinsics, this is safe to call;
1504     /// it does not require an `unsafe` block.
1505     /// Therefore, implementations must not require the user to uphold
1506     /// any safety invariants.
1507     ///
1508     /// The stabilized version of this intrinsic is
1509     /// [`f32::min`]
1510     #[rustc_safe_intrinsic]
1511     pub fn minnumf32(x: f32, y: f32) -> f32;
1512     /// Returns the minimum of two `f64` values.
1513     ///
1514     /// Note that, unlike most intrinsics, this is safe to call;
1515     /// it does not require an `unsafe` block.
1516     /// Therefore, implementations must not require the user to uphold
1517     /// any safety invariants.
1518     ///
1519     /// The stabilized version of this intrinsic is
1520     /// [`f64::min`]
1521     #[rustc_safe_intrinsic]
1522     pub fn minnumf64(x: f64, y: f64) -> f64;
1523     /// Returns the maximum of two `f32` values.
1524     ///
1525     /// Note that, unlike most intrinsics, this is safe to call;
1526     /// it does not require an `unsafe` block.
1527     /// Therefore, implementations must not require the user to uphold
1528     /// any safety invariants.
1529     ///
1530     /// The stabilized version of this intrinsic is
1531     /// [`f32::max`]
1532     #[rustc_safe_intrinsic]
1533     pub fn maxnumf32(x: f32, y: f32) -> f32;
1534     /// Returns the maximum of two `f64` values.
1535     ///
1536     /// Note that, unlike most intrinsics, this is safe to call;
1537     /// it does not require an `unsafe` block.
1538     /// Therefore, implementations must not require the user to uphold
1539     /// any safety invariants.
1540     ///
1541     /// The stabilized version of this intrinsic is
1542     /// [`f64::max`]
1543     #[rustc_safe_intrinsic]
1544     pub fn maxnumf64(x: f64, y: f64) -> f64;
1545
1546     /// Copies the sign from `y` to `x` for `f32` values.
1547     ///
1548     /// The stabilized version of this intrinsic is
1549     /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
1550     pub fn copysignf32(x: f32, y: f32) -> f32;
1551     /// Copies the sign from `y` to `x` for `f64` values.
1552     ///
1553     /// The stabilized version of this intrinsic is
1554     /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
1555     pub fn copysignf64(x: f64, y: f64) -> f64;
1556
1557     /// Returns the largest integer less than or equal to an `f32`.
1558     ///
1559     /// The stabilized version of this intrinsic is
1560     /// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1561     pub fn floorf32(x: f32) -> f32;
1562     /// Returns the largest integer less than or equal to an `f64`.
1563     ///
1564     /// The stabilized version of this intrinsic is
1565     /// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1566     pub fn floorf64(x: f64) -> f64;
1567
1568     /// Returns the smallest integer greater than or equal to an `f32`.
1569     ///
1570     /// The stabilized version of this intrinsic is
1571     /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1572     pub fn ceilf32(x: f32) -> f32;
1573     /// Returns the smallest integer greater than or equal to an `f64`.
1574     ///
1575     /// The stabilized version of this intrinsic is
1576     /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1577     pub fn ceilf64(x: f64) -> f64;
1578
1579     /// Returns the integer part of an `f32`.
1580     ///
1581     /// The stabilized version of this intrinsic is
1582     /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1583     pub fn truncf32(x: f32) -> f32;
1584     /// Returns the integer part of an `f64`.
1585     ///
1586     /// The stabilized version of this intrinsic is
1587     /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1588     pub fn truncf64(x: f64) -> f64;
1589
1590     /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1591     /// if the argument is not an integer.
1592     pub fn rintf32(x: f32) -> f32;
1593     /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1594     /// if the argument is not an integer.
1595     pub fn rintf64(x: f64) -> f64;
1596
1597     /// Returns the nearest integer to an `f32`.
1598     ///
1599     /// This intrinsic does not have a stable counterpart.
1600     pub fn nearbyintf32(x: f32) -> f32;
1601     /// Returns the nearest integer to an `f64`.
1602     ///
1603     /// This intrinsic does not have a stable counterpart.
1604     pub fn nearbyintf64(x: f64) -> f64;
1605
1606     /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1607     ///
1608     /// The stabilized version of this intrinsic is
1609     /// [`f32::round`](../../std/primitive.f32.html#method.round)
1610     pub fn roundf32(x: f32) -> f32;
1611     /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1612     ///
1613     /// The stabilized version of this intrinsic is
1614     /// [`f64::round`](../../std/primitive.f64.html#method.round)
1615     pub fn roundf64(x: f64) -> f64;
1616
1617     /// Float addition that allows optimizations based on algebraic rules.
1618     /// May assume inputs are finite.
1619     ///
1620     /// This intrinsic does not have a stable counterpart.
1621     pub fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1622
1623     /// Float subtraction that allows optimizations based on algebraic rules.
1624     /// May assume inputs are finite.
1625     ///
1626     /// This intrinsic does not have a stable counterpart.
1627     pub fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1628
1629     /// Float multiplication that allows optimizations based on algebraic rules.
1630     /// May assume inputs are finite.
1631     ///
1632     /// This intrinsic does not have a stable counterpart.
1633     pub fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1634
1635     /// Float division that allows optimizations based on algebraic rules.
1636     /// May assume inputs are finite.
1637     ///
1638     /// This intrinsic does not have a stable counterpart.
1639     pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1640
1641     /// Float remainder that allows optimizations based on algebraic rules.
1642     /// May assume inputs are finite.
1643     ///
1644     /// This intrinsic does not have a stable counterpart.
1645     pub fn frem_fast<T: Copy>(a: T, b: T) -> T;
1646
1647     /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1648     /// (<https://github.com/rust-lang/rust/issues/10184>)
1649     ///
1650     /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1651     pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
1652
1653     /// Returns the number of bits set in an integer type `T`
1654     ///
1655     /// Note that, unlike most intrinsics, this is safe to call;
1656     /// it does not require an `unsafe` block.
1657     /// Therefore, implementations must not require the user to uphold
1658     /// any safety invariants.
1659     ///
1660     /// The stabilized versions of this intrinsic are available on the integer
1661     /// primitives via the `count_ones` method. For example,
1662     /// [`u32::count_ones`]
1663     #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1664     #[rustc_safe_intrinsic]
1665     pub fn ctpop<T: Copy>(x: T) -> T;
1666
1667     /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1668     ///
1669     /// Note that, unlike most intrinsics, this is safe to call;
1670     /// it does not require an `unsafe` block.
1671     /// Therefore, implementations must not require the user to uphold
1672     /// any safety invariants.
1673     ///
1674     /// The stabilized versions of this intrinsic are available on the integer
1675     /// primitives via the `leading_zeros` method. For example,
1676     /// [`u32::leading_zeros`]
1677     ///
1678     /// # Examples
1679     ///
1680     /// ```
1681     /// #![feature(core_intrinsics)]
1682     ///
1683     /// use std::intrinsics::ctlz;
1684     ///
1685     /// let x = 0b0001_1100_u8;
1686     /// let num_leading = ctlz(x);
1687     /// assert_eq!(num_leading, 3);
1688     /// ```
1689     ///
1690     /// An `x` with value `0` will return the bit width of `T`.
1691     ///
1692     /// ```
1693     /// #![feature(core_intrinsics)]
1694     ///
1695     /// use std::intrinsics::ctlz;
1696     ///
1697     /// let x = 0u16;
1698     /// let num_leading = ctlz(x);
1699     /// assert_eq!(num_leading, 16);
1700     /// ```
1701     #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1702     #[rustc_safe_intrinsic]
1703     pub fn ctlz<T: Copy>(x: T) -> T;
1704
1705     /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1706     /// given an `x` with value `0`.
1707     ///
1708     /// This intrinsic does not have a stable counterpart.
1709     ///
1710     /// # Examples
1711     ///
1712     /// ```
1713     /// #![feature(core_intrinsics)]
1714     ///
1715     /// use std::intrinsics::ctlz_nonzero;
1716     ///
1717     /// let x = 0b0001_1100_u8;
1718     /// let num_leading = unsafe { ctlz_nonzero(x) };
1719     /// assert_eq!(num_leading, 3);
1720     /// ```
1721     #[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
1722     pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
1723
1724     /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1725     ///
1726     /// Note that, unlike most intrinsics, this is safe to call;
1727     /// it does not require an `unsafe` block.
1728     /// Therefore, implementations must not require the user to uphold
1729     /// any safety invariants.
1730     ///
1731     /// The stabilized versions of this intrinsic are available on the integer
1732     /// primitives via the `trailing_zeros` method. For example,
1733     /// [`u32::trailing_zeros`]
1734     ///
1735     /// # Examples
1736     ///
1737     /// ```
1738     /// #![feature(core_intrinsics)]
1739     ///
1740     /// use std::intrinsics::cttz;
1741     ///
1742     /// let x = 0b0011_1000_u8;
1743     /// let num_trailing = cttz(x);
1744     /// assert_eq!(num_trailing, 3);
1745     /// ```
1746     ///
1747     /// An `x` with value `0` will return the bit width of `T`:
1748     ///
1749     /// ```
1750     /// #![feature(core_intrinsics)]
1751     ///
1752     /// use std::intrinsics::cttz;
1753     ///
1754     /// let x = 0u16;
1755     /// let num_trailing = cttz(x);
1756     /// assert_eq!(num_trailing, 16);
1757     /// ```
1758     #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1759     #[rustc_safe_intrinsic]
1760     pub fn cttz<T: Copy>(x: T) -> T;
1761
1762     /// Like `cttz`, but extra-unsafe as it returns `undef` when
1763     /// given an `x` with value `0`.
1764     ///
1765     /// This intrinsic does not have a stable counterpart.
1766     ///
1767     /// # Examples
1768     ///
1769     /// ```
1770     /// #![feature(core_intrinsics)]
1771     ///
1772     /// use std::intrinsics::cttz_nonzero;
1773     ///
1774     /// let x = 0b0011_1000_u8;
1775     /// let num_trailing = unsafe { cttz_nonzero(x) };
1776     /// assert_eq!(num_trailing, 3);
1777     /// ```
1778     #[rustc_const_stable(feature = "const_cttz_nonzero", since = "1.53.0")]
1779     pub fn cttz_nonzero<T: Copy>(x: T) -> T;
1780
1781     /// Reverses the bytes in an integer type `T`.
1782     ///
1783     /// Note that, unlike most intrinsics, this is safe to call;
1784     /// it does not require an `unsafe` block.
1785     /// Therefore, implementations must not require the user to uphold
1786     /// any safety invariants.
1787     ///
1788     /// The stabilized versions of this intrinsic are available on the integer
1789     /// primitives via the `swap_bytes` method. For example,
1790     /// [`u32::swap_bytes`]
1791     #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1792     #[rustc_safe_intrinsic]
1793     pub fn bswap<T: Copy>(x: T) -> T;
1794
1795     /// Reverses the bits in an integer type `T`.
1796     ///
1797     /// Note that, unlike most intrinsics, this is safe to call;
1798     /// it does not require an `unsafe` block.
1799     /// Therefore, implementations must not require the user to uphold
1800     /// any safety invariants.
1801     ///
1802     /// The stabilized versions of this intrinsic are available on the integer
1803     /// primitives via the `reverse_bits` method. For example,
1804     /// [`u32::reverse_bits`]
1805     #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1806     #[rustc_safe_intrinsic]
1807     pub fn bitreverse<T: Copy>(x: T) -> T;
1808
1809     /// Performs checked integer addition.
1810     ///
1811     /// Note that, unlike most intrinsics, this is safe to call;
1812     /// it does not require an `unsafe` block.
1813     /// Therefore, implementations must not require the user to uphold
1814     /// any safety invariants.
1815     ///
1816     /// The stabilized versions of this intrinsic are available on the integer
1817     /// primitives via the `overflowing_add` method. For example,
1818     /// [`u32::overflowing_add`]
1819     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1820     #[rustc_safe_intrinsic]
1821     pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1822
1823     /// Performs checked integer subtraction
1824     ///
1825     /// Note that, unlike most intrinsics, this is safe to call;
1826     /// it does not require an `unsafe` block.
1827     /// Therefore, implementations must not require the user to uphold
1828     /// any safety invariants.
1829     ///
1830     /// The stabilized versions of this intrinsic are available on the integer
1831     /// primitives via the `overflowing_sub` method. For example,
1832     /// [`u32::overflowing_sub`]
1833     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1834     #[rustc_safe_intrinsic]
1835     pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1836
1837     /// Performs checked integer multiplication
1838     ///
1839     /// Note that, unlike most intrinsics, this is safe to call;
1840     /// it does not require an `unsafe` block.
1841     /// Therefore, implementations must not require the user to uphold
1842     /// any safety invariants.
1843     ///
1844     /// The stabilized versions of this intrinsic are available on the integer
1845     /// primitives via the `overflowing_mul` method. For example,
1846     /// [`u32::overflowing_mul`]
1847     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1848     #[rustc_safe_intrinsic]
1849     pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1850
1851     /// Performs an exact division, resulting in undefined behavior where
1852     /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1853     ///
1854     /// This intrinsic does not have a stable counterpart.
1855     #[rustc_const_unstable(feature = "const_exact_div", issue = "none")]
1856     pub fn exact_div<T: Copy>(x: T, y: T) -> T;
1857
1858     /// Performs an unchecked division, resulting in undefined behavior
1859     /// where `y == 0` or `x == T::MIN && y == -1`
1860     ///
1861     /// Safe wrappers for this intrinsic are available on the integer
1862     /// primitives via the `checked_div` method. For example,
1863     /// [`u32::checked_div`]
1864     #[rustc_const_stable(feature = "const_int_unchecked_div", since = "1.52.0")]
1865     pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1866     /// Returns the remainder of an unchecked division, resulting in
1867     /// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
1868     ///
1869     /// Safe wrappers for this intrinsic are available on the integer
1870     /// primitives via the `checked_rem` method. For example,
1871     /// [`u32::checked_rem`]
1872     #[rustc_const_stable(feature = "const_int_unchecked_rem", since = "1.52.0")]
1873     pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1874
1875     /// Performs an unchecked left shift, resulting in undefined behavior when
1876     /// `y < 0` or `y >= N`, where N is the width of T in bits.
1877     ///
1878     /// Safe wrappers for this intrinsic are available on the integer
1879     /// primitives via the `checked_shl` method. For example,
1880     /// [`u32::checked_shl`]
1881     #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1882     pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
1883     /// Performs an unchecked right shift, resulting in undefined behavior when
1884     /// `y < 0` or `y >= N`, where N is the width of T in bits.
1885     ///
1886     /// Safe wrappers for this intrinsic are available on the integer
1887     /// primitives via the `checked_shr` method. For example,
1888     /// [`u32::checked_shr`]
1889     #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1890     pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
1891
1892     /// Returns the result of an unchecked addition, resulting in
1893     /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1894     ///
1895     /// This intrinsic does not have a stable counterpart.
1896     #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1897     pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1898
1899     /// Returns the result of an unchecked subtraction, resulting in
1900     /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1901     ///
1902     /// This intrinsic does not have a stable counterpart.
1903     #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1904     pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1905
1906     /// Returns the result of an unchecked multiplication, resulting in
1907     /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1908     ///
1909     /// This intrinsic does not have a stable counterpart.
1910     #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1911     pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1912
1913     /// Performs rotate left.
1914     ///
1915     /// Note that, unlike most intrinsics, this is safe to call;
1916     /// it does not require an `unsafe` block.
1917     /// Therefore, implementations must not require the user to uphold
1918     /// any safety invariants.
1919     ///
1920     /// The stabilized versions of this intrinsic are available on the integer
1921     /// primitives via the `rotate_left` method. For example,
1922     /// [`u32::rotate_left`]
1923     #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1924     #[rustc_safe_intrinsic]
1925     pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
1926
1927     /// Performs rotate right.
1928     ///
1929     /// Note that, unlike most intrinsics, this is safe to call;
1930     /// it does not require an `unsafe` block.
1931     /// Therefore, implementations must not require the user to uphold
1932     /// any safety invariants.
1933     ///
1934     /// The stabilized versions of this intrinsic are available on the integer
1935     /// primitives via the `rotate_right` method. For example,
1936     /// [`u32::rotate_right`]
1937     #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1938     #[rustc_safe_intrinsic]
1939     pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
1940
1941     /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1942     ///
1943     /// Note that, unlike most intrinsics, this is safe to call;
1944     /// it does not require an `unsafe` block.
1945     /// Therefore, implementations must not require the user to uphold
1946     /// any safety invariants.
1947     ///
1948     /// The stabilized versions of this intrinsic are available on the integer
1949     /// primitives via the `wrapping_add` method. For example,
1950     /// [`u32::wrapping_add`]
1951     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1952     #[rustc_safe_intrinsic]
1953     pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
1954     /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1955     ///
1956     /// Note that, unlike most intrinsics, this is safe to call;
1957     /// it does not require an `unsafe` block.
1958     /// Therefore, implementations must not require the user to uphold
1959     /// any safety invariants.
1960     ///
1961     /// The stabilized versions of this intrinsic are available on the integer
1962     /// primitives via the `wrapping_sub` method. For example,
1963     /// [`u32::wrapping_sub`]
1964     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1965     #[rustc_safe_intrinsic]
1966     pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
1967     /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1968     ///
1969     /// Note that, unlike most intrinsics, this is safe to call;
1970     /// it does not require an `unsafe` block.
1971     /// Therefore, implementations must not require the user to uphold
1972     /// any safety invariants.
1973     ///
1974     /// The stabilized versions of this intrinsic are available on the integer
1975     /// primitives via the `wrapping_mul` method. For example,
1976     /// [`u32::wrapping_mul`]
1977     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1978     #[rustc_safe_intrinsic]
1979     pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
1980
1981     /// Computes `a + b`, saturating at numeric bounds.
1982     ///
1983     /// Note that, unlike most intrinsics, this is safe to call;
1984     /// it does not require an `unsafe` block.
1985     /// Therefore, implementations must not require the user to uphold
1986     /// any safety invariants.
1987     ///
1988     /// The stabilized versions of this intrinsic are available on the integer
1989     /// primitives via the `saturating_add` method. For example,
1990     /// [`u32::saturating_add`]
1991     #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1992     #[rustc_safe_intrinsic]
1993     pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
1994     /// Computes `a - b`, saturating at numeric bounds.
1995     ///
1996     /// Note that, unlike most intrinsics, this is safe to call;
1997     /// it does not require an `unsafe` block.
1998     /// Therefore, implementations must not require the user to uphold
1999     /// any safety invariants.
2000     ///
2001     /// The stabilized versions of this intrinsic are available on the integer
2002     /// primitives via the `saturating_sub` method. For example,
2003     /// [`u32::saturating_sub`]
2004     #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
2005     #[rustc_safe_intrinsic]
2006     pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
2007
2008     /// Returns the value of the discriminant for the variant in 'v';
2009     /// if `T` has no discriminant, returns `0`.
2010     ///
2011     /// Note that, unlike most intrinsics, this is safe to call;
2012     /// it does not require an `unsafe` block.
2013     /// Therefore, implementations must not require the user to uphold
2014     /// any safety invariants.
2015     ///
2016     /// The stabilized version of this intrinsic is [`core::mem::discriminant`].
2017     #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
2018     #[rustc_safe_intrinsic]
2019     pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
2020
2021     /// Returns the number of variants of the type `T` cast to a `usize`;
2022     /// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
2023     ///
2024     /// Note that, unlike most intrinsics, this is safe to call;
2025     /// it does not require an `unsafe` block.
2026     /// Therefore, implementations must not require the user to uphold
2027     /// any safety invariants.
2028     ///
2029     /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
2030     #[rustc_const_unstable(feature = "variant_count", issue = "73662")]
2031     #[rustc_safe_intrinsic]
2032     pub fn variant_count<T>() -> usize;
2033
2034     /// Rust's "try catch" construct which invokes the function pointer `try_fn`
2035     /// with the data pointer `data`.
2036     ///
2037     /// The third argument is a function called if a panic occurs. This function
2038     /// takes the data pointer and a pointer to the target-specific exception
2039     /// object that was caught. For more information see the compiler's
2040     /// source as well as std's catch implementation.
2041     pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
2042
2043     /// Emits a `!nontemporal` store according to LLVM (see their docs).
2044     /// Probably will never become stable.
2045     pub fn nontemporal_store<T>(ptr: *mut T, val: T);
2046
2047     /// See documentation of `<*const T>::offset_from` for details.
2048     #[rustc_const_stable(feature = "const_ptr_offset_from", since = "1.65.0")]
2049     pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
2050
2051     /// See documentation of `<*const T>::sub_ptr` for details.
2052     #[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
2053     pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
2054
2055     /// See documentation of `<*const T>::guaranteed_eq` for details.
2056     /// Returns `2` if the result is unknown.
2057     /// Returns `1` if the pointers are guaranteed equal
2058     /// Returns `0` if the pointers are guaranteed inequal
2059     ///
2060     /// Note that, unlike most intrinsics, this is safe to call;
2061     /// it does not require an `unsafe` block.
2062     /// Therefore, implementations must not require the user to uphold
2063     /// any safety invariants.
2064     #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
2065     #[rustc_safe_intrinsic]
2066     pub fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8;
2067
2068     /// Allocates a block of memory at compile time.
2069     /// At runtime, just returns a null pointer.
2070     ///
2071     /// # Safety
2072     ///
2073     /// - The `align` argument must be a power of two.
2074     ///    - At compile time, a compile error occurs if this constraint is violated.
2075     ///    - At runtime, it is not checked.
2076     #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2077     pub fn const_allocate(size: usize, align: usize) -> *mut u8;
2078
2079     /// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
2080     /// At runtime, does nothing.
2081     ///
2082     /// # Safety
2083     ///
2084     /// - The `align` argument must be a power of two.
2085     ///    - At compile time, a compile error occurs if this constraint is violated.
2086     ///    - At runtime, it is not checked.
2087     /// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
2088     /// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
2089     #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2090     pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);
2091
2092     /// Determines whether the raw bytes of the two values are equal.
2093     ///
2094     /// This is particularly handy for arrays, since it allows things like just
2095     /// comparing `i96`s instead of forcing `alloca`s for `[6 x i16]`.
2096     ///
2097     /// Above some backend-decided threshold this will emit calls to `memcmp`,
2098     /// like slice equality does, instead of causing massive code size.
2099     ///
2100     /// # Safety
2101     ///
2102     /// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized or carry a
2103     /// pointer value.
2104     /// Note that this is a stricter criterion than just the *values* being
2105     /// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
2106     ///
2107     /// (The implementation is allowed to branch on the results of comparisons,
2108     /// which is UB if any of their inputs are `undef`.)
2109     #[rustc_const_unstable(feature = "const_intrinsic_raw_eq", issue = "none")]
2110     pub fn raw_eq<T>(a: &T, b: &T) -> bool;
2111
2112     /// See documentation of [`std::hint::black_box`] for details.
2113     ///
2114     /// [`std::hint::black_box`]: crate::hint::black_box
2115     #[rustc_const_unstable(feature = "const_black_box", issue = "none")]
2116     #[rustc_safe_intrinsic]
2117     pub fn black_box<T>(dummy: T) -> T;
2118
2119     /// `ptr` must point to a vtable.
2120     /// The intrinsic will return the size stored in that vtable.
2121     pub fn vtable_size(ptr: *const ()) -> usize;
2122
2123     /// `ptr` must point to a vtable.
2124     /// The intrinsic will return the alignment stored in that vtable.
2125     pub fn vtable_align(ptr: *const ()) -> usize;
2126
2127     /// Selects which function to call depending on the context.
2128     ///
2129     /// If this function is evaluated at compile-time, then a call to this
2130     /// intrinsic will be replaced with a call to `called_in_const`. It gets
2131     /// replaced with a call to `called_at_rt` otherwise.
2132     ///
2133     /// # Type Requirements
2134     ///
2135     /// The two functions must be both function items. They cannot be function
2136     /// pointers or closures. The first function must be a `const fn`.
2137     ///
2138     /// `arg` will be the tupled arguments that will be passed to either one of
2139     /// the two functions, therefore, both functions must accept the same type of
2140     /// arguments. Both functions must return RET.
2141     ///
2142     /// # Safety
2143     ///
2144     /// The two functions must behave observably equivalent. Safe code in other
2145     /// crates may assume that calling a `const fn` at compile-time and at run-time
2146     /// produces the same result. A function that produces a different result when
2147     /// evaluated at run-time, or has any other observable side-effects, is
2148     /// *unsound*.
2149     ///
2150     /// Here is an example of how this could cause a problem:
2151     /// ```no_run
2152     /// #![feature(const_eval_select)]
2153     /// #![feature(core_intrinsics)]
2154     /// use std::hint::unreachable_unchecked;
2155     /// use std::intrinsics::const_eval_select;
2156     ///
2157     /// // Crate A
2158     /// pub const fn inconsistent() -> i32 {
2159     ///     fn runtime() -> i32 { 1 }
2160     ///     const fn compiletime() -> i32 { 2 }
2161     ///
2162     ///     unsafe {
2163     //          // ⚠ This code violates the required equivalence of `compiletime`
2164     ///         // and `runtime`.
2165     ///         const_eval_select((), compiletime, runtime)
2166     ///     }
2167     /// }
2168     ///
2169     /// // Crate B
2170     /// const X: i32 = inconsistent();
2171     /// let x = inconsistent();
2172     /// if x != X { unsafe { unreachable_unchecked(); }}
2173     /// ```
2174     ///
2175     /// This code causes Undefined Behavior when being run, since the
2176     /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2177     /// which violates the principle that a `const fn` must behave the same at
2178     /// compile-time and at run-time. The unsafe code in crate B is fine.
2179     #[cfg(bootstrap)]
2180     #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2181     pub fn const_eval_select<ARG, F, G, RET>(arg: ARG, called_in_const: F, called_at_rt: G) -> RET
2182     where
2183         G: FnOnce<ARG, Output = RET>,
2184         F: FnOnce<ARG, Output = RET>;
2185
2186     /// Selects which function to call depending on the context.
2187     ///
2188     /// If this function is evaluated at compile-time, then a call to this
2189     /// intrinsic will be replaced with a call to `called_in_const`. It gets
2190     /// replaced with a call to `called_at_rt` otherwise.
2191     ///
2192     /// # Type Requirements
2193     ///
2194     /// The two functions must be both function items. They cannot be function
2195     /// pointers or closures. The first function must be a `const fn`.
2196     ///
2197     /// `arg` will be the tupled arguments that will be passed to either one of
2198     /// the two functions, therefore, both functions must accept the same type of
2199     /// arguments. Both functions must return RET.
2200     ///
2201     /// # Safety
2202     ///
2203     /// The two functions must behave observably equivalent. Safe code in other
2204     /// crates may assume that calling a `const fn` at compile-time and at run-time
2205     /// produces the same result. A function that produces a different result when
2206     /// evaluated at run-time, or has any other observable side-effects, is
2207     /// *unsound*.
2208     ///
2209     /// Here is an example of how this could cause a problem:
2210     /// ```no_run
2211     /// #![feature(const_eval_select)]
2212     /// #![feature(core_intrinsics)]
2213     /// use std::hint::unreachable_unchecked;
2214     /// use std::intrinsics::const_eval_select;
2215     ///
2216     /// // Crate A
2217     /// pub const fn inconsistent() -> i32 {
2218     ///     fn runtime() -> i32 { 1 }
2219     ///     const fn compiletime() -> i32 { 2 }
2220     ///
2221     ///     unsafe {
2222     //          // ⚠ This code violates the required equivalence of `compiletime`
2223     ///         // and `runtime`.
2224     ///         const_eval_select((), compiletime, runtime)
2225     ///     }
2226     /// }
2227     ///
2228     /// // Crate B
2229     /// const X: i32 = inconsistent();
2230     /// let x = inconsistent();
2231     /// if x != X { unsafe { unreachable_unchecked(); }}
2232     /// ```
2233     ///
2234     /// This code causes Undefined Behavior when being run, since the
2235     /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2236     /// which violates the principle that a `const fn` must behave the same at
2237     /// compile-time and at run-time. The unsafe code in crate B is fine.
2238     #[cfg(not(bootstrap))]
2239     #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2240     pub fn const_eval_select<ARG: Tuple, F, G, RET>(
2241         arg: ARG,
2242         called_in_const: F,
2243         called_at_rt: G,
2244     ) -> RET
2245     where
2246         G: FnOnce<ARG, Output = RET>,
2247         F: FnOnce<ARG, Output = RET>;
2248 }
2249
2250 // Some functions are defined here because they accidentally got made
2251 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
2252 // (`transmute` also falls into this category, but it cannot be wrapped due to the
2253 // check that `T` and `U` have the same size.)
2254
2255 /// Check that the preconditions of an unsafe function are followed, if debug_assertions are on,
2256 /// and only at runtime.
2257 ///
2258 /// This macro should be called as `assert_unsafe_precondition!([Generics](name: Type) => Expression)`
2259 /// where the names specified will be moved into the macro as captured variables, and defines an item
2260 /// to call `const_eval_select` on. The tokens inside the square brackets are used to denote generics
2261 /// for the function declaractions and can be omitted if there is no generics.
2262 ///
2263 /// # Safety
2264 ///
2265 /// Invoking this macro is only sound if the following code is already UB when the passed
2266 /// expression evaluates to false.
2267 ///
2268 /// This macro expands to a check at runtime if debug_assertions is set. It has no effect at
2269 /// compile time, but the semantics of the contained `const_eval_select` must be the same at
2270 /// runtime and at compile time. Thus if the expression evaluates to false, this macro produces
2271 /// different behavior at compile time and at runtime, and invoking it is incorrect.
2272 ///
2273 /// So in a sense it is UB if this macro is useful, but we expect callers of `unsafe fn` to make
2274 /// the occasional mistake, and this check should help them figure things out.
2275 #[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn
2276 macro_rules! assert_unsafe_precondition {
2277     ($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr) => {
2278         if cfg!(debug_assertions) {
2279             // allow non_snake_case to allow capturing const generics
2280             #[allow(non_snake_case)]
2281             #[inline(always)]
2282             fn runtime$(<$($tt)*>)?($($i:$ty),*) {
2283                 if !$e {
2284                     // don't unwind to reduce impact on code size
2285                     ::core::panicking::panic_str_nounwind(
2286                         concat!("unsafe precondition(s) violated: ", $name)
2287                     );
2288                 }
2289             }
2290             #[allow(non_snake_case)]
2291             const fn comptime$(<$($tt)*>)?($(_:$ty),*) {}
2292
2293             ::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
2294         }
2295     };
2296 }
2297 pub(crate) use assert_unsafe_precondition;
2298
2299 /// Checks whether `ptr` is properly aligned with respect to
2300 /// `align_of::<T>()`.
2301 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
2302     !ptr.is_null() && ptr.is_aligned()
2303 }
2304
2305 /// Checks whether an allocation of `len` instances of `T` exceeds
2306 /// the maximum allowed allocation size.
2307 pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool {
2308     let max_len = const {
2309         let size = crate::mem::size_of::<T>();
2310         if size == 0 { usize::MAX } else { isize::MAX as usize / size }
2311     };
2312     len <= max_len
2313 }
2314
2315 /// Checks whether the regions of memory starting at `src` and `dst` of size
2316 /// `count * size_of::<T>()` do *not* overlap.
2317 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
2318     let src_usize = src.addr();
2319     let dst_usize = dst.addr();
2320     let size = mem::size_of::<T>().checked_mul(count).unwrap();
2321     let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
2322     // If the absolute distance between the ptrs is at least as big as the size of the buffer,
2323     // they do not overlap.
2324     diff >= size
2325 }
2326
2327 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2328 /// and destination must *not* overlap.
2329 ///
2330 /// For regions of memory which might overlap, use [`copy`] instead.
2331 ///
2332 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
2333 /// with the argument order swapped.
2334 ///
2335 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2336 /// requirements of `T`. The initialization state is preserved exactly.
2337 ///
2338 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
2339 ///
2340 /// # Safety
2341 ///
2342 /// Behavior is undefined if any of the following conditions are violated:
2343 ///
2344 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2345 ///
2346 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2347 ///
2348 /// * Both `src` and `dst` must be properly aligned.
2349 ///
2350 /// * The region of memory beginning at `src` with a size of `count *
2351 ///   size_of::<T>()` bytes must *not* overlap with the region of memory
2352 ///   beginning at `dst` with the same size.
2353 ///
2354 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
2355 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
2356 /// in the region beginning at `*src` and the region beginning at `*dst` can
2357 /// [violate memory safety][read-ownership].
2358 ///
2359 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2360 /// `0`, the pointers must be non-null and properly aligned.
2361 ///
2362 /// [`read`]: crate::ptr::read
2363 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2364 /// [valid]: crate::ptr#safety
2365 ///
2366 /// # Examples
2367 ///
2368 /// Manually implement [`Vec::append`]:
2369 ///
2370 /// ```
2371 /// use std::ptr;
2372 ///
2373 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
2374 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
2375 ///     let src_len = src.len();
2376 ///     let dst_len = dst.len();
2377 ///
2378 ///     // Ensure that `dst` has enough capacity to hold all of `src`.
2379 ///     dst.reserve(src_len);
2380 ///
2381 ///     unsafe {
2382 ///         // The call to add is always safe because `Vec` will never
2383 ///         // allocate more than `isize::MAX` bytes.
2384 ///         let dst_ptr = dst.as_mut_ptr().add(dst_len);
2385 ///         let src_ptr = src.as_ptr();
2386 ///
2387 ///         // Truncate `src` without dropping its contents. We do this first,
2388 ///         // to avoid problems in case something further down panics.
2389 ///         src.set_len(0);
2390 ///
2391 ///         // The two regions cannot overlap because mutable references do
2392 ///         // not alias, and two different vectors cannot own the same
2393 ///         // memory.
2394 ///         ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
2395 ///
2396 ///         // Notify `dst` that it now holds the contents of `src`.
2397 ///         dst.set_len(dst_len + src_len);
2398 ///     }
2399 /// }
2400 ///
2401 /// let mut a = vec!['r'];
2402 /// let mut b = vec!['u', 's', 't'];
2403 ///
2404 /// append(&mut a, &mut b);
2405 ///
2406 /// assert_eq!(a, &['r', 'u', 's', 't']);
2407 /// assert!(b.is_empty());
2408 /// ```
2409 ///
2410 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
2411 #[doc(alias = "memcpy")]
2412 #[stable(feature = "rust1", since = "1.0.0")]
2413 #[rustc_allowed_through_unstable_modules]
2414 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2415 #[inline]
2416 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2417 pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
2418     extern "rust-intrinsic" {
2419         #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2420         pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
2421     }
2422
2423     // SAFETY: the safety contract for `copy_nonoverlapping` must be
2424     // upheld by the caller.
2425     unsafe {
2426         assert_unsafe_precondition!(
2427             "ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
2428             and the specified memory ranges do not overlap",
2429             [T](src: *const T, dst: *mut T, count: usize) =>
2430             is_aligned_and_not_null(src)
2431                 && is_aligned_and_not_null(dst)
2432                 && is_nonoverlapping(src, dst, count)
2433         );
2434         copy_nonoverlapping(src, dst, count)
2435     }
2436 }
2437
2438 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2439 /// and destination may overlap.
2440 ///
2441 /// If the source and destination will *never* overlap,
2442 /// [`copy_nonoverlapping`] can be used instead.
2443 ///
2444 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
2445 /// order swapped. Copying takes place as if the bytes were copied from `src`
2446 /// to a temporary array and then copied from the array to `dst`.
2447 ///
2448 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2449 /// requirements of `T`. The initialization state is preserved exactly.
2450 ///
2451 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
2452 ///
2453 /// # Safety
2454 ///
2455 /// Behavior is undefined if any of the following conditions are violated:
2456 ///
2457 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2458 ///
2459 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2460 ///
2461 /// * Both `src` and `dst` must be properly aligned.
2462 ///
2463 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
2464 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
2465 /// in the region beginning at `*src` and the region beginning at `*dst` can
2466 /// [violate memory safety][read-ownership].
2467 ///
2468 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2469 /// `0`, the pointers must be non-null and properly aligned.
2470 ///
2471 /// [`read`]: crate::ptr::read
2472 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2473 /// [valid]: crate::ptr#safety
2474 ///
2475 /// # Examples
2476 ///
2477 /// Efficiently create a Rust vector from an unsafe buffer:
2478 ///
2479 /// ```
2480 /// use std::ptr;
2481 ///
2482 /// /// # Safety
2483 /// ///
2484 /// /// * `ptr` must be correctly aligned for its type and non-zero.
2485 /// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
2486 /// /// * Those elements must not be used after calling this function unless `T: Copy`.
2487 /// # #[allow(dead_code)]
2488 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
2489 ///     let mut dst = Vec::with_capacity(elts);
2490 ///
2491 ///     // SAFETY: Our precondition ensures the source is aligned and valid,
2492 ///     // and `Vec::with_capacity` ensures that we have usable space to write them.
2493 ///     ptr::copy(ptr, dst.as_mut_ptr(), elts);
2494 ///
2495 ///     // SAFETY: We created it with this much capacity earlier,
2496 ///     // and the previous `copy` has initialized these elements.
2497 ///     dst.set_len(elts);
2498 ///     dst
2499 /// }
2500 /// ```
2501 #[doc(alias = "memmove")]
2502 #[stable(feature = "rust1", since = "1.0.0")]
2503 #[rustc_allowed_through_unstable_modules]
2504 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2505 #[inline]
2506 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2507 pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
2508     extern "rust-intrinsic" {
2509         #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2510         fn copy<T>(src: *const T, dst: *mut T, count: usize);
2511     }
2512
2513     // SAFETY: the safety contract for `copy` must be upheld by the caller.
2514     unsafe {
2515         assert_unsafe_precondition!(
2516             "ptr::copy requires that both pointer arguments are aligned aligned and non-null",
2517             [T](src: *const T, dst: *mut T) =>
2518             is_aligned_and_not_null(src) && is_aligned_and_not_null(dst)
2519         );
2520         copy(src, dst, count)
2521     }
2522 }
2523
2524 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
2525 /// `val`.
2526 ///
2527 /// `write_bytes` is similar to C's [`memset`], but sets `count *
2528 /// size_of::<T>()` bytes to `val`.
2529 ///
2530 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
2531 ///
2532 /// # Safety
2533 ///
2534 /// Behavior is undefined if any of the following conditions are violated:
2535 ///
2536 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2537 ///
2538 /// * `dst` must be properly aligned.
2539 ///
2540 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2541 /// `0`, the pointer must be non-null and properly aligned.
2542 ///
2543 /// Additionally, note that changing `*dst` in this way can easily lead to undefined behavior (UB)
2544 /// later if the written bytes are not a valid representation of some `T`. For instance, the
2545 /// following is an **incorrect** use of this function:
2546 ///
2547 /// ```rust,no_run
2548 /// unsafe {
2549 ///     let mut value: u8 = 0;
2550 ///     let ptr: *mut bool = &mut value as *mut u8 as *mut bool;
2551 ///     let _bool = ptr.read(); // This is fine, `ptr` points to a valid `bool`.
2552 ///     ptr.write_bytes(42u8, 1); // This function itself does not cause UB...
2553 ///     let _bool = ptr.read(); // ...but it makes this operation UB! ⚠️
2554 /// }
2555 /// ```
2556 ///
2557 /// [valid]: crate::ptr#safety
2558 ///
2559 /// # Examples
2560 ///
2561 /// Basic usage:
2562 ///
2563 /// ```
2564 /// use std::ptr;
2565 ///
2566 /// let mut vec = vec![0u32; 4];
2567 /// unsafe {
2568 ///     let vec_ptr = vec.as_mut_ptr();
2569 ///     ptr::write_bytes(vec_ptr, 0xfe, 2);
2570 /// }
2571 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
2572 /// ```
2573 #[doc(alias = "memset")]
2574 #[stable(feature = "rust1", since = "1.0.0")]
2575 #[rustc_allowed_through_unstable_modules]
2576 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2577 #[inline]
2578 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2579 pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
2580     extern "rust-intrinsic" {
2581         #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2582         fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
2583     }
2584
2585     // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
2586     unsafe {
2587         assert_unsafe_precondition!(
2588             "ptr::write_bytes requires that the destination pointer is aligned and non-null",
2589             [T](dst: *mut T) => is_aligned_and_not_null(dst)
2590         );
2591         write_bytes(dst, val, count)
2592     }
2593 }