]> git.lizzy.rs Git - rust.git/blob - library/core/src/intrinsics.rs
less specific wording
[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`. This will statically either panic, or do nothing.
963     ///
964     /// This intrinsic does not have a stable counterpart.
965     #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
966     #[rustc_safe_intrinsic]
967     #[cfg(not(bootstrap))]
968     pub fn assert_mem_uninitialized_valid<T>();
969
970     /// Gets a reference to a static `Location` indicating where it was called.
971     ///
972     /// Note that, unlike most intrinsics, this is safe to call;
973     /// it does not require an `unsafe` block.
974     /// Therefore, implementations must not require the user to uphold
975     /// any safety invariants.
976     ///
977     /// Consider using [`core::panic::Location::caller`] instead.
978     #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
979     #[rustc_safe_intrinsic]
980     pub fn caller_location() -> &'static crate::panic::Location<'static>;
981
982     /// Moves a value out of scope without running drop glue.
983     ///
984     /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses
985     /// `ManuallyDrop` instead.
986     ///
987     /// Note that, unlike most intrinsics, this is safe to call;
988     /// it does not require an `unsafe` block.
989     /// Therefore, implementations must not require the user to uphold
990     /// any safety invariants.
991     #[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")]
992     #[rustc_safe_intrinsic]
993     pub fn forget<T: ?Sized>(_: T);
994
995     /// Reinterprets the bits of a value of one type as another type.
996     ///
997     /// Both types must have the same size. Compilation will fail if this is not guaranteed.
998     ///
999     /// `transmute` is semantically equivalent to a bitwise move of one type
1000     /// into another. It copies the bits from the source value into the
1001     /// destination value, then forgets the original. Note that source and destination
1002     /// are passed by-value, which means if `Src` or `Dst` contain padding, that padding
1003     /// is *not* guaranteed to be preserved by `transmute`.
1004     ///
1005     /// Both the argument and the result must be [valid](../../nomicon/what-unsafe-does.html) at
1006     /// their given type. Violating this condition leads to [undefined behavior][ub]. The compiler
1007     /// will generate code *assuming that you, the programmer, ensure that there will never be
1008     /// undefined behavior*. It is therefore your responsibility to guarantee that every value
1009     /// passed to `transmute` is valid at both types `Src` and `Dst`. Failing to uphold this condition
1010     /// may lead to unexpected and unstable compilation results. This makes `transmute` **incredibly
1011     /// unsafe**. `transmute` should be the absolute last resort.
1012     ///
1013     /// Transmuting pointers to integers in a `const` context is [undefined behavior][ub].
1014     /// Any attempt to use the resulting value for integer operations will abort const-evaluation.
1015     /// (And even outside `const`, such transmutation is touching on many unspecified aspects of the
1016     /// Rust memory model and should be avoided. See below for alternatives.)
1017     ///
1018     /// Because `transmute` is a by-value operation, alignment of the *transmuted values
1019     /// themselves* is not a concern. As with any other function, the compiler already ensures
1020     /// both `Src` and `Dst` are properly aligned. However, when transmuting values that *point
1021     /// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper
1022     /// alignment of the pointed-to values.
1023     ///
1024     /// The [nomicon](../../nomicon/transmutes.html) has additional documentation.
1025     ///
1026     /// [ub]: ../../reference/behavior-considered-undefined.html
1027     ///
1028     /// # Examples
1029     ///
1030     /// There are a few things that `transmute` is really useful for.
1031     ///
1032     /// Turning a pointer into a function pointer. This is *not* portable to
1033     /// machines where function pointers and data pointers have different sizes.
1034     ///
1035     /// ```
1036     /// fn foo() -> i32 {
1037     ///     0
1038     /// }
1039     /// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer.
1040     /// // This avoids an integer-to-pointer `transmute`, which can be problematic.
1041     /// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine.
1042     /// let pointer = foo as *const ();
1043     /// let function = unsafe {
1044     ///     std::mem::transmute::<*const (), fn() -> i32>(pointer)
1045     /// };
1046     /// assert_eq!(function(), 0);
1047     /// ```
1048     ///
1049     /// Extending a lifetime, or shortening an invariant lifetime. This is
1050     /// advanced, very unsafe Rust!
1051     ///
1052     /// ```
1053     /// struct R<'a>(&'a i32);
1054     /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
1055     ///     std::mem::transmute::<R<'b>, R<'static>>(r)
1056     /// }
1057     ///
1058     /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
1059     ///                                              -> &'b mut R<'c> {
1060     ///     std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
1061     /// }
1062     /// ```
1063     ///
1064     /// # Alternatives
1065     ///
1066     /// Don't despair: many uses of `transmute` can be achieved through other means.
1067     /// Below are common applications of `transmute` which can be replaced with safer
1068     /// constructs.
1069     ///
1070     /// Turning raw bytes (`&[u8]`) into `u32`, `f64`, etc.:
1071     ///
1072     /// ```
1073     /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
1074     ///
1075     /// let num = unsafe {
1076     ///     std::mem::transmute::<[u8; 4], u32>(raw_bytes)
1077     /// };
1078     ///
1079     /// // use `u32::from_ne_bytes` instead
1080     /// let num = u32::from_ne_bytes(raw_bytes);
1081     /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
1082     /// let num = u32::from_le_bytes(raw_bytes);
1083     /// assert_eq!(num, 0x12345678);
1084     /// let num = u32::from_be_bytes(raw_bytes);
1085     /// assert_eq!(num, 0x78563412);
1086     /// ```
1087     ///
1088     /// Turning a pointer into a `usize`:
1089     ///
1090     /// ```no_run
1091     /// let ptr = &0;
1092     /// let ptr_num_transmute = unsafe {
1093     ///     std::mem::transmute::<&i32, usize>(ptr)
1094     /// };
1095     ///
1096     /// // Use an `as` cast instead
1097     /// let ptr_num_cast = ptr as *const i32 as usize;
1098     /// ```
1099     ///
1100     /// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined
1101     /// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave
1102     /// as expected -- this is touching on many unspecified aspects of the Rust memory model.
1103     /// Depending on what the code is doing, the following alternatives are preferable to
1104     /// pointer-to-integer transmutation:
1105     /// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a
1106     ///   type for that buffer, it can use [`MaybeUninit`][mem::MaybeUninit].
1107     /// - If the code actually wants to work on the address the pointer points to, it can use `as`
1108     ///   casts or [`ptr.addr()`][pointer::addr].
1109     ///
1110     /// Turning a `*mut T` into an `&mut T`:
1111     ///
1112     /// ```
1113     /// let ptr: *mut i32 = &mut 0;
1114     /// let ref_transmuted = unsafe {
1115     ///     std::mem::transmute::<*mut i32, &mut i32>(ptr)
1116     /// };
1117     ///
1118     /// // Use a reborrow instead
1119     /// let ref_casted = unsafe { &mut *ptr };
1120     /// ```
1121     ///
1122     /// Turning an `&mut T` into an `&mut U`:
1123     ///
1124     /// ```
1125     /// let ptr = &mut 0;
1126     /// let val_transmuted = unsafe {
1127     ///     std::mem::transmute::<&mut i32, &mut u32>(ptr)
1128     /// };
1129     ///
1130     /// // Now, put together `as` and reborrowing - note the chaining of `as`
1131     /// // `as` is not transitive
1132     /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
1133     /// ```
1134     ///
1135     /// Turning an `&str` into a `&[u8]`:
1136     ///
1137     /// ```
1138     /// // this is not a good way to do this.
1139     /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
1140     /// assert_eq!(slice, &[82, 117, 115, 116]);
1141     ///
1142     /// // You could use `str::as_bytes`
1143     /// let slice = "Rust".as_bytes();
1144     /// assert_eq!(slice, &[82, 117, 115, 116]);
1145     ///
1146     /// // Or, just use a byte string, if you have control over the string
1147     /// // literal
1148     /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
1149     /// ```
1150     ///
1151     /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`.
1152     ///
1153     /// To transmute the inner type of the contents of a container, you must make sure to not
1154     /// violate any of the container's invariants. For `Vec`, this means that both the size
1155     /// *and alignment* of the inner types have to match. Other containers might rely on the
1156     /// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't
1157     /// be possible at all without violating the container invariants.
1158     ///
1159     /// ```
1160     /// let store = [0, 1, 2, 3];
1161     /// let v_orig = store.iter().collect::<Vec<&i32>>();
1162     ///
1163     /// // clone the vector as we will reuse them later
1164     /// let v_clone = v_orig.clone();
1165     ///
1166     /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
1167     /// // bad idea and could cause Undefined Behavior.
1168     /// // However, it is no-copy.
1169     /// let v_transmuted = unsafe {
1170     ///     std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
1171     /// };
1172     ///
1173     /// let v_clone = v_orig.clone();
1174     ///
1175     /// // This is the suggested, safe way.
1176     /// // It does copy the entire vector, though, into a new array.
1177     /// let v_collected = v_clone.into_iter()
1178     ///                          .map(Some)
1179     ///                          .collect::<Vec<Option<&i32>>>();
1180     ///
1181     /// let v_clone = v_orig.clone();
1182     ///
1183     /// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the
1184     /// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but
1185     /// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`),
1186     /// // this has all the same caveats. Besides the information provided above, also consult the
1187     /// // [`from_raw_parts`] documentation.
1188     /// let v_from_raw = unsafe {
1189     // FIXME Update this when vec_into_raw_parts is stabilized
1190     ///     // Ensure the original vector is not dropped.
1191     ///     let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
1192     ///     Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
1193     ///                         v_clone.len(),
1194     ///                         v_clone.capacity())
1195     /// };
1196     /// ```
1197     ///
1198     /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
1199     ///
1200     /// Implementing `split_at_mut`:
1201     ///
1202     /// ```
1203     /// use std::{slice, mem};
1204     ///
1205     /// // There are multiple ways to do this, and there are multiple problems
1206     /// // with the following (transmute) way.
1207     /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
1208     ///                              -> (&mut [T], &mut [T]) {
1209     ///     let len = slice.len();
1210     ///     assert!(mid <= len);
1211     ///     unsafe {
1212     ///         let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
1213     ///         // first: transmute is not type safe; all it checks is that T and
1214     ///         // U are of the same size. Second, right here, you have two
1215     ///         // mutable references pointing to the same memory.
1216     ///         (&mut slice[0..mid], &mut slice2[mid..len])
1217     ///     }
1218     /// }
1219     ///
1220     /// // This gets rid of the type safety problems; `&mut *` will *only* give
1221     /// // you an `&mut T` from an `&mut T` or `*mut T`.
1222     /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
1223     ///                          -> (&mut [T], &mut [T]) {
1224     ///     let len = slice.len();
1225     ///     assert!(mid <= len);
1226     ///     unsafe {
1227     ///         let slice2 = &mut *(slice as *mut [T]);
1228     ///         // however, you still have two mutable references pointing to
1229     ///         // the same memory.
1230     ///         (&mut slice[0..mid], &mut slice2[mid..len])
1231     ///     }
1232     /// }
1233     ///
1234     /// // This is how the standard library does it. This is the best method, if
1235     /// // you need to do something like this
1236     /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
1237     ///                       -> (&mut [T], &mut [T]) {
1238     ///     let len = slice.len();
1239     ///     assert!(mid <= len);
1240     ///     unsafe {
1241     ///         let ptr = slice.as_mut_ptr();
1242     ///         // This now has three mutable references pointing at the same
1243     ///         // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
1244     ///         // `slice` is never used after `let ptr = ...`, and so one can
1245     ///         // treat it as "dead", and therefore, you only have two real
1246     ///         // mutable slices.
1247     ///         (slice::from_raw_parts_mut(ptr, mid),
1248     ///          slice::from_raw_parts_mut(ptr.add(mid), len - mid))
1249     ///     }
1250     /// }
1251     /// ```
1252     #[stable(feature = "rust1", since = "1.0.0")]
1253     #[rustc_allowed_through_unstable_modules]
1254     #[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
1255     #[rustc_diagnostic_item = "transmute"]
1256     pub fn transmute<Src, Dst>(src: Src) -> Dst;
1257
1258     /// Returns `true` if the actual type given as `T` requires drop
1259     /// glue; returns `false` if the actual type provided for `T`
1260     /// implements `Copy`.
1261     ///
1262     /// If the actual type neither requires drop glue nor implements
1263     /// `Copy`, then the return value of this function is unspecified.
1264     ///
1265     /// Note that, unlike most intrinsics, this is safe to call;
1266     /// it does not require an `unsafe` block.
1267     /// Therefore, implementations must not require the user to uphold
1268     /// any safety invariants.
1269     ///
1270     /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1271     #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1272     #[rustc_safe_intrinsic]
1273     pub fn needs_drop<T: ?Sized>() -> bool;
1274
1275     /// Calculates the offset from a pointer.
1276     ///
1277     /// This is implemented as an intrinsic to avoid converting to and from an
1278     /// integer, since the conversion would throw away aliasing information.
1279     ///
1280     /// # Safety
1281     ///
1282     /// Both the starting and resulting pointer must be either in bounds or one
1283     /// byte past the end of an allocated object. If either pointer is out of
1284     /// bounds or arithmetic overflow occurs then any further use of the
1285     /// returned value will result in undefined behavior.
1286     ///
1287     /// The stabilized version of this intrinsic is [`pointer::offset`].
1288     #[must_use = "returns a new pointer rather than modifying its argument"]
1289     #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1290     pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1291
1292     /// Calculates the offset from a pointer, potentially wrapping.
1293     ///
1294     /// This is implemented as an intrinsic to avoid converting to and from an
1295     /// integer, since the conversion inhibits certain optimizations.
1296     ///
1297     /// # Safety
1298     ///
1299     /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1300     /// resulting pointer to point into or one byte past the end of an allocated
1301     /// object, and it wraps with two's complement arithmetic. The resulting
1302     /// value is not necessarily valid to be used to actually access memory.
1303     ///
1304     /// The stabilized version of this intrinsic is [`pointer::wrapping_offset`].
1305     #[must_use = "returns a new pointer rather than modifying its argument"]
1306     #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1307     pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1308
1309     /// Masks out bits of the pointer according to a mask.
1310     ///
1311     /// Note that, unlike most intrinsics, this is safe to call;
1312     /// it does not require an `unsafe` block.
1313     /// Therefore, implementations must not require the user to uphold
1314     /// any safety invariants.
1315     ///
1316     /// Consider using [`pointer::mask`] instead.
1317     #[rustc_safe_intrinsic]
1318     pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;
1319
1320     /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1321     /// a size of `count` * `size_of::<T>()` and an alignment of
1322     /// `min_align_of::<T>()`
1323     ///
1324     /// The volatile parameter is set to `true`, so it will not be optimized out
1325     /// unless size is equal to zero.
1326     ///
1327     /// This intrinsic does not have a stable counterpart.
1328     pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1329     /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1330     /// a size of `count * size_of::<T>()` and an alignment of
1331     /// `min_align_of::<T>()`
1332     ///
1333     /// The volatile parameter is set to `true`, so it will not be optimized out
1334     /// unless size is equal to zero.
1335     ///
1336     /// This intrinsic does not have a stable counterpart.
1337     pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1338     /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1339     /// size of `count * size_of::<T>()` and an alignment of
1340     /// `min_align_of::<T>()`.
1341     ///
1342     /// The volatile parameter is set to `true`, so it will not be optimized out
1343     /// unless size is equal to zero.
1344     ///
1345     /// This intrinsic does not have a stable counterpart.
1346     pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1347
1348     /// Performs a volatile load from the `src` pointer.
1349     ///
1350     /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`].
1351     pub fn volatile_load<T>(src: *const T) -> T;
1352     /// Performs a volatile store to the `dst` pointer.
1353     ///
1354     /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`].
1355     pub fn volatile_store<T>(dst: *mut T, val: T);
1356
1357     /// Performs a volatile load from the `src` pointer
1358     /// The pointer is not required to be aligned.
1359     ///
1360     /// This intrinsic does not have a stable counterpart.
1361     pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1362     /// Performs a volatile store to the `dst` pointer.
1363     /// The pointer is not required to be aligned.
1364     ///
1365     /// This intrinsic does not have a stable counterpart.
1366     pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1367
1368     /// Returns the square root of an `f32`
1369     ///
1370     /// The stabilized version of this intrinsic is
1371     /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1372     pub fn sqrtf32(x: f32) -> f32;
1373     /// Returns the square root of an `f64`
1374     ///
1375     /// The stabilized version of this intrinsic is
1376     /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1377     pub fn sqrtf64(x: f64) -> f64;
1378
1379     /// Raises an `f32` to an integer power.
1380     ///
1381     /// The stabilized version of this intrinsic is
1382     /// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1383     pub fn powif32(a: f32, x: i32) -> f32;
1384     /// Raises an `f64` to an integer power.
1385     ///
1386     /// The stabilized version of this intrinsic is
1387     /// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1388     pub fn powif64(a: f64, x: i32) -> f64;
1389
1390     /// Returns the sine of an `f32`.
1391     ///
1392     /// The stabilized version of this intrinsic is
1393     /// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1394     pub fn sinf32(x: f32) -> f32;
1395     /// Returns the sine of an `f64`.
1396     ///
1397     /// The stabilized version of this intrinsic is
1398     /// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1399     pub fn sinf64(x: f64) -> f64;
1400
1401     /// Returns the cosine of an `f32`.
1402     ///
1403     /// The stabilized version of this intrinsic is
1404     /// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1405     pub fn cosf32(x: f32) -> f32;
1406     /// Returns the cosine of an `f64`.
1407     ///
1408     /// The stabilized version of this intrinsic is
1409     /// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1410     pub fn cosf64(x: f64) -> f64;
1411
1412     /// Raises an `f32` to an `f32` power.
1413     ///
1414     /// The stabilized version of this intrinsic is
1415     /// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1416     pub fn powf32(a: f32, x: f32) -> f32;
1417     /// Raises an `f64` to an `f64` power.
1418     ///
1419     /// The stabilized version of this intrinsic is
1420     /// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1421     pub fn powf64(a: f64, x: f64) -> f64;
1422
1423     /// Returns the exponential of an `f32`.
1424     ///
1425     /// The stabilized version of this intrinsic is
1426     /// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1427     pub fn expf32(x: f32) -> f32;
1428     /// Returns the exponential of an `f64`.
1429     ///
1430     /// The stabilized version of this intrinsic is
1431     /// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1432     pub fn expf64(x: f64) -> f64;
1433
1434     /// Returns 2 raised to the power of an `f32`.
1435     ///
1436     /// The stabilized version of this intrinsic is
1437     /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1438     pub fn exp2f32(x: f32) -> f32;
1439     /// Returns 2 raised to the power of an `f64`.
1440     ///
1441     /// The stabilized version of this intrinsic is
1442     /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1443     pub fn exp2f64(x: f64) -> f64;
1444
1445     /// Returns the natural logarithm of an `f32`.
1446     ///
1447     /// The stabilized version of this intrinsic is
1448     /// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1449     pub fn logf32(x: f32) -> f32;
1450     /// Returns the natural logarithm of an `f64`.
1451     ///
1452     /// The stabilized version of this intrinsic is
1453     /// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1454     pub fn logf64(x: f64) -> f64;
1455
1456     /// Returns the base 10 logarithm of an `f32`.
1457     ///
1458     /// The stabilized version of this intrinsic is
1459     /// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1460     pub fn log10f32(x: f32) -> f32;
1461     /// Returns the base 10 logarithm of an `f64`.
1462     ///
1463     /// The stabilized version of this intrinsic is
1464     /// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1465     pub fn log10f64(x: f64) -> f64;
1466
1467     /// Returns the base 2 logarithm of an `f32`.
1468     ///
1469     /// The stabilized version of this intrinsic is
1470     /// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1471     pub fn log2f32(x: f32) -> f32;
1472     /// Returns the base 2 logarithm of an `f64`.
1473     ///
1474     /// The stabilized version of this intrinsic is
1475     /// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1476     pub fn log2f64(x: f64) -> f64;
1477
1478     /// Returns `a * b + c` for `f32` values.
1479     ///
1480     /// The stabilized version of this intrinsic is
1481     /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1482     pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1483     /// Returns `a * b + c` for `f64` values.
1484     ///
1485     /// The stabilized version of this intrinsic is
1486     /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1487     pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1488
1489     /// Returns the absolute value of an `f32`.
1490     ///
1491     /// The stabilized version of this intrinsic is
1492     /// [`f32::abs`](../../std/primitive.f32.html#method.abs)
1493     pub fn fabsf32(x: f32) -> f32;
1494     /// Returns the absolute value of an `f64`.
1495     ///
1496     /// The stabilized version of this intrinsic is
1497     /// [`f64::abs`](../../std/primitive.f64.html#method.abs)
1498     pub fn fabsf64(x: f64) -> f64;
1499
1500     /// Returns the minimum of two `f32` values.
1501     ///
1502     /// Note that, unlike most intrinsics, this is safe to call;
1503     /// it does not require an `unsafe` block.
1504     /// Therefore, implementations must not require the user to uphold
1505     /// any safety invariants.
1506     ///
1507     /// The stabilized version of this intrinsic is
1508     /// [`f32::min`]
1509     #[rustc_safe_intrinsic]
1510     pub fn minnumf32(x: f32, y: f32) -> f32;
1511     /// Returns the minimum of two `f64` values.
1512     ///
1513     /// Note that, unlike most intrinsics, this is safe to call;
1514     /// it does not require an `unsafe` block.
1515     /// Therefore, implementations must not require the user to uphold
1516     /// any safety invariants.
1517     ///
1518     /// The stabilized version of this intrinsic is
1519     /// [`f64::min`]
1520     #[rustc_safe_intrinsic]
1521     pub fn minnumf64(x: f64, y: f64) -> f64;
1522     /// Returns the maximum of two `f32` values.
1523     ///
1524     /// Note that, unlike most intrinsics, this is safe to call;
1525     /// it does not require an `unsafe` block.
1526     /// Therefore, implementations must not require the user to uphold
1527     /// any safety invariants.
1528     ///
1529     /// The stabilized version of this intrinsic is
1530     /// [`f32::max`]
1531     #[rustc_safe_intrinsic]
1532     pub fn maxnumf32(x: f32, y: f32) -> f32;
1533     /// Returns the maximum of two `f64` values.
1534     ///
1535     /// Note that, unlike most intrinsics, this is safe to call;
1536     /// it does not require an `unsafe` block.
1537     /// Therefore, implementations must not require the user to uphold
1538     /// any safety invariants.
1539     ///
1540     /// The stabilized version of this intrinsic is
1541     /// [`f64::max`]
1542     #[rustc_safe_intrinsic]
1543     pub fn maxnumf64(x: f64, y: f64) -> f64;
1544
1545     /// Copies the sign from `y` to `x` for `f32` values.
1546     ///
1547     /// The stabilized version of this intrinsic is
1548     /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
1549     pub fn copysignf32(x: f32, y: f32) -> f32;
1550     /// Copies the sign from `y` to `x` for `f64` values.
1551     ///
1552     /// The stabilized version of this intrinsic is
1553     /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
1554     pub fn copysignf64(x: f64, y: f64) -> f64;
1555
1556     /// Returns the largest integer less than or equal to an `f32`.
1557     ///
1558     /// The stabilized version of this intrinsic is
1559     /// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1560     pub fn floorf32(x: f32) -> f32;
1561     /// Returns the largest integer less than or equal to an `f64`.
1562     ///
1563     /// The stabilized version of this intrinsic is
1564     /// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1565     pub fn floorf64(x: f64) -> f64;
1566
1567     /// Returns the smallest integer greater than or equal to an `f32`.
1568     ///
1569     /// The stabilized version of this intrinsic is
1570     /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1571     pub fn ceilf32(x: f32) -> f32;
1572     /// Returns the smallest integer greater than or equal to an `f64`.
1573     ///
1574     /// The stabilized version of this intrinsic is
1575     /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1576     pub fn ceilf64(x: f64) -> f64;
1577
1578     /// Returns the integer part of an `f32`.
1579     ///
1580     /// The stabilized version of this intrinsic is
1581     /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1582     pub fn truncf32(x: f32) -> f32;
1583     /// Returns the integer part of an `f64`.
1584     ///
1585     /// The stabilized version of this intrinsic is
1586     /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1587     pub fn truncf64(x: f64) -> f64;
1588
1589     /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1590     /// if the argument is not an integer.
1591     pub fn rintf32(x: f32) -> f32;
1592     /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1593     /// if the argument is not an integer.
1594     pub fn rintf64(x: f64) -> f64;
1595
1596     /// Returns the nearest integer to an `f32`.
1597     ///
1598     /// This intrinsic does not have a stable counterpart.
1599     pub fn nearbyintf32(x: f32) -> f32;
1600     /// Returns the nearest integer to an `f64`.
1601     ///
1602     /// This intrinsic does not have a stable counterpart.
1603     pub fn nearbyintf64(x: f64) -> f64;
1604
1605     /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1606     ///
1607     /// The stabilized version of this intrinsic is
1608     /// [`f32::round`](../../std/primitive.f32.html#method.round)
1609     pub fn roundf32(x: f32) -> f32;
1610     /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1611     ///
1612     /// The stabilized version of this intrinsic is
1613     /// [`f64::round`](../../std/primitive.f64.html#method.round)
1614     pub fn roundf64(x: f64) -> f64;
1615
1616     /// Float addition that allows optimizations based on algebraic rules.
1617     /// May assume inputs are finite.
1618     ///
1619     /// This intrinsic does not have a stable counterpart.
1620     pub fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1621
1622     /// Float subtraction that allows optimizations based on algebraic rules.
1623     /// May assume inputs are finite.
1624     ///
1625     /// This intrinsic does not have a stable counterpart.
1626     pub fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1627
1628     /// Float multiplication that allows optimizations based on algebraic rules.
1629     /// May assume inputs are finite.
1630     ///
1631     /// This intrinsic does not have a stable counterpart.
1632     pub fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1633
1634     /// Float division that allows optimizations based on algebraic rules.
1635     /// May assume inputs are finite.
1636     ///
1637     /// This intrinsic does not have a stable counterpart.
1638     pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1639
1640     /// Float remainder that allows optimizations based on algebraic rules.
1641     /// May assume inputs are finite.
1642     ///
1643     /// This intrinsic does not have a stable counterpart.
1644     pub fn frem_fast<T: Copy>(a: T, b: T) -> T;
1645
1646     /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1647     /// (<https://github.com/rust-lang/rust/issues/10184>)
1648     ///
1649     /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1650     pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
1651
1652     /// Returns the number of bits set in an integer type `T`
1653     ///
1654     /// Note that, unlike most intrinsics, this is safe to call;
1655     /// it does not require an `unsafe` block.
1656     /// Therefore, implementations must not require the user to uphold
1657     /// any safety invariants.
1658     ///
1659     /// The stabilized versions of this intrinsic are available on the integer
1660     /// primitives via the `count_ones` method. For example,
1661     /// [`u32::count_ones`]
1662     #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1663     #[rustc_safe_intrinsic]
1664     pub fn ctpop<T: Copy>(x: T) -> T;
1665
1666     /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1667     ///
1668     /// Note that, unlike most intrinsics, this is safe to call;
1669     /// it does not require an `unsafe` block.
1670     /// Therefore, implementations must not require the user to uphold
1671     /// any safety invariants.
1672     ///
1673     /// The stabilized versions of this intrinsic are available on the integer
1674     /// primitives via the `leading_zeros` method. For example,
1675     /// [`u32::leading_zeros`]
1676     ///
1677     /// # Examples
1678     ///
1679     /// ```
1680     /// #![feature(core_intrinsics)]
1681     ///
1682     /// use std::intrinsics::ctlz;
1683     ///
1684     /// let x = 0b0001_1100_u8;
1685     /// let num_leading = ctlz(x);
1686     /// assert_eq!(num_leading, 3);
1687     /// ```
1688     ///
1689     /// An `x` with value `0` will return the bit width of `T`.
1690     ///
1691     /// ```
1692     /// #![feature(core_intrinsics)]
1693     ///
1694     /// use std::intrinsics::ctlz;
1695     ///
1696     /// let x = 0u16;
1697     /// let num_leading = ctlz(x);
1698     /// assert_eq!(num_leading, 16);
1699     /// ```
1700     #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1701     #[rustc_safe_intrinsic]
1702     pub fn ctlz<T: Copy>(x: T) -> T;
1703
1704     /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1705     /// given an `x` with value `0`.
1706     ///
1707     /// This intrinsic does not have a stable counterpart.
1708     ///
1709     /// # Examples
1710     ///
1711     /// ```
1712     /// #![feature(core_intrinsics)]
1713     ///
1714     /// use std::intrinsics::ctlz_nonzero;
1715     ///
1716     /// let x = 0b0001_1100_u8;
1717     /// let num_leading = unsafe { ctlz_nonzero(x) };
1718     /// assert_eq!(num_leading, 3);
1719     /// ```
1720     #[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
1721     pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
1722
1723     /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1724     ///
1725     /// Note that, unlike most intrinsics, this is safe to call;
1726     /// it does not require an `unsafe` block.
1727     /// Therefore, implementations must not require the user to uphold
1728     /// any safety invariants.
1729     ///
1730     /// The stabilized versions of this intrinsic are available on the integer
1731     /// primitives via the `trailing_zeros` method. For example,
1732     /// [`u32::trailing_zeros`]
1733     ///
1734     /// # Examples
1735     ///
1736     /// ```
1737     /// #![feature(core_intrinsics)]
1738     ///
1739     /// use std::intrinsics::cttz;
1740     ///
1741     /// let x = 0b0011_1000_u8;
1742     /// let num_trailing = cttz(x);
1743     /// assert_eq!(num_trailing, 3);
1744     /// ```
1745     ///
1746     /// An `x` with value `0` will return the bit width of `T`:
1747     ///
1748     /// ```
1749     /// #![feature(core_intrinsics)]
1750     ///
1751     /// use std::intrinsics::cttz;
1752     ///
1753     /// let x = 0u16;
1754     /// let num_trailing = cttz(x);
1755     /// assert_eq!(num_trailing, 16);
1756     /// ```
1757     #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1758     #[rustc_safe_intrinsic]
1759     pub fn cttz<T: Copy>(x: T) -> T;
1760
1761     /// Like `cttz`, but extra-unsafe as it returns `undef` when
1762     /// given an `x` with value `0`.
1763     ///
1764     /// This intrinsic does not have a stable counterpart.
1765     ///
1766     /// # Examples
1767     ///
1768     /// ```
1769     /// #![feature(core_intrinsics)]
1770     ///
1771     /// use std::intrinsics::cttz_nonzero;
1772     ///
1773     /// let x = 0b0011_1000_u8;
1774     /// let num_trailing = unsafe { cttz_nonzero(x) };
1775     /// assert_eq!(num_trailing, 3);
1776     /// ```
1777     #[rustc_const_stable(feature = "const_cttz_nonzero", since = "1.53.0")]
1778     pub fn cttz_nonzero<T: Copy>(x: T) -> T;
1779
1780     /// Reverses the bytes in an integer type `T`.
1781     ///
1782     /// Note that, unlike most intrinsics, this is safe to call;
1783     /// it does not require an `unsafe` block.
1784     /// Therefore, implementations must not require the user to uphold
1785     /// any safety invariants.
1786     ///
1787     /// The stabilized versions of this intrinsic are available on the integer
1788     /// primitives via the `swap_bytes` method. For example,
1789     /// [`u32::swap_bytes`]
1790     #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1791     #[rustc_safe_intrinsic]
1792     pub fn bswap<T: Copy>(x: T) -> T;
1793
1794     /// Reverses the bits in an integer type `T`.
1795     ///
1796     /// Note that, unlike most intrinsics, this is safe to call;
1797     /// it does not require an `unsafe` block.
1798     /// Therefore, implementations must not require the user to uphold
1799     /// any safety invariants.
1800     ///
1801     /// The stabilized versions of this intrinsic are available on the integer
1802     /// primitives via the `reverse_bits` method. For example,
1803     /// [`u32::reverse_bits`]
1804     #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1805     #[rustc_safe_intrinsic]
1806     pub fn bitreverse<T: Copy>(x: T) -> T;
1807
1808     /// Performs checked integer addition.
1809     ///
1810     /// Note that, unlike most intrinsics, this is safe to call;
1811     /// it does not require an `unsafe` block.
1812     /// Therefore, implementations must not require the user to uphold
1813     /// any safety invariants.
1814     ///
1815     /// The stabilized versions of this intrinsic are available on the integer
1816     /// primitives via the `overflowing_add` method. For example,
1817     /// [`u32::overflowing_add`]
1818     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1819     #[rustc_safe_intrinsic]
1820     pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1821
1822     /// Performs checked integer subtraction
1823     ///
1824     /// Note that, unlike most intrinsics, this is safe to call;
1825     /// it does not require an `unsafe` block.
1826     /// Therefore, implementations must not require the user to uphold
1827     /// any safety invariants.
1828     ///
1829     /// The stabilized versions of this intrinsic are available on the integer
1830     /// primitives via the `overflowing_sub` method. For example,
1831     /// [`u32::overflowing_sub`]
1832     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1833     #[rustc_safe_intrinsic]
1834     pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1835
1836     /// Performs checked integer multiplication
1837     ///
1838     /// Note that, unlike most intrinsics, this is safe to call;
1839     /// it does not require an `unsafe` block.
1840     /// Therefore, implementations must not require the user to uphold
1841     /// any safety invariants.
1842     ///
1843     /// The stabilized versions of this intrinsic are available on the integer
1844     /// primitives via the `overflowing_mul` method. For example,
1845     /// [`u32::overflowing_mul`]
1846     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1847     #[rustc_safe_intrinsic]
1848     pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1849
1850     /// Performs an exact division, resulting in undefined behavior where
1851     /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1852     ///
1853     /// This intrinsic does not have a stable counterpart.
1854     #[rustc_const_unstable(feature = "const_exact_div", issue = "none")]
1855     pub fn exact_div<T: Copy>(x: T, y: T) -> T;
1856
1857     /// Performs an unchecked division, resulting in undefined behavior
1858     /// where `y == 0` or `x == T::MIN && y == -1`
1859     ///
1860     /// Safe wrappers for this intrinsic are available on the integer
1861     /// primitives via the `checked_div` method. For example,
1862     /// [`u32::checked_div`]
1863     #[rustc_const_stable(feature = "const_int_unchecked_div", since = "1.52.0")]
1864     pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1865     /// Returns the remainder of an unchecked division, resulting in
1866     /// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
1867     ///
1868     /// Safe wrappers for this intrinsic are available on the integer
1869     /// primitives via the `checked_rem` method. For example,
1870     /// [`u32::checked_rem`]
1871     #[rustc_const_stable(feature = "const_int_unchecked_rem", since = "1.52.0")]
1872     pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1873
1874     /// Performs an unchecked left shift, resulting in undefined behavior when
1875     /// `y < 0` or `y >= N`, where N is the width of T in bits.
1876     ///
1877     /// Safe wrappers for this intrinsic are available on the integer
1878     /// primitives via the `checked_shl` method. For example,
1879     /// [`u32::checked_shl`]
1880     #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1881     pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
1882     /// Performs an unchecked right shift, resulting in undefined behavior when
1883     /// `y < 0` or `y >= N`, where N is the width of T in bits.
1884     ///
1885     /// Safe wrappers for this intrinsic are available on the integer
1886     /// primitives via the `checked_shr` method. For example,
1887     /// [`u32::checked_shr`]
1888     #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1889     pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
1890
1891     /// Returns the result of an unchecked addition, resulting in
1892     /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1893     ///
1894     /// This intrinsic does not have a stable counterpart.
1895     #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1896     pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1897
1898     /// Returns the result of an unchecked subtraction, resulting in
1899     /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1900     ///
1901     /// This intrinsic does not have a stable counterpart.
1902     #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1903     pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1904
1905     /// Returns the result of an unchecked multiplication, resulting in
1906     /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1907     ///
1908     /// This intrinsic does not have a stable counterpart.
1909     #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1910     pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1911
1912     /// Performs rotate left.
1913     ///
1914     /// Note that, unlike most intrinsics, this is safe to call;
1915     /// it does not require an `unsafe` block.
1916     /// Therefore, implementations must not require the user to uphold
1917     /// any safety invariants.
1918     ///
1919     /// The stabilized versions of this intrinsic are available on the integer
1920     /// primitives via the `rotate_left` method. For example,
1921     /// [`u32::rotate_left`]
1922     #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1923     #[rustc_safe_intrinsic]
1924     pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
1925
1926     /// Performs rotate right.
1927     ///
1928     /// Note that, unlike most intrinsics, this is safe to call;
1929     /// it does not require an `unsafe` block.
1930     /// Therefore, implementations must not require the user to uphold
1931     /// any safety invariants.
1932     ///
1933     /// The stabilized versions of this intrinsic are available on the integer
1934     /// primitives via the `rotate_right` method. For example,
1935     /// [`u32::rotate_right`]
1936     #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1937     #[rustc_safe_intrinsic]
1938     pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
1939
1940     /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1941     ///
1942     /// Note that, unlike most intrinsics, this is safe to call;
1943     /// it does not require an `unsafe` block.
1944     /// Therefore, implementations must not require the user to uphold
1945     /// any safety invariants.
1946     ///
1947     /// The stabilized versions of this intrinsic are available on the integer
1948     /// primitives via the `wrapping_add` method. For example,
1949     /// [`u32::wrapping_add`]
1950     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1951     #[rustc_safe_intrinsic]
1952     pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
1953     /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1954     ///
1955     /// Note that, unlike most intrinsics, this is safe to call;
1956     /// it does not require an `unsafe` block.
1957     /// Therefore, implementations must not require the user to uphold
1958     /// any safety invariants.
1959     ///
1960     /// The stabilized versions of this intrinsic are available on the integer
1961     /// primitives via the `wrapping_sub` method. For example,
1962     /// [`u32::wrapping_sub`]
1963     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1964     #[rustc_safe_intrinsic]
1965     pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
1966     /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1967     ///
1968     /// Note that, unlike most intrinsics, this is safe to call;
1969     /// it does not require an `unsafe` block.
1970     /// Therefore, implementations must not require the user to uphold
1971     /// any safety invariants.
1972     ///
1973     /// The stabilized versions of this intrinsic are available on the integer
1974     /// primitives via the `wrapping_mul` method. For example,
1975     /// [`u32::wrapping_mul`]
1976     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1977     #[rustc_safe_intrinsic]
1978     pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
1979
1980     /// Computes `a + b`, saturating at numeric bounds.
1981     ///
1982     /// Note that, unlike most intrinsics, this is safe to call;
1983     /// it does not require an `unsafe` block.
1984     /// Therefore, implementations must not require the user to uphold
1985     /// any safety invariants.
1986     ///
1987     /// The stabilized versions of this intrinsic are available on the integer
1988     /// primitives via the `saturating_add` method. For example,
1989     /// [`u32::saturating_add`]
1990     #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1991     #[rustc_safe_intrinsic]
1992     pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
1993     /// Computes `a - b`, saturating at numeric bounds.
1994     ///
1995     /// Note that, unlike most intrinsics, this is safe to call;
1996     /// it does not require an `unsafe` block.
1997     /// Therefore, implementations must not require the user to uphold
1998     /// any safety invariants.
1999     ///
2000     /// The stabilized versions of this intrinsic are available on the integer
2001     /// primitives via the `saturating_sub` method. For example,
2002     /// [`u32::saturating_sub`]
2003     #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
2004     #[rustc_safe_intrinsic]
2005     pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
2006
2007     /// Returns the value of the discriminant for the variant in 'v';
2008     /// if `T` has no discriminant, returns `0`.
2009     ///
2010     /// Note that, unlike most intrinsics, this is safe to call;
2011     /// it does not require an `unsafe` block.
2012     /// Therefore, implementations must not require the user to uphold
2013     /// any safety invariants.
2014     ///
2015     /// The stabilized version of this intrinsic is [`core::mem::discriminant`].
2016     #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
2017     #[rustc_safe_intrinsic]
2018     pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
2019
2020     /// Returns the number of variants of the type `T` cast to a `usize`;
2021     /// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
2022     ///
2023     /// Note that, unlike most intrinsics, this is safe to call;
2024     /// it does not require an `unsafe` block.
2025     /// Therefore, implementations must not require the user to uphold
2026     /// any safety invariants.
2027     ///
2028     /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
2029     #[rustc_const_unstable(feature = "variant_count", issue = "73662")]
2030     #[rustc_safe_intrinsic]
2031     pub fn variant_count<T>() -> usize;
2032
2033     /// Rust's "try catch" construct which invokes the function pointer `try_fn`
2034     /// with the data pointer `data`.
2035     ///
2036     /// The third argument is a function called if a panic occurs. This function
2037     /// takes the data pointer and a pointer to the target-specific exception
2038     /// object that was caught. For more information see the compiler's
2039     /// source as well as std's catch implementation.
2040     pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
2041
2042     /// Emits a `!nontemporal` store according to LLVM (see their docs).
2043     /// Probably will never become stable.
2044     pub fn nontemporal_store<T>(ptr: *mut T, val: T);
2045
2046     /// See documentation of `<*const T>::offset_from` for details.
2047     #[rustc_const_stable(feature = "const_ptr_offset_from", since = "1.65.0")]
2048     pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
2049
2050     /// See documentation of `<*const T>::sub_ptr` for details.
2051     #[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
2052     pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
2053
2054     /// See documentation of `<*const T>::guaranteed_eq` for details.
2055     /// Returns `2` if the result is unknown.
2056     /// Returns `1` if the pointers are guaranteed equal
2057     /// Returns `0` if the pointers are guaranteed inequal
2058     ///
2059     /// Note that, unlike most intrinsics, this is safe to call;
2060     /// it does not require an `unsafe` block.
2061     /// Therefore, implementations must not require the user to uphold
2062     /// any safety invariants.
2063     #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
2064     #[rustc_safe_intrinsic]
2065     pub fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8;
2066
2067     /// Allocates a block of memory at compile time.
2068     /// At runtime, just returns a null pointer.
2069     ///
2070     /// # Safety
2071     ///
2072     /// - The `align` argument must be a power of two.
2073     ///    - At compile time, a compile error occurs if this constraint is violated.
2074     ///    - At runtime, it is not checked.
2075     #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2076     pub fn const_allocate(size: usize, align: usize) -> *mut u8;
2077
2078     /// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
2079     /// At runtime, does nothing.
2080     ///
2081     /// # Safety
2082     ///
2083     /// - The `align` argument must be a power of two.
2084     ///    - At compile time, a compile error occurs if this constraint is violated.
2085     ///    - At runtime, it is not checked.
2086     /// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
2087     /// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
2088     #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2089     pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);
2090
2091     /// Determines whether the raw bytes of the two values are equal.
2092     ///
2093     /// This is particularly handy for arrays, since it allows things like just
2094     /// comparing `i96`s instead of forcing `alloca`s for `[6 x i16]`.
2095     ///
2096     /// Above some backend-decided threshold this will emit calls to `memcmp`,
2097     /// like slice equality does, instead of causing massive code size.
2098     ///
2099     /// # Safety
2100     ///
2101     /// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized or carry a
2102     /// pointer value.
2103     /// Note that this is a stricter criterion than just the *values* being
2104     /// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
2105     ///
2106     /// (The implementation is allowed to branch on the results of comparisons,
2107     /// which is UB if any of their inputs are `undef`.)
2108     #[rustc_const_unstable(feature = "const_intrinsic_raw_eq", issue = "none")]
2109     pub fn raw_eq<T>(a: &T, b: &T) -> bool;
2110
2111     /// See documentation of [`std::hint::black_box`] for details.
2112     ///
2113     /// [`std::hint::black_box`]: crate::hint::black_box
2114     #[rustc_const_unstable(feature = "const_black_box", issue = "none")]
2115     #[rustc_safe_intrinsic]
2116     pub fn black_box<T>(dummy: T) -> T;
2117
2118     /// `ptr` must point to a vtable.
2119     /// The intrinsic will return the size stored in that vtable.
2120     pub fn vtable_size(ptr: *const ()) -> usize;
2121
2122     /// `ptr` must point to a vtable.
2123     /// The intrinsic will return the alignment stored in that vtable.
2124     pub fn vtable_align(ptr: *const ()) -> usize;
2125
2126     /// Selects which function to call depending on the context.
2127     ///
2128     /// If this function is evaluated at compile-time, then a call to this
2129     /// intrinsic will be replaced with a call to `called_in_const`. It gets
2130     /// replaced with a call to `called_at_rt` otherwise.
2131     ///
2132     /// # Type Requirements
2133     ///
2134     /// The two functions must be both function items. They cannot be function
2135     /// pointers or closures. The first function must be a `const fn`.
2136     ///
2137     /// `arg` will be the tupled arguments that will be passed to either one of
2138     /// the two functions, therefore, both functions must accept the same type of
2139     /// arguments. Both functions must return RET.
2140     ///
2141     /// # Safety
2142     ///
2143     /// The two functions must behave observably equivalent. Safe code in other
2144     /// crates may assume that calling a `const fn` at compile-time and at run-time
2145     /// produces the same result. A function that produces a different result when
2146     /// evaluated at run-time, or has any other observable side-effects, is
2147     /// *unsound*.
2148     ///
2149     /// Here is an example of how this could cause a problem:
2150     /// ```no_run
2151     /// #![feature(const_eval_select)]
2152     /// #![feature(core_intrinsics)]
2153     /// use std::hint::unreachable_unchecked;
2154     /// use std::intrinsics::const_eval_select;
2155     ///
2156     /// // Crate A
2157     /// pub const fn inconsistent() -> i32 {
2158     ///     fn runtime() -> i32 { 1 }
2159     ///     const fn compiletime() -> i32 { 2 }
2160     ///
2161     ///     unsafe {
2162     //          // ⚠ This code violates the required equivalence of `compiletime`
2163     ///         // and `runtime`.
2164     ///         const_eval_select((), compiletime, runtime)
2165     ///     }
2166     /// }
2167     ///
2168     /// // Crate B
2169     /// const X: i32 = inconsistent();
2170     /// let x = inconsistent();
2171     /// if x != X { unsafe { unreachable_unchecked(); }}
2172     /// ```
2173     ///
2174     /// This code causes Undefined Behavior when being run, since the
2175     /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2176     /// which violates the principle that a `const fn` must behave the same at
2177     /// compile-time and at run-time. The unsafe code in crate B is fine.
2178     #[cfg(bootstrap)]
2179     #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2180     pub fn const_eval_select<ARG, F, G, RET>(arg: ARG, called_in_const: F, called_at_rt: G) -> RET
2181     where
2182         G: FnOnce<ARG, Output = RET>,
2183         F: FnOnce<ARG, Output = RET>;
2184
2185     /// Selects which function to call depending on the context.
2186     ///
2187     /// If this function is evaluated at compile-time, then a call to this
2188     /// intrinsic will be replaced with a call to `called_in_const`. It gets
2189     /// replaced with a call to `called_at_rt` otherwise.
2190     ///
2191     /// # Type Requirements
2192     ///
2193     /// The two functions must be both function items. They cannot be function
2194     /// pointers or closures. The first function must be a `const fn`.
2195     ///
2196     /// `arg` will be the tupled arguments that will be passed to either one of
2197     /// the two functions, therefore, both functions must accept the same type of
2198     /// arguments. Both functions must return RET.
2199     ///
2200     /// # Safety
2201     ///
2202     /// The two functions must behave observably equivalent. Safe code in other
2203     /// crates may assume that calling a `const fn` at compile-time and at run-time
2204     /// produces the same result. A function that produces a different result when
2205     /// evaluated at run-time, or has any other observable side-effects, is
2206     /// *unsound*.
2207     ///
2208     /// Here is an example of how this could cause a problem:
2209     /// ```no_run
2210     /// #![feature(const_eval_select)]
2211     /// #![feature(core_intrinsics)]
2212     /// use std::hint::unreachable_unchecked;
2213     /// use std::intrinsics::const_eval_select;
2214     ///
2215     /// // Crate A
2216     /// pub const fn inconsistent() -> i32 {
2217     ///     fn runtime() -> i32 { 1 }
2218     ///     const fn compiletime() -> i32 { 2 }
2219     ///
2220     ///     unsafe {
2221     //          // ⚠ This code violates the required equivalence of `compiletime`
2222     ///         // and `runtime`.
2223     ///         const_eval_select((), compiletime, runtime)
2224     ///     }
2225     /// }
2226     ///
2227     /// // Crate B
2228     /// const X: i32 = inconsistent();
2229     /// let x = inconsistent();
2230     /// if x != X { unsafe { unreachable_unchecked(); }}
2231     /// ```
2232     ///
2233     /// This code causes Undefined Behavior when being run, since the
2234     /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2235     /// which violates the principle that a `const fn` must behave the same at
2236     /// compile-time and at run-time. The unsafe code in crate B is fine.
2237     #[cfg(not(bootstrap))]
2238     #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2239     pub fn const_eval_select<ARG: Tuple, F, G, RET>(
2240         arg: ARG,
2241         called_in_const: F,
2242         called_at_rt: G,
2243     ) -> RET
2244     where
2245         G: FnOnce<ARG, Output = RET>,
2246         F: FnOnce<ARG, Output = RET>;
2247 }
2248
2249 // Some functions are defined here because they accidentally got made
2250 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
2251 // (`transmute` also falls into this category, but it cannot be wrapped due to the
2252 // check that `T` and `U` have the same size.)
2253
2254 /// Check that the preconditions of an unsafe function are followed, if debug_assertions are on,
2255 /// and only at runtime.
2256 ///
2257 /// This macro should be called as `assert_unsafe_precondition!([Generics](name: Type) => Expression)`
2258 /// where the names specified will be moved into the macro as captured variables, and defines an item
2259 /// to call `const_eval_select` on. The tokens inside the square brackets are used to denote generics
2260 /// for the function declaractions and can be omitted if there is no generics.
2261 ///
2262 /// # Safety
2263 ///
2264 /// Invoking this macro is only sound if the following code is already UB when the passed
2265 /// expression evaluates to false.
2266 ///
2267 /// This macro expands to a check at runtime if debug_assertions is set. It has no effect at
2268 /// compile time, but the semantics of the contained `const_eval_select` must be the same at
2269 /// runtime and at compile time. Thus if the expression evaluates to false, this macro produces
2270 /// different behavior at compile time and at runtime, and invoking it is incorrect.
2271 ///
2272 /// So in a sense it is UB if this macro is useful, but we expect callers of `unsafe fn` to make
2273 /// the occasional mistake, and this check should help them figure things out.
2274 #[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn
2275 macro_rules! assert_unsafe_precondition {
2276     ($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr) => {
2277         if cfg!(debug_assertions) {
2278             // allow non_snake_case to allow capturing const generics
2279             #[allow(non_snake_case)]
2280             #[inline(always)]
2281             fn runtime$(<$($tt)*>)?($($i:$ty),*) {
2282                 if !$e {
2283                     // don't unwind to reduce impact on code size
2284                     ::core::panicking::panic_str_nounwind(
2285                         concat!("unsafe precondition(s) violated: ", $name)
2286                     );
2287                 }
2288             }
2289             #[allow(non_snake_case)]
2290             const fn comptime$(<$($tt)*>)?($(_:$ty),*) {}
2291
2292             ::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
2293         }
2294     };
2295 }
2296 pub(crate) use assert_unsafe_precondition;
2297
2298 /// Checks whether `ptr` is properly aligned with respect to
2299 /// `align_of::<T>()`.
2300 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
2301     !ptr.is_null() && ptr.is_aligned()
2302 }
2303
2304 /// Checks whether an allocation of `len` instances of `T` exceeds
2305 /// the maximum allowed allocation size.
2306 pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool {
2307     let max_len = const {
2308         let size = crate::mem::size_of::<T>();
2309         if size == 0 { usize::MAX } else { isize::MAX as usize / size }
2310     };
2311     len <= max_len
2312 }
2313
2314 /// Checks whether the regions of memory starting at `src` and `dst` of size
2315 /// `count * size_of::<T>()` do *not* overlap.
2316 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
2317     let src_usize = src.addr();
2318     let dst_usize = dst.addr();
2319     let size = mem::size_of::<T>().checked_mul(count).unwrap();
2320     let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
2321     // If the absolute distance between the ptrs is at least as big as the size of the buffer,
2322     // they do not overlap.
2323     diff >= size
2324 }
2325
2326 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2327 /// and destination must *not* overlap.
2328 ///
2329 /// For regions of memory which might overlap, use [`copy`] instead.
2330 ///
2331 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
2332 /// with the argument order swapped.
2333 ///
2334 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2335 /// requirements of `T`. The initialization state is preserved exactly.
2336 ///
2337 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
2338 ///
2339 /// # Safety
2340 ///
2341 /// Behavior is undefined if any of the following conditions are violated:
2342 ///
2343 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2344 ///
2345 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2346 ///
2347 /// * Both `src` and `dst` must be properly aligned.
2348 ///
2349 /// * The region of memory beginning at `src` with a size of `count *
2350 ///   size_of::<T>()` bytes must *not* overlap with the region of memory
2351 ///   beginning at `dst` with the same size.
2352 ///
2353 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
2354 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
2355 /// in the region beginning at `*src` and the region beginning at `*dst` can
2356 /// [violate memory safety][read-ownership].
2357 ///
2358 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2359 /// `0`, the pointers must be non-null and properly aligned.
2360 ///
2361 /// [`read`]: crate::ptr::read
2362 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2363 /// [valid]: crate::ptr#safety
2364 ///
2365 /// # Examples
2366 ///
2367 /// Manually implement [`Vec::append`]:
2368 ///
2369 /// ```
2370 /// use std::ptr;
2371 ///
2372 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
2373 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
2374 ///     let src_len = src.len();
2375 ///     let dst_len = dst.len();
2376 ///
2377 ///     // Ensure that `dst` has enough capacity to hold all of `src`.
2378 ///     dst.reserve(src_len);
2379 ///
2380 ///     unsafe {
2381 ///         // The call to add is always safe because `Vec` will never
2382 ///         // allocate more than `isize::MAX` bytes.
2383 ///         let dst_ptr = dst.as_mut_ptr().add(dst_len);
2384 ///         let src_ptr = src.as_ptr();
2385 ///
2386 ///         // Truncate `src` without dropping its contents. We do this first,
2387 ///         // to avoid problems in case something further down panics.
2388 ///         src.set_len(0);
2389 ///
2390 ///         // The two regions cannot overlap because mutable references do
2391 ///         // not alias, and two different vectors cannot own the same
2392 ///         // memory.
2393 ///         ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
2394 ///
2395 ///         // Notify `dst` that it now holds the contents of `src`.
2396 ///         dst.set_len(dst_len + src_len);
2397 ///     }
2398 /// }
2399 ///
2400 /// let mut a = vec!['r'];
2401 /// let mut b = vec!['u', 's', 't'];
2402 ///
2403 /// append(&mut a, &mut b);
2404 ///
2405 /// assert_eq!(a, &['r', 'u', 's', 't']);
2406 /// assert!(b.is_empty());
2407 /// ```
2408 ///
2409 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
2410 #[doc(alias = "memcpy")]
2411 #[stable(feature = "rust1", since = "1.0.0")]
2412 #[rustc_allowed_through_unstable_modules]
2413 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2414 #[inline]
2415 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2416 pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
2417     extern "rust-intrinsic" {
2418         #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2419         pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
2420     }
2421
2422     // SAFETY: the safety contract for `copy_nonoverlapping` must be
2423     // upheld by the caller.
2424     unsafe {
2425         assert_unsafe_precondition!(
2426             "ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
2427             and the specified memory ranges do not overlap",
2428             [T](src: *const T, dst: *mut T, count: usize) =>
2429             is_aligned_and_not_null(src)
2430                 && is_aligned_and_not_null(dst)
2431                 && is_nonoverlapping(src, dst, count)
2432         );
2433         copy_nonoverlapping(src, dst, count)
2434     }
2435 }
2436
2437 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2438 /// and destination may overlap.
2439 ///
2440 /// If the source and destination will *never* overlap,
2441 /// [`copy_nonoverlapping`] can be used instead.
2442 ///
2443 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
2444 /// order swapped. Copying takes place as if the bytes were copied from `src`
2445 /// to a temporary array and then copied from the array to `dst`.
2446 ///
2447 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2448 /// requirements of `T`. The initialization state is preserved exactly.
2449 ///
2450 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
2451 ///
2452 /// # Safety
2453 ///
2454 /// Behavior is undefined if any of the following conditions are violated:
2455 ///
2456 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2457 ///
2458 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2459 ///
2460 /// * Both `src` and `dst` must be properly aligned.
2461 ///
2462 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
2463 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
2464 /// in the region beginning at `*src` and the region beginning at `*dst` can
2465 /// [violate memory safety][read-ownership].
2466 ///
2467 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2468 /// `0`, the pointers must be non-null and properly aligned.
2469 ///
2470 /// [`read`]: crate::ptr::read
2471 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2472 /// [valid]: crate::ptr#safety
2473 ///
2474 /// # Examples
2475 ///
2476 /// Efficiently create a Rust vector from an unsafe buffer:
2477 ///
2478 /// ```
2479 /// use std::ptr;
2480 ///
2481 /// /// # Safety
2482 /// ///
2483 /// /// * `ptr` must be correctly aligned for its type and non-zero.
2484 /// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
2485 /// /// * Those elements must not be used after calling this function unless `T: Copy`.
2486 /// # #[allow(dead_code)]
2487 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
2488 ///     let mut dst = Vec::with_capacity(elts);
2489 ///
2490 ///     // SAFETY: Our precondition ensures the source is aligned and valid,
2491 ///     // and `Vec::with_capacity` ensures that we have usable space to write them.
2492 ///     ptr::copy(ptr, dst.as_mut_ptr(), elts);
2493 ///
2494 ///     // SAFETY: We created it with this much capacity earlier,
2495 ///     // and the previous `copy` has initialized these elements.
2496 ///     dst.set_len(elts);
2497 ///     dst
2498 /// }
2499 /// ```
2500 #[doc(alias = "memmove")]
2501 #[stable(feature = "rust1", since = "1.0.0")]
2502 #[rustc_allowed_through_unstable_modules]
2503 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2504 #[inline]
2505 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2506 pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
2507     extern "rust-intrinsic" {
2508         #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2509         fn copy<T>(src: *const T, dst: *mut T, count: usize);
2510     }
2511
2512     // SAFETY: the safety contract for `copy` must be upheld by the caller.
2513     unsafe {
2514         assert_unsafe_precondition!(
2515             "ptr::copy requires that both pointer arguments are aligned aligned and non-null",
2516             [T](src: *const T, dst: *mut T) =>
2517             is_aligned_and_not_null(src) && is_aligned_and_not_null(dst)
2518         );
2519         copy(src, dst, count)
2520     }
2521 }
2522
2523 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
2524 /// `val`.
2525 ///
2526 /// `write_bytes` is similar to C's [`memset`], but sets `count *
2527 /// size_of::<T>()` bytes to `val`.
2528 ///
2529 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
2530 ///
2531 /// # Safety
2532 ///
2533 /// Behavior is undefined if any of the following conditions are violated:
2534 ///
2535 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2536 ///
2537 /// * `dst` must be properly aligned.
2538 ///
2539 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2540 /// `0`, the pointer must be non-null and properly aligned.
2541 ///
2542 /// Additionally, note that changing `*dst` in this way can easily lead to undefined behavior (UB)
2543 /// later if the written bytes are not a valid representation of some `T`. For instance, the
2544 /// following is an **incorrect** use of this function:
2545 ///
2546 /// ```rust,no_run
2547 /// unsafe {
2548 ///     let mut value: u8 = 0;
2549 ///     let ptr: *mut bool = &mut value as *mut u8 as *mut bool;
2550 ///     let _bool = ptr.read(); // This is fine, `ptr` points to a valid `bool`.
2551 ///     ptr.write_bytes(42u8, 1); // This function itself does not cause UB...
2552 ///     let _bool = ptr.read(); // ...but it makes this operation UB! ⚠️
2553 /// }
2554 /// ```
2555 ///
2556 /// [valid]: crate::ptr#safety
2557 ///
2558 /// # Examples
2559 ///
2560 /// Basic usage:
2561 ///
2562 /// ```
2563 /// use std::ptr;
2564 ///
2565 /// let mut vec = vec![0u32; 4];
2566 /// unsafe {
2567 ///     let vec_ptr = vec.as_mut_ptr();
2568 ///     ptr::write_bytes(vec_ptr, 0xfe, 2);
2569 /// }
2570 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
2571 /// ```
2572 #[doc(alias = "memset")]
2573 #[stable(feature = "rust1", since = "1.0.0")]
2574 #[rustc_allowed_through_unstable_modules]
2575 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2576 #[inline]
2577 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2578 pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
2579     extern "rust-intrinsic" {
2580         #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2581         fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
2582     }
2583
2584     // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
2585     unsafe {
2586         assert_unsafe_precondition!(
2587             "ptr::write_bytes requires that the destination pointer is aligned and non-null",
2588             [T](dst: *mut T) => is_aligned_and_not_null(dst)
2589         );
2590         write_bytes(dst, val, count)
2591     }
2592 }