]> git.lizzy.rs Git - rust.git/blob - src/libcore/intrinsics.rs
Rollup merge of #68473 - nopsledder:rust_sanitizer_fuchsia, r=alexcrichton
[rust.git] / src / libcore / intrinsics.rs
1 //! Compiler intrinsics.
2 //!
3 //! The corresponding definitions are in `librustc_codegen_llvm/intrinsic.rs`.
4 //! The corresponding const implementations are in `librustc_mir/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 //! `librustc_mir/interpret/intrinsics.rs` and add a
14 //! `#[rustc_const_unstable(feature = "foo", issue = "01234")]` to the intrinsic.
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 consulation, 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]: http://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]: http://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::mem;
58
59 #[stable(feature = "drop_in_place", since = "1.8.0")]
60 #[rustc_deprecated(
61     reason = "no longer an intrinsic - use `ptr::drop_in_place` directly",
62     since = "1.18.0"
63 )]
64 pub use crate::ptr::drop_in_place;
65
66 extern "rust-intrinsic" {
67     // N.B., these intrinsics take raw pointers because they mutate aliased
68     // memory, which is not valid for either `&` or `&mut`.
69
70     /// Stores a value if the current value is the same as the `old` value.
71     /// The stabilized version of this intrinsic is available on the
72     /// `std::sync::atomic` types via the `compare_exchange` method by passing
73     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
74     /// as both the `success` and `failure` parameters. For example,
75     /// [`AtomicBool::compare_exchange`][compare_exchange].
76     ///
77     /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
78     pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> (T, bool);
79     /// Stores a value if the current value is the same as the `old` value.
80     /// The stabilized version of this intrinsic is available on the
81     /// `std::sync::atomic` types via the `compare_exchange` method by passing
82     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
83     /// as both the `success` and `failure` parameters. For example,
84     /// [`AtomicBool::compare_exchange`][compare_exchange].
85     ///
86     /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
87     pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
88     /// Stores a value if the current value is the same as the `old` value.
89     /// The stabilized version of this intrinsic is available on the
90     /// `std::sync::atomic` types via the `compare_exchange` method by passing
91     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
92     /// as the `success` and
93     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
94     /// as the `failure` parameters. For example,
95     /// [`AtomicBool::compare_exchange`][compare_exchange].
96     ///
97     /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
98     pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
99     /// Stores a value if the current value is the same as the `old` value.
100     /// The stabilized version of this intrinsic is available on the
101     /// `std::sync::atomic` types via the `compare_exchange` method by passing
102     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
103     /// as the `success` and
104     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
105     /// as the `failure` parameters. For example,
106     /// [`AtomicBool::compare_exchange`][compare_exchange].
107     ///
108     /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
109     pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
110     /// Stores a value if the current value is the same as the `old` value.
111     /// The stabilized version of this intrinsic is available on the
112     /// `std::sync::atomic` types via the `compare_exchange` method by passing
113     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
114     /// as both the `success` and `failure` parameters. For example,
115     /// [`AtomicBool::compare_exchange`][compare_exchange].
116     ///
117     /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
118     pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
119     /// Stores a value if the current value is the same as the `old` value.
120     /// The stabilized version of this intrinsic is available on the
121     /// `std::sync::atomic` types via the `compare_exchange` method by passing
122     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
123     /// as the `success` and
124     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
125     /// as the `failure` parameters. For example,
126     /// [`AtomicBool::compare_exchange`][compare_exchange].
127     ///
128     /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
129     pub fn atomic_cxchg_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
130     /// Stores a value if the current value is the same as the `old` value.
131     /// The stabilized version of this intrinsic is available on the
132     /// `std::sync::atomic` types via the `compare_exchange` method by passing
133     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
134     /// as the `success` and
135     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
136     /// as the `failure` parameters. For example,
137     /// [`AtomicBool::compare_exchange`][compare_exchange].
138     ///
139     /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
140     pub fn atomic_cxchg_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
141     /// Stores a value if the current value is the same as the `old` value.
142     /// The stabilized version of this intrinsic is available on the
143     /// `std::sync::atomic` types via the `compare_exchange` method by passing
144     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
145     /// as the `success` and
146     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
147     /// as the `failure` parameters. For example,
148     /// [`AtomicBool::compare_exchange`][compare_exchange].
149     ///
150     /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
151     pub fn atomic_cxchg_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
152     /// Stores a value if the current value is the same as the `old` value.
153     /// The stabilized version of this intrinsic is available on the
154     /// `std::sync::atomic` types via the `compare_exchange` method by passing
155     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
156     /// as the `success` and
157     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
158     /// as the `failure` parameters. For example,
159     /// [`AtomicBool::compare_exchange`][compare_exchange].
160     ///
161     /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
162     pub fn atomic_cxchg_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
163
164     /// Stores a value if the current value is the same as the `old` value.
165     /// The stabilized version of this intrinsic is available on the
166     /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
167     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
168     /// as both the `success` and `failure` parameters. For example,
169     /// [`AtomicBool::compare_exchange_weak`][cew].
170     ///
171     /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
172     pub fn atomic_cxchgweak<T>(dst: *mut T, old: T, src: T) -> (T, bool);
173     /// Stores a value if the current value is the same as the `old` value.
174     /// The stabilized version of this intrinsic is available on the
175     /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
176     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
177     /// as both the `success` and `failure` parameters. For example,
178     /// [`AtomicBool::compare_exchange_weak`][cew].
179     ///
180     /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
181     pub fn atomic_cxchgweak_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
182     /// Stores a value if the current value is the same as the `old` value.
183     /// The stabilized version of this intrinsic is available on the
184     /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
185     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
186     /// as the `success` and
187     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
188     /// as the `failure` parameters. For example,
189     /// [`AtomicBool::compare_exchange_weak`][cew].
190     ///
191     /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
192     pub fn atomic_cxchgweak_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
193     /// Stores a value if the current value is the same as the `old` value.
194     /// The stabilized version of this intrinsic is available on the
195     /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
196     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
197     /// as the `success` and
198     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
199     /// as the `failure` parameters. For example,
200     /// [`AtomicBool::compare_exchange_weak`][cew].
201     ///
202     /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
203     pub fn atomic_cxchgweak_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
204     /// Stores a value if the current value is the same as the `old` value.
205     /// The stabilized version of this intrinsic is available on the
206     /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
207     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
208     /// as both the `success` and `failure` parameters. For example,
209     /// [`AtomicBool::compare_exchange_weak`][cew].
210     ///
211     /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
212     pub fn atomic_cxchgweak_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
213     /// Stores a value if the current value is the same as the `old` value.
214     /// The stabilized version of this intrinsic is available on the
215     /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
216     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
217     /// as the `success` and
218     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
219     /// as the `failure` parameters. For example,
220     /// [`AtomicBool::compare_exchange_weak`][cew].
221     ///
222     /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
223     pub fn atomic_cxchgweak_failrelaxed<T>(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     /// The stabilized version of this intrinsic is available on the
226     /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
227     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
228     /// as the `success` and
229     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
230     /// as the `failure` parameters. For example,
231     /// [`AtomicBool::compare_exchange_weak`][cew].
232     ///
233     /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
234     pub fn atomic_cxchgweak_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
235     /// Stores a value if the current value is the same as the `old` value.
236     /// The stabilized version of this intrinsic is available on the
237     /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
238     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
239     /// as the `success` and
240     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
241     /// as the `failure` parameters. For example,
242     /// [`AtomicBool::compare_exchange_weak`][cew].
243     ///
244     /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
245     pub fn atomic_cxchgweak_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
246     /// Stores a value if the current value is the same as the `old` value.
247     /// The stabilized version of this intrinsic is available on the
248     /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
249     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
250     /// as the `success` and
251     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
252     /// as the `failure` parameters. For example,
253     /// [`AtomicBool::compare_exchange_weak`][cew].
254     ///
255     /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
256     pub fn atomic_cxchgweak_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
257
258     /// Loads the current value of the pointer.
259     /// The stabilized version of this intrinsic is available on the
260     /// `std::sync::atomic` types via the `load` method by passing
261     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
262     /// as the `order`. For example,
263     /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
264     pub fn atomic_load<T>(src: *const T) -> T;
265     /// Loads the current value of the pointer.
266     /// The stabilized version of this intrinsic is available on the
267     /// `std::sync::atomic` types via the `load` method by passing
268     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
269     /// as the `order`. For example,
270     /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
271     pub fn atomic_load_acq<T>(src: *const T) -> T;
272     /// Loads the current value of the pointer.
273     /// The stabilized version of this intrinsic is available on the
274     /// `std::sync::atomic` types via the `load` method by passing
275     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
276     /// as the `order`. For example,
277     /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
278     pub fn atomic_load_relaxed<T>(src: *const T) -> T;
279     pub fn atomic_load_unordered<T>(src: *const T) -> T;
280
281     /// Stores the value at the specified memory location.
282     /// The stabilized version of this intrinsic is available on the
283     /// `std::sync::atomic` types via the `store` method by passing
284     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
285     /// as the `order`. For example,
286     /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
287     pub fn atomic_store<T>(dst: *mut T, val: T);
288     /// Stores the value at the specified memory location.
289     /// The stabilized version of this intrinsic is available on the
290     /// `std::sync::atomic` types via the `store` method by passing
291     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
292     /// as the `order`. For example,
293     /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
294     pub fn atomic_store_rel<T>(dst: *mut T, val: T);
295     /// Stores the value at the specified memory location.
296     /// The stabilized version of this intrinsic is available on the
297     /// `std::sync::atomic` types via the `store` method by passing
298     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
299     /// as the `order`. For example,
300     /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
301     pub fn atomic_store_relaxed<T>(dst: *mut T, val: T);
302     pub fn atomic_store_unordered<T>(dst: *mut T, val: T);
303
304     /// Stores the value at the specified memory location, returning the old value.
305     /// The stabilized version of this intrinsic is available on the
306     /// `std::sync::atomic` types via the `swap` method by passing
307     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
308     /// as the `order`. For example,
309     /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
310     pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
311     /// Stores the value at the specified memory location, returning the old value.
312     /// The stabilized version of this intrinsic is available on the
313     /// `std::sync::atomic` types via the `swap` method by passing
314     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
315     /// as the `order`. For example,
316     /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
317     pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
318     /// Stores the value at the specified memory location, returning the old value.
319     /// The stabilized version of this intrinsic is available on the
320     /// `std::sync::atomic` types via the `swap` method by passing
321     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
322     /// as the `order`. For example,
323     /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
324     pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
325     /// Stores the value at the specified memory location, returning the old value.
326     /// The stabilized version of this intrinsic is available on the
327     /// `std::sync::atomic` types via the `swap` method by passing
328     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
329     /// as the `order`. For example,
330     /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
331     pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T;
332     /// Stores the value at the specified memory location, returning the old value.
333     /// The stabilized version of this intrinsic is available on the
334     /// `std::sync::atomic` types via the `swap` method by passing
335     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
336     /// as the `order`. For example,
337     /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
338     pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T;
339
340     /// Adds to the current value, returning the previous value.
341     /// The stabilized version of this intrinsic is available on the
342     /// `std::sync::atomic` types via the `fetch_add` method by passing
343     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
344     /// as the `order`. For example,
345     /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
346     pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
347     /// Adds to the current value, returning the previous value.
348     /// The stabilized version of this intrinsic is available on the
349     /// `std::sync::atomic` types via the `fetch_add` method by passing
350     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
351     /// as the `order`. For example,
352     /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
353     pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
354     /// Adds to the current value, returning the previous value.
355     /// The stabilized version of this intrinsic is available on the
356     /// `std::sync::atomic` types via the `fetch_add` method by passing
357     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
358     /// as the `order`. For example,
359     /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
360     pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
361     /// Adds to the current value, returning the previous value.
362     /// The stabilized version of this intrinsic is available on the
363     /// `std::sync::atomic` types via the `fetch_add` method by passing
364     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
365     /// as the `order`. For example,
366     /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
367     pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T;
368     /// Adds to the current value, returning the previous value.
369     /// The stabilized version of this intrinsic is available on the
370     /// `std::sync::atomic` types via the `fetch_add` method by passing
371     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
372     /// as the `order`. For example,
373     /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
374     pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T;
375
376     /// Subtract from the current value, returning the previous value.
377     /// The stabilized version of this intrinsic is available on the
378     /// `std::sync::atomic` types via the `fetch_sub` method by passing
379     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
380     /// as the `order`. For example,
381     /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
382     pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
383     /// Subtract from the current value, returning the previous value.
384     /// The stabilized version of this intrinsic is available on the
385     /// `std::sync::atomic` types via the `fetch_sub` method by passing
386     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
387     /// as the `order`. For example,
388     /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
389     pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
390     /// Subtract from the current value, returning the previous value.
391     /// The stabilized version of this intrinsic is available on the
392     /// `std::sync::atomic` types via the `fetch_sub` method by passing
393     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
394     /// as the `order`. For example,
395     /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
396     pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
397     /// Subtract from the current value, returning the previous value.
398     /// The stabilized version of this intrinsic is available on the
399     /// `std::sync::atomic` types via the `fetch_sub` method by passing
400     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
401     /// as the `order`. For example,
402     /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
403     pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T;
404     /// Subtract from the current value, returning the previous value.
405     /// The stabilized version of this intrinsic is available on the
406     /// `std::sync::atomic` types via the `fetch_sub` method by passing
407     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
408     /// as the `order`. For example,
409     /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
410     pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T;
411
412     /// Bitwise and with the current value, returning the previous value.
413     /// The stabilized version of this intrinsic is available on the
414     /// `std::sync::atomic` types via the `fetch_and` method by passing
415     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
416     /// as the `order`. For example,
417     /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
418     pub fn atomic_and<T>(dst: *mut T, src: T) -> T;
419     /// Bitwise and with the current value, returning the previous value.
420     /// The stabilized version of this intrinsic is available on the
421     /// `std::sync::atomic` types via the `fetch_and` method by passing
422     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
423     /// as the `order`. For example,
424     /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
425     pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T;
426     /// Bitwise and with the current value, returning the previous value.
427     /// The stabilized version of this intrinsic is available on the
428     /// `std::sync::atomic` types via the `fetch_and` method by passing
429     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
430     /// as the `order`. For example,
431     /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
432     pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T;
433     /// Bitwise and with the current value, returning the previous value.
434     /// The stabilized version of this intrinsic is available on the
435     /// `std::sync::atomic` types via the `fetch_and` method by passing
436     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
437     /// as the `order`. For example,
438     /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
439     pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T;
440     /// Bitwise and with the current value, returning the previous value.
441     /// The stabilized version of this intrinsic is available on the
442     /// `std::sync::atomic` types via the `fetch_and` method by passing
443     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
444     /// as the `order`. For example,
445     /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
446     pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T;
447
448     /// Bitwise nand with the current value, returning the previous value.
449     /// The stabilized version of this intrinsic is available on the
450     /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
451     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
452     /// as the `order`. For example,
453     /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
454     pub fn atomic_nand<T>(dst: *mut T, src: T) -> T;
455     /// Bitwise nand with the current value, returning the previous value.
456     /// The stabilized version of this intrinsic is available on the
457     /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
458     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
459     /// as the `order`. For example,
460     /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
461     pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T;
462     /// Bitwise nand with the current value, returning the previous value.
463     /// The stabilized version of this intrinsic is available on the
464     /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
465     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
466     /// as the `order`. For example,
467     /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
468     pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T;
469     /// Bitwise nand with the current value, returning the previous value.
470     /// The stabilized version of this intrinsic is available on the
471     /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
472     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
473     /// as the `order`. For example,
474     /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
475     pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T;
476     /// Bitwise nand with the current value, returning the previous value.
477     /// The stabilized version of this intrinsic is available on the
478     /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
479     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
480     /// as the `order`. For example,
481     /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
482     pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T;
483
484     /// Bitwise or with the current value, returning the previous value.
485     /// The stabilized version of this intrinsic is available on the
486     /// `std::sync::atomic` types via the `fetch_or` method by passing
487     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
488     /// as the `order`. For example,
489     /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
490     pub fn atomic_or<T>(dst: *mut T, src: T) -> T;
491     /// Bitwise or with the current value, returning the previous value.
492     /// The stabilized version of this intrinsic is available on the
493     /// `std::sync::atomic` types via the `fetch_or` method by passing
494     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
495     /// as the `order`. For example,
496     /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
497     pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T;
498     /// Bitwise or with the current value, returning the previous value.
499     /// The stabilized version of this intrinsic is available on the
500     /// `std::sync::atomic` types via the `fetch_or` method by passing
501     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
502     /// as the `order`. For example,
503     /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
504     pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T;
505     /// Bitwise or with the current value, returning the previous value.
506     /// The stabilized version of this intrinsic is available on the
507     /// `std::sync::atomic` types via the `fetch_or` method by passing
508     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
509     /// as the `order`. For example,
510     /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
511     pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T;
512     /// Bitwise or with the current value, returning the previous value.
513     /// The stabilized version of this intrinsic is available on the
514     /// `std::sync::atomic` types via the `fetch_or` method by passing
515     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
516     /// as the `order`. For example,
517     /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
518     pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T;
519
520     /// Bitwise xor with the current value, returning the previous value.
521     /// The stabilized version of this intrinsic is available on the
522     /// `std::sync::atomic` types via the `fetch_xor` method by passing
523     /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
524     /// as the `order`. For example,
525     /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
526     pub fn atomic_xor<T>(dst: *mut T, src: T) -> T;
527     /// Bitwise xor with the current value, returning the previous value.
528     /// The stabilized version of this intrinsic is available on the
529     /// `std::sync::atomic` types via the `fetch_xor` method by passing
530     /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
531     /// as the `order`. For example,
532     /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
533     pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T;
534     /// Bitwise xor with the current value, returning the previous value.
535     /// The stabilized version of this intrinsic is available on the
536     /// `std::sync::atomic` types via the `fetch_xor` method by passing
537     /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
538     /// as the `order`. For example,
539     /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
540     pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T;
541     /// Bitwise xor with the current value, returning the previous value.
542     /// The stabilized version of this intrinsic is available on the
543     /// `std::sync::atomic` types via the `fetch_xor` method by passing
544     /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
545     /// as the `order`. For example,
546     /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
547     pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T;
548     /// Bitwise xor with the current value, returning the previous value.
549     /// The stabilized version of this intrinsic is available on the
550     /// `std::sync::atomic` types via the `fetch_xor` method by passing
551     /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
552     /// as the `order`. For example,
553     /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
554     pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T;
555
556     pub fn atomic_max<T>(dst: *mut T, src: T) -> T;
557     pub fn atomic_max_acq<T>(dst: *mut T, src: T) -> T;
558     pub fn atomic_max_rel<T>(dst: *mut T, src: T) -> T;
559     pub fn atomic_max_acqrel<T>(dst: *mut T, src: T) -> T;
560     pub fn atomic_max_relaxed<T>(dst: *mut T, src: T) -> T;
561
562     pub fn atomic_min<T>(dst: *mut T, src: T) -> T;
563     pub fn atomic_min_acq<T>(dst: *mut T, src: T) -> T;
564     pub fn atomic_min_rel<T>(dst: *mut T, src: T) -> T;
565     pub fn atomic_min_acqrel<T>(dst: *mut T, src: T) -> T;
566     pub fn atomic_min_relaxed<T>(dst: *mut T, src: T) -> T;
567
568     pub fn atomic_umin<T>(dst: *mut T, src: T) -> T;
569     pub fn atomic_umin_acq<T>(dst: *mut T, src: T) -> T;
570     pub fn atomic_umin_rel<T>(dst: *mut T, src: T) -> T;
571     pub fn atomic_umin_acqrel<T>(dst: *mut T, src: T) -> T;
572     pub fn atomic_umin_relaxed<T>(dst: *mut T, src: T) -> T;
573
574     pub fn atomic_umax<T>(dst: *mut T, src: T) -> T;
575     pub fn atomic_umax_acq<T>(dst: *mut T, src: T) -> T;
576     pub fn atomic_umax_rel<T>(dst: *mut T, src: T) -> T;
577     pub fn atomic_umax_acqrel<T>(dst: *mut T, src: T) -> T;
578     pub fn atomic_umax_relaxed<T>(dst: *mut T, src: T) -> T;
579
580     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
581     /// if supported; otherwise, it is a no-op.
582     /// Prefetches have no effect on the behavior of the program but can change its performance
583     /// characteristics.
584     ///
585     /// The `locality` argument must be a constant integer and is a temporal locality specifier
586     /// ranging from (0) - no locality, to (3) - extremely local keep in cache
587     pub fn prefetch_read_data<T>(data: *const T, locality: i32);
588     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
589     /// if supported; otherwise, it is a no-op.
590     /// Prefetches have no effect on the behavior of the program but can change its performance
591     /// characteristics.
592     ///
593     /// The `locality` argument must be a constant integer and is a temporal locality specifier
594     /// ranging from (0) - no locality, to (3) - extremely local keep in cache
595     pub fn prefetch_write_data<T>(data: *const T, locality: i32);
596     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
597     /// if supported; otherwise, it is a no-op.
598     /// Prefetches have no effect on the behavior of the program but can change its performance
599     /// characteristics.
600     ///
601     /// The `locality` argument must be a constant integer and is a temporal locality specifier
602     /// ranging from (0) - no locality, to (3) - extremely local keep in cache
603     pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
604     /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
605     /// if supported; otherwise, it is a no-op.
606     /// Prefetches have no effect on the behavior of the program but can change its performance
607     /// characteristics.
608     ///
609     /// The `locality` argument must be a constant integer and is a temporal locality specifier
610     /// ranging from (0) - no locality, to (3) - extremely local keep in cache
611     pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
612 }
613
614 extern "rust-intrinsic" {
615
616     pub fn atomic_fence();
617     pub fn atomic_fence_acq();
618     pub fn atomic_fence_rel();
619     pub fn atomic_fence_acqrel();
620
621     /// A compiler-only memory barrier.
622     ///
623     /// Memory accesses will never be reordered across this barrier by the
624     /// compiler, but no instructions will be emitted for it. This is
625     /// appropriate for operations on the same thread that may be preempted,
626     /// such as when interacting with signal handlers.
627     pub fn atomic_singlethreadfence();
628     pub fn atomic_singlethreadfence_acq();
629     pub fn atomic_singlethreadfence_rel();
630     pub fn atomic_singlethreadfence_acqrel();
631
632     /// Magic intrinsic that derives its meaning from attributes
633     /// attached to the function.
634     ///
635     /// For example, dataflow uses this to inject static assertions so
636     /// that `rustc_peek(potentially_uninitialized)` would actually
637     /// double-check that dataflow did indeed compute that it is
638     /// uninitialized at that point in the control flow.
639     pub fn rustc_peek<T>(_: T) -> T;
640
641     /// Aborts the execution of the process.
642     ///
643     /// The stabilized version of this intrinsic is
644     /// [`std::process::abort`](../../std/process/fn.abort.html)
645     pub fn abort() -> !;
646
647     /// Tells LLVM that this point in the code is not reachable, enabling
648     /// further optimizations.
649     ///
650     /// N.B., this is very different from the `unreachable!()` macro: Unlike the
651     /// macro, which panics when it is executed, it is *undefined behavior* to
652     /// reach code marked with this function.
653     ///
654     /// The stabilized version of this intrinsic is
655     /// [`std::hint::unreachable_unchecked`](../../std/hint/fn.unreachable_unchecked.html).
656     pub fn unreachable() -> !;
657
658     /// Informs the optimizer that a condition is always true.
659     /// If the condition is false, the behavior is undefined.
660     ///
661     /// No code is generated for this intrinsic, but the optimizer will try
662     /// to preserve it (and its condition) between passes, which may interfere
663     /// with optimization of surrounding code and reduce performance. It should
664     /// not be used if the invariant can be discovered by the optimizer on its
665     /// own, or if it does not enable any significant optimizations.
666     pub fn assume(b: bool);
667
668     /// Hints to the compiler that branch condition is likely to be true.
669     /// Returns the value passed to it.
670     ///
671     /// Any use other than with `if` statements will probably not have an effect.
672     pub fn likely(b: bool) -> bool;
673
674     /// Hints to the compiler that branch condition is likely to be false.
675     /// Returns the value passed to it.
676     ///
677     /// Any use other than with `if` statements will probably not have an effect.
678     pub fn unlikely(b: bool) -> bool;
679
680     /// Executes a breakpoint trap, for inspection by a debugger.
681     pub fn breakpoint();
682
683     /// The size of a type in bytes.
684     ///
685     /// More specifically, this is the offset in bytes between successive
686     /// items of the same type, including alignment padding.
687     ///
688     /// The stabilized version of this intrinsic is
689     /// [`std::mem::size_of`](../../std/mem/fn.size_of.html).
690     #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
691     pub fn size_of<T>() -> usize;
692
693     /// Moves a value to an uninitialized memory location.
694     ///
695     /// Drop glue is not run on the destination.
696     pub fn move_val_init<T>(dst: *mut T, src: T);
697
698     #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
699     pub fn min_align_of<T>() -> usize;
700     #[rustc_const_unstable(feature = "const_pref_align_of", issue = "none")]
701     pub fn pref_align_of<T>() -> usize;
702
703     /// The size of the referenced value in bytes.
704     ///
705     /// The stabilized version of this intrinsic is
706     /// [`std::mem::size_of_val`](../../std/mem/fn.size_of_val.html).
707     pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
708     pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
709
710     /// Gets a static string slice containing the name of a type.
711     #[rustc_const_unstable(feature = "const_type_name", issue = "none")]
712     pub fn type_name<T: ?Sized>() -> &'static str;
713
714     /// Gets an identifier which is globally unique to the specified type. This
715     /// function will return the same value for a type regardless of whichever
716     /// crate it is invoked in.
717     #[rustc_const_unstable(feature = "const_type_id", issue = "none")]
718     pub fn type_id<T: ?Sized + 'static>() -> u64;
719
720     /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
721     /// This will statically either panic, or do nothing.
722     pub fn panic_if_uninhabited<T>();
723
724     /// Gets a reference to a static `Location` indicating where it was called.
725     #[rustc_const_unstable(feature = "const_caller_location", issue = "47809")]
726     pub fn caller_location() -> &'static crate::panic::Location<'static>;
727
728     /// Creates a value initialized to zero.
729     ///
730     /// `init` is unsafe because it returns a zeroed-out datum,
731     /// which is unsafe unless `T` is `Copy`. Also, even if T is
732     /// `Copy`, an all-zero value may not correspond to any legitimate
733     /// state for the type in question.
734     #[unstable(
735         feature = "core_intrinsics",
736         reason = "intrinsics are unlikely to ever be stabilized, instead \
737                          they should be used through stabilized interfaces \
738                          in the rest of the standard library",
739         issue = "none"
740     )]
741     #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned", since = "1.38.0")]
742     pub fn init<T>() -> T;
743
744     /// Creates an uninitialized value.
745     ///
746     /// `uninit` is unsafe because there is no guarantee of what its
747     /// contents are. In particular its drop-flag may be set to any
748     /// state, which means it may claim either dropped or
749     /// undropped. In the general case one must use `ptr::write` to
750     /// initialize memory previous set to the result of `uninit`.
751     #[unstable(
752         feature = "core_intrinsics",
753         reason = "intrinsics are unlikely to ever be stabilized, instead \
754                          they should be used through stabilized interfaces \
755                          in the rest of the standard library",
756         issue = "none"
757     )]
758     #[rustc_deprecated(reason = "superseded by MaybeUninit, removal planned", since = "1.38.0")]
759     pub fn uninit<T>() -> T;
760
761     /// Moves a value out of scope without running drop glue.
762     pub fn forget<T: ?Sized>(_: T);
763
764     /// Reinterprets the bits of a value of one type as another type.
765     ///
766     /// Both types must have the same size. Neither the original, nor the result,
767     /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
768     ///
769     /// `transmute` is semantically equivalent to a bitwise move of one type
770     /// into another. It copies the bits from the source value into the
771     /// destination value, then forgets the original. It's equivalent to C's
772     /// `memcpy` under the hood, just like `transmute_copy`.
773     ///
774     /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
775     /// cause [undefined behavior][ub] with this function. `transmute` should be
776     /// the absolute last resort.
777     ///
778     /// The [nomicon](../../nomicon/transmutes.html) has additional
779     /// documentation.
780     ///
781     /// [ub]: ../../reference/behavior-considered-undefined.html
782     ///
783     /// # Examples
784     ///
785     /// There are a few things that `transmute` is really useful for.
786     ///
787     /// Turning a pointer into a function pointer. This is *not* portable to
788     /// machines where function pointers and data pointers have different sizes.
789     ///
790     /// ```
791     /// fn foo() -> i32 {
792     ///     0
793     /// }
794     /// let pointer = foo as *const ();
795     /// let function = unsafe {
796     ///     std::mem::transmute::<*const (), fn() -> i32>(pointer)
797     /// };
798     /// assert_eq!(function(), 0);
799     /// ```
800     ///
801     /// Extending a lifetime, or shortening an invariant lifetime. This is
802     /// advanced, very unsafe Rust!
803     ///
804     /// ```
805     /// struct R<'a>(&'a i32);
806     /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
807     ///     std::mem::transmute::<R<'b>, R<'static>>(r)
808     /// }
809     ///
810     /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
811     ///                                              -> &'b mut R<'c> {
812     ///     std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
813     /// }
814     /// ```
815     ///
816     /// # Alternatives
817     ///
818     /// Don't despair: many uses of `transmute` can be achieved through other means.
819     /// Below are common applications of `transmute` which can be replaced with safer
820     /// constructs.
821     ///
822     /// Turning a pointer into a `usize`:
823     ///
824     /// ```
825     /// let ptr = &0;
826     /// let ptr_num_transmute = unsafe {
827     ///     std::mem::transmute::<&i32, usize>(ptr)
828     /// };
829     ///
830     /// // Use an `as` cast instead
831     /// let ptr_num_cast = ptr as *const i32 as usize;
832     /// ```
833     ///
834     /// Turning a `*mut T` into an `&mut T`:
835     ///
836     /// ```
837     /// let ptr: *mut i32 = &mut 0;
838     /// let ref_transmuted = unsafe {
839     ///     std::mem::transmute::<*mut i32, &mut i32>(ptr)
840     /// };
841     ///
842     /// // Use a reborrow instead
843     /// let ref_casted = unsafe { &mut *ptr };
844     /// ```
845     ///
846     /// Turning an `&mut T` into an `&mut U`:
847     ///
848     /// ```
849     /// let ptr = &mut 0;
850     /// let val_transmuted = unsafe {
851     ///     std::mem::transmute::<&mut i32, &mut u32>(ptr)
852     /// };
853     ///
854     /// // Now, put together `as` and reborrowing - note the chaining of `as`
855     /// // `as` is not transitive
856     /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
857     /// ```
858     ///
859     /// Turning an `&str` into an `&[u8]`:
860     ///
861     /// ```
862     /// // this is not a good way to do this.
863     /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
864     /// assert_eq!(slice, &[82, 117, 115, 116]);
865     ///
866     /// // You could use `str::as_bytes`
867     /// let slice = "Rust".as_bytes();
868     /// assert_eq!(slice, &[82, 117, 115, 116]);
869     ///
870     /// // Or, just use a byte string, if you have control over the string
871     /// // literal
872     /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
873     /// ```
874     ///
875     /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
876     ///
877     /// ```
878     /// let store = [0, 1, 2, 3];
879     /// let v_orig = store.iter().collect::<Vec<&i32>>();
880     ///
881     /// // clone the vector as we will reuse them later
882     /// let v_clone = v_orig.clone();
883     ///
884     /// // Using transmute: this is Undefined Behavior, and a bad idea.
885     /// // However, it is no-copy.
886     /// let v_transmuted = unsafe {
887     ///     std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
888     /// };
889     ///
890     /// let v_clone = v_orig.clone();
891     ///
892     /// // This is the suggested, safe way.
893     /// // It does copy the entire vector, though, into a new array.
894     /// let v_collected = v_clone.into_iter()
895     ///                          .map(Some)
896     ///                          .collect::<Vec<Option<&i32>>>();
897     ///
898     /// let v_clone = v_orig.clone();
899     ///
900     /// // The no-copy, unsafe way, still using transmute, but not UB.
901     /// // This is equivalent to the original, but safer, and reuses the
902     /// // same `Vec` internals. Therefore, the new inner type must have the
903     /// // exact same size, and the same alignment, as the old type.
904     /// // The same caveats exist for this method as transmute, for
905     /// // the original inner type (`&i32`) to the converted inner type
906     /// // (`Option<&i32>`), so read the nomicon pages linked above.
907     /// let v_from_raw = unsafe {
908     // FIXME Update this when vec_into_raw_parts is stabilized
909     ///     // Ensure the original vector is not dropped.
910     ///     let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
911     ///     Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
912     ///                         v_clone.len(),
913     ///                         v_clone.capacity())
914     /// };
915     /// ```
916     ///
917     /// Implementing `split_at_mut`:
918     ///
919     /// ```
920     /// use std::{slice, mem};
921     ///
922     /// // There are multiple ways to do this, and there are multiple problems
923     /// // with the following (transmute) way.
924     /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
925     ///                              -> (&mut [T], &mut [T]) {
926     ///     let len = slice.len();
927     ///     assert!(mid <= len);
928     ///     unsafe {
929     ///         let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
930     ///         // first: transmute is not typesafe; all it checks is that T and
931     ///         // U are of the same size. Second, right here, you have two
932     ///         // mutable references pointing to the same memory.
933     ///         (&mut slice[0..mid], &mut slice2[mid..len])
934     ///     }
935     /// }
936     ///
937     /// // This gets rid of the typesafety problems; `&mut *` will *only* give
938     /// // you an `&mut T` from an `&mut T` or `*mut T`.
939     /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
940     ///                          -> (&mut [T], &mut [T]) {
941     ///     let len = slice.len();
942     ///     assert!(mid <= len);
943     ///     unsafe {
944     ///         let slice2 = &mut *(slice as *mut [T]);
945     ///         // however, you still have two mutable references pointing to
946     ///         // the same memory.
947     ///         (&mut slice[0..mid], &mut slice2[mid..len])
948     ///     }
949     /// }
950     ///
951     /// // This is how the standard library does it. This is the best method, if
952     /// // you need to do something like this
953     /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
954     ///                       -> (&mut [T], &mut [T]) {
955     ///     let len = slice.len();
956     ///     assert!(mid <= len);
957     ///     unsafe {
958     ///         let ptr = slice.as_mut_ptr();
959     ///         // This now has three mutable references pointing at the same
960     ///         // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
961     ///         // `slice` is never used after `let ptr = ...`, and so one can
962     ///         // treat it as "dead", and therefore, you only have two real
963     ///         // mutable slices.
964     ///         (slice::from_raw_parts_mut(ptr, mid),
965     ///          slice::from_raw_parts_mut(ptr.add(mid), len - mid))
966     ///     }
967     /// }
968     /// ```
969     #[stable(feature = "rust1", since = "1.0.0")]
970     #[rustc_const_unstable(feature = "const_transmute", issue = "53605")]
971     pub fn transmute<T, U>(e: T) -> U;
972
973     /// Returns `true` if the actual type given as `T` requires drop
974     /// glue; returns `false` if the actual type provided for `T`
975     /// implements `Copy`.
976     ///
977     /// If the actual type neither requires drop glue nor implements
978     /// `Copy`, then may return `true` or `false`.
979     ///
980     /// The stabilized version of this intrinsic is
981     /// [`std::mem::needs_drop`](../../std/mem/fn.needs_drop.html).
982     #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
983     pub fn needs_drop<T>() -> bool;
984
985     /// Calculates the offset from a pointer.
986     ///
987     /// This is implemented as an intrinsic to avoid converting to and from an
988     /// integer, since the conversion would throw away aliasing information.
989     ///
990     /// # Safety
991     ///
992     /// Both the starting and resulting pointer must be either in bounds or one
993     /// byte past the end of an allocated object. If either pointer is out of
994     /// bounds or arithmetic overflow occurs then any further use of the
995     /// returned value will result in undefined behavior.
996     pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
997
998     /// Calculates the offset from a pointer, potentially wrapping.
999     ///
1000     /// This is implemented as an intrinsic to avoid converting to and from an
1001     /// integer, since the conversion inhibits certain optimizations.
1002     ///
1003     /// # Safety
1004     ///
1005     /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1006     /// resulting pointer to point into or one byte past the end of an allocated
1007     /// object, and it wraps with two's complement arithmetic. The resulting
1008     /// value is not necessarily valid to be used to actually access memory.
1009     pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1010
1011     /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1012     /// a size of `count` * `size_of::<T>()` and an alignment of
1013     /// `min_align_of::<T>()`
1014     ///
1015     /// The volatile parameter is set to `true`, so it will not be optimized out
1016     /// unless size is equal to zero.
1017     pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1018     /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1019     /// a size of `count` * `size_of::<T>()` and an alignment of
1020     /// `min_align_of::<T>()`
1021     ///
1022     /// The volatile parameter is set to `true`, so it will not be optimized out
1023     /// unless size is equal to zero.
1024     pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1025     /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1026     /// size of `count` * `size_of::<T>()` and an alignment of
1027     /// `min_align_of::<T>()`.
1028     ///
1029     /// The volatile parameter is set to `true`, so it will not be optimized out
1030     /// unless size is equal to zero.
1031     pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1032
1033     /// Performs a volatile load from the `src` pointer.
1034     /// The stabilized version of this intrinsic is
1035     /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1036     pub fn volatile_load<T>(src: *const T) -> T;
1037     /// Performs a volatile store to the `dst` pointer.
1038     /// The stabilized version of this intrinsic is
1039     /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1040     pub fn volatile_store<T>(dst: *mut T, val: T);
1041
1042     /// Performs a volatile load from the `src` pointer
1043     /// The pointer is not required to be aligned.
1044     pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1045     /// Performs a volatile store to the `dst` pointer.
1046     /// The pointer is not required to be aligned.
1047     pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1048
1049     /// Returns the square root of an `f32`
1050     pub fn sqrtf32(x: f32) -> f32;
1051     /// Returns the square root of an `f64`
1052     pub fn sqrtf64(x: f64) -> f64;
1053
1054     /// Raises an `f32` to an integer power.
1055     pub fn powif32(a: f32, x: i32) -> f32;
1056     /// Raises an `f64` to an integer power.
1057     pub fn powif64(a: f64, x: i32) -> f64;
1058
1059     /// Returns the sine of an `f32`.
1060     pub fn sinf32(x: f32) -> f32;
1061     /// Returns the sine of an `f64`.
1062     pub fn sinf64(x: f64) -> f64;
1063
1064     /// Returns the cosine of an `f32`.
1065     pub fn cosf32(x: f32) -> f32;
1066     /// Returns the cosine of an `f64`.
1067     pub fn cosf64(x: f64) -> f64;
1068
1069     /// Raises an `f32` to an `f32` power.
1070     pub fn powf32(a: f32, x: f32) -> f32;
1071     /// Raises an `f64` to an `f64` power.
1072     pub fn powf64(a: f64, x: f64) -> f64;
1073
1074     /// Returns the exponential of an `f32`.
1075     pub fn expf32(x: f32) -> f32;
1076     /// Returns the exponential of an `f64`.
1077     pub fn expf64(x: f64) -> f64;
1078
1079     /// Returns 2 raised to the power of an `f32`.
1080     pub fn exp2f32(x: f32) -> f32;
1081     /// Returns 2 raised to the power of an `f64`.
1082     pub fn exp2f64(x: f64) -> f64;
1083
1084     /// Returns the natural logarithm of an `f32`.
1085     pub fn logf32(x: f32) -> f32;
1086     /// Returns the natural logarithm of an `f64`.
1087     pub fn logf64(x: f64) -> f64;
1088
1089     /// Returns the base 10 logarithm of an `f32`.
1090     pub fn log10f32(x: f32) -> f32;
1091     /// Returns the base 10 logarithm of an `f64`.
1092     pub fn log10f64(x: f64) -> f64;
1093
1094     /// Returns the base 2 logarithm of an `f32`.
1095     pub fn log2f32(x: f32) -> f32;
1096     /// Returns the base 2 logarithm of an `f64`.
1097     pub fn log2f64(x: f64) -> f64;
1098
1099     /// Returns `a * b + c` for `f32` values.
1100     pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1101     /// Returns `a * b + c` for `f64` values.
1102     pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1103
1104     /// Returns the absolute value of an `f32`.
1105     pub fn fabsf32(x: f32) -> f32;
1106     /// Returns the absolute value of an `f64`.
1107     pub fn fabsf64(x: f64) -> f64;
1108
1109     /// Returns the minimum of two `f32` values.
1110     pub fn minnumf32(x: f32, y: f32) -> f32;
1111     /// Returns the minimum of two `f64` values.
1112     pub fn minnumf64(x: f64, y: f64) -> f64;
1113     /// Returns the maximum of two `f32` values.
1114     pub fn maxnumf32(x: f32, y: f32) -> f32;
1115     /// Returns the maximum of two `f64` values.
1116     pub fn maxnumf64(x: f64, y: f64) -> f64;
1117
1118     /// Copies the sign from `y` to `x` for `f32` values.
1119     pub fn copysignf32(x: f32, y: f32) -> f32;
1120     /// Copies the sign from `y` to `x` for `f64` values.
1121     pub fn copysignf64(x: f64, y: f64) -> f64;
1122
1123     /// Returns the largest integer less than or equal to an `f32`.
1124     pub fn floorf32(x: f32) -> f32;
1125     /// Returns the largest integer less than or equal to an `f64`.
1126     pub fn floorf64(x: f64) -> f64;
1127
1128     /// Returns the smallest integer greater than or equal to an `f32`.
1129     pub fn ceilf32(x: f32) -> f32;
1130     /// Returns the smallest integer greater than or equal to an `f64`.
1131     pub fn ceilf64(x: f64) -> f64;
1132
1133     /// Returns the integer part of an `f32`.
1134     pub fn truncf32(x: f32) -> f32;
1135     /// Returns the integer part of an `f64`.
1136     pub fn truncf64(x: f64) -> f64;
1137
1138     /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1139     /// if the argument is not an integer.
1140     pub fn rintf32(x: f32) -> f32;
1141     /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1142     /// if the argument is not an integer.
1143     pub fn rintf64(x: f64) -> f64;
1144
1145     /// Returns the nearest integer to an `f32`.
1146     pub fn nearbyintf32(x: f32) -> f32;
1147     /// Returns the nearest integer to an `f64`.
1148     pub fn nearbyintf64(x: f64) -> f64;
1149
1150     /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1151     pub fn roundf32(x: f32) -> f32;
1152     /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1153     pub fn roundf64(x: f64) -> f64;
1154
1155     /// Float addition that allows optimizations based on algebraic rules.
1156     /// May assume inputs are finite.
1157     pub fn fadd_fast<T>(a: T, b: T) -> T;
1158
1159     /// Float subtraction that allows optimizations based on algebraic rules.
1160     /// May assume inputs are finite.
1161     pub fn fsub_fast<T>(a: T, b: T) -> T;
1162
1163     /// Float multiplication that allows optimizations based on algebraic rules.
1164     /// May assume inputs are finite.
1165     pub fn fmul_fast<T>(a: T, b: T) -> T;
1166
1167     /// Float division that allows optimizations based on algebraic rules.
1168     /// May assume inputs are finite.
1169     pub fn fdiv_fast<T>(a: T, b: T) -> T;
1170
1171     /// Float remainder that allows optimizations based on algebraic rules.
1172     /// May assume inputs are finite.
1173     pub fn frem_fast<T>(a: T, b: T) -> T;
1174
1175     /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1176     /// https://github.com/rust-lang/rust/issues/10184
1177     pub fn float_to_int_approx_unchecked<Float, Int>(value: Float) -> Int;
1178
1179     /// Returns the number of bits set in an integer type `T`
1180     #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1181     pub fn ctpop<T>(x: T) -> T;
1182
1183     /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1184     ///
1185     /// # Examples
1186     ///
1187     /// ```
1188     /// #![feature(core_intrinsics)]
1189     ///
1190     /// use std::intrinsics::ctlz;
1191     ///
1192     /// let x = 0b0001_1100_u8;
1193     /// let num_leading = ctlz(x);
1194     /// assert_eq!(num_leading, 3);
1195     /// ```
1196     ///
1197     /// An `x` with value `0` will return the bit width of `T`.
1198     ///
1199     /// ```
1200     /// #![feature(core_intrinsics)]
1201     ///
1202     /// use std::intrinsics::ctlz;
1203     ///
1204     /// let x = 0u16;
1205     /// let num_leading = ctlz(x);
1206     /// assert_eq!(num_leading, 16);
1207     /// ```
1208     #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1209     pub fn ctlz<T>(x: T) -> T;
1210
1211     /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1212     /// given an `x` with value `0`.
1213     ///
1214     /// # Examples
1215     ///
1216     /// ```
1217     /// #![feature(core_intrinsics)]
1218     ///
1219     /// use std::intrinsics::ctlz_nonzero;
1220     ///
1221     /// let x = 0b0001_1100_u8;
1222     /// let num_leading = unsafe { ctlz_nonzero(x) };
1223     /// assert_eq!(num_leading, 3);
1224     /// ```
1225     #[rustc_const_unstable(feature = "constctlz", issue = "none")]
1226     pub fn ctlz_nonzero<T>(x: T) -> T;
1227
1228     /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1229     ///
1230     /// # Examples
1231     ///
1232     /// ```
1233     /// #![feature(core_intrinsics)]
1234     ///
1235     /// use std::intrinsics::cttz;
1236     ///
1237     /// let x = 0b0011_1000_u8;
1238     /// let num_trailing = cttz(x);
1239     /// assert_eq!(num_trailing, 3);
1240     /// ```
1241     ///
1242     /// An `x` with value `0` will return the bit width of `T`:
1243     ///
1244     /// ```
1245     /// #![feature(core_intrinsics)]
1246     ///
1247     /// use std::intrinsics::cttz;
1248     ///
1249     /// let x = 0u16;
1250     /// let num_trailing = cttz(x);
1251     /// assert_eq!(num_trailing, 16);
1252     /// ```
1253     #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1254     pub fn cttz<T>(x: T) -> T;
1255
1256     /// Like `cttz`, but extra-unsafe as it returns `undef` when
1257     /// given an `x` with value `0`.
1258     ///
1259     /// # Examples
1260     ///
1261     /// ```
1262     /// #![feature(core_intrinsics)]
1263     ///
1264     /// use std::intrinsics::cttz_nonzero;
1265     ///
1266     /// let x = 0b0011_1000_u8;
1267     /// let num_trailing = unsafe { cttz_nonzero(x) };
1268     /// assert_eq!(num_trailing, 3);
1269     /// ```
1270     #[rustc_const_unstable(feature = "const_cttz", issue = "none")]
1271     pub fn cttz_nonzero<T>(x: T) -> T;
1272
1273     /// Reverses the bytes in an integer type `T`.
1274     #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1275     pub fn bswap<T>(x: T) -> T;
1276
1277     /// Reverses the bits in an integer type `T`.
1278     #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1279     pub fn bitreverse<T>(x: T) -> T;
1280
1281     /// Performs checked integer addition.
1282     /// The stabilized versions of this intrinsic are available on the integer
1283     /// primitives via the `overflowing_add` method. For example,
1284     /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1285     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1286     pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
1287
1288     /// Performs checked integer subtraction
1289     /// The stabilized versions of this intrinsic are available on the integer
1290     /// primitives via the `overflowing_sub` method. For example,
1291     /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1292     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1293     pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
1294
1295     /// Performs checked integer multiplication
1296     /// The stabilized versions of this intrinsic are available on the integer
1297     /// primitives via the `overflowing_mul` method. For example,
1298     /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1299     #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1300     pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
1301
1302     /// Performs an exact division, resulting in undefined behavior where
1303     /// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
1304     pub fn exact_div<T>(x: T, y: T) -> T;
1305
1306     /// Performs an unchecked division, resulting in undefined behavior
1307     /// where y = 0 or x = `T::min_value()` and y = -1
1308     pub fn unchecked_div<T>(x: T, y: T) -> T;
1309     /// Returns the remainder of an unchecked division, resulting in
1310     /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
1311     pub fn unchecked_rem<T>(x: T, y: T) -> T;
1312
1313     /// Performs an unchecked left shift, resulting in undefined behavior when
1314     /// y < 0 or y >= N, where N is the width of T in bits.
1315     #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1316     pub fn unchecked_shl<T>(x: T, y: T) -> T;
1317     /// Performs an unchecked right shift, resulting in undefined behavior when
1318     /// y < 0 or y >= N, where N is the width of T in bits.
1319     #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1320     pub fn unchecked_shr<T>(x: T, y: T) -> T;
1321
1322     /// Returns the result of an unchecked addition, resulting in
1323     /// undefined behavior when `x + y > T::max_value()` or `x + y < T::min_value()`.
1324     pub fn unchecked_add<T>(x: T, y: T) -> T;
1325
1326     /// Returns the result of an unchecked subtraction, resulting in
1327     /// undefined behavior when `x - y > T::max_value()` or `x - y < T::min_value()`.
1328     pub fn unchecked_sub<T>(x: T, y: T) -> T;
1329
1330     /// Returns the result of an unchecked multiplication, resulting in
1331     /// undefined behavior when `x * y > T::max_value()` or `x * y < T::min_value()`.
1332     pub fn unchecked_mul<T>(x: T, y: T) -> T;
1333
1334     /// Performs rotate left.
1335     /// The stabilized versions of this intrinsic are available on the integer
1336     /// primitives via the `rotate_left` method. For example,
1337     /// [`std::u32::rotate_left`](../../std/primitive.u32.html#method.rotate_left)
1338     #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1339     pub fn rotate_left<T>(x: T, y: T) -> T;
1340
1341     /// Performs rotate right.
1342     /// The stabilized versions of this intrinsic are available on the integer
1343     /// primitives via the `rotate_right` method. For example,
1344     /// [`std::u32::rotate_right`](../../std/primitive.u32.html#method.rotate_right)
1345     #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1346     pub fn rotate_right<T>(x: T, y: T) -> T;
1347
1348     /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1349     /// The stabilized versions of this intrinsic are available on the integer
1350     /// primitives via the `wrapping_add` method. For example,
1351     /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1352     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1353     pub fn wrapping_add<T>(a: T, b: T) -> T;
1354     /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1355     /// The stabilized versions of this intrinsic are available on the integer
1356     /// primitives via the `wrapping_sub` method. For example,
1357     /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1358     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1359     pub fn wrapping_sub<T>(a: T, b: T) -> T;
1360     /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1361     /// The stabilized versions of this intrinsic are available on the integer
1362     /// primitives via the `wrapping_mul` method. For example,
1363     /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1364     #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1365     pub fn wrapping_mul<T>(a: T, b: T) -> T;
1366
1367     /// Computes `a + b`, while saturating at numeric bounds.
1368     /// The stabilized versions of this intrinsic are available on the integer
1369     /// primitives via the `saturating_add` method. For example,
1370     /// [`std::u32::saturating_add`](../../std/primitive.u32.html#method.saturating_add)
1371     #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1372     pub fn saturating_add<T>(a: T, b: T) -> T;
1373     /// Computes `a - b`, while saturating at numeric bounds.
1374     /// The stabilized versions of this intrinsic are available on the integer
1375     /// primitives via the `saturating_sub` method. For example,
1376     /// [`std::u32::saturating_sub`](../../std/primitive.u32.html#method.saturating_sub)
1377     #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1378     pub fn saturating_sub<T>(a: T, b: T) -> T;
1379
1380     /// Returns the value of the discriminant for the variant in 'v',
1381     /// cast to a `u64`; if `T` has no discriminant, returns 0.
1382     pub fn discriminant_value<T>(v: &T) -> u64;
1383
1384     /// Rust's "try catch" construct which invokes the function pointer `f` with
1385     /// the data pointer `data`.
1386     ///
1387     /// The third pointer is a target-specific data pointer which is filled in
1388     /// with the specifics of the exception that occurred. For examples on Unix
1389     /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1390     /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1391     /// source as well as std's catch implementation.
1392     pub fn r#try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
1393
1394     /// Emits a `!nontemporal` store according to LLVM (see their docs).
1395     /// Probably will never become stable.
1396     pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1397
1398     /// See documentation of `<*const T>::offset_from` for details.
1399     #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "none")]
1400     pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1401
1402     /// Internal hook used by Miri to implement unwinding.
1403     /// Compiles to a NOP during non-Miri codegen.
1404     ///
1405     /// Perma-unstable: do not use
1406     pub fn miri_start_panic(data: *mut (dyn crate::any::Any + crate::marker::Send)) -> ();
1407 }
1408
1409 // Some functions are defined here because they accidentally got made
1410 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
1411 // (`transmute` also falls into this category, but it cannot be wrapped due to the
1412 // check that `T` and `U` have the same size.)
1413
1414 /// Checks whether `ptr` is properly aligned with respect to
1415 /// `align_of::<T>()`.
1416 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1417     !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1418 }
1419
1420 /// Checks whether the regions of memory starting at `src` and `dst` of size
1421 /// `count * size_of::<T>()` overlap.
1422 fn overlaps<T>(src: *const T, dst: *const T, count: usize) -> bool {
1423     let src_usize = src as usize;
1424     let dst_usize = dst as usize;
1425     let size = mem::size_of::<T>().checked_mul(count).unwrap();
1426     let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
1427     size > diff
1428 }
1429
1430 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1431 /// and destination must *not* overlap.
1432 ///
1433 /// For regions of memory which might overlap, use [`copy`] instead.
1434 ///
1435 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
1436 /// with the argument order swapped.
1437 ///
1438 /// [`copy`]: ./fn.copy.html
1439 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
1440 ///
1441 /// # Safety
1442 ///
1443 /// Behavior is undefined if any of the following conditions are violated:
1444 ///
1445 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1446 ///
1447 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1448 ///
1449 /// * Both `src` and `dst` must be properly aligned.
1450 ///
1451 /// * The region of memory beginning at `src` with a size of `count *
1452 ///   size_of::<T>()` bytes must *not* overlap with the region of memory
1453 ///   beginning at `dst` with the same size.
1454 ///
1455 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
1456 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
1457 /// in the region beginning at `*src` and the region beginning at `*dst` can
1458 /// [violate memory safety][read-ownership].
1459 ///
1460 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1461 /// `0`, the pointers must be non-NULL and properly aligned.
1462 ///
1463 /// [`Copy`]: ../marker/trait.Copy.html
1464 /// [`read`]: ../ptr/fn.read.html
1465 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1466 /// [valid]: ../ptr/index.html#safety
1467 ///
1468 /// # Examples
1469 ///
1470 /// Manually implement [`Vec::append`]:
1471 ///
1472 /// ```
1473 /// use std::ptr;
1474 ///
1475 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
1476 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
1477 ///     let src_len = src.len();
1478 ///     let dst_len = dst.len();
1479 ///
1480 ///     // Ensure that `dst` has enough capacity to hold all of `src`.
1481 ///     dst.reserve(src_len);
1482 ///
1483 ///     unsafe {
1484 ///         // The call to offset is always safe because `Vec` will never
1485 ///         // allocate more than `isize::MAX` bytes.
1486 ///         let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
1487 ///         let src_ptr = src.as_ptr();
1488 ///
1489 ///         // Truncate `src` without dropping its contents. We do this first,
1490 ///         // to avoid problems in case something further down panics.
1491 ///         src.set_len(0);
1492 ///
1493 ///         // The two regions cannot overlap because mutable references do
1494 ///         // not alias, and two different vectors cannot own the same
1495 ///         // memory.
1496 ///         ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
1497 ///
1498 ///         // Notify `dst` that it now holds the contents of `src`.
1499 ///         dst.set_len(dst_len + src_len);
1500 ///     }
1501 /// }
1502 ///
1503 /// let mut a = vec!['r'];
1504 /// let mut b = vec!['u', 's', 't'];
1505 ///
1506 /// append(&mut a, &mut b);
1507 ///
1508 /// assert_eq!(a, &['r', 'u', 's', 't']);
1509 /// assert!(b.is_empty());
1510 /// ```
1511 ///
1512 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
1513 #[stable(feature = "rust1", since = "1.0.0")]
1514 #[inline]
1515 pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
1516     extern "rust-intrinsic" {
1517         fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1518     }
1519
1520     debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1521     debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1522     debug_assert!(!overlaps(src, dst, count), "attempt to copy to overlapping memory");
1523     copy_nonoverlapping(src, dst, count)
1524 }
1525
1526 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
1527 /// and destination may overlap.
1528 ///
1529 /// If the source and destination will *never* overlap,
1530 /// [`copy_nonoverlapping`] can be used instead.
1531 ///
1532 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
1533 /// order swapped. Copying takes place as if the bytes were copied from `src`
1534 /// to a temporary array and then copied from the array to `dst`.
1535 ///
1536 /// [`copy_nonoverlapping`]: ./fn.copy_nonoverlapping.html
1537 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
1538 ///
1539 /// # Safety
1540 ///
1541 /// Behavior is undefined if any of the following conditions are violated:
1542 ///
1543 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
1544 ///
1545 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1546 ///
1547 /// * Both `src` and `dst` must be properly aligned.
1548 ///
1549 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
1550 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
1551 /// in the region beginning at `*src` and the region beginning at `*dst` can
1552 /// [violate memory safety][read-ownership].
1553 ///
1554 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1555 /// `0`, the pointers must be non-NULL and properly aligned.
1556 ///
1557 /// [`Copy`]: ../marker/trait.Copy.html
1558 /// [`read`]: ../ptr/fn.read.html
1559 /// [read-ownership]: ../ptr/fn.read.html#ownership-of-the-returned-value
1560 /// [valid]: ../ptr/index.html#safety
1561 ///
1562 /// # Examples
1563 ///
1564 /// Efficiently create a Rust vector from an unsafe buffer:
1565 ///
1566 /// ```
1567 /// use std::ptr;
1568 ///
1569 /// # #[allow(dead_code)]
1570 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1571 ///     let mut dst = Vec::with_capacity(elts);
1572 ///     dst.set_len(elts);
1573 ///     ptr::copy(ptr, dst.as_mut_ptr(), elts);
1574 ///     dst
1575 /// }
1576 /// ```
1577 #[stable(feature = "rust1", since = "1.0.0")]
1578 #[inline]
1579 pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
1580     extern "rust-intrinsic" {
1581         fn copy<T>(src: *const T, dst: *mut T, count: usize);
1582     }
1583
1584     debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1585     debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1586     copy(src, dst, count)
1587 }
1588
1589 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
1590 /// `val`.
1591 ///
1592 /// `write_bytes` is similar to C's [`memset`], but sets `count *
1593 /// size_of::<T>()` bytes to `val`.
1594 ///
1595 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
1596 ///
1597 /// # Safety
1598 ///
1599 /// Behavior is undefined if any of the following conditions are violated:
1600 ///
1601 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
1602 ///
1603 /// * `dst` must be properly aligned.
1604 ///
1605 /// Additionally, the caller must ensure that writing `count *
1606 /// size_of::<T>()` bytes to the given region of memory results in a valid
1607 /// value of `T`. Using a region of memory typed as a `T` that contains an
1608 /// invalid value of `T` is undefined behavior.
1609 ///
1610 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
1611 /// `0`, the pointer must be non-NULL and properly aligned.
1612 ///
1613 /// [valid]: ../ptr/index.html#safety
1614 ///
1615 /// # Examples
1616 ///
1617 /// Basic usage:
1618 ///
1619 /// ```
1620 /// use std::ptr;
1621 ///
1622 /// let mut vec = vec![0u32; 4];
1623 /// unsafe {
1624 ///     let vec_ptr = vec.as_mut_ptr();
1625 ///     ptr::write_bytes(vec_ptr, 0xfe, 2);
1626 /// }
1627 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
1628 /// ```
1629 ///
1630 /// Creating an invalid value:
1631 ///
1632 /// ```
1633 /// use std::ptr;
1634 ///
1635 /// let mut v = Box::new(0i32);
1636 ///
1637 /// unsafe {
1638 ///     // Leaks the previously held value by overwriting the `Box<T>` with
1639 ///     // a null pointer.
1640 ///     ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
1641 /// }
1642 ///
1643 /// // At this point, using or dropping `v` results in undefined behavior.
1644 /// // drop(v); // ERROR
1645 ///
1646 /// // Even leaking `v` "uses" it, and hence is undefined behavior.
1647 /// // mem::forget(v); // ERROR
1648 ///
1649 /// // In fact, `v` is invalid according to basic type layout invariants, so *any*
1650 /// // operation touching it is undefined behavior.
1651 /// // let v2 = v; // ERROR
1652 ///
1653 /// unsafe {
1654 ///     // Let us instead put in a valid value
1655 ///     ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
1656 /// }
1657 ///
1658 /// // Now the box is fine
1659 /// assert_eq!(*v, 42);
1660 /// ```
1661 #[stable(feature = "rust1", since = "1.0.0")]
1662 #[inline]
1663 pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
1664     extern "rust-intrinsic" {
1665         fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1666     }
1667
1668     debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
1669     write_bytes(dst, val, count)
1670 }