intrinsics.rs 98.2 KB
Newer Older
A
Alexander Regueiro 已提交
1
//! Compiler intrinsics.
S
Steve Klabnik 已提交
2
//!
3 4
//! The corresponding definitions are in `compiler/rustc_codegen_llvm/src/intrinsic.rs`.
//! The corresponding const implementations are in `compiler/rustc_mir/src/interpret/intrinsics.rs`
5 6 7 8 9 10 11
//!
//! # Const intrinsics
//!
//! Note: any changes to the constness of intrinsics should be discussed with the language team.
//! This includes changes in the stability of the constness.
//!
//! In order to make an intrinsic usable at compile-time, one needs to copy the implementation
G
Guillaume Gomez 已提交
12
//! from <https://github.com/rust-lang/miri/blob/master/src/shims/intrinsics.rs> to
13
//! `compiler/rustc_mir/src/interpret/intrinsics.rs` and add a
O
Oliver Scherer 已提交
14
//! `#[rustc_const_unstable(feature = "foo", issue = "01234")]` to the intrinsic.
15 16 17
//!
//! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute,
//! the intrinsic's attribute must be `rustc_const_stable`, too. Such a change should not be done
A
Andy Russell 已提交
18
//! without T-lang consultation, because it bakes a feature into the language that cannot be
19
//! replicated in user code without compiler support.
S
Steve Klabnik 已提交
20 21 22 23 24 25 26 27
//!
//! # Volatiles
//!
//! The volatile intrinsics provide operations intended to act on I/O
//! memory, which are guaranteed to not be reordered by the compiler
//! across other volatile intrinsics. See the LLVM documentation on
//! [[volatile]].
//!
S
Smitty 已提交
28
//! [volatile]: https://llvm.org/docs/LangRef.html#volatile-memory-accesses
S
Steve Klabnik 已提交
29 30 31 32 33 34 35
//!
//! # Atomics
//!
//! The atomic intrinsics provide common atomic operations on machine
//! words, with multiple possible memory orderings. They obey the same
//! semantics as C++11. See the LLVM documentation on [[atomics]].
//!
S
Smitty 已提交
36
//! [atomics]: https://llvm.org/docs/Atomics.html
S
Steve Klabnik 已提交
37 38 39 40 41 42 43 44 45 46
//!
//! A quick refresher on memory ordering:
//!
//! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
//!   take place after the barrier.
//! * Release - a barrier for releasing a lock. Preceding reads and writes
//!   take place before the barrier.
//! * Sequentially consistent - sequentially consistent operations are
//!   guaranteed to happen in order. This is the standard mode for working
//!   with atomic types and is equivalent to Java's `volatile`.
D
Daniel Micay 已提交
47

M
Mark Rousskov 已提交
48 49 50
#![unstable(
    feature = "core_intrinsics",
    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",
M
Mark Rousskov 已提交
53 54
    issue = "none"
)]
A
Aaron Turon 已提交
55
#![allow(missing_docs)]
56

57
use crate::marker::DiscriminantKind;
58 59
use crate::mem;

60
// These imports are used for simplifying intra-doc links
61
#[allow(unused_imports)]
62
#[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))]
63
use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering};
64

A
Ariel Ben-Yehuda 已提交
65
#[stable(feature = "drop_in_place", since = "1.8.0")]
M
Mark Rousskov 已提交
66 67
#[rustc_deprecated(
    reason = "no longer an intrinsic - use `ptr::drop_in_place` directly",
68
    since = "1.52.0"
M
Mark Rousskov 已提交
69
)]
70 71 72 73 74
#[inline]
pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
    // SAFETY: see `ptr::drop_in_place`
    unsafe { crate::ptr::drop_in_place(to_drop) }
}
75

A
Ariel Ben-Yehuda 已提交
76
extern "rust-intrinsic" {
77
    // N.B., these intrinsics take raw pointers because they mutate aliased
78 79
    // memory, which is not valid for either `&` or `&mut`.

80
    /// Stores a value if the current value is the same as the `old` value.
81
    ///
82
    /// The stabilized version of this intrinsic is available on the
83 84 85
    /// [`atomic`] types via the `compare_exchange` method by passing
    /// [`Ordering::SeqCst`] as both the `success` and `failure` parameters.
    /// For example, [`AtomicBool::compare_exchange`].
86
    pub fn atomic_cxchg<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
87
    /// Stores a value if the current value is the same as the `old` value.
88
    ///
89
    /// The stabilized version of this intrinsic is available on the
90 91 92
    /// [`atomic`] types via the `compare_exchange` method by passing
    /// [`Ordering::Acquire`] as both the `success` and `failure` parameters.
    /// For example, [`AtomicBool::compare_exchange`].
93
    pub fn atomic_cxchg_acq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
94
    /// Stores a value if the current value is the same as the `old` value.
95
    ///
96
    /// The stabilized version of this intrinsic is available on the
97 98 99
    /// [`atomic`] types via the `compare_exchange` method by passing
    /// [`Ordering::Release`] as the `success` and [`Ordering::Relaxed`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
100
    pub fn atomic_cxchg_rel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
101
    /// Stores a value if the current value is the same as the `old` value.
102
    ///
103
    /// The stabilized version of this intrinsic is available on the
104 105 106
    /// [`atomic`] types via the `compare_exchange` method by passing
    /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Acquire`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
107
    pub fn atomic_cxchg_acqrel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
108
    /// Stores a value if the current value is the same as the `old` value.
109
    ///
110
    /// The stabilized version of this intrinsic is available on the
111 112 113
    /// [`atomic`] types via the `compare_exchange` method by passing
    /// [`Ordering::Relaxed`] as both the `success` and `failure` parameters.
    /// For example, [`AtomicBool::compare_exchange`].
114
    pub fn atomic_cxchg_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
115
    /// Stores a value if the current value is the same as the `old` value.
116
    ///
117
    /// The stabilized version of this intrinsic is available on the
118 119 120
    /// [`atomic`] types via the `compare_exchange` method by passing
    /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Relaxed`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
121
    pub fn atomic_cxchg_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
122
    /// Stores a value if the current value is the same as the `old` value.
123
    ///
124
    /// The stabilized version of this intrinsic is available on the
125 126 127
    /// [`atomic`] types via the `compare_exchange` method by passing
    /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Acquire`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
128
    pub fn atomic_cxchg_failacq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
129
    /// Stores a value if the current value is the same as the `old` value.
130
    ///
131
    /// The stabilized version of this intrinsic is available on the
132 133 134
    /// [`atomic`] types via the `compare_exchange` method by passing
    /// [`Ordering::Acquire`] as the `success` and [`Ordering::Relaxed`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
135
    pub fn atomic_cxchg_acq_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
136
    /// Stores a value if the current value is the same as the `old` value.
137
    ///
138
    /// The stabilized version of this intrinsic is available on the
139 140 141
    /// [`atomic`] types via the `compare_exchange` method by passing
    /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Relaxed`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange`].
142
    pub fn atomic_cxchg_acqrel_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
143

144
    /// Stores a value if the current value is the same as the `old` value.
145
    ///
146
    /// The stabilized version of this intrinsic is available on the
147 148 149
    /// [`atomic`] types via the `compare_exchange_weak` method by passing
    /// [`Ordering::SeqCst`] as both the `success` and `failure` parameters.
    /// For example, [`AtomicBool::compare_exchange_weak`].
150
    pub fn atomic_cxchgweak<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
151
    /// Stores a value if the current value is the same as the `old` value.
152
    ///
153
    /// The stabilized version of this intrinsic is available on the
154 155 156
    /// [`atomic`] types via the `compare_exchange_weak` method by passing
    /// [`Ordering::Acquire`] as both the `success` and `failure` parameters.
    /// For example, [`AtomicBool::compare_exchange_weak`].
157
    pub fn atomic_cxchgweak_acq<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
158
    /// Stores a value if the current value is the same as the `old` value.
159
    ///
160
    /// The stabilized version of this intrinsic is available on the
161 162 163
    /// [`atomic`] types via the `compare_exchange_weak` method by passing
    /// [`Ordering::Release`] as the `success` and [`Ordering::Relaxed`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
164
    pub fn atomic_cxchgweak_rel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
165
    /// Stores a value if the current value is the same as the `old` value.
166
    ///
167
    /// The stabilized version of this intrinsic is available on the
168 169 170
    /// [`atomic`] types via the `compare_exchange_weak` method by passing
    /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Acquire`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
171
    pub fn atomic_cxchgweak_acqrel<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
172
    /// Stores a value if the current value is the same as the `old` value.
173
    ///
174
    /// The stabilized version of this intrinsic is available on the
175 176 177
    /// [`atomic`] types via the `compare_exchange_weak` method by passing
    /// [`Ordering::Relaxed`] as both the `success` and `failure` parameters.
    /// For example, [`AtomicBool::compare_exchange_weak`].
178
    pub fn atomic_cxchgweak_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
179
    /// Stores a value if the current value is the same as the `old` value.
180
    ///
181
    /// The stabilized version of this intrinsic is available on the
182 183 184
    /// [`atomic`] types via the `compare_exchange_weak` method by passing
    /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Relaxed`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
185
    pub fn atomic_cxchgweak_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
186
    /// Stores a value if the current value is the same as the `old` value.
187
    ///
188
    /// The stabilized version of this intrinsic is available on the
189 190 191
    /// [`atomic`] types via the `compare_exchange_weak` method by passing
    /// [`Ordering::SeqCst`] as the `success` and [`Ordering::Acquire`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
192
    pub fn atomic_cxchgweak_failacq<T: Copy>(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
    ///
195
    /// The stabilized version of this intrinsic is available on the
196 197 198
    /// [`atomic`] types via the `compare_exchange_weak` method by passing
    /// [`Ordering::Acquire`] as the `success` and [`Ordering::Relaxed`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
199
    pub fn atomic_cxchgweak_acq_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
200
    /// Stores a value if the current value is the same as the `old` value.
201
    ///
202
    /// The stabilized version of this intrinsic is available on the
203 204 205
    /// [`atomic`] types via the `compare_exchange_weak` method by passing
    /// [`Ordering::AcqRel`] as the `success` and [`Ordering::Relaxed`] as the
    /// `failure` parameters. For example, [`AtomicBool::compare_exchange_weak`].
206
    pub fn atomic_cxchgweak_acqrel_failrelaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
207

208
    /// Loads the current value of the pointer.
209
    ///
210
    /// The stabilized version of this intrinsic is available on the
211 212
    /// [`atomic`] types via the `load` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`].
213
    pub fn atomic_load<T: Copy>(src: *const T) -> T;
214
    /// Loads the current value of the pointer.
215
    ///
216
    /// The stabilized version of this intrinsic is available on the
217 218
    /// [`atomic`] types via the `load` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`].
219
    pub fn atomic_load_acq<T: Copy>(src: *const T) -> T;
220
    /// Loads the current value of the pointer.
221
    ///
222
    /// The stabilized version of this intrinsic is available on the
223 224
    /// [`atomic`] types via the `load` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`].
225 226
    pub fn atomic_load_relaxed<T: Copy>(src: *const T) -> T;
    pub fn atomic_load_unordered<T: Copy>(src: *const T) -> T;
227

228
    /// Stores the value at the specified memory location.
229
    ///
230
    /// The stabilized version of this intrinsic is available on the
231 232
    /// [`atomic`] types via the `store` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`].
233
    pub fn atomic_store<T: Copy>(dst: *mut T, val: T);
234
    /// Stores the value at the specified memory location.
235
    ///
236
    /// The stabilized version of this intrinsic is available on the
237 238
    /// [`atomic`] types via the `store` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`].
239
    pub fn atomic_store_rel<T: Copy>(dst: *mut T, val: T);
240
    /// Stores the value at the specified memory location.
241
    ///
242
    /// The stabilized version of this intrinsic is available on the
243 244
    /// [`atomic`] types via the `store` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`].
245 246
    pub fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T);
    pub fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T);
247

248
    /// Stores the value at the specified memory location, returning the old value.
249
    ///
250
    /// The stabilized version of this intrinsic is available on the
251 252
    /// [`atomic`] types via the `swap` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`].
253
    pub fn atomic_xchg<T: Copy>(dst: *mut T, src: T) -> T;
254
    /// Stores the value at the specified memory location, returning the old value.
255
    ///
256
    /// The stabilized version of this intrinsic is available on the
257 258
    /// [`atomic`] types via the `swap` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`].
259
    pub fn atomic_xchg_acq<T: Copy>(dst: *mut T, src: T) -> T;
260
    /// Stores the value at the specified memory location, returning the old value.
261
    ///
262
    /// The stabilized version of this intrinsic is available on the
263 264
    /// [`atomic`] types via the `swap` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`].
265
    pub fn atomic_xchg_rel<T: Copy>(dst: *mut T, src: T) -> T;
266
    /// Stores the value at the specified memory location, returning the old value.
267
    ///
268
    /// The stabilized version of this intrinsic is available on the
269 270
    /// [`atomic`] types via the `swap` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`].
271
    pub fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
272
    /// Stores the value at the specified memory location, returning the old value.
273
    ///
274
    /// The stabilized version of this intrinsic is available on the
275 276
    /// [`atomic`] types via the `swap` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`].
277
    pub fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
278

A
Alexander Regueiro 已提交
279
    /// Adds to the current value, returning the previous value.
280
    ///
281
    /// The stabilized version of this intrinsic is available on the
282 283
    /// [`atomic`] types via the `fetch_add` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`].
284
    pub fn atomic_xadd<T: Copy>(dst: *mut T, src: T) -> T;
A
Alexander Regueiro 已提交
285
    /// Adds to the current value, returning the previous value.
286
    ///
287
    /// The stabilized version of this intrinsic is available on the
288 289
    /// [`atomic`] types via the `fetch_add` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`].
290
    pub fn atomic_xadd_acq<T: Copy>(dst: *mut T, src: T) -> T;
A
Alexander Regueiro 已提交
291
    /// Adds to the current value, returning the previous value.
292
    ///
293
    /// The stabilized version of this intrinsic is available on the
294 295
    /// [`atomic`] types via the `fetch_add` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`].
296
    pub fn atomic_xadd_rel<T: Copy>(dst: *mut T, src: T) -> T;
A
Alexander Regueiro 已提交
297
    /// Adds to the current value, returning the previous value.
298
    ///
299
    /// The stabilized version of this intrinsic is available on the
300 301
    /// [`atomic`] types via the `fetch_add` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`].
302
    pub fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
A
Alexander Regueiro 已提交
303
    /// Adds to the current value, returning the previous value.
304
    ///
305
    /// The stabilized version of this intrinsic is available on the
306 307
    /// [`atomic`] types via the `fetch_add` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`].
308
    pub fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
309

310
    /// Subtract from the current value, returning the previous value.
311
    ///
312
    /// The stabilized version of this intrinsic is available on the
313 314
    /// [`atomic`] types via the `fetch_sub` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
315
    pub fn atomic_xsub<T: Copy>(dst: *mut T, src: T) -> T;
316
    /// Subtract from the current value, returning the previous value.
317
    ///
318
    /// The stabilized version of this intrinsic is available on the
319 320
    /// [`atomic`] types via the `fetch_sub` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
321
    pub fn atomic_xsub_acq<T: Copy>(dst: *mut T, src: T) -> T;
322
    /// Subtract from the current value, returning the previous value.
323
    ///
324
    /// The stabilized version of this intrinsic is available on the
325 326
    /// [`atomic`] types via the `fetch_sub` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
327
    pub fn atomic_xsub_rel<T: Copy>(dst: *mut T, src: T) -> T;
328
    /// Subtract from the current value, returning the previous value.
329
    ///
330
    /// The stabilized version of this intrinsic is available on the
331 332
    /// [`atomic`] types via the `fetch_sub` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
333
    pub fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
334
    /// Subtract from the current value, returning the previous value.
335
    ///
336
    /// The stabilized version of this intrinsic is available on the
337 338
    /// [`atomic`] types via the `fetch_sub` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
339
    pub fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
340

341
    /// Bitwise and with the current value, returning the previous value.
342
    ///
343
    /// The stabilized version of this intrinsic is available on the
344 345
    /// [`atomic`] types via the `fetch_and` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`].
346
    pub fn atomic_and<T: Copy>(dst: *mut T, src: T) -> T;
347
    /// Bitwise and with the current value, returning the previous value.
348
    ///
349
    /// The stabilized version of this intrinsic is available on the
350 351
    /// [`atomic`] types via the `fetch_and` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`].
352
    pub fn atomic_and_acq<T: Copy>(dst: *mut T, src: T) -> T;
353
    /// Bitwise and with the current value, returning the previous value.
354
    ///
355
    /// The stabilized version of this intrinsic is available on the
356 357
    /// [`atomic`] types via the `fetch_and` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`].
358
    pub fn atomic_and_rel<T: Copy>(dst: *mut T, src: T) -> T;
359
    /// Bitwise and with the current value, returning the previous value.
360
    ///
361
    /// The stabilized version of this intrinsic is available on the
362 363
    /// [`atomic`] types via the `fetch_and` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`].
364
    pub fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
365
    /// Bitwise and with the current value, returning the previous value.
366
    ///
367
    /// The stabilized version of this intrinsic is available on the
368 369
    /// [`atomic`] types via the `fetch_and` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`].
370
    pub fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
371

372
    /// Bitwise nand with the current value, returning the previous value.
373
    ///
374
    /// The stabilized version of this intrinsic is available on the
375 376
    /// [`AtomicBool`] type via the `fetch_nand` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`].
377
    pub fn atomic_nand<T: Copy>(dst: *mut T, src: T) -> T;
378
    /// Bitwise nand with the current value, returning the previous value.
379
    ///
380
    /// The stabilized version of this intrinsic is available on the
381 382
    /// [`AtomicBool`] type via the `fetch_nand` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`].
383
    pub fn atomic_nand_acq<T: Copy>(dst: *mut T, src: T) -> T;
384
    /// Bitwise nand with the current value, returning the previous value.
385
    ///
386
    /// The stabilized version of this intrinsic is available on the
387 388
    /// [`AtomicBool`] type via the `fetch_nand` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`].
389
    pub fn atomic_nand_rel<T: Copy>(dst: *mut T, src: T) -> T;
390
    /// Bitwise nand with the current value, returning the previous value.
391
    ///
392
    /// The stabilized version of this intrinsic is available on the
393 394
    /// [`AtomicBool`] type via the `fetch_nand` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`].
395
    pub fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
396
    /// Bitwise nand with the current value, returning the previous value.
397
    ///
398
    /// The stabilized version of this intrinsic is available on the
399 400
    /// [`AtomicBool`] type via the `fetch_nand` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`].
401
    pub fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
402

403
    /// Bitwise or with the current value, returning the previous value.
404
    ///
405
    /// The stabilized version of this intrinsic is available on the
406 407
    /// [`atomic`] types via the `fetch_or` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`].
408
    pub fn atomic_or<T: Copy>(dst: *mut T, src: T) -> T;
409
    /// Bitwise or with the current value, returning the previous value.
410
    ///
411
    /// The stabilized version of this intrinsic is available on the
412 413
    /// [`atomic`] types via the `fetch_or` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`].
414
    pub fn atomic_or_acq<T: Copy>(dst: *mut T, src: T) -> T;
415
    /// Bitwise or with the current value, returning the previous value.
416
    ///
417
    /// The stabilized version of this intrinsic is available on the
418 419
    /// [`atomic`] types via the `fetch_or` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`].
420
    pub fn atomic_or_rel<T: Copy>(dst: *mut T, src: T) -> T;
421
    /// Bitwise or with the current value, returning the previous value.
422
    ///
423
    /// The stabilized version of this intrinsic is available on the
424 425
    /// [`atomic`] types via the `fetch_or` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`].
426
    pub fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
427
    /// Bitwise or with the current value, returning the previous value.
428
    ///
429
    /// The stabilized version of this intrinsic is available on the
430 431
    /// [`atomic`] types via the `fetch_or` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`].
432
    pub fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
433

434
    /// Bitwise xor with the current value, returning the previous value.
435
    ///
436
    /// The stabilized version of this intrinsic is available on the
437 438
    /// [`atomic`] types via the `fetch_xor` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`].
439
    pub fn atomic_xor<T: Copy>(dst: *mut T, src: T) -> T;
440
    /// Bitwise xor with the current value, returning the previous value.
441
    ///
442
    /// The stabilized version of this intrinsic is available on the
443 444
    /// [`atomic`] types via the `fetch_xor` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`].
445
    pub fn atomic_xor_acq<T: Copy>(dst: *mut T, src: T) -> T;
446
    /// Bitwise xor with the current value, returning the previous value.
447
    ///
448
    /// The stabilized version of this intrinsic is available on the
449 450
    /// [`atomic`] types via the `fetch_xor` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`].
451
    pub fn atomic_xor_rel<T: Copy>(dst: *mut T, src: T) -> T;
452
    /// Bitwise xor with the current value, returning the previous value.
453
    ///
454
    /// The stabilized version of this intrinsic is available on the
455 456
    /// [`atomic`] types via the `fetch_xor` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`].
457
    pub fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
458
    /// Bitwise xor with the current value, returning the previous value.
459
    ///
460
    /// The stabilized version of this intrinsic is available on the
461 462
    /// [`atomic`] types via the `fetch_xor` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`].
463
    pub fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
464

465
    /// Maximum with the current value using a signed comparison.
466
    ///
467
    /// The stabilized version of this intrinsic is available on the
468 469
    /// [`atomic`] signed integer types via the `fetch_max` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`].
470
    pub fn atomic_max<T: Copy>(dst: *mut T, src: T) -> T;
471
    /// Maximum with the current value using a signed comparison.
472
    ///
473
    /// The stabilized version of this intrinsic is available on the
474 475
    /// [`atomic`] signed integer types via the `fetch_max` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`].
476
    pub fn atomic_max_acq<T: Copy>(dst: *mut T, src: T) -> T;
477
    /// Maximum with the current value using a signed comparison.
478
    ///
479
    /// The stabilized version of this intrinsic is available on the
480 481
    /// [`atomic`] signed integer types via the `fetch_max` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`].
482
    pub fn atomic_max_rel<T: Copy>(dst: *mut T, src: T) -> T;
483
    /// Maximum with the current value using a signed comparison.
484
    ///
485
    /// The stabilized version of this intrinsic is available on the
486 487
    /// [`atomic`] signed integer types via the `fetch_max` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`].
488
    pub fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
489
    /// Maximum with the current value.
490
    ///
491
    /// The stabilized version of this intrinsic is available on the
492 493
    /// [`atomic`] signed integer types via the `fetch_max` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`].
494
    pub fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
495

496
    /// Minimum with the current value using a signed comparison.
497
    ///
498
    /// The stabilized version of this intrinsic is available on the
499 500
    /// [`atomic`] signed integer types via the `fetch_min` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`].
501
    pub fn atomic_min<T: Copy>(dst: *mut T, src: T) -> T;
502
    /// Minimum with the current value using a signed comparison.
503
    ///
504
    /// The stabilized version of this intrinsic is available on the
505 506
    /// [`atomic`] signed integer types via the `fetch_min` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`].
507
    pub fn atomic_min_acq<T: Copy>(dst: *mut T, src: T) -> T;
508
    /// Minimum with the current value using a signed comparison.
509
    ///
510
    /// The stabilized version of this intrinsic is available on the
511 512
    /// [`atomic`] signed integer types via the `fetch_min` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`].
513
    pub fn atomic_min_rel<T: Copy>(dst: *mut T, src: T) -> T;
514
    /// Minimum with the current value using a signed comparison.
515
    ///
516
    /// The stabilized version of this intrinsic is available on the
517 518
    /// [`atomic`] signed integer types via the `fetch_min` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`].
519
    pub fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
520
    /// Minimum with the current value using a signed comparison.
521
    ///
522
    /// The stabilized version of this intrinsic is available on the
523 524
    /// [`atomic`] signed integer types via the `fetch_min` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`].
525
    pub fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
526

527
    /// Minimum with the current value using an unsigned comparison.
528
    ///
529
    /// The stabilized version of this intrinsic is available on the
530 531
    /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`].
532
    pub fn atomic_umin<T: Copy>(dst: *mut T, src: T) -> T;
533
    /// Minimum with the current value using an unsigned comparison.
534
    ///
535
    /// The stabilized version of this intrinsic is available on the
536 537
    /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`].
538
    pub fn atomic_umin_acq<T: Copy>(dst: *mut T, src: T) -> T;
539
    /// Minimum with the current value using an unsigned comparison.
540
    ///
541
    /// The stabilized version of this intrinsic is available on the
542 543
    /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`].
544
    pub fn atomic_umin_rel<T: Copy>(dst: *mut T, src: T) -> T;
545
    /// Minimum with the current value using an unsigned comparison.
546
    ///
547
    /// The stabilized version of this intrinsic is available on the
548 549
    /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`].
550
    pub fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
551
    /// Minimum with the current value using an unsigned comparison.
552
    ///
553
    /// The stabilized version of this intrinsic is available on the
554 555
    /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`].
556
    pub fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
557

558
    /// Maximum with the current value using an unsigned comparison.
559
    ///
560
    /// The stabilized version of this intrinsic is available on the
561 562
    /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
    /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`].
563
    pub fn atomic_umax<T: Copy>(dst: *mut T, src: T) -> T;
564
    /// Maximum with the current value using an unsigned comparison.
565
    ///
566
    /// The stabilized version of this intrinsic is available on the
567 568
    /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
    /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`].
569
    pub fn atomic_umax_acq<T: Copy>(dst: *mut T, src: T) -> T;
570
    /// Maximum with the current value using an unsigned comparison.
571
    ///
572
    /// The stabilized version of this intrinsic is available on the
573 574
    /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
    /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`].
575
    pub fn atomic_umax_rel<T: Copy>(dst: *mut T, src: T) -> T;
576
    /// Maximum with the current value using an unsigned comparison.
577
    ///
578
    /// The stabilized version of this intrinsic is available on the
579 580
    /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
    /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`].
581
    pub fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
582
    /// Maximum with the current value using an unsigned comparison.
583
    ///
584
    /// The stabilized version of this intrinsic is available on the
585 586
    /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
    /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`].
587
    pub fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
588 589

    /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
A
Alexander Regueiro 已提交
590
    /// if supported; otherwise, it is a no-op.
591 592 593 594
    /// Prefetches have no effect on the behavior of the program but can change its performance
    /// characteristics.
    ///
    /// The `locality` argument must be a constant integer and is a temporal locality specifier
595 596 597
    /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
    ///
    /// This intrinsic does not have a stable counterpart.
598 599
    pub fn prefetch_read_data<T>(data: *const T, locality: i32);
    /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
A
Alexander Regueiro 已提交
600
    /// if supported; otherwise, it is a no-op.
601 602 603 604
    /// Prefetches have no effect on the behavior of the program but can change its performance
    /// characteristics.
    ///
    /// The `locality` argument must be a constant integer and is a temporal locality specifier
605 606 607
    /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
    ///
    /// This intrinsic does not have a stable counterpart.
608 609
    pub fn prefetch_write_data<T>(data: *const T, locality: i32);
    /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
A
Alexander Regueiro 已提交
610
    /// if supported; otherwise, it is a no-op.
611 612 613 614
    /// Prefetches have no effect on the behavior of the program but can change its performance
    /// characteristics.
    ///
    /// The `locality` argument must be a constant integer and is a temporal locality specifier
615 616 617
    /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
    ///
    /// This intrinsic does not have a stable counterpart.
618 619
    pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
    /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
A
Alexander Regueiro 已提交
620
    /// if supported; otherwise, it is a no-op.
621 622 623 624
    /// Prefetches have no effect on the behavior of the program but can change its performance
    /// characteristics.
    ///
    /// The `locality` argument must be a constant integer and is a temporal locality specifier
625 626 627
    /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
    ///
    /// This intrinsic does not have a stable counterpart.
628
    pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
629 630 631
}

extern "rust-intrinsic" {
632
    /// An atomic fence.
633
    ///
634
    /// The stabilized version of this intrinsic is available in
635
    /// [`atomic::fence`] by passing [`Ordering::SeqCst`]
636
    /// as the `order`.
637
    pub fn atomic_fence();
638
    /// An atomic fence.
639
    ///
640
    /// The stabilized version of this intrinsic is available in
641
    /// [`atomic::fence`] by passing [`Ordering::Acquire`]
642
    /// as the `order`.
643
    pub fn atomic_fence_acq();
644
    /// An atomic fence.
645
    ///
646
    /// The stabilized version of this intrinsic is available in
647
    /// [`atomic::fence`] by passing [`Ordering::Release`]
648
    /// as the `order`.
649
    pub fn atomic_fence_rel();
650
    /// An atomic fence.
651
    ///
652
    /// The stabilized version of this intrinsic is available in
653
    /// [`atomic::fence`] by passing [`Ordering::AcqRel`]
654
    /// as the `order`.
655 656
    pub fn atomic_fence_acqrel();

657 658
    /// A compiler-only memory barrier.
    ///
659 660 661 662
    /// Memory accesses will never be reordered across this barrier by the
    /// compiler, but no instructions will be emitted for it. This is
    /// appropriate for operations on the same thread that may be preempted,
    /// such as when interacting with signal handlers.
663 664
    ///
    /// The stabilized version of this intrinsic is available in
665
    /// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`]
666
    /// as the `order`.
667
    pub fn atomic_singlethreadfence();
668 669 670 671 672 673
    /// A compiler-only memory barrier.
    ///
    /// Memory accesses will never be reordered across this barrier by the
    /// compiler, but no instructions will be emitted for it. This is
    /// appropriate for operations on the same thread that may be preempted,
    /// such as when interacting with signal handlers.
674 675
    ///
    /// The stabilized version of this intrinsic is available in
676
    /// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`]
677
    /// as the `order`.
678
    pub fn atomic_singlethreadfence_acq();
679 680 681 682 683 684
    /// A compiler-only memory barrier.
    ///
    /// Memory accesses will never be reordered across this barrier by the
    /// compiler, but no instructions will be emitted for it. This is
    /// appropriate for operations on the same thread that may be preempted,
    /// such as when interacting with signal handlers.
685 686
    ///
    /// The stabilized version of this intrinsic is available in
687
    /// [`atomic::compiler_fence`] by passing [`Ordering::Release`]
688
    /// as the `order`.
689
    pub fn atomic_singlethreadfence_rel();
690 691 692 693 694 695
    /// A compiler-only memory barrier.
    ///
    /// Memory accesses will never be reordered across this barrier by the
    /// compiler, but no instructions will be emitted for it. This is
    /// appropriate for operations on the same thread that may be preempted,
    /// such as when interacting with signal handlers.
696 697
    ///
    /// The stabilized version of this intrinsic is available in
698
    /// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`]
699
    /// as the `order`.
700 701
    pub fn atomic_singlethreadfence_acqrel();

702 703 704 705
    /// Magic intrinsic that derives its meaning from attributes
    /// attached to the function.
    ///
    /// For example, dataflow uses this to inject static assertions so
706
    /// that `rustc_peek(potentially_uninitialized)` would actually
707 708
    /// double-check that dataflow did indeed compute that it is
    /// uninitialized at that point in the control flow.
709 710
    ///
    /// This intrinsic should not be used outside of the compiler.
711 712
    pub fn rustc_peek<T>(_: T) -> T;

713
    /// Aborts the execution of the process.
714
    ///
715 716 717 718 719
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
720
    /// [`std::process::abort`](../../std/process/fn.abort.html) is to be preferred if possible,
M
Mara Bos 已提交
721
    /// as its behavior is more user-friendly and more stable.
722
    ///
723 724
    /// The current implementation of `intrinsics::abort` is to invoke an invalid instruction,
    /// on most platforms.
725
    /// On Unix, the
726
    /// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or
727
    /// `SIGBUS`.  The precise behaviour is not guaranteed and not stable.
728 729
    pub fn abort() -> !;

730 731
    /// Informs the optimizer that this point in the code is not reachable,
    /// enabling further optimizations.
K
Keegan McAllister 已提交
732
    ///
733
    /// N.B., this is very different from the `unreachable!()` macro: Unlike the
734 735
    /// macro, which panics when it is executed, it is *undefined behavior* to
    /// reach code marked with this function.
736
    ///
737
    /// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`].
738
    #[rustc_const_unstable(feature = "const_unreachable_unchecked", issue = "53188")]
K
Keegan McAllister 已提交
739 740
    pub fn unreachable() -> !;

741
    /// Informs the optimizer that a condition is always true.
V
Viktor Dahl 已提交
742 743
    /// If the condition is false, the behavior is undefined.
    ///
744 745 746 747 748
    /// No code is generated for this intrinsic, but the optimizer will try
    /// to preserve it (and its condition) between passes, which may interfere
    /// with optimization of surrounding code and reduce performance. It should
    /// not be used if the invariant can be discovered by the optimizer on its
    /// own, or if it does not enable any significant optimizations.
749 750
    ///
    /// This intrinsic does not have a stable counterpart.
751
    #[rustc_const_unstable(feature = "const_assume", issue = "76972")]
V
Viktor Dahl 已提交
752 753
    pub fn assume(b: bool);

754 755 756 757
    /// Hints to the compiler that branch condition is likely to be true.
    /// Returns the value passed to it.
    ///
    /// Any use other than with `if` statements will probably not have an effect.
758
    ///
759 760 761 762 763
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
764
    /// This intrinsic does not have a stable counterpart.
G
Gary Guo 已提交
765
    #[rustc_const_unstable(feature = "const_likely", issue = "none")]
766 767 768 769 770 771
    pub fn likely(b: bool) -> bool;

    /// Hints to the compiler that branch condition is likely to be false.
    /// Returns the value passed to it.
    ///
    /// Any use other than with `if` statements will probably not have an effect.
772
    ///
773 774 775 776 777
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
778
    /// This intrinsic does not have a stable counterpart.
G
Gary Guo 已提交
779
    #[rustc_const_unstable(feature = "const_likely", issue = "none")]
780 781
    pub fn unlikely(b: bool) -> bool;

782
    /// Executes a breakpoint trap, for inspection by a debugger.
783 784
    ///
    /// This intrinsic does not have a stable counterpart.
785 786
    pub fn breakpoint();

B
Brian Anderson 已提交
787 788
    /// The size of a type in bytes.
    ///
789 790 791 792 793
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
794 795
    /// More specifically, this is the offset in bytes between successive
    /// items of the same type, including alignment padding.
796
    ///
797
    /// The stabilized version of this intrinsic is [`core::mem::size_of`].
798
    #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
799
    pub fn size_of<T>() -> usize;
800

801 802
    /// The minimum alignment of a type.
    ///
803 804 805 806 807
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
808
    /// The stabilized version of this intrinsic is [`core::mem::align_of`].
809
    #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
810
    pub fn min_align_of<T>() -> usize;
A
Andy Russell 已提交
811
    /// The preferred alignment of a type.
812 813
    ///
    /// This intrinsic does not have a stable counterpart.
814
    #[rustc_const_unstable(feature = "const_pref_align_of", issue = "none")]
815
    pub fn pref_align_of<T>() -> usize;
816

817 818
    /// The size of the referenced value in bytes.
    ///
819
    /// The stabilized version of this intrinsic is [`mem::size_of_val`].
820
    #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
821
    pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
822 823
    /// The required alignment of the referenced value.
    ///
824
    /// The stabilized version of this intrinsic is [`core::mem::align_of_val`].
825
    #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
826 827
    pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;

828
    /// Gets a static string slice containing the name of a type.
829
    ///
830 831 832 833 834
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
835
    /// The stabilized version of this intrinsic is [`core::any::type_name`].
836
    #[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
837 838
    pub fn type_name<T: ?Sized>() -> &'static str;

A
Alex Crichton 已提交
839 840 841
    /// Gets an identifier which is globally unique to the specified type. This
    /// function will return the same value for a type regardless of whichever
    /// crate it is invoked in.
842
    ///
843 844 845 846 847
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
848
    /// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
849
    #[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
850 851
    pub fn type_id<T: ?Sized + 'static>() -> u64;

852 853
    /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
    /// This will statically either panic, or do nothing.
854 855
    ///
    /// This intrinsic does not have a stable counterpart.
A
Albin Hedman 已提交
856
    #[rustc_const_unstable(feature = "const_assert_type", issue = "none")]
857 858
    pub fn assert_inhabited<T>();

859 860
    /// A guard for unsafe functions that cannot ever be executed if `T` does not permit
    /// zero-initialization: This will statically either panic, or do nothing.
861 862
    ///
    /// This intrinsic does not have a stable counterpart.
863
    pub fn assert_zero_valid<T>();
864 865 866

    /// A guard for unsafe functions that cannot ever be executed if `T` has invalid
    /// bit patterns: This will statically either panic, or do nothing.
867 868
    ///
    /// This intrinsic does not have a stable counterpart.
869
    pub fn assert_uninit_valid<T>();
870

871
    /// Gets a reference to a static `Location` indicating where it was called.
872
    ///
873 874 875 876 877
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
878
    /// Consider using [`core::panic::Location::caller`] instead.
879
    #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
880 881
    pub fn caller_location() -> &'static crate::panic::Location<'static>;

882
    /// Moves a value out of scope without running drop glue.
883
    ///
884 885
    /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses
    /// `ManuallyDrop` instead.
886 887 888 889 890
    ///
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
891
    #[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")]
892
    pub fn forget<T: ?Sized>(_: T);
893

K
Keegan McAllister 已提交
894 895 896
    /// Reinterprets the bits of a value of one type as another type.
    ///
    /// Both types must have the same size. Neither the original, nor the result,
897
    /// may be an [invalid value](../../nomicon/what-unsafe-does.html).
898
    ///
U
ubsan 已提交
899
    /// `transmute` is semantically equivalent to a bitwise move of one type
K
Keegan McAllister 已提交
900 901 902
    /// into another. It copies the bits from the source value into the
    /// destination value, then forgets the original. It's equivalent to C's
    /// `memcpy` under the hood, just like `transmute_copy`.
903
    ///
904 905 906 907 908 909
    /// Because `transmute` is a by-value operation, alignment of the *transmuted values
    /// themselves* is not a concern. As with any other function, the compiler already ensures
    /// both `T` and `U` are properly aligned. However, when transmuting values that *point
    /// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper
    /// alignment of the pointed-to values.
    ///
K
Keegan McAllister 已提交
910 911
    /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
    /// cause [undefined behavior][ub] with this function. `transmute` should be
912 913
    /// the absolute last resort.
    ///
U
ubsan 已提交
914 915
    /// The [nomicon](../../nomicon/transmutes.html) has additional
    /// documentation.
916
    ///
S
Steve Klabnik 已提交
917
    /// [ub]: ../../reference/behavior-considered-undefined.html
K
Keegan McAllister 已提交
918
    ///
A
Alexis Beingessner 已提交
919
    /// # Examples
920
    ///
U
ubsan 已提交
921 922
    /// There are a few things that `transmute` is really useful for.
    ///
K
Keegan McAllister 已提交
923 924
    /// Turning a pointer into a function pointer. This is *not* portable to
    /// machines where function pointers and data pointers have different sizes.
U
ubsan 已提交
925 926 927 928 929 930 931 932 933 934 935 936
    ///
    /// ```
    /// fn foo() -> i32 {
    ///     0
    /// }
    /// let pointer = foo as *const ();
    /// let function = unsafe {
    ///     std::mem::transmute::<*const (), fn() -> i32>(pointer)
    /// };
    /// assert_eq!(function(), 0);
    /// ```
    ///
K
Keegan McAllister 已提交
937 938
    /// Extending a lifetime, or shortening an invariant lifetime. This is
    /// advanced, very unsafe Rust!
U
ubsan 已提交
939 940 941 942 943 944 945 946 947 948 949 950 951
    ///
    /// ```
    /// struct R<'a>(&'a i32);
    /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
    ///     std::mem::transmute::<R<'b>, R<'static>>(r)
    /// }
    ///
    /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
    ///                                              -> &'b mut R<'c> {
    ///     std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
    /// }
    /// ```
    ///
U
ubsan 已提交
952 953
    /// # Alternatives
    ///
K
Keegan McAllister 已提交
954 955 956
    /// Don't despair: many uses of `transmute` can be achieved through other means.
    /// Below are common applications of `transmute` which can be replaced with safer
    /// constructs.
957
    ///
958 959 960 961 962 963
    /// Turning raw bytes(`&[u8]`) to `u32`, `f64`, etc.:
    ///
    /// ```
    /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
    ///
    /// let num = unsafe {
A
Austin Keeley 已提交
964
    ///     std::mem::transmute::<[u8; 4], u32>(raw_bytes)
965 966 967 968
    /// };
    ///
    /// // use `u32::from_ne_bytes` instead
    /// let num = u32::from_ne_bytes(raw_bytes);
969
    /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
970 971 972 973 974 975
    /// let num = u32::from_le_bytes(raw_bytes);
    /// assert_eq!(num, 0x12345678);
    /// let num = u32::from_be_bytes(raw_bytes);
    /// assert_eq!(num, 0x78563412);
    /// ```
    ///
U
ubsan 已提交
976
    /// Turning a pointer into a `usize`:
U
ubsan 已提交
977
    ///
978
    /// ```
U
ubsan 已提交
979
    /// let ptr = &0;
U
ubsan 已提交
980 981 982
    /// let ptr_num_transmute = unsafe {
    ///     std::mem::transmute::<&i32, usize>(ptr)
    /// };
K
Keegan McAllister 已提交
983
    ///
U
ubsan 已提交
984
    /// // Use an `as` cast instead
U
ubsan 已提交
985
    /// let ptr_num_cast = ptr as *const i32 as usize;
986
    /// ```
U
ubsan 已提交
987
    ///
U
ubsan 已提交
988
    /// Turning a `*mut T` into an `&mut T`:
U
ubsan 已提交
989
    ///
U
ubsan 已提交
990 991
    /// ```
    /// let ptr: *mut i32 = &mut 0;
U
ubsan 已提交
992 993 994
    /// let ref_transmuted = unsafe {
    ///     std::mem::transmute::<*mut i32, &mut i32>(ptr)
    /// };
K
Keegan McAllister 已提交
995
    ///
U
ubsan 已提交
996
    /// // Use a reborrow instead
U
ubsan 已提交
997
    /// let ref_casted = unsafe { &mut *ptr };
U
ubsan 已提交
998
    /// ```
U
ubsan 已提交
999
    ///
U
ubsan 已提交
1000
    /// Turning an `&mut T` into an `&mut U`:
U
ubsan 已提交
1001
    ///
U
ubsan 已提交
1002 1003
    /// ```
    /// let ptr = &mut 0;
U
ubsan 已提交
1004 1005 1006
    /// let val_transmuted = unsafe {
    ///     std::mem::transmute::<&mut i32, &mut u32>(ptr)
    /// };
K
Keegan McAllister 已提交
1007
    ///
U
ubsan 已提交
1008 1009
    /// // Now, put together `as` and reborrowing - note the chaining of `as`
    /// // `as` is not transitive
U
ubsan 已提交
1010
    /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
U
ubsan 已提交
1011
    /// ```
1012
    ///
U
ubsan 已提交
1013
    /// Turning an `&str` into an `&[u8]`:
1014
    ///
U
ubsan 已提交
1015 1016
    /// ```
    /// // this is not a good way to do this.
U
ubsan 已提交
1017
    /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
U
ubsan 已提交
1018
    /// assert_eq!(slice, &[82, 117, 115, 116]);
K
Keegan McAllister 已提交
1019
    ///
U
ubsan 已提交
1020 1021
    /// // You could use `str::as_bytes`
    /// let slice = "Rust".as_bytes();
U
ubsan 已提交
1022
    /// assert_eq!(slice, &[82, 117, 115, 116]);
K
Keegan McAllister 已提交
1023
    ///
U
ubsan 已提交
1024 1025
    /// // Or, just use a byte string, if you have control over the string
    /// // literal
U
ubsan 已提交
1026
    /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
U
ubsan 已提交
1027
    /// ```
U
ubsan 已提交
1028
    ///
1029 1030 1031 1032 1033 1034 1035
    /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`.
    ///
    /// To transmute the inner type of the contents of a container, you must make sure to not
    /// violate any of the container's invariants. For `Vec`, this means that both the size
    /// *and alignment* of the inner types have to match. Other containers might rely on the
    /// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't
    /// be possible at all without violating the container invariants.
U
ubsan 已提交
1036
    ///
U
ubsan 已提交
1037 1038
    /// ```
    /// let store = [0, 1, 2, 3];
1039 1040 1041 1042
    /// let v_orig = store.iter().collect::<Vec<&i32>>();
    ///
    /// // clone the vector as we will reuse them later
    /// let v_clone = v_orig.clone();
K
Keegan McAllister 已提交
1043
    ///
R
Ralf Jung 已提交
1044
    /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
R
Ralf Jung 已提交
1045
    /// // bad idea and could cause Undefined Behavior.
U
ubsan 已提交
1046
    /// // However, it is no-copy.
U
ubsan 已提交
1047
    /// let v_transmuted = unsafe {
1048
    ///     std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
U
ubsan 已提交
1049
    /// };
K
Keegan McAllister 已提交
1050
    ///
1051 1052
    /// let v_clone = v_orig.clone();
    ///
U
ubsan 已提交
1053
    /// // This is the suggested, safe way.
K
Keegan McAllister 已提交
1054
    /// // It does copy the entire vector, though, into a new array.
1055 1056 1057 1058 1059
    /// let v_collected = v_clone.into_iter()
    ///                          .map(Some)
    ///                          .collect::<Vec<Option<&i32>>>();
    ///
    /// let v_clone = v_orig.clone();
K
Keegan McAllister 已提交
1060
    ///
1061 1062 1063 1064 1065
    /// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the
    /// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but
    /// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`),
    /// // this has all the same caveats. Besides the information provided above, also consult the
    /// // [`from_raw_parts`] documentation.
U
ubsan 已提交
1066
    /// let v_from_raw = unsafe {
J
Jake Goulding 已提交
1067
    // FIXME Update this when vec_into_raw_parts is stabilized
1068 1069 1070 1071 1072
    ///     // Ensure the original vector is not dropped.
    ///     let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
    ///     Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
    ///                         v_clone.len(),
    ///                         v_clone.capacity())
U
ubsan 已提交
1073
    /// };
U
ubsan 已提交
1074
    /// ```
U
ubsan 已提交
1075
    ///
R
Ralf Jung 已提交
1076 1077
    /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
    ///
U
ubsan 已提交
1078 1079
    /// Implementing `split_at_mut`:
    ///
U
ubsan 已提交
1080 1081
    /// ```
    /// use std::{slice, mem};
K
Keegan McAllister 已提交
1082
    ///
A
Alexander Regueiro 已提交
1083 1084
    /// // There are multiple ways to do this, and there are multiple problems
    /// // with the following (transmute) way.
U
ubsan 已提交
1085
    /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
U
ubsan 已提交
1086 1087
    ///                              -> (&mut [T], &mut [T]) {
    ///     let len = slice.len();
U
ubsan 已提交
1088
    ///     assert!(mid <= len);
U
ubsan 已提交
1089
    ///     unsafe {
U
ubsan 已提交
1090
    ///         let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
A
Andy Russell 已提交
1091
    ///         // first: transmute is not type safe; all it checks is that T and
U
ubsan 已提交
1092
    ///         // U are of the same size. Second, right here, you have two
U
ubsan 已提交
1093
    ///         // mutable references pointing to the same memory.
U
ubsan 已提交
1094
    ///         (&mut slice[0..mid], &mut slice2[mid..len])
U
ubsan 已提交
1095
    ///     }
U
ubsan 已提交
1096
    /// }
K
Keegan McAllister 已提交
1097
    ///
A
Andy Russell 已提交
1098
    /// // This gets rid of the type safety problems; `&mut *` will *only* give
U
ubsan 已提交
1099
    /// // you an `&mut T` from an `&mut T` or `*mut T`.
U
ubsan 已提交
1100
    /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
U
ubsan 已提交
1101 1102
    ///                          -> (&mut [T], &mut [T]) {
    ///     let len = slice.len();
U
ubsan 已提交
1103
    ///     assert!(mid <= len);
U
ubsan 已提交
1104 1105 1106
    ///     unsafe {
    ///         let slice2 = &mut *(slice as *mut [T]);
    ///         // however, you still have two mutable references pointing to
U
ubsan 已提交
1107
    ///         // the same memory.
U
ubsan 已提交
1108
    ///         (&mut slice[0..mid], &mut slice2[mid..len])
U
ubsan 已提交
1109 1110
    ///     }
    /// }
K
Keegan McAllister 已提交
1111
    ///
U
ubsan 已提交
1112 1113
    /// // This is how the standard library does it. This is the best method, if
    /// // you need to do something like this
U
ubsan 已提交
1114
    /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
U
ubsan 已提交
1115
    ///                       -> (&mut [T], &mut [T]) {
U
ubsan 已提交
1116 1117
    ///     let len = slice.len();
    ///     assert!(mid <= len);
U
ubsan 已提交
1118
    ///     unsafe {
U
ubsan 已提交
1119
    ///         let ptr = slice.as_mut_ptr();
U
ubsan 已提交
1120 1121
    ///         // This now has three mutable references pointing at the same
    ///         // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
U
ubsan 已提交
1122 1123 1124
    ///         // `slice` is never used after `let ptr = ...`, and so one can
    ///         // treat it as "dead", and therefore, you only have two real
    ///         // mutable slices.
U
ubsan 已提交
1125
    ///         (slice::from_raw_parts_mut(ptr, mid),
1126
    ///          slice::from_raw_parts_mut(ptr.add(mid), len - mid))
U
ubsan 已提交
1127
    ///     }
1128
    /// }
1129
    /// ```
B
Brian Anderson 已提交
1130
    #[stable(feature = "rust1", since = "1.0.0")]
1131 1132 1133
    // NOTE: While this makes the intrinsic const stable, we have some custom code in const fn
    // checks that prevent its use within `const fn`.
    #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
M
Mark Rousskov 已提交
1134
    #[rustc_diagnostic_item = "transmute"]
1135
    pub fn transmute<T, U>(e: T) -> U;
1136

1137 1138 1139 1140 1141
    /// Returns `true` if the actual type given as `T` requires drop
    /// glue; returns `false` if the actual type provided for `T`
    /// implements `Copy`.
    ///
    /// If the actual type neither requires drop glue nor implements
1142
    /// `Copy`, then the return value of this function is unspecified.
1143
    ///
1144 1145 1146 1147 1148
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1149
    /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1150
    #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1151
    pub fn needs_drop<T>() -> bool;
1152

S
Simonas Kazlauskas 已提交
1153
    /// Calculates the offset from a pointer.
D
Daniel Micay 已提交
1154
    ///
1155 1156
    /// This is implemented as an intrinsic to avoid converting to and from an
    /// integer, since the conversion would throw away aliasing information.
S
Simonas Kazlauskas 已提交
1157 1158 1159 1160 1161 1162 1163
    ///
    /// # Safety
    ///
    /// Both the starting and resulting pointer must be either in bounds or one
    /// byte past the end of an allocated object. If either pointer is out of
    /// bounds or arithmetic overflow occurs then any further use of the
    /// returned value will result in undefined behavior.
1164
    ///
1165
    /// The stabilized version of this intrinsic is [`pointer::offset`].
S
Steve Klabnik 已提交
1166
    #[must_use = "returns a new pointer rather than modifying its argument"]
1167
    #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
1168
    pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
D
Daniel Micay 已提交
1169

1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
    /// Calculates the offset from a pointer, potentially wrapping.
    ///
    /// This is implemented as an intrinsic to avoid converting to and from an
    /// integer, since the conversion inhibits certain optimizations.
    ///
    /// # Safety
    ///
    /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
    /// resulting pointer to point into or one byte past the end of an allocated
    /// object, and it wraps with two's complement arithmetic. The resulting
    /// value is not necessarily valid to be used to actually access memory.
1181
    ///
1182
    /// The stabilized version of this intrinsic is [`pointer::wrapping_offset`].
S
Steve Klabnik 已提交
1183
    #[must_use = "returns a new pointer rather than modifying its argument"]
1184
    #[rustc_const_unstable(feature = "const_ptr_offset", issue = "71499")]
1185 1186
    pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;

1187 1188 1189 1190
    /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
    /// a size of `count` * `size_of::<T>()` and an alignment of
    /// `min_align_of::<T>()`
    ///
1191 1192
    /// The volatile parameter is set to `true`, so it will not be optimized out
    /// unless size is equal to zero.
1193 1194
    ///
    /// This intrinsic does not have a stable counterpart.
M
Mark Rousskov 已提交
1195
    pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1196
    /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1197
    /// a size of `count * size_of::<T>()` and an alignment of
1198 1199
    /// `min_align_of::<T>()`
    ///
1200
    /// The volatile parameter is set to `true`, so it will not be optimized out
R
Ralf Jung 已提交
1201
    /// unless size is equal to zero.
1202 1203
    ///
    /// This intrinsic does not have a stable counterpart.
1204
    pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1205
    /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1206
    /// size of `count * size_of::<T>()` and an alignment of
1207 1208
    /// `min_align_of::<T>()`.
    ///
1209 1210
    /// The volatile parameter is set to `true`, so it will not be optimized out
    /// unless size is equal to zero.
1211 1212
    ///
    /// This intrinsic does not have a stable counterpart.
1213
    pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1214

A
Alexander Regueiro 已提交
1215
    /// Performs a volatile load from the `src` pointer.
1216
    ///
1217
    /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`].
1218
    pub fn volatile_load<T>(src: *const T) -> T;
A
Alexander Regueiro 已提交
1219
    /// Performs a volatile store to the `dst` pointer.
1220
    ///
1221
    /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`].
1222 1223
    pub fn volatile_store<T>(dst: *mut T, val: T);

A
Alexander Regueiro 已提交
1224
    /// Performs a volatile load from the `src` pointer
1225
    /// The pointer is not required to be aligned.
1226 1227
    ///
    /// This intrinsic does not have a stable counterpart.
1228
    pub fn unaligned_volatile_load<T>(src: *const T) -> T;
A
Alexander Regueiro 已提交
1229
    /// Performs a volatile store to the `dst` pointer.
1230
    /// The pointer is not required to be aligned.
1231 1232
    ///
    /// This intrinsic does not have a stable counterpart.
1233 1234
    pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);

L
lucy 已提交
1235
    /// Returns the square root of an `f32`
1236
    ///
1237
    /// The stabilized version of this intrinsic is
1238
    /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1239
    pub fn sqrtf32(x: f32) -> f32;
L
lucy 已提交
1240
    /// Returns the square root of an `f64`
1241
    ///
1242
    /// The stabilized version of this intrinsic is
1243
    /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1244
    pub fn sqrtf64(x: f64) -> f64;
1245

L
lucy 已提交
1246
    /// Raises an `f32` to an integer power.
1247
    ///
1248
    /// The stabilized version of this intrinsic is
1249
    /// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1250
    pub fn powif32(a: f32, x: i32) -> f32;
L
lucy 已提交
1251
    /// Raises an `f64` to an integer power.
1252
    ///
1253
    /// The stabilized version of this intrinsic is
1254
    /// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1255
    pub fn powif64(a: f64, x: i32) -> f64;
1256

L
lucy 已提交
1257
    /// Returns the sine of an `f32`.
1258
    ///
1259
    /// The stabilized version of this intrinsic is
1260
    /// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1261
    pub fn sinf32(x: f32) -> f32;
L
lucy 已提交
1262
    /// Returns the sine of an `f64`.
1263
    ///
1264
    /// The stabilized version of this intrinsic is
1265
    /// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1266
    pub fn sinf64(x: f64) -> f64;
1267

L
lucy 已提交
1268
    /// Returns the cosine of an `f32`.
1269
    ///
1270
    /// The stabilized version of this intrinsic is
1271
    /// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1272
    pub fn cosf32(x: f32) -> f32;
L
lucy 已提交
1273
    /// Returns the cosine of an `f64`.
1274
    ///
1275
    /// The stabilized version of this intrinsic is
1276
    /// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1277
    pub fn cosf64(x: f64) -> f64;
1278

L
lucy 已提交
1279
    /// Raises an `f32` to an `f32` power.
1280
    ///
1281
    /// The stabilized version of this intrinsic is
1282
    /// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1283
    pub fn powf32(a: f32, x: f32) -> f32;
L
lucy 已提交
1284
    /// Raises an `f64` to an `f64` power.
1285
    ///
1286
    /// The stabilized version of this intrinsic is
1287
    /// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1288
    pub fn powf64(a: f64, x: f64) -> f64;
1289

L
lucy 已提交
1290
    /// Returns the exponential of an `f32`.
1291
    ///
1292
    /// The stabilized version of this intrinsic is
1293
    /// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1294
    pub fn expf32(x: f32) -> f32;
L
lucy 已提交
1295
    /// Returns the exponential of an `f64`.
1296
    ///
1297
    /// The stabilized version of this intrinsic is
1298
    /// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1299
    pub fn expf64(x: f64) -> f64;
1300

L
lucy 已提交
1301
    /// Returns 2 raised to the power of an `f32`.
1302
    ///
1303
    /// The stabilized version of this intrinsic is
1304
    /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1305
    pub fn exp2f32(x: f32) -> f32;
L
lucy 已提交
1306
    /// Returns 2 raised to the power of an `f64`.
1307
    ///
1308
    /// The stabilized version of this intrinsic is
1309
    /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1310
    pub fn exp2f64(x: f64) -> f64;
1311

L
lucy 已提交
1312
    /// Returns the natural logarithm of an `f32`.
1313
    ///
1314
    /// The stabilized version of this intrinsic is
1315
    /// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1316
    pub fn logf32(x: f32) -> f32;
L
lucy 已提交
1317
    /// Returns the natural logarithm of an `f64`.
1318
    ///
1319
    /// The stabilized version of this intrinsic is
1320
    /// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1321
    pub fn logf64(x: f64) -> f64;
1322

L
lucy 已提交
1323
    /// Returns the base 10 logarithm of an `f32`.
1324
    ///
1325
    /// The stabilized version of this intrinsic is
1326
    /// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1327
    pub fn log10f32(x: f32) -> f32;
L
lucy 已提交
1328
    /// Returns the base 10 logarithm of an `f64`.
1329
    ///
1330
    /// The stabilized version of this intrinsic is
1331
    /// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1332
    pub fn log10f64(x: f64) -> f64;
1333

L
lucy 已提交
1334
    /// Returns the base 2 logarithm of an `f32`.
1335
    ///
1336
    /// The stabilized version of this intrinsic is
1337
    /// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1338
    pub fn log2f32(x: f32) -> f32;
L
lucy 已提交
1339
    /// Returns the base 2 logarithm of an `f64`.
1340
    ///
1341
    /// The stabilized version of this intrinsic is
1342
    /// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1343 1344
    pub fn log2f64(x: f64) -> f64;

L
lucy 已提交
1345
    /// Returns `a * b + c` for `f32` values.
1346
    ///
1347
    /// The stabilized version of this intrinsic is
1348
    /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1349
    pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
L
lucy 已提交
1350
    /// Returns `a * b + c` for `f64` values.
1351
    ///
1352
    /// The stabilized version of this intrinsic is
1353
    /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1354
    pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1355

L
lucy 已提交
1356
    /// Returns the absolute value of an `f32`.
1357
    ///
1358
    /// The stabilized version of this intrinsic is
1359
    /// [`f32::abs`](../../std/primitive.f32.html#method.abs)
1360
    pub fn fabsf32(x: f32) -> f32;
L
lucy 已提交
1361
    /// Returns the absolute value of an `f64`.
1362
    ///
1363
    /// The stabilized version of this intrinsic is
1364
    /// [`f64::abs`](../../std/primitive.f64.html#method.abs)
1365
    pub fn fabsf64(x: f64) -> f64;
1366

1367
    /// Returns the minimum of two `f32` values.
1368
    ///
1369 1370 1371 1372 1373
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1374
    /// The stabilized version of this intrinsic is
1375
    /// [`f32::min`]
1376 1377
    pub fn minnumf32(x: f32, y: f32) -> f32;
    /// Returns the minimum of two `f64` values.
1378
    ///
1379 1380 1381 1382 1383
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1384
    /// The stabilized version of this intrinsic is
1385
    /// [`f64::min`]
1386 1387
    pub fn minnumf64(x: f64, y: f64) -> f64;
    /// Returns the maximum of two `f32` values.
1388
    ///
1389 1390 1391 1392 1393
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1394
    /// The stabilized version of this intrinsic is
1395
    /// [`f32::max`]
1396 1397
    pub fn maxnumf32(x: f32, y: f32) -> f32;
    /// Returns the maximum of two `f64` values.
1398
    ///
1399 1400 1401 1402 1403
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1404
    /// The stabilized version of this intrinsic is
1405
    /// [`f64::max`]
1406 1407
    pub fn maxnumf64(x: f64, y: f64) -> f64;

L
lucy 已提交
1408
    /// Copies the sign from `y` to `x` for `f32` values.
1409
    ///
1410
    /// The stabilized version of this intrinsic is
1411
    /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
1412
    pub fn copysignf32(x: f32, y: f32) -> f32;
L
lucy 已提交
1413
    /// Copies the sign from `y` to `x` for `f64` values.
1414
    ///
1415
    /// The stabilized version of this intrinsic is
1416
    /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
1417
    pub fn copysignf64(x: f64, y: f64) -> f64;
1418

L
lucy 已提交
1419
    /// Returns the largest integer less than or equal to an `f32`.
1420
    ///
1421
    /// The stabilized version of this intrinsic is
1422
    /// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1423
    pub fn floorf32(x: f32) -> f32;
L
lucy 已提交
1424
    /// Returns the largest integer less than or equal to an `f64`.
1425
    ///
1426
    /// The stabilized version of this intrinsic is
1427
    /// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1428 1429
    pub fn floorf64(x: f64) -> f64;

L
lucy 已提交
1430
    /// Returns the smallest integer greater than or equal to an `f32`.
1431
    ///
1432
    /// The stabilized version of this intrinsic is
1433
    /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1434
    pub fn ceilf32(x: f32) -> f32;
L
lucy 已提交
1435
    /// Returns the smallest integer greater than or equal to an `f64`.
1436
    ///
1437
    /// The stabilized version of this intrinsic is
1438
    /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1439
    pub fn ceilf64(x: f64) -> f64;
1440

L
lucy 已提交
1441
    /// Returns the integer part of an `f32`.
1442
    ///
1443
    /// The stabilized version of this intrinsic is
1444
    /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1445
    pub fn truncf32(x: f32) -> f32;
L
lucy 已提交
1446
    /// Returns the integer part of an `f64`.
1447
    ///
1448
    /// The stabilized version of this intrinsic is
1449
    /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1450
    pub fn truncf64(x: f64) -> f64;
1451

L
lucy 已提交
1452 1453
    /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
    /// if the argument is not an integer.
1454
    pub fn rintf32(x: f32) -> f32;
L
lucy 已提交
1455 1456
    /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
    /// if the argument is not an integer.
1457 1458
    pub fn rintf64(x: f64) -> f64;

L
lucy 已提交
1459
    /// Returns the nearest integer to an `f32`.
1460 1461
    ///
    /// This intrinsic does not have a stable counterpart.
1462
    pub fn nearbyintf32(x: f32) -> f32;
L
lucy 已提交
1463
    /// Returns the nearest integer to an `f64`.
1464 1465
    ///
    /// This intrinsic does not have a stable counterpart.
1466 1467
    pub fn nearbyintf64(x: f64) -> f64;

L
lucy 已提交
1468
    /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1469
    ///
1470
    /// The stabilized version of this intrinsic is
1471
    /// [`f32::round`](../../std/primitive.f32.html#method.round)
1472
    pub fn roundf32(x: f32) -> f32;
L
lucy 已提交
1473
    /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1474
    ///
1475
    /// The stabilized version of this intrinsic is
1476
    /// [`f64::round`](../../std/primitive.f64.html#method.round)
1477
    pub fn roundf64(x: f64) -> f64;
A
Alex Crichton 已提交
1478

1479 1480
    /// Float addition that allows optimizations based on algebraic rules.
    /// May assume inputs are finite.
1481 1482
    ///
    /// This intrinsic does not have a stable counterpart.
1483
    pub fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1484 1485 1486

    /// Float subtraction that allows optimizations based on algebraic rules.
    /// May assume inputs are finite.
1487 1488
    ///
    /// This intrinsic does not have a stable counterpart.
1489
    pub fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1490 1491 1492

    /// Float multiplication that allows optimizations based on algebraic rules.
    /// May assume inputs are finite.
1493 1494
    ///
    /// This intrinsic does not have a stable counterpart.
1495
    pub fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1496 1497 1498

    /// Float division that allows optimizations based on algebraic rules.
    /// May assume inputs are finite.
1499 1500
    ///
    /// This intrinsic does not have a stable counterpart.
1501
    pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1502 1503 1504

    /// Float remainder that allows optimizations based on algebraic rules.
    /// May assume inputs are finite.
1505 1506
    ///
    /// This intrinsic does not have a stable counterpart.
1507
    pub fn frem_fast<T: Copy>(a: T, b: T) -> T;
1508

1509 1510 1511
    /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
    /// (<https://github.com/rust-lang/rust/issues/10184>)
    ///
1512
    /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1513 1514
    pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;

1515
    /// Returns the number of bits set in an integer type `T`
1516
    ///
1517 1518 1519 1520 1521
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1522 1523
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `count_ones` method. For example,
1524
    /// [`u32::count_ones`]
1525
    #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1526
    pub fn ctpop<T: Copy>(x: T) -> T;
1527

1528 1529
    /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
    ///
1530 1531 1532 1533 1534
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1535 1536
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `leading_zeros` method. For example,
1537
    /// [`u32::leading_zeros`]
1538 1539 1540 1541 1542 1543 1544 1545 1546
    ///
    /// # Examples
    ///
    /// ```
    /// #![feature(core_intrinsics)]
    ///
    /// use std::intrinsics::ctlz;
    ///
    /// let x = 0b0001_1100_u8;
1547
    /// let num_leading = ctlz(x);
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
    /// assert_eq!(num_leading, 3);
    /// ```
    ///
    /// An `x` with value `0` will return the bit width of `T`.
    ///
    /// ```
    /// #![feature(core_intrinsics)]
    ///
    /// use std::intrinsics::ctlz;
    ///
    /// let x = 0u16;
1559
    /// let num_leading = ctlz(x);
1560 1561
    /// assert_eq!(num_leading, 16);
    /// ```
1562
    #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1563
    pub fn ctlz<T: Copy>(x: T) -> T;
1564

1565 1566 1567
    /// Like `ctlz`, but extra-unsafe as it returns `undef` when
    /// given an `x` with value `0`.
    ///
1568 1569
    /// This intrinsic does not have a stable counterpart.
    ///
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
    /// # Examples
    ///
    /// ```
    /// #![feature(core_intrinsics)]
    ///
    /// use std::intrinsics::ctlz_nonzero;
    ///
    /// let x = 0b0001_1100_u8;
    /// let num_leading = unsafe { ctlz_nonzero(x) };
    /// assert_eq!(num_leading, 3);
    /// ```
1581
    #[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
1582
    pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
1583

1584 1585
    /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
    ///
1586 1587 1588 1589 1590
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1591 1592
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `trailing_zeros` method. For example,
1593
    /// [`u32::trailing_zeros`]
1594 1595 1596 1597 1598 1599 1600 1601 1602
    ///
    /// # Examples
    ///
    /// ```
    /// #![feature(core_intrinsics)]
    ///
    /// use std::intrinsics::cttz;
    ///
    /// let x = 0b0011_1000_u8;
1603
    /// let num_trailing = cttz(x);
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
    /// assert_eq!(num_trailing, 3);
    /// ```
    ///
    /// An `x` with value `0` will return the bit width of `T`:
    ///
    /// ```
    /// #![feature(core_intrinsics)]
    ///
    /// use std::intrinsics::cttz;
    ///
    /// let x = 0u16;
1615
    /// let num_trailing = cttz(x);
1616 1617
    /// assert_eq!(num_trailing, 16);
    /// ```
1618
    #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1619
    pub fn cttz<T: Copy>(x: T) -> T;
1620

1621 1622 1623
    /// Like `cttz`, but extra-unsafe as it returns `undef` when
    /// given an `x` with value `0`.
    ///
1624 1625
    /// This intrinsic does not have a stable counterpart.
    ///
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
    /// # Examples
    ///
    /// ```
    /// #![feature(core_intrinsics)]
    ///
    /// use std::intrinsics::cttz_nonzero;
    ///
    /// let x = 0b0011_1000_u8;
    /// let num_trailing = unsafe { cttz_nonzero(x) };
    /// assert_eq!(num_trailing, 3);
    /// ```
A
Andreas Jonson 已提交
1637
    #[rustc_const_stable(feature = "const_cttz", since = "1.53.0")]
1638
    pub fn cttz_nonzero<T: Copy>(x: T) -> T;
1639

1640
    /// Reverses the bytes in an integer type `T`.
1641
    ///
1642 1643 1644 1645 1646
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1647 1648
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `swap_bytes` method. For example,
1649
    /// [`u32::swap_bytes`]
1650
    #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1651
    pub fn bswap<T: Copy>(x: T) -> T;
1652

A
Amanieu d'Antras 已提交
1653
    /// Reverses the bits in an integer type `T`.
1654
    ///
1655 1656 1657 1658 1659
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1660 1661
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `reverse_bits` method. For example,
1662
    /// [`u32::reverse_bits`]
1663
    #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1664
    pub fn bitreverse<T: Copy>(x: T) -> T;
A
Amanieu d'Antras 已提交
1665

1666
    /// Performs checked integer addition.
1667
    ///
1668 1669 1670 1671 1672
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1673 1674
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `overflowing_add` method. For example,
1675
    /// [`u32::overflowing_add`]
1676
    #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1677
    pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1678 1679

    /// Performs checked integer subtraction
1680
    ///
1681 1682 1683 1684 1685
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1686 1687
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `overflowing_sub` method. For example,
1688
    /// [`u32::overflowing_sub`]
1689
    #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1690
    pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1691 1692

    /// Performs checked integer multiplication
1693
    ///
1694 1695 1696 1697 1698
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1699 1700
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `overflowing_mul` method. For example,
1701
    /// [`u32::overflowing_mul`]
1702
    #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1703
    pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1704

1705
    /// Performs an exact division, resulting in undefined behavior where
1706
    /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1707 1708
    ///
    /// This intrinsic does not have a stable counterpart.
1709
    pub fn exact_div<T: Copy>(x: T, y: T) -> T;
1710

1711
    /// Performs an unchecked division, resulting in undefined behavior
1712
    /// where `y == 0` or `x == T::MIN && y == -1`
1713
    ///
1714
    /// Safe wrappers for this intrinsic are available on the integer
1715
    /// primitives via the `checked_div` method. For example,
1716
    /// [`u32::checked_div`]
1717
    #[rustc_const_stable(feature = "const_int_unchecked_arith", since = "1.52.0")]
1718
    pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1719
    /// Returns the remainder of an unchecked division, resulting in
1720
    /// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
1721
    ///
1722
    /// Safe wrappers for this intrinsic are available on the integer
1723
    /// primitives via the `checked_rem` method. For example,
1724
    /// [`u32::checked_rem`]
1725
    #[rustc_const_stable(feature = "const_int_unchecked_arith", since = "1.52.0")]
1726
    pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1727

1728
    /// Performs an unchecked left shift, resulting in undefined behavior when
1729
    /// `y < 0` or `y >= N`, where N is the width of T in bits.
1730
    ///
1731
    /// Safe wrappers for this intrinsic are available on the integer
1732
    /// primitives via the `checked_shl` method. For example,
1733
    /// [`u32::checked_shl`]
1734
    #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1735
    pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
1736
    /// Performs an unchecked right shift, resulting in undefined behavior when
1737
    /// `y < 0` or `y >= N`, where N is the width of T in bits.
1738
    ///
1739
    /// Safe wrappers for this intrinsic are available on the integer
1740
    /// primitives via the `checked_shr` method. For example,
1741
    /// [`u32::checked_shr`]
1742
    #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1743
    pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
1744

1745
    /// Returns the result of an unchecked addition, resulting in
1746
    /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1747 1748
    ///
    /// This intrinsic does not have a stable counterpart.
1749
    #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1750
    pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1751

B
Brian Wignall 已提交
1752
    /// Returns the result of an unchecked subtraction, resulting in
1753
    /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1754 1755
    ///
    /// This intrinsic does not have a stable counterpart.
1756
    #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1757
    pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1758 1759

    /// Returns the result of an unchecked multiplication, resulting in
1760
    /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1761 1762
    ///
    /// This intrinsic does not have a stable counterpart.
1763
    #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1764
    pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1765

1766
    /// Performs rotate left.
1767
    ///
1768 1769 1770 1771 1772
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1773 1774
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `rotate_left` method. For example,
1775
    /// [`u32::rotate_left`]
1776
    #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1777
    pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
1778 1779

    /// Performs rotate right.
1780
    ///
1781 1782 1783 1784 1785
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1786 1787
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `rotate_right` method. For example,
1788
    /// [`u32::rotate_right`]
1789
    #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1790
    pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
1791

1792
    /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1793
    ///
1794 1795 1796 1797 1798
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1799
    /// The stabilized versions of this intrinsic are available on the integer
M
mbartlett21 已提交
1800 1801
    /// primitives via the `wrapping_add` method. For example,
    /// [`u32::wrapping_add`]
1802
    #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1803
    pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
1804
    /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1805
    ///
1806 1807 1808 1809 1810
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1811
    /// The stabilized versions of this intrinsic are available on the integer
M
mbartlett21 已提交
1812 1813
    /// primitives via the `wrapping_sub` method. For example,
    /// [`u32::wrapping_sub`]
1814
    #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1815
    pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
1816
    /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1817
    ///
1818 1819 1820 1821 1822
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1823
    /// The stabilized versions of this intrinsic are available on the integer
M
mbartlett21 已提交
1824 1825
    /// primitives via the `wrapping_mul` method. For example,
    /// [`u32::wrapping_mul`]
1826
    #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1827
    pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
1828

1829
    /// Computes `a + b`, saturating at numeric bounds.
1830
    ///
1831 1832 1833 1834 1835
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1836 1837
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `saturating_add` method. For example,
1838
    /// [`u32::saturating_add`]
1839
    #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1840
    pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
1841
    /// Computes `a - b`, saturating at numeric bounds.
1842
    ///
1843 1844 1845 1846 1847
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1848 1849
    /// The stabilized versions of this intrinsic are available on the integer
    /// primitives via the `saturating_sub` method. For example,
1850
    /// [`u32::saturating_sub`]
1851
    #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1852
    pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
1853

1854 1855
    /// Returns the value of the discriminant for the variant in 'v';
    /// if `T` has no discriminant, returns `0`.
1856
    ///
1857 1858 1859 1860 1861
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1862
    /// The stabilized version of this intrinsic is [`core::mem::discriminant`].
B
Bastian Kauschke 已提交
1863
    #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
1864
    pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
1865

N
Nathan Corbyn 已提交
1866
    /// Returns the number of variants of the type `T` cast to a `usize`;
1867
    /// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
N
Nathan Corbyn 已提交
1868
    ///
1869 1870 1871 1872 1873
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
    ///
1874
    /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
N
Nathan Corbyn 已提交
1875 1876 1877
    #[rustc_const_unstable(feature = "variant_count", issue = "73662")]
    pub fn variant_count<T>() -> usize;

1878 1879
    /// Rust's "try catch" construct which invokes the function pointer `try_fn`
    /// with the data pointer `data`.
1880
    ///
1881 1882 1883
    /// The third argument is a function called if a panic occurs. This function
    /// takes the data pointer and a pointer to the target-specific exception
    /// object that was caught. For more information see the compiler's
1884
    /// source as well as std's catch implementation.
1885
    pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
O
Oliver Schneider 已提交
1886

1887 1888 1889
    /// Emits a `!nontemporal` store according to LLVM (see their docs).
    /// Probably will never become stable.
    pub fn nontemporal_store<T>(ptr: *mut T, val: T);
1890 1891

    /// See documentation of `<*const T>::offset_from` for details.
1892
    #[rustc_const_unstable(feature = "const_ptr_offset_from", issue = "41079")]
1893
    pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
1894

1895
    /// See documentation of `<*const T>::guaranteed_eq` for details.
1896 1897 1898 1899 1900
    ///
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
1901 1902 1903 1904
    #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
    pub fn ptr_guaranteed_eq<T>(ptr: *const T, other: *const T) -> bool;

    /// See documentation of `<*const T>::guaranteed_ne` for details.
1905 1906 1907 1908 1909
    ///
    /// Note that, unlike most intrinsics, this is safe to call;
    /// it does not require an `unsafe` block.
    /// Therefore, implementations must not require the user to uphold
    /// any safety invariants.
1910 1911
    #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
    pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;
V
Vishnunarayan K I 已提交
1912 1913

    /// Allocate at compile time. Should not be called at runtime.
V
Vishnunarayan K I 已提交
1914
    #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
V
Vishnunarayan K I 已提交
1915
    pub fn const_allocate(size: usize, align: usize) -> *mut u8;
1916 1917
}

R
Ralf Jung 已提交
1918 1919 1920 1921
// Some functions are defined here because they accidentally got made
// available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
// (`transmute` also falls into this category, but it cannot be wrapped due to the
// check that `T` and `U` have the same size.)
1922

1923 1924 1925 1926 1927 1928
/// Checks whether `ptr` is properly aligned with respect to
/// `align_of::<T>()`.
pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
    !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
}

1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
/// and destination must *not* overlap.
///
/// For regions of memory which might overlap, use [`copy`] instead.
///
/// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
/// with the argument order swapped.
///
/// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
///
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
///
/// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
///
/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
///
/// * Both `src` and `dst` must be properly aligned.
///
/// * The region of memory beginning at `src` with a size of `count *
///   size_of::<T>()` bytes must *not* overlap with the region of memory
///   beginning at `dst` with the same size.
///
/// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
/// in the region beginning at `*src` and the region beginning at `*dst` can
/// [violate memory safety][read-ownership].
///
/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
/// `0`, the pointers must be non-null and properly aligned.
///
/// [`read`]: crate::ptr::read
/// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
/// [valid]: crate::ptr#safety
///
/// # Examples
///
/// Manually implement [`Vec::append`]:
///
/// ```
/// use std::ptr;
///
/// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
/// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
///     let src_len = src.len();
///     let dst_len = dst.len();
///
///     // Ensure that `dst` has enough capacity to hold all of `src`.
///     dst.reserve(src_len);
///
///     unsafe {
///         // The call to offset is always safe because `Vec` will never
///         // allocate more than `isize::MAX` bytes.
///         let dst_ptr = dst.as_mut_ptr().offset(dst_len as isize);
///         let src_ptr = src.as_ptr();
///
///         // Truncate `src` without dropping its contents. We do this first,
///         // to avoid problems in case something further down panics.
///         src.set_len(0);
///
///         // The two regions cannot overlap because mutable references do
///         // not alias, and two different vectors cannot own the same
///         // memory.
///         ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
///
///         // Notify `dst` that it now holds the contents of `src`.
///         dst.set_len(dst_len + src_len);
///     }
/// }
///
/// let mut a = vec!['r'];
/// let mut b = vec!['u', 's', 't'];
///
/// append(&mut a, &mut b);
///
/// assert_eq!(a, &['r', 'u', 's', 't']);
/// assert!(b.is_empty());
/// ```
///
/// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
#[doc(alias = "memcpy")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
#[inline]
pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
    extern "rust-intrinsic" {
        #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
        pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
    }

    // FIXME: Perform these checks only at run time
    /*if cfg!(debug_assertions)
        && !(is_aligned_and_not_null(src)
            && is_aligned_and_not_null(dst)
            && is_nonoverlapping(src, dst, count))
    {
        // Not panicking to keep codegen impact smaller.
        abort();
    }*/

    // SAFETY: the safety contract for `copy_nonoverlapping` must be
    // upheld by the caller.
    unsafe { copy_nonoverlapping(src, dst, count) }
}

/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
/// and destination may overlap.
///
/// If the source and destination will *never* overlap,
/// [`copy_nonoverlapping`] can be used instead.
///
/// `copy` is semantically equivalent to C's [`memmove`], but with the argument
/// order swapped. Copying takes place as if the bytes were copied from `src`
/// to a temporary array and then copied from the array to `dst`.
///
/// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
///
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
///
/// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
///
/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
///
/// * Both `src` and `dst` must be properly aligned.
///
/// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
/// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
/// in the region beginning at `*src` and the region beginning at `*dst` can
/// [violate memory safety][read-ownership].
///
/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
/// `0`, the pointers must be non-null and properly aligned.
///
/// [`read`]: crate::ptr::read
/// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
/// [valid]: crate::ptr#safety
///
/// # Examples
///
/// Efficiently create a Rust vector from an unsafe buffer:
///
/// ```
/// use std::ptr;
///
/// /// # Safety
/// ///
/// /// * `ptr` must be correctly aligned for its type and non-zero.
/// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
/// /// * Those elements must not be used after calling this function unless `T: Copy`.
/// # #[allow(dead_code)]
/// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
///     let mut dst = Vec::with_capacity(elts);
///
///     // SAFETY: Our precondition ensures the source is aligned and valid,
///     // and `Vec::with_capacity` ensures that we have usable space to write them.
///     ptr::copy(ptr, dst.as_mut_ptr(), elts);
///
///     // SAFETY: We created it with this much capacity earlier,
///     // and the previous `copy` has initialized these elements.
///     dst.set_len(elts);
///     dst
/// }
/// ```
#[doc(alias = "memmove")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
#[inline]
pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
    extern "rust-intrinsic" {
        #[rustc_const_unstable(feature = "const_intrinsic_copy", issue = "80697")]
        fn copy<T>(src: *const T, dst: *mut T, count: usize);
    }

    // FIXME: Perform these checks only at run time
    /*if cfg!(debug_assertions) && !(is_aligned_and_not_null(src) && is_aligned_and_not_null(dst)) {
        // Not panicking to keep codegen impact smaller.
        abort();
    }*/

    // SAFETY: the safety contract for `copy` must be upheld by the caller.
    unsafe { copy(src, dst, count) }
}

2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
/// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
/// `val`.
///
/// `write_bytes` is similar to C's [`memset`], but sets `count *
/// size_of::<T>()` bytes to `val`.
///
/// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
///
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
///
/// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
///
/// * `dst` must be properly aligned.
///
/// Additionally, the caller must ensure that writing `count *
/// size_of::<T>()` bytes to the given region of memory results in a valid
/// value of `T`. Using a region of memory typed as a `T` that contains an
/// invalid value of `T` is undefined behavior.
///
/// Note that even if the effectively copied size (`count * size_of::<T>()`) is
B
Brent Kerby 已提交
2137
/// `0`, the pointer must be non-null and properly aligned.
2138
///
D
Denis Vasilik 已提交
2139
/// [valid]: crate::ptr#safety
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use std::ptr;
///
/// let mut vec = vec![0u32; 4];
/// unsafe {
///     let vec_ptr = vec.as_mut_ptr();
///     ptr::write_bytes(vec_ptr, 0xfe, 2);
/// }
/// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
/// ```
///
/// Creating an invalid value:
///
/// ```
/// use std::ptr;
///
/// let mut v = Box::new(0i32);
///
/// unsafe {
///     // Leaks the previously held value by overwriting the `Box<T>` with
///     // a null pointer.
///     ptr::write_bytes(&mut v as *mut Box<i32>, 0, 1);
/// }
///
/// // At this point, using or dropping `v` results in undefined behavior.
/// // drop(v); // ERROR
///
/// // Even leaking `v` "uses" it, and hence is undefined behavior.
/// // mem::forget(v); // ERROR
///
/// // In fact, `v` is invalid according to basic type layout invariants, so *any*
/// // operation touching it is undefined behavior.
/// // let v2 = v; // ERROR
///
/// unsafe {
///     // Let us instead put in a valid value
///     ptr::write(&mut v as *mut Box<i32>, Box::new(42i32));
/// }
///
/// // Now the box is fine
/// assert_eq!(*v, 42);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
R
Ralf Jung 已提交
2190 2191 2192
    extern "rust-intrinsic" {
        fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
    }
2193 2194

    debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
2195 2196 2197

    // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
    unsafe { write_bytes(dst, val, count) }
2198
}