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