1 //! Compiler intrinsics.
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>.
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.
11 //! In order to make an intrinsic usable at compile-time, one needs to copy the implementation
12 //! from <https://github.com/rust-lang/miri/blob/master/src/shims/intrinsics.rs> to
13 //! <https://github.com/rust-lang/rust/blob/master/compiler/rustc_const_eval/src/interpret/intrinsics.rs> and add a
14 //! `#[rustc_const_unstable(feature = "const_such_and_such", issue = "01234")]` to the intrinsic declaration.
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.
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
28 //! [volatile]: https://llvm.org/docs/LangRef.html#volatile-memory-accesses
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]].
36 //! [atomics]: https://llvm.org/docs/Atomics.html
38 //! A quick refresher on memory ordering:
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`.
49 feature = "core_intrinsics",
50 reason = "intrinsics are unlikely to ever be stabilized, instead \
51 they should be used through stabilized interfaces \
52 in the rest of the standard library",
55 #![allow(missing_docs)]
57 use crate::marker::DiscriminantKind;
58 #[cfg(not(bootstrap))]
59 use crate::marker::Tuple;
62 #[cfg(not(bootstrap))]
65 // These imports are used for simplifying intra-doc links
66 #[allow(unused_imports)]
67 #[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))]
68 use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering};
70 #[stable(feature = "drop_in_place", since = "1.8.0")]
71 #[rustc_allowed_through_unstable_modules]
72 #[deprecated(note = "no longer an intrinsic - use `ptr::drop_in_place` directly", since = "1.52.0")]
74 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
75 // SAFETY: see `ptr::drop_in_place`
76 unsafe { crate::ptr::drop_in_place(to_drop) }
79 extern "rust-intrinsic" {
80 // N.B., these intrinsics take raw pointers because they mutate aliased
81 // memory, which is not valid for either `&` or `&mut`.
83 /// Stores a value if the current value is the same as the `old` value.
85 /// The stabilized version of this intrinsic is available on the
86 /// [`atomic`] types via the `compare_exchange` method by passing
87 /// [`Ordering::Relaxed`] as both the success and failure parameters.
88 /// For example, [`AtomicBool::compare_exchange`].
89 pub fn atomic_cxchg_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
90 /// Stores a value if the current value is the same as the `old` value.
92 /// The stabilized version of this intrinsic is available on the
93 /// [`atomic`] types via the `compare_exchange` method by passing
94 /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
95 /// For example, [`AtomicBool::compare_exchange`].
96 pub fn atomic_cxchg_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
97 /// Stores a value if the current value is the same as the `old` value.
99 /// The stabilized version of this intrinsic is available on the
100 /// [`atomic`] types via the `compare_exchange` method by passing
101 /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
102 /// For example, [`AtomicBool::compare_exchange`].
103 pub fn atomic_cxchg_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
104 /// Stores a value if the current value is the same as the `old` value.
106 /// The stabilized version of this intrinsic is available on the
107 /// [`atomic`] types via the `compare_exchange` method by passing
108 /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
109 /// For example, [`AtomicBool::compare_exchange`].
110 pub fn atomic_cxchg_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
111 /// Stores a value if the current value is the same as the `old` value.
113 /// The stabilized version of this intrinsic is available on the
114 /// [`atomic`] types via the `compare_exchange` method by passing
115 /// [`Ordering::Acquire`] as both the success and failure parameters.
116 /// For example, [`AtomicBool::compare_exchange`].
117 pub fn atomic_cxchg_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
118 /// Stores a value if the current value is the same as the `old` value.
120 /// The stabilized version of this intrinsic is available on the
121 /// [`atomic`] types via the `compare_exchange` method by passing
122 /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
123 /// For example, [`AtomicBool::compare_exchange`].
124 pub fn atomic_cxchg_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
125 /// Stores a value if the current value is the same as the `old` value.
127 /// The stabilized version of this intrinsic is available on the
128 /// [`atomic`] types via the `compare_exchange` method by passing
129 /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
130 /// For example, [`AtomicBool::compare_exchange`].
131 pub fn atomic_cxchg_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
132 /// Stores a value if the current value is the same as the `old` value.
134 /// The stabilized version of this intrinsic is available on the
135 /// [`atomic`] types via the `compare_exchange` method by passing
136 /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
137 /// For example, [`AtomicBool::compare_exchange`].
138 pub fn atomic_cxchg_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
139 /// Stores a value if the current value is the same as the `old` value.
141 /// The stabilized version of this intrinsic is available on the
142 /// [`atomic`] types via the `compare_exchange` method by passing
143 /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
144 /// For example, [`AtomicBool::compare_exchange`].
145 pub fn atomic_cxchg_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
146 /// Stores a value if the current value is the same as the `old` value.
148 /// The stabilized version of this intrinsic is available on the
149 /// [`atomic`] types via the `compare_exchange` method by passing
150 /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
151 /// For example, [`AtomicBool::compare_exchange`].
152 pub fn atomic_cxchg_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
153 /// Stores a value if the current value is the same as the `old` value.
155 /// The stabilized version of this intrinsic is available on the
156 /// [`atomic`] types via the `compare_exchange` method by passing
157 /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
158 /// For example, [`AtomicBool::compare_exchange`].
159 pub fn atomic_cxchg_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
160 /// Stores a value if the current value is the same as the `old` value.
162 /// The stabilized version of this intrinsic is available on the
163 /// [`atomic`] types via the `compare_exchange` method by passing
164 /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
165 /// For example, [`AtomicBool::compare_exchange`].
166 pub fn atomic_cxchg_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
167 /// Stores a value if the current value is the same as the `old` value.
169 /// The stabilized version of this intrinsic is available on the
170 /// [`atomic`] types via the `compare_exchange` method by passing
171 /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
172 /// For example, [`AtomicBool::compare_exchange`].
173 pub fn atomic_cxchg_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
174 /// Stores a value if the current value is the same as the `old` value.
176 /// The stabilized version of this intrinsic is available on the
177 /// [`atomic`] types via the `compare_exchange` method by passing
178 /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
179 /// For example, [`AtomicBool::compare_exchange`].
180 pub fn atomic_cxchg_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
181 /// Stores a value if the current value is the same as the `old` value.
183 /// The stabilized version of this intrinsic is available on the
184 /// [`atomic`] types via the `compare_exchange` method by passing
185 /// [`Ordering::SeqCst`] as both the success and failure parameters.
186 /// For example, [`AtomicBool::compare_exchange`].
187 pub fn atomic_cxchg_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
189 /// Stores a value if the current value is the same as the `old` value.
191 /// The stabilized version of this intrinsic is available on the
192 /// [`atomic`] types via the `compare_exchange_weak` method by passing
193 /// [`Ordering::Relaxed`] as both the success and failure parameters.
194 /// For example, [`AtomicBool::compare_exchange_weak`].
195 pub fn atomic_cxchgweak_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
196 /// Stores a value if the current value is the same as the `old` value.
198 /// The stabilized version of this intrinsic is available on the
199 /// [`atomic`] types via the `compare_exchange_weak` method by passing
200 /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters.
201 /// For example, [`AtomicBool::compare_exchange_weak`].
202 pub fn atomic_cxchgweak_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
203 /// Stores a value if the current value is the same as the `old` value.
205 /// The stabilized version of this intrinsic is available on the
206 /// [`atomic`] types via the `compare_exchange_weak` method by passing
207 /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters.
208 /// For example, [`AtomicBool::compare_exchange_weak`].
209 pub fn atomic_cxchgweak_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
210 /// Stores a value if the current value is the same as the `old` value.
212 /// The stabilized version of this intrinsic is available on the
213 /// [`atomic`] types via the `compare_exchange_weak` method by passing
214 /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters.
215 /// For example, [`AtomicBool::compare_exchange_weak`].
216 pub fn atomic_cxchgweak_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
217 /// Stores a value if the current value is the same as the `old` value.
219 /// The stabilized version of this intrinsic is available on the
220 /// [`atomic`] types via the `compare_exchange_weak` method by passing
221 /// [`Ordering::Acquire`] as both the success and failure parameters.
222 /// For example, [`AtomicBool::compare_exchange_weak`].
223 pub fn atomic_cxchgweak_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
224 /// Stores a value if the current value is the same as the `old` value.
226 /// The stabilized version of this intrinsic is available on the
227 /// [`atomic`] types via the `compare_exchange_weak` method by passing
228 /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters.
229 /// For example, [`AtomicBool::compare_exchange_weak`].
230 pub fn atomic_cxchgweak_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
231 /// Stores a value if the current value is the same as the `old` value.
233 /// The stabilized version of this intrinsic is available on the
234 /// [`atomic`] types via the `compare_exchange_weak` method by passing
235 /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters.
236 /// For example, [`AtomicBool::compare_exchange_weak`].
237 pub fn atomic_cxchgweak_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
238 /// Stores a value if the current value is the same as the `old` value.
240 /// The stabilized version of this intrinsic is available on the
241 /// [`atomic`] types via the `compare_exchange_weak` method by passing
242 /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters.
243 /// For example, [`AtomicBool::compare_exchange_weak`].
244 pub fn atomic_cxchgweak_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
245 /// Stores a value if the current value is the same as the `old` value.
247 /// The stabilized version of this intrinsic is available on the
248 /// [`atomic`] types via the `compare_exchange_weak` method by passing
249 /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters.
250 /// For example, [`AtomicBool::compare_exchange_weak`].
251 pub fn atomic_cxchgweak_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
252 /// Stores a value if the current value is the same as the `old` value.
254 /// The stabilized version of this intrinsic is available on the
255 /// [`atomic`] types via the `compare_exchange_weak` method by passing
256 /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters.
257 /// For example, [`AtomicBool::compare_exchange_weak`].
258 pub fn atomic_cxchgweak_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
259 /// Stores a value if the current value is the same as the `old` value.
261 /// The stabilized version of this intrinsic is available on the
262 /// [`atomic`] types via the `compare_exchange_weak` method by passing
263 /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters.
264 /// For example, [`AtomicBool::compare_exchange_weak`].
265 pub fn atomic_cxchgweak_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
266 /// Stores a value if the current value is the same as the `old` value.
268 /// The stabilized version of this intrinsic is available on the
269 /// [`atomic`] types via the `compare_exchange_weak` method by passing
270 /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters.
271 /// For example, [`AtomicBool::compare_exchange_weak`].
272 pub fn atomic_cxchgweak_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
273 /// Stores a value if the current value is the same as the `old` value.
275 /// The stabilized version of this intrinsic is available on the
276 /// [`atomic`] types via the `compare_exchange_weak` method by passing
277 /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters.
278 /// For example, [`AtomicBool::compare_exchange_weak`].
279 pub fn atomic_cxchgweak_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
280 /// Stores a value if the current value is the same as the `old` value.
282 /// The stabilized version of this intrinsic is available on the
283 /// [`atomic`] types via the `compare_exchange_weak` method by passing
284 /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters.
285 /// For example, [`AtomicBool::compare_exchange_weak`].
286 pub fn atomic_cxchgweak_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
287 /// Stores a value if the current value is the same as the `old` value.
289 /// The stabilized version of this intrinsic is available on the
290 /// [`atomic`] types via the `compare_exchange_weak` method by passing
291 /// [`Ordering::SeqCst`] as both the success and failure parameters.
292 /// For example, [`AtomicBool::compare_exchange_weak`].
293 pub fn atomic_cxchgweak_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool);
295 /// Loads the current value of the pointer.
297 /// The stabilized version of this intrinsic is available on the
298 /// [`atomic`] types via the `load` method by passing
299 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`].
300 pub fn atomic_load_seqcst<T: Copy>(src: *const T) -> T;
301 /// Loads the current value of the pointer.
303 /// The stabilized version of this intrinsic is available on the
304 /// [`atomic`] types via the `load` method by passing
305 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`].
306 pub fn atomic_load_acquire<T: Copy>(src: *const T) -> T;
307 /// Loads the current value of the pointer.
309 /// The stabilized version of this intrinsic is available on the
310 /// [`atomic`] types via the `load` method by passing
311 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`].
312 pub fn atomic_load_relaxed<T: Copy>(src: *const T) -> T;
313 pub fn atomic_load_unordered<T: Copy>(src: *const T) -> T;
315 /// Stores the value at the specified memory location.
317 /// The stabilized version of this intrinsic is available on the
318 /// [`atomic`] types via the `store` method by passing
319 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`].
320 pub fn atomic_store_seqcst<T: Copy>(dst: *mut T, val: T);
321 /// Stores the value at the specified memory location.
323 /// The stabilized version of this intrinsic is available on the
324 /// [`atomic`] types via the `store` method by passing
325 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`].
326 pub fn atomic_store_release<T: Copy>(dst: *mut T, val: T);
327 /// Stores the value at the specified memory location.
329 /// The stabilized version of this intrinsic is available on the
330 /// [`atomic`] types via the `store` method by passing
331 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`].
332 pub fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T);
333 pub fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T);
335 /// Stores the value at the specified memory location, returning the old value.
337 /// The stabilized version of this intrinsic is available on the
338 /// [`atomic`] types via the `swap` method by passing
339 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`].
340 pub fn atomic_xchg_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
341 /// Stores the value at the specified memory location, returning the old value.
343 /// The stabilized version of this intrinsic is available on the
344 /// [`atomic`] types via the `swap` method by passing
345 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`].
346 pub fn atomic_xchg_acquire<T: Copy>(dst: *mut T, src: T) -> T;
347 /// Stores the value at the specified memory location, returning the old value.
349 /// The stabilized version of this intrinsic is available on the
350 /// [`atomic`] types via the `swap` method by passing
351 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`].
352 pub fn atomic_xchg_release<T: Copy>(dst: *mut T, src: T) -> T;
353 /// Stores the value at the specified memory location, returning the old value.
355 /// The stabilized version of this intrinsic is available on the
356 /// [`atomic`] types via the `swap` method by passing
357 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`].
358 pub fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
359 /// Stores the value at the specified memory location, returning the old value.
361 /// The stabilized version of this intrinsic is available on the
362 /// [`atomic`] types via the `swap` method by passing
363 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`].
364 pub fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
366 /// Adds to the current value, returning the previous value.
368 /// The stabilized version of this intrinsic is available on the
369 /// [`atomic`] types via the `fetch_add` method by passing
370 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`].
371 pub fn atomic_xadd_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
372 /// Adds to the current value, returning the previous value.
374 /// The stabilized version of this intrinsic is available on the
375 /// [`atomic`] types via the `fetch_add` method by passing
376 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`].
377 pub fn atomic_xadd_acquire<T: Copy>(dst: *mut T, src: T) -> T;
378 /// Adds to the current value, returning the previous value.
380 /// The stabilized version of this intrinsic is available on the
381 /// [`atomic`] types via the `fetch_add` method by passing
382 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`].
383 pub fn atomic_xadd_release<T: Copy>(dst: *mut T, src: T) -> T;
384 /// Adds to the current value, returning the previous value.
386 /// The stabilized version of this intrinsic is available on the
387 /// [`atomic`] types via the `fetch_add` method by passing
388 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`].
389 pub fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
390 /// Adds to the current value, returning the previous value.
392 /// The stabilized version of this intrinsic is available on the
393 /// [`atomic`] types via the `fetch_add` method by passing
394 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`].
395 pub fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
397 /// Subtract from the current value, returning the previous value.
399 /// The stabilized version of this intrinsic is available on the
400 /// [`atomic`] types via the `fetch_sub` method by passing
401 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
402 pub fn atomic_xsub_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
403 /// Subtract from the current value, returning the previous value.
405 /// The stabilized version of this intrinsic is available on the
406 /// [`atomic`] types via the `fetch_sub` method by passing
407 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
408 pub fn atomic_xsub_acquire<T: Copy>(dst: *mut T, src: T) -> T;
409 /// Subtract from the current value, returning the previous value.
411 /// The stabilized version of this intrinsic is available on the
412 /// [`atomic`] types via the `fetch_sub` method by passing
413 /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
414 pub fn atomic_xsub_release<T: Copy>(dst: *mut T, src: T) -> T;
415 /// Subtract from the current value, returning the previous value.
417 /// The stabilized version of this intrinsic is available on the
418 /// [`atomic`] types via the `fetch_sub` method by passing
419 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
420 pub fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
421 /// Subtract from the current value, returning the previous value.
423 /// The stabilized version of this intrinsic is available on the
424 /// [`atomic`] types via the `fetch_sub` method by passing
425 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`].
426 pub fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
428 /// Bitwise and with the current value, returning the previous value.
430 /// The stabilized version of this intrinsic is available on the
431 /// [`atomic`] types via the `fetch_and` method by passing
432 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`].
433 pub fn atomic_and_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
434 /// Bitwise and with the current value, returning the previous value.
436 /// The stabilized version of this intrinsic is available on the
437 /// [`atomic`] types via the `fetch_and` method by passing
438 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`].
439 pub fn atomic_and_acquire<T: Copy>(dst: *mut T, src: T) -> T;
440 /// Bitwise and with the current value, returning the previous value.
442 /// The stabilized version of this intrinsic is available on the
443 /// [`atomic`] types via the `fetch_and` method by passing
444 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`].
445 pub fn atomic_and_release<T: Copy>(dst: *mut T, src: T) -> T;
446 /// Bitwise and with the current value, returning the previous value.
448 /// The stabilized version of this intrinsic is available on the
449 /// [`atomic`] types via the `fetch_and` method by passing
450 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`].
451 pub fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
452 /// Bitwise and with the current value, returning the previous value.
454 /// The stabilized version of this intrinsic is available on the
455 /// [`atomic`] types via the `fetch_and` method by passing
456 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`].
457 pub fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
459 /// Bitwise nand with the current value, returning the previous value.
461 /// The stabilized version of this intrinsic is available on the
462 /// [`AtomicBool`] type via the `fetch_nand` method by passing
463 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`].
464 pub fn atomic_nand_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
465 /// Bitwise nand with the current value, returning the previous value.
467 /// The stabilized version of this intrinsic is available on the
468 /// [`AtomicBool`] type via the `fetch_nand` method by passing
469 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`].
470 pub fn atomic_nand_acquire<T: Copy>(dst: *mut T, src: T) -> T;
471 /// Bitwise nand with the current value, returning the previous value.
473 /// The stabilized version of this intrinsic is available on the
474 /// [`AtomicBool`] type via the `fetch_nand` method by passing
475 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`].
476 pub fn atomic_nand_release<T: Copy>(dst: *mut T, src: T) -> T;
477 /// Bitwise nand with the current value, returning the previous value.
479 /// The stabilized version of this intrinsic is available on the
480 /// [`AtomicBool`] type via the `fetch_nand` method by passing
481 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`].
482 pub fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
483 /// Bitwise nand with the current value, returning the previous value.
485 /// The stabilized version of this intrinsic is available on the
486 /// [`AtomicBool`] type via the `fetch_nand` method by passing
487 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`].
488 pub fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
490 /// Bitwise or with the current value, returning the previous value.
492 /// The stabilized version of this intrinsic is available on the
493 /// [`atomic`] types via the `fetch_or` method by passing
494 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`].
495 pub fn atomic_or_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
496 /// Bitwise or with the current value, returning the previous value.
498 /// The stabilized version of this intrinsic is available on the
499 /// [`atomic`] types via the `fetch_or` method by passing
500 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`].
501 pub fn atomic_or_acquire<T: Copy>(dst: *mut T, src: T) -> T;
502 /// Bitwise or with the current value, returning the previous value.
504 /// The stabilized version of this intrinsic is available on the
505 /// [`atomic`] types via the `fetch_or` method by passing
506 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`].
507 pub fn atomic_or_release<T: Copy>(dst: *mut T, src: T) -> T;
508 /// Bitwise or with the current value, returning the previous value.
510 /// The stabilized version of this intrinsic is available on the
511 /// [`atomic`] types via the `fetch_or` method by passing
512 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`].
513 pub fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
514 /// Bitwise or with the current value, returning the previous value.
516 /// The stabilized version of this intrinsic is available on the
517 /// [`atomic`] types via the `fetch_or` method by passing
518 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`].
519 pub fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
521 /// Bitwise xor with the current value, returning the previous value.
523 /// The stabilized version of this intrinsic is available on the
524 /// [`atomic`] types via the `fetch_xor` method by passing
525 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`].
526 pub fn atomic_xor_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
527 /// Bitwise xor with the current value, returning the previous value.
529 /// The stabilized version of this intrinsic is available on the
530 /// [`atomic`] types via the `fetch_xor` method by passing
531 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`].
532 pub fn atomic_xor_acquire<T: Copy>(dst: *mut T, src: T) -> T;
533 /// Bitwise xor with the current value, returning the previous value.
535 /// The stabilized version of this intrinsic is available on the
536 /// [`atomic`] types via the `fetch_xor` method by passing
537 /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`].
538 pub fn atomic_xor_release<T: Copy>(dst: *mut T, src: T) -> T;
539 /// Bitwise xor with the current value, returning the previous value.
541 /// The stabilized version of this intrinsic is available on the
542 /// [`atomic`] types via the `fetch_xor` method by passing
543 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`].
544 pub fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
545 /// Bitwise xor with the current value, returning the previous value.
547 /// The stabilized version of this intrinsic is available on the
548 /// [`atomic`] types via the `fetch_xor` method by passing
549 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`].
550 pub fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
552 /// Maximum with the current value using a signed comparison.
554 /// The stabilized version of this intrinsic is available on the
555 /// [`atomic`] signed integer types via the `fetch_max` method by passing
556 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`].
557 pub fn atomic_max_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
558 /// Maximum with the current value using a signed comparison.
560 /// The stabilized version of this intrinsic is available on the
561 /// [`atomic`] signed integer types via the `fetch_max` method by passing
562 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`].
563 pub fn atomic_max_acquire<T: Copy>(dst: *mut T, src: T) -> T;
564 /// Maximum with the current value using a signed comparison.
566 /// The stabilized version of this intrinsic is available on the
567 /// [`atomic`] signed integer types via the `fetch_max` method by passing
568 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`].
569 pub fn atomic_max_release<T: Copy>(dst: *mut T, src: T) -> T;
570 /// Maximum with the current value using a signed comparison.
572 /// The stabilized version of this intrinsic is available on the
573 /// [`atomic`] signed integer types via the `fetch_max` method by passing
574 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`].
575 pub fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
576 /// Maximum with the current value.
578 /// The stabilized version of this intrinsic is available on the
579 /// [`atomic`] signed integer types via the `fetch_max` method by passing
580 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`].
581 pub fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
583 /// Minimum with the current value using a signed comparison.
585 /// The stabilized version of this intrinsic is available on the
586 /// [`atomic`] signed integer types via the `fetch_min` method by passing
587 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`].
588 pub fn atomic_min_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
589 /// Minimum with the current value using a signed comparison.
591 /// The stabilized version of this intrinsic is available on the
592 /// [`atomic`] signed integer types via the `fetch_min` method by passing
593 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`].
594 pub fn atomic_min_acquire<T: Copy>(dst: *mut T, src: T) -> T;
595 /// Minimum with the current value using a signed comparison.
597 /// The stabilized version of this intrinsic is available on the
598 /// [`atomic`] signed integer types via the `fetch_min` method by passing
599 /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`].
600 pub fn atomic_min_release<T: Copy>(dst: *mut T, src: T) -> T;
601 /// Minimum with the current value using a signed comparison.
603 /// The stabilized version of this intrinsic is available on the
604 /// [`atomic`] signed integer types via the `fetch_min` method by passing
605 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`].
606 pub fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
607 /// Minimum with the current value using a signed comparison.
609 /// The stabilized version of this intrinsic is available on the
610 /// [`atomic`] signed integer types via the `fetch_min` method by passing
611 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`].
612 pub fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
614 /// Minimum with the current value using an unsigned comparison.
616 /// The stabilized version of this intrinsic is available on the
617 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
618 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`].
619 pub fn atomic_umin_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
620 /// Minimum with the current value using an unsigned comparison.
622 /// The stabilized version of this intrinsic is available on the
623 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
624 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`].
625 pub fn atomic_umin_acquire<T: Copy>(dst: *mut T, src: T) -> T;
626 /// Minimum with the current value using an unsigned comparison.
628 /// The stabilized version of this intrinsic is available on the
629 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
630 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`].
631 pub fn atomic_umin_release<T: Copy>(dst: *mut T, src: T) -> T;
632 /// Minimum with the current value using an unsigned comparison.
634 /// The stabilized version of this intrinsic is available on the
635 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
636 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`].
637 pub fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
638 /// Minimum with the current value using an unsigned comparison.
640 /// The stabilized version of this intrinsic is available on the
641 /// [`atomic`] unsigned integer types via the `fetch_min` method by passing
642 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`].
643 pub fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
645 /// Maximum with the current value using an unsigned comparison.
647 /// The stabilized version of this intrinsic is available on the
648 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
649 /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`].
650 pub fn atomic_umax_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
651 /// Maximum with the current value using an unsigned comparison.
653 /// The stabilized version of this intrinsic is available on the
654 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
655 /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`].
656 pub fn atomic_umax_acquire<T: Copy>(dst: *mut T, src: T) -> T;
657 /// Maximum with the current value using an unsigned comparison.
659 /// The stabilized version of this intrinsic is available on the
660 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
661 /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`].
662 pub fn atomic_umax_release<T: Copy>(dst: *mut T, src: T) -> T;
663 /// Maximum with the current value using an unsigned comparison.
665 /// The stabilized version of this intrinsic is available on the
666 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
667 /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`].
668 pub fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T;
669 /// Maximum with the current value using an unsigned comparison.
671 /// The stabilized version of this intrinsic is available on the
672 /// [`atomic`] unsigned integer types via the `fetch_max` method by passing
673 /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`].
674 pub fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T;
678 /// The stabilized version of this intrinsic is available in
679 /// [`atomic::fence`] by passing [`Ordering::SeqCst`]
681 pub fn atomic_fence_seqcst();
684 /// The stabilized version of this intrinsic is available in
685 /// [`atomic::fence`] by passing [`Ordering::Acquire`]
687 pub fn atomic_fence_acquire();
690 /// The stabilized version of this intrinsic is available in
691 /// [`atomic::fence`] by passing [`Ordering::Release`]
693 pub fn atomic_fence_release();
696 /// The stabilized version of this intrinsic is available in
697 /// [`atomic::fence`] by passing [`Ordering::AcqRel`]
699 pub fn atomic_fence_acqrel();
701 /// A compiler-only memory barrier.
703 /// Memory accesses will never be reordered across this barrier by the
704 /// compiler, but no instructions will be emitted for it. This is
705 /// appropriate for operations on the same thread that may be preempted,
706 /// such as when interacting with signal handlers.
708 /// The stabilized version of this intrinsic is available in
709 /// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`]
711 pub fn atomic_singlethreadfence_seqcst();
712 /// A compiler-only memory barrier.
714 /// Memory accesses will never be reordered across this barrier by the
715 /// compiler, but no instructions will be emitted for it. This is
716 /// appropriate for operations on the same thread that may be preempted,
717 /// such as when interacting with signal handlers.
719 /// The stabilized version of this intrinsic is available in
720 /// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`]
722 pub fn atomic_singlethreadfence_acquire();
723 /// A compiler-only memory barrier.
725 /// Memory accesses will never be reordered across this barrier by the
726 /// compiler, but no instructions will be emitted for it. This is
727 /// appropriate for operations on the same thread that may be preempted,
728 /// such as when interacting with signal handlers.
730 /// The stabilized version of this intrinsic is available in
731 /// [`atomic::compiler_fence`] by passing [`Ordering::Release`]
733 pub fn atomic_singlethreadfence_release();
734 /// A compiler-only memory barrier.
736 /// Memory accesses will never be reordered across this barrier by the
737 /// compiler, but no instructions will be emitted for it. This is
738 /// appropriate for operations on the same thread that may be preempted,
739 /// such as when interacting with signal handlers.
741 /// The stabilized version of this intrinsic is available in
742 /// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`]
744 pub fn atomic_singlethreadfence_acqrel();
746 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
747 /// if supported; otherwise, it is a no-op.
748 /// Prefetches have no effect on the behavior of the program but can change its performance
751 /// The `locality` argument must be a constant integer and is a temporal locality specifier
752 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
754 /// This intrinsic does not have a stable counterpart.
755 pub fn prefetch_read_data<T>(data: *const T, locality: i32);
756 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
757 /// if supported; otherwise, it is a no-op.
758 /// Prefetches have no effect on the behavior of the program but can change its performance
761 /// The `locality` argument must be a constant integer and is a temporal locality specifier
762 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
764 /// This intrinsic does not have a stable counterpart.
765 pub fn prefetch_write_data<T>(data: *const T, locality: i32);
766 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
767 /// if supported; otherwise, it is a no-op.
768 /// Prefetches have no effect on the behavior of the program but can change its performance
771 /// The `locality` argument must be a constant integer and is a temporal locality specifier
772 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
774 /// This intrinsic does not have a stable counterpart.
775 pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
776 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
777 /// if supported; otherwise, it is a no-op.
778 /// Prefetches have no effect on the behavior of the program but can change its performance
781 /// The `locality` argument must be a constant integer and is a temporal locality specifier
782 /// ranging from (0) - no locality, to (3) - extremely local keep in cache.
784 /// This intrinsic does not have a stable counterpart.
785 pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
787 /// Magic intrinsic that derives its meaning from attributes
788 /// attached to the function.
790 /// For example, dataflow uses this to inject static assertions so
791 /// that `rustc_peek(potentially_uninitialized)` would actually
792 /// double-check that dataflow did indeed compute that it is
793 /// uninitialized at that point in the control flow.
795 /// This intrinsic should not be used outside of the compiler.
796 #[rustc_safe_intrinsic]
797 pub fn rustc_peek<T>(_: T) -> T;
799 /// Aborts the execution of the process.
801 /// Note that, unlike most intrinsics, this is safe to call;
802 /// it does not require an `unsafe` block.
803 /// Therefore, implementations must not require the user to uphold
804 /// any safety invariants.
806 /// [`std::process::abort`](../../std/process/fn.abort.html) is to be preferred if possible,
807 /// as its behavior is more user-friendly and more stable.
809 /// The current implementation of `intrinsics::abort` is to invoke an invalid instruction,
810 /// on most platforms.
812 /// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or
813 /// `SIGBUS`. The precise behaviour is not guaranteed and not stable.
814 #[rustc_safe_intrinsic]
817 /// Informs the optimizer that this point in the code is not reachable,
818 /// enabling further optimizations.
820 /// N.B., this is very different from the `unreachable!()` macro: Unlike the
821 /// macro, which panics when it is executed, it is *undefined behavior* to
822 /// reach code marked with this function.
824 /// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`].
825 #[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
826 pub fn unreachable() -> !;
828 /// Informs the optimizer that a condition is always true.
829 /// If the condition is false, the behavior is undefined.
831 /// No code is generated for this intrinsic, but the optimizer will try
832 /// to preserve it (and its condition) between passes, which may interfere
833 /// with optimization of surrounding code and reduce performance. It should
834 /// not be used if the invariant can be discovered by the optimizer on its
835 /// own, or if it does not enable any significant optimizations.
837 /// This intrinsic does not have a stable counterpart.
838 #[rustc_const_unstable(feature = "const_assume", issue = "76972")]
839 pub fn assume(b: bool);
841 /// Hints to the compiler that branch condition is likely to be true.
842 /// Returns the value passed to it.
844 /// Any use other than with `if` statements will probably not have an effect.
846 /// Note that, unlike most intrinsics, this is safe to call;
847 /// it does not require an `unsafe` block.
848 /// Therefore, implementations must not require the user to uphold
849 /// any safety invariants.
851 /// This intrinsic does not have a stable counterpart.
852 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
853 #[rustc_safe_intrinsic]
854 pub fn likely(b: bool) -> bool;
856 /// Hints to the compiler that branch condition is likely to be false.
857 /// Returns the value passed to it.
859 /// Any use other than with `if` statements will probably not have an effect.
861 /// Note that, unlike most intrinsics, this is safe to call;
862 /// it does not require an `unsafe` block.
863 /// Therefore, implementations must not require the user to uphold
864 /// any safety invariants.
866 /// This intrinsic does not have a stable counterpart.
867 #[rustc_const_unstable(feature = "const_likely", issue = "none")]
868 #[rustc_safe_intrinsic]
869 pub fn unlikely(b: bool) -> bool;
871 /// Executes a breakpoint trap, for inspection by a debugger.
873 /// This intrinsic does not have a stable counterpart.
876 /// The size of a type in bytes.
878 /// Note that, unlike most intrinsics, this is safe to call;
879 /// it does not require an `unsafe` block.
880 /// Therefore, implementations must not require the user to uphold
881 /// any safety invariants.
883 /// More specifically, this is the offset in bytes between successive
884 /// items of the same type, including alignment padding.
886 /// The stabilized version of this intrinsic is [`core::mem::size_of`].
887 #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
888 #[rustc_safe_intrinsic]
889 pub fn size_of<T>() -> usize;
891 /// The minimum alignment of a type.
893 /// Note that, unlike most intrinsics, this is safe to call;
894 /// it does not require an `unsafe` block.
895 /// Therefore, implementations must not require the user to uphold
896 /// any safety invariants.
898 /// The stabilized version of this intrinsic is [`core::mem::align_of`].
899 #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")]
900 #[rustc_safe_intrinsic]
901 pub fn min_align_of<T>() -> usize;
902 /// The preferred alignment of a type.
904 /// This intrinsic does not have a stable counterpart.
905 /// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971).
906 #[rustc_const_unstable(feature = "const_pref_align_of", issue = "91971")]
907 pub fn pref_align_of<T>() -> usize;
909 /// The size of the referenced value in bytes.
911 /// The stabilized version of this intrinsic is [`mem::size_of_val`].
912 #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
913 pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
914 /// The required alignment of the referenced value.
916 /// The stabilized version of this intrinsic is [`core::mem::align_of_val`].
917 #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
918 pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
920 /// Gets a static string slice containing the name of a type.
922 /// Note that, unlike most intrinsics, this is safe to call;
923 /// it does not require an `unsafe` block.
924 /// Therefore, implementations must not require the user to uphold
925 /// any safety invariants.
927 /// The stabilized version of this intrinsic is [`core::any::type_name`].
928 #[rustc_const_unstable(feature = "const_type_name", issue = "63084")]
929 #[rustc_safe_intrinsic]
930 pub fn type_name<T: ?Sized>() -> &'static str;
932 /// Gets an identifier which is globally unique to the specified type. This
933 /// function will return the same value for a type regardless of whichever
934 /// crate it is invoked in.
936 /// Note that, unlike most intrinsics, this is safe to call;
937 /// it does not require an `unsafe` block.
938 /// Therefore, implementations must not require the user to uphold
939 /// any safety invariants.
941 /// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
942 #[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
943 #[rustc_safe_intrinsic]
944 pub fn type_id<T: ?Sized + 'static>() -> u64;
946 /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:
947 /// This will statically either panic, or do nothing.
949 /// This intrinsic does not have a stable counterpart.
950 #[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
951 #[rustc_safe_intrinsic]
952 pub fn assert_inhabited<T>();
954 /// A guard for unsafe functions that cannot ever be executed if `T` does not permit
955 /// zero-initialization: This will statically either panic, or do nothing.
957 /// This intrinsic does not have a stable counterpart.
958 #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
959 #[rustc_safe_intrinsic]
960 pub fn assert_zero_valid<T>();
962 /// A guard for unsafe functions that cannot ever be executed if `T` has invalid
963 /// bit patterns: This will statically either panic, or do nothing.
965 /// This intrinsic does not have a stable counterpart.
966 #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")]
967 #[rustc_safe_intrinsic]
968 pub fn assert_uninit_valid<T>();
970 /// Gets a reference to a static `Location` indicating where it was called.
972 /// Note that, unlike most intrinsics, this is safe to call;
973 /// it does not require an `unsafe` block.
974 /// Therefore, implementations must not require the user to uphold
975 /// any safety invariants.
977 /// Consider using [`core::panic::Location::caller`] instead.
978 #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")]
979 #[rustc_safe_intrinsic]
980 pub fn caller_location() -> &'static crate::panic::Location<'static>;
982 /// Moves a value out of scope without running drop glue.
984 /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses
985 /// `ManuallyDrop` instead.
987 /// Note that, unlike most intrinsics, this is safe to call;
988 /// it does not require an `unsafe` block.
989 /// Therefore, implementations must not require the user to uphold
990 /// any safety invariants.
991 #[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")]
992 #[rustc_safe_intrinsic]
993 pub fn forget<T: ?Sized>(_: T);
995 /// Reinterprets the bits of a value of one type as another type.
997 /// Both types must have the same size. Compilation will fail if this is not guaranteed.
999 /// `transmute` is semantically equivalent to a bitwise move of one type
1000 /// into another. It copies the bits from the source value into the
1001 /// destination value, then forgets the original. Note that source and destination
1002 /// are passed by-value, which means if `Src` or `Dst` contain padding, that padding
1003 /// is *not* guaranteed to be preserved by `transmute`.
1005 /// Both the argument and the result must be [valid](../../nomicon/what-unsafe-does.html) at
1006 /// their given type. Violating this condition leads to [undefined behavior][ub]. The compiler
1007 /// will generate code *assuming that you, the programmer, ensure that there will never be
1008 /// undefined behavior*. It is therefore your responsibility to guarantee that every value
1009 /// passed to `transmute` is valid at both types `Src` and `Dst`. Failing to uphold this condition
1010 /// may lead to unexpected and unstable compilation results. This makes `transmute` **incredibly
1011 /// unsafe**. `transmute` should be the absolute last resort.
1013 /// Transmuting pointers to integers in a `const` context is [undefined behavior][ub].
1014 /// Any attempt to use the resulting value for integer operations will abort const-evaluation.
1015 /// (And even outside `const`, such transmutation is touching on many unspecified aspects of the
1016 /// Rust memory model and should be avoided. See below for alternatives.)
1018 /// Because `transmute` is a by-value operation, alignment of the *transmuted values
1019 /// themselves* is not a concern. As with any other function, the compiler already ensures
1020 /// both `Src` and `Dst` are properly aligned. However, when transmuting values that *point
1021 /// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper
1022 /// alignment of the pointed-to values.
1024 /// The [nomicon](../../nomicon/transmutes.html) has additional documentation.
1026 /// [ub]: ../../reference/behavior-considered-undefined.html
1030 /// There are a few things that `transmute` is really useful for.
1032 /// Turning a pointer into a function pointer. This is *not* portable to
1033 /// machines where function pointers and data pointers have different sizes.
1036 /// fn foo() -> i32 {
1039 /// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer.
1040 /// // This avoids an integer-to-pointer `transmute`, which can be problematic.
1041 /// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine.
1042 /// let pointer = foo as *const ();
1043 /// let function = unsafe {
1044 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
1046 /// assert_eq!(function(), 0);
1049 /// Extending a lifetime, or shortening an invariant lifetime. This is
1050 /// advanced, very unsafe Rust!
1053 /// struct R<'a>(&'a i32);
1054 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
1055 /// std::mem::transmute::<R<'b>, R<'static>>(r)
1058 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
1059 /// -> &'b mut R<'c> {
1060 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
1066 /// Don't despair: many uses of `transmute` can be achieved through other means.
1067 /// Below are common applications of `transmute` which can be replaced with safer
1070 /// Turning raw bytes (`&[u8]`) into `u32`, `f64`, etc.:
1073 /// let raw_bytes = [0x78, 0x56, 0x34, 0x12];
1075 /// let num = unsafe {
1076 /// std::mem::transmute::<[u8; 4], u32>(raw_bytes)
1079 /// // use `u32::from_ne_bytes` instead
1080 /// let num = u32::from_ne_bytes(raw_bytes);
1081 /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness
1082 /// let num = u32::from_le_bytes(raw_bytes);
1083 /// assert_eq!(num, 0x12345678);
1084 /// let num = u32::from_be_bytes(raw_bytes);
1085 /// assert_eq!(num, 0x78563412);
1088 /// Turning a pointer into a `usize`:
1092 /// let ptr_num_transmute = unsafe {
1093 /// std::mem::transmute::<&i32, usize>(ptr)
1096 /// // Use an `as` cast instead
1097 /// let ptr_num_cast = ptr as *const i32 as usize;
1100 /// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined
1101 /// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave
1102 /// as expected -- this is touching on many unspecified aspects of the Rust memory model.
1103 /// Depending on what the code is doing, the following alternatives are preferable to
1104 /// pointer-to-integer transmutation:
1105 /// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a
1106 /// type for that buffer, it can use [`MaybeUninit`][mem::MaybeUninit].
1107 /// - If the code actually wants to work on the address the pointer points to, it can use `as`
1108 /// casts or [`ptr.addr()`][pointer::addr].
1110 /// Turning a `*mut T` into an `&mut T`:
1113 /// let ptr: *mut i32 = &mut 0;
1114 /// let ref_transmuted = unsafe {
1115 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
1118 /// // Use a reborrow instead
1119 /// let ref_casted = unsafe { &mut *ptr };
1122 /// Turning an `&mut T` into an `&mut U`:
1125 /// let ptr = &mut 0;
1126 /// let val_transmuted = unsafe {
1127 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
1130 /// // Now, put together `as` and reborrowing - note the chaining of `as`
1131 /// // `as` is not transitive
1132 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
1135 /// Turning an `&str` into a `&[u8]`:
1138 /// // this is not a good way to do this.
1139 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
1140 /// assert_eq!(slice, &[82, 117, 115, 116]);
1142 /// // You could use `str::as_bytes`
1143 /// let slice = "Rust".as_bytes();
1144 /// assert_eq!(slice, &[82, 117, 115, 116]);
1146 /// // Or, just use a byte string, if you have control over the string
1148 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
1151 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`.
1153 /// To transmute the inner type of the contents of a container, you must make sure to not
1154 /// violate any of the container's invariants. For `Vec`, this means that both the size
1155 /// *and alignment* of the inner types have to match. Other containers might rely on the
1156 /// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't
1157 /// be possible at all without violating the container invariants.
1160 /// let store = [0, 1, 2, 3];
1161 /// let v_orig = store.iter().collect::<Vec<&i32>>();
1163 /// // clone the vector as we will reuse them later
1164 /// let v_clone = v_orig.clone();
1166 /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a
1167 /// // bad idea and could cause Undefined Behavior.
1168 /// // However, it is no-copy.
1169 /// let v_transmuted = unsafe {
1170 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone)
1173 /// let v_clone = v_orig.clone();
1175 /// // This is the suggested, safe way.
1176 /// // It does copy the entire vector, though, into a new array.
1177 /// let v_collected = v_clone.into_iter()
1179 /// .collect::<Vec<Option<&i32>>>();
1181 /// let v_clone = v_orig.clone();
1183 /// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the
1184 /// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but
1185 /// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`),
1186 /// // this has all the same caveats. Besides the information provided above, also consult the
1187 /// // [`from_raw_parts`] documentation.
1188 /// let v_from_raw = unsafe {
1189 // FIXME Update this when vec_into_raw_parts is stabilized
1190 /// // Ensure the original vector is not dropped.
1191 /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
1192 /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
1194 /// v_clone.capacity())
1198 /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts
1200 /// Implementing `split_at_mut`:
1203 /// use std::{slice, mem};
1205 /// // There are multiple ways to do this, and there are multiple problems
1206 /// // with the following (transmute) way.
1207 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
1208 /// -> (&mut [T], &mut [T]) {
1209 /// let len = slice.len();
1210 /// assert!(mid <= len);
1212 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
1213 /// // first: transmute is not type safe; all it checks is that T and
1214 /// // U are of the same size. Second, right here, you have two
1215 /// // mutable references pointing to the same memory.
1216 /// (&mut slice[0..mid], &mut slice2[mid..len])
1220 /// // This gets rid of the type safety problems; `&mut *` will *only* give
1221 /// // you an `&mut T` from an `&mut T` or `*mut T`.
1222 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
1223 /// -> (&mut [T], &mut [T]) {
1224 /// let len = slice.len();
1225 /// assert!(mid <= len);
1227 /// let slice2 = &mut *(slice as *mut [T]);
1228 /// // however, you still have two mutable references pointing to
1229 /// // the same memory.
1230 /// (&mut slice[0..mid], &mut slice2[mid..len])
1234 /// // This is how the standard library does it. This is the best method, if
1235 /// // you need to do something like this
1236 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
1237 /// -> (&mut [T], &mut [T]) {
1238 /// let len = slice.len();
1239 /// assert!(mid <= len);
1241 /// let ptr = slice.as_mut_ptr();
1242 /// // This now has three mutable references pointing at the same
1243 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
1244 /// // `slice` is never used after `let ptr = ...`, and so one can
1245 /// // treat it as "dead", and therefore, you only have two real
1246 /// // mutable slices.
1247 /// (slice::from_raw_parts_mut(ptr, mid),
1248 /// slice::from_raw_parts_mut(ptr.add(mid), len - mid))
1252 #[stable(feature = "rust1", since = "1.0.0")]
1253 #[rustc_allowed_through_unstable_modules]
1254 #[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
1255 #[rustc_diagnostic_item = "transmute"]
1256 pub fn transmute<Src, Dst>(src: Src) -> Dst;
1258 /// Returns `true` if the actual type given as `T` requires drop
1259 /// glue; returns `false` if the actual type provided for `T`
1260 /// implements `Copy`.
1262 /// If the actual type neither requires drop glue nor implements
1263 /// `Copy`, then the return value of this function is unspecified.
1265 /// Note that, unlike most intrinsics, this is safe to call;
1266 /// it does not require an `unsafe` block.
1267 /// Therefore, implementations must not require the user to uphold
1268 /// any safety invariants.
1270 /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop).
1271 #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")]
1272 #[rustc_safe_intrinsic]
1273 pub fn needs_drop<T: ?Sized>() -> bool;
1275 /// Calculates the offset from a pointer.
1277 /// This is implemented as an intrinsic to avoid converting to and from an
1278 /// integer, since the conversion would throw away aliasing information.
1282 /// Both the starting and resulting pointer must be either in bounds or one
1283 /// byte past the end of an allocated object. If either pointer is out of
1284 /// bounds or arithmetic overflow occurs then any further use of the
1285 /// returned value will result in undefined behavior.
1287 /// The stabilized version of this intrinsic is [`pointer::offset`].
1288 #[must_use = "returns a new pointer rather than modifying its argument"]
1289 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1290 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1292 /// Calculates the offset from a pointer, potentially wrapping.
1294 /// This is implemented as an intrinsic to avoid converting to and from an
1295 /// integer, since the conversion inhibits certain optimizations.
1299 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
1300 /// resulting pointer to point into or one byte past the end of an allocated
1301 /// object, and it wraps with two's complement arithmetic. The resulting
1302 /// value is not necessarily valid to be used to actually access memory.
1304 /// The stabilized version of this intrinsic is [`pointer::wrapping_offset`].
1305 #[must_use = "returns a new pointer rather than modifying its argument"]
1306 #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
1307 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
1309 /// Masks out bits of the pointer according to a mask.
1311 /// Note that, unlike most intrinsics, this is safe to call;
1312 /// it does not require an `unsafe` block.
1313 /// Therefore, implementations must not require the user to uphold
1314 /// any safety invariants.
1316 /// Consider using [`pointer::mask`] instead.
1317 #[rustc_safe_intrinsic]
1318 pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;
1320 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1321 /// a size of `count` * `size_of::<T>()` and an alignment of
1322 /// `min_align_of::<T>()`
1324 /// The volatile parameter is set to `true`, so it will not be optimized out
1325 /// unless size is equal to zero.
1327 /// This intrinsic does not have a stable counterpart.
1328 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize);
1329 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1330 /// a size of `count * size_of::<T>()` and an alignment of
1331 /// `min_align_of::<T>()`
1333 /// The volatile parameter is set to `true`, so it will not be optimized out
1334 /// unless size is equal to zero.
1336 /// This intrinsic does not have a stable counterpart.
1337 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1338 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1339 /// size of `count * size_of::<T>()` and an alignment of
1340 /// `min_align_of::<T>()`.
1342 /// The volatile parameter is set to `true`, so it will not be optimized out
1343 /// unless size is equal to zero.
1345 /// This intrinsic does not have a stable counterpart.
1346 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1348 /// Performs a volatile load from the `src` pointer.
1350 /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`].
1351 pub fn volatile_load<T>(src: *const T) -> T;
1352 /// Performs a volatile store to the `dst` pointer.
1354 /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`].
1355 pub fn volatile_store<T>(dst: *mut T, val: T);
1357 /// Performs a volatile load from the `src` pointer
1358 /// The pointer is not required to be aligned.
1360 /// This intrinsic does not have a stable counterpart.
1361 pub fn unaligned_volatile_load<T>(src: *const T) -> T;
1362 /// Performs a volatile store to the `dst` pointer.
1363 /// The pointer is not required to be aligned.
1365 /// This intrinsic does not have a stable counterpart.
1366 pub fn unaligned_volatile_store<T>(dst: *mut T, val: T);
1368 /// Returns the square root of an `f32`
1370 /// The stabilized version of this intrinsic is
1371 /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt)
1372 pub fn sqrtf32(x: f32) -> f32;
1373 /// Returns the square root of an `f64`
1375 /// The stabilized version of this intrinsic is
1376 /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt)
1377 pub fn sqrtf64(x: f64) -> f64;
1379 /// Raises an `f32` to an integer power.
1381 /// The stabilized version of this intrinsic is
1382 /// [`f32::powi`](../../std/primitive.f32.html#method.powi)
1383 pub fn powif32(a: f32, x: i32) -> f32;
1384 /// Raises an `f64` to an integer power.
1386 /// The stabilized version of this intrinsic is
1387 /// [`f64::powi`](../../std/primitive.f64.html#method.powi)
1388 pub fn powif64(a: f64, x: i32) -> f64;
1390 /// Returns the sine of an `f32`.
1392 /// The stabilized version of this intrinsic is
1393 /// [`f32::sin`](../../std/primitive.f32.html#method.sin)
1394 pub fn sinf32(x: f32) -> f32;
1395 /// Returns the sine of an `f64`.
1397 /// The stabilized version of this intrinsic is
1398 /// [`f64::sin`](../../std/primitive.f64.html#method.sin)
1399 pub fn sinf64(x: f64) -> f64;
1401 /// Returns the cosine of an `f32`.
1403 /// The stabilized version of this intrinsic is
1404 /// [`f32::cos`](../../std/primitive.f32.html#method.cos)
1405 pub fn cosf32(x: f32) -> f32;
1406 /// Returns the cosine of an `f64`.
1408 /// The stabilized version of this intrinsic is
1409 /// [`f64::cos`](../../std/primitive.f64.html#method.cos)
1410 pub fn cosf64(x: f64) -> f64;
1412 /// Raises an `f32` to an `f32` power.
1414 /// The stabilized version of this intrinsic is
1415 /// [`f32::powf`](../../std/primitive.f32.html#method.powf)
1416 pub fn powf32(a: f32, x: f32) -> f32;
1417 /// Raises an `f64` to an `f64` power.
1419 /// The stabilized version of this intrinsic is
1420 /// [`f64::powf`](../../std/primitive.f64.html#method.powf)
1421 pub fn powf64(a: f64, x: f64) -> f64;
1423 /// Returns the exponential of an `f32`.
1425 /// The stabilized version of this intrinsic is
1426 /// [`f32::exp`](../../std/primitive.f32.html#method.exp)
1427 pub fn expf32(x: f32) -> f32;
1428 /// Returns the exponential of an `f64`.
1430 /// The stabilized version of this intrinsic is
1431 /// [`f64::exp`](../../std/primitive.f64.html#method.exp)
1432 pub fn expf64(x: f64) -> f64;
1434 /// Returns 2 raised to the power of an `f32`.
1436 /// The stabilized version of this intrinsic is
1437 /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2)
1438 pub fn exp2f32(x: f32) -> f32;
1439 /// Returns 2 raised to the power of an `f64`.
1441 /// The stabilized version of this intrinsic is
1442 /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2)
1443 pub fn exp2f64(x: f64) -> f64;
1445 /// Returns the natural logarithm of an `f32`.
1447 /// The stabilized version of this intrinsic is
1448 /// [`f32::ln`](../../std/primitive.f32.html#method.ln)
1449 pub fn logf32(x: f32) -> f32;
1450 /// Returns the natural logarithm of an `f64`.
1452 /// The stabilized version of this intrinsic is
1453 /// [`f64::ln`](../../std/primitive.f64.html#method.ln)
1454 pub fn logf64(x: f64) -> f64;
1456 /// Returns the base 10 logarithm of an `f32`.
1458 /// The stabilized version of this intrinsic is
1459 /// [`f32::log10`](../../std/primitive.f32.html#method.log10)
1460 pub fn log10f32(x: f32) -> f32;
1461 /// Returns the base 10 logarithm of an `f64`.
1463 /// The stabilized version of this intrinsic is
1464 /// [`f64::log10`](../../std/primitive.f64.html#method.log10)
1465 pub fn log10f64(x: f64) -> f64;
1467 /// Returns the base 2 logarithm of an `f32`.
1469 /// The stabilized version of this intrinsic is
1470 /// [`f32::log2`](../../std/primitive.f32.html#method.log2)
1471 pub fn log2f32(x: f32) -> f32;
1472 /// Returns the base 2 logarithm of an `f64`.
1474 /// The stabilized version of this intrinsic is
1475 /// [`f64::log2`](../../std/primitive.f64.html#method.log2)
1476 pub fn log2f64(x: f64) -> f64;
1478 /// Returns `a * b + c` for `f32` values.
1480 /// The stabilized version of this intrinsic is
1481 /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add)
1482 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1483 /// Returns `a * b + c` for `f64` values.
1485 /// The stabilized version of this intrinsic is
1486 /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add)
1487 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1489 /// Returns the absolute value of an `f32`.
1491 /// The stabilized version of this intrinsic is
1492 /// [`f32::abs`](../../std/primitive.f32.html#method.abs)
1493 pub fn fabsf32(x: f32) -> f32;
1494 /// Returns the absolute value of an `f64`.
1496 /// The stabilized version of this intrinsic is
1497 /// [`f64::abs`](../../std/primitive.f64.html#method.abs)
1498 pub fn fabsf64(x: f64) -> f64;
1500 /// Returns the minimum of two `f32` values.
1502 /// Note that, unlike most intrinsics, this is safe to call;
1503 /// it does not require an `unsafe` block.
1504 /// Therefore, implementations must not require the user to uphold
1505 /// any safety invariants.
1507 /// The stabilized version of this intrinsic is
1509 #[rustc_safe_intrinsic]
1510 pub fn minnumf32(x: f32, y: f32) -> f32;
1511 /// Returns the minimum of two `f64` values.
1513 /// Note that, unlike most intrinsics, this is safe to call;
1514 /// it does not require an `unsafe` block.
1515 /// Therefore, implementations must not require the user to uphold
1516 /// any safety invariants.
1518 /// The stabilized version of this intrinsic is
1520 #[rustc_safe_intrinsic]
1521 pub fn minnumf64(x: f64, y: f64) -> f64;
1522 /// Returns the maximum of two `f32` values.
1524 /// Note that, unlike most intrinsics, this is safe to call;
1525 /// it does not require an `unsafe` block.
1526 /// Therefore, implementations must not require the user to uphold
1527 /// any safety invariants.
1529 /// The stabilized version of this intrinsic is
1531 #[rustc_safe_intrinsic]
1532 pub fn maxnumf32(x: f32, y: f32) -> f32;
1533 /// Returns the maximum of two `f64` values.
1535 /// Note that, unlike most intrinsics, this is safe to call;
1536 /// it does not require an `unsafe` block.
1537 /// Therefore, implementations must not require the user to uphold
1538 /// any safety invariants.
1540 /// The stabilized version of this intrinsic is
1542 #[rustc_safe_intrinsic]
1543 pub fn maxnumf64(x: f64, y: f64) -> f64;
1545 /// Copies the sign from `y` to `x` for `f32` values.
1547 /// The stabilized version of this intrinsic is
1548 /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign)
1549 pub fn copysignf32(x: f32, y: f32) -> f32;
1550 /// Copies the sign from `y` to `x` for `f64` values.
1552 /// The stabilized version of this intrinsic is
1553 /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign)
1554 pub fn copysignf64(x: f64, y: f64) -> f64;
1556 /// Returns the largest integer less than or equal to an `f32`.
1558 /// The stabilized version of this intrinsic is
1559 /// [`f32::floor`](../../std/primitive.f32.html#method.floor)
1560 pub fn floorf32(x: f32) -> f32;
1561 /// Returns the largest integer less than or equal to an `f64`.
1563 /// The stabilized version of this intrinsic is
1564 /// [`f64::floor`](../../std/primitive.f64.html#method.floor)
1565 pub fn floorf64(x: f64) -> f64;
1567 /// Returns the smallest integer greater than or equal to an `f32`.
1569 /// The stabilized version of this intrinsic is
1570 /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
1571 pub fn ceilf32(x: f32) -> f32;
1572 /// Returns the smallest integer greater than or equal to an `f64`.
1574 /// The stabilized version of this intrinsic is
1575 /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
1576 pub fn ceilf64(x: f64) -> f64;
1578 /// Returns the integer part of an `f32`.
1580 /// The stabilized version of this intrinsic is
1581 /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
1582 pub fn truncf32(x: f32) -> f32;
1583 /// Returns the integer part of an `f64`.
1585 /// The stabilized version of this intrinsic is
1586 /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
1587 pub fn truncf64(x: f64) -> f64;
1589 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1590 /// if the argument is not an integer.
1591 pub fn rintf32(x: f32) -> f32;
1592 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1593 /// if the argument is not an integer.
1594 pub fn rintf64(x: f64) -> f64;
1596 /// Returns the nearest integer to an `f32`.
1598 /// This intrinsic does not have a stable counterpart.
1599 pub fn nearbyintf32(x: f32) -> f32;
1600 /// Returns the nearest integer to an `f64`.
1602 /// This intrinsic does not have a stable counterpart.
1603 pub fn nearbyintf64(x: f64) -> f64;
1605 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1607 /// The stabilized version of this intrinsic is
1608 /// [`f32::round`](../../std/primitive.f32.html#method.round)
1609 pub fn roundf32(x: f32) -> f32;
1610 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1612 /// The stabilized version of this intrinsic is
1613 /// [`f64::round`](../../std/primitive.f64.html#method.round)
1614 pub fn roundf64(x: f64) -> f64;
1616 /// Float addition that allows optimizations based on algebraic rules.
1617 /// May assume inputs are finite.
1619 /// This intrinsic does not have a stable counterpart.
1620 pub fn fadd_fast<T: Copy>(a: T, b: T) -> T;
1622 /// Float subtraction that allows optimizations based on algebraic rules.
1623 /// May assume inputs are finite.
1625 /// This intrinsic does not have a stable counterpart.
1626 pub fn fsub_fast<T: Copy>(a: T, b: T) -> T;
1628 /// Float multiplication that allows optimizations based on algebraic rules.
1629 /// May assume inputs are finite.
1631 /// This intrinsic does not have a stable counterpart.
1632 pub fn fmul_fast<T: Copy>(a: T, b: T) -> T;
1634 /// Float division that allows optimizations based on algebraic rules.
1635 /// May assume inputs are finite.
1637 /// This intrinsic does not have a stable counterpart.
1638 pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T;
1640 /// Float remainder that allows optimizations based on algebraic rules.
1641 /// May assume inputs are finite.
1643 /// This intrinsic does not have a stable counterpart.
1644 pub fn frem_fast<T: Copy>(a: T, b: T) -> T;
1646 /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range
1647 /// (<https://github.com/rust-lang/rust/issues/10184>)
1649 /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`].
1650 pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int;
1652 /// Returns the number of bits set in an integer type `T`
1654 /// Note that, unlike most intrinsics, this is safe to call;
1655 /// it does not require an `unsafe` block.
1656 /// Therefore, implementations must not require the user to uphold
1657 /// any safety invariants.
1659 /// The stabilized versions of this intrinsic are available on the integer
1660 /// primitives via the `count_ones` method. For example,
1661 /// [`u32::count_ones`]
1662 #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")]
1663 #[rustc_safe_intrinsic]
1664 pub fn ctpop<T: Copy>(x: T) -> T;
1666 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1668 /// Note that, unlike most intrinsics, this is safe to call;
1669 /// it does not require an `unsafe` block.
1670 /// Therefore, implementations must not require the user to uphold
1671 /// any safety invariants.
1673 /// The stabilized versions of this intrinsic are available on the integer
1674 /// primitives via the `leading_zeros` method. For example,
1675 /// [`u32::leading_zeros`]
1680 /// #![feature(core_intrinsics)]
1682 /// use std::intrinsics::ctlz;
1684 /// let x = 0b0001_1100_u8;
1685 /// let num_leading = ctlz(x);
1686 /// assert_eq!(num_leading, 3);
1689 /// An `x` with value `0` will return the bit width of `T`.
1692 /// #![feature(core_intrinsics)]
1694 /// use std::intrinsics::ctlz;
1697 /// let num_leading = ctlz(x);
1698 /// assert_eq!(num_leading, 16);
1700 #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")]
1701 #[rustc_safe_intrinsic]
1702 pub fn ctlz<T: Copy>(x: T) -> T;
1704 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1705 /// given an `x` with value `0`.
1707 /// This intrinsic does not have a stable counterpart.
1712 /// #![feature(core_intrinsics)]
1714 /// use std::intrinsics::ctlz_nonzero;
1716 /// let x = 0b0001_1100_u8;
1717 /// let num_leading = unsafe { ctlz_nonzero(x) };
1718 /// assert_eq!(num_leading, 3);
1720 #[rustc_const_stable(feature = "constctlz", since = "1.50.0")]
1721 pub fn ctlz_nonzero<T: Copy>(x: T) -> T;
1723 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1725 /// Note that, unlike most intrinsics, this is safe to call;
1726 /// it does not require an `unsafe` block.
1727 /// Therefore, implementations must not require the user to uphold
1728 /// any safety invariants.
1730 /// The stabilized versions of this intrinsic are available on the integer
1731 /// primitives via the `trailing_zeros` method. For example,
1732 /// [`u32::trailing_zeros`]
1737 /// #![feature(core_intrinsics)]
1739 /// use std::intrinsics::cttz;
1741 /// let x = 0b0011_1000_u8;
1742 /// let num_trailing = cttz(x);
1743 /// assert_eq!(num_trailing, 3);
1746 /// An `x` with value `0` will return the bit width of `T`:
1749 /// #![feature(core_intrinsics)]
1751 /// use std::intrinsics::cttz;
1754 /// let num_trailing = cttz(x);
1755 /// assert_eq!(num_trailing, 16);
1757 #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")]
1758 #[rustc_safe_intrinsic]
1759 pub fn cttz<T: Copy>(x: T) -> T;
1761 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1762 /// given an `x` with value `0`.
1764 /// This intrinsic does not have a stable counterpart.
1769 /// #![feature(core_intrinsics)]
1771 /// use std::intrinsics::cttz_nonzero;
1773 /// let x = 0b0011_1000_u8;
1774 /// let num_trailing = unsafe { cttz_nonzero(x) };
1775 /// assert_eq!(num_trailing, 3);
1777 #[rustc_const_stable(feature = "const_cttz_nonzero", since = "1.53.0")]
1778 pub fn cttz_nonzero<T: Copy>(x: T) -> T;
1780 /// Reverses the bytes in an integer type `T`.
1782 /// Note that, unlike most intrinsics, this is safe to call;
1783 /// it does not require an `unsafe` block.
1784 /// Therefore, implementations must not require the user to uphold
1785 /// any safety invariants.
1787 /// The stabilized versions of this intrinsic are available on the integer
1788 /// primitives via the `swap_bytes` method. For example,
1789 /// [`u32::swap_bytes`]
1790 #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")]
1791 #[rustc_safe_intrinsic]
1792 pub fn bswap<T: Copy>(x: T) -> T;
1794 /// Reverses the bits in an integer type `T`.
1796 /// Note that, unlike most intrinsics, this is safe to call;
1797 /// it does not require an `unsafe` block.
1798 /// Therefore, implementations must not require the user to uphold
1799 /// any safety invariants.
1801 /// The stabilized versions of this intrinsic are available on the integer
1802 /// primitives via the `reverse_bits` method. For example,
1803 /// [`u32::reverse_bits`]
1804 #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")]
1805 #[rustc_safe_intrinsic]
1806 pub fn bitreverse<T: Copy>(x: T) -> T;
1808 /// Performs checked integer addition.
1810 /// Note that, unlike most intrinsics, this is safe to call;
1811 /// it does not require an `unsafe` block.
1812 /// Therefore, implementations must not require the user to uphold
1813 /// any safety invariants.
1815 /// The stabilized versions of this intrinsic are available on the integer
1816 /// primitives via the `overflowing_add` method. For example,
1817 /// [`u32::overflowing_add`]
1818 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1819 #[rustc_safe_intrinsic]
1820 pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1822 /// Performs checked integer subtraction
1824 /// Note that, unlike most intrinsics, this is safe to call;
1825 /// it does not require an `unsafe` block.
1826 /// Therefore, implementations must not require the user to uphold
1827 /// any safety invariants.
1829 /// The stabilized versions of this intrinsic are available on the integer
1830 /// primitives via the `overflowing_sub` method. For example,
1831 /// [`u32::overflowing_sub`]
1832 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1833 #[rustc_safe_intrinsic]
1834 pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1836 /// Performs checked integer multiplication
1838 /// Note that, unlike most intrinsics, this is safe to call;
1839 /// it does not require an `unsafe` block.
1840 /// Therefore, implementations must not require the user to uphold
1841 /// any safety invariants.
1843 /// The stabilized versions of this intrinsic are available on the integer
1844 /// primitives via the `overflowing_mul` method. For example,
1845 /// [`u32::overflowing_mul`]
1846 #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")]
1847 #[rustc_safe_intrinsic]
1848 pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);
1850 /// Performs an exact division, resulting in undefined behavior where
1851 /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`
1853 /// This intrinsic does not have a stable counterpart.
1854 pub fn exact_div<T: Copy>(x: T, y: T) -> T;
1856 /// Performs an unchecked division, resulting in undefined behavior
1857 /// where `y == 0` or `x == T::MIN && y == -1`
1859 /// Safe wrappers for this intrinsic are available on the integer
1860 /// primitives via the `checked_div` method. For example,
1861 /// [`u32::checked_div`]
1862 #[rustc_const_stable(feature = "const_int_unchecked_div", since = "1.52.0")]
1863 pub fn unchecked_div<T: Copy>(x: T, y: T) -> T;
1864 /// Returns the remainder of an unchecked division, resulting in
1865 /// undefined behavior when `y == 0` or `x == T::MIN && y == -1`
1867 /// Safe wrappers for this intrinsic are available on the integer
1868 /// primitives via the `checked_rem` method. For example,
1869 /// [`u32::checked_rem`]
1870 #[rustc_const_stable(feature = "const_int_unchecked_rem", since = "1.52.0")]
1871 pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T;
1873 /// Performs an unchecked left shift, resulting in undefined behavior when
1874 /// `y < 0` or `y >= N`, where N is the width of T in bits.
1876 /// Safe wrappers for this intrinsic are available on the integer
1877 /// primitives via the `checked_shl` method. For example,
1878 /// [`u32::checked_shl`]
1879 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1880 pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T;
1881 /// Performs an unchecked right shift, resulting in undefined behavior when
1882 /// `y < 0` or `y >= N`, where N is the width of T in bits.
1884 /// Safe wrappers for this intrinsic are available on the integer
1885 /// primitives via the `checked_shr` method. For example,
1886 /// [`u32::checked_shr`]
1887 #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")]
1888 pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T;
1890 /// Returns the result of an unchecked addition, resulting in
1891 /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
1893 /// This intrinsic does not have a stable counterpart.
1894 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1895 pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
1897 /// Returns the result of an unchecked subtraction, resulting in
1898 /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
1900 /// This intrinsic does not have a stable counterpart.
1901 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1902 pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
1904 /// Returns the result of an unchecked multiplication, resulting in
1905 /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
1907 /// This intrinsic does not have a stable counterpart.
1908 #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")]
1909 pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;
1911 /// Performs rotate left.
1913 /// Note that, unlike most intrinsics, this is safe to call;
1914 /// it does not require an `unsafe` block.
1915 /// Therefore, implementations must not require the user to uphold
1916 /// any safety invariants.
1918 /// The stabilized versions of this intrinsic are available on the integer
1919 /// primitives via the `rotate_left` method. For example,
1920 /// [`u32::rotate_left`]
1921 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1922 #[rustc_safe_intrinsic]
1923 pub fn rotate_left<T: Copy>(x: T, y: T) -> T;
1925 /// Performs rotate right.
1927 /// Note that, unlike most intrinsics, this is safe to call;
1928 /// it does not require an `unsafe` block.
1929 /// Therefore, implementations must not require the user to uphold
1930 /// any safety invariants.
1932 /// The stabilized versions of this intrinsic are available on the integer
1933 /// primitives via the `rotate_right` method. For example,
1934 /// [`u32::rotate_right`]
1935 #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")]
1936 #[rustc_safe_intrinsic]
1937 pub fn rotate_right<T: Copy>(x: T, y: T) -> T;
1939 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1941 /// Note that, unlike most intrinsics, this is safe to call;
1942 /// it does not require an `unsafe` block.
1943 /// Therefore, implementations must not require the user to uphold
1944 /// any safety invariants.
1946 /// The stabilized versions of this intrinsic are available on the integer
1947 /// primitives via the `wrapping_add` method. For example,
1948 /// [`u32::wrapping_add`]
1949 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1950 #[rustc_safe_intrinsic]
1951 pub fn wrapping_add<T: Copy>(a: T, b: T) -> T;
1952 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1954 /// Note that, unlike most intrinsics, this is safe to call;
1955 /// it does not require an `unsafe` block.
1956 /// Therefore, implementations must not require the user to uphold
1957 /// any safety invariants.
1959 /// The stabilized versions of this intrinsic are available on the integer
1960 /// primitives via the `wrapping_sub` method. For example,
1961 /// [`u32::wrapping_sub`]
1962 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1963 #[rustc_safe_intrinsic]
1964 pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T;
1965 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1967 /// Note that, unlike most intrinsics, this is safe to call;
1968 /// it does not require an `unsafe` block.
1969 /// Therefore, implementations must not require the user to uphold
1970 /// any safety invariants.
1972 /// The stabilized versions of this intrinsic are available on the integer
1973 /// primitives via the `wrapping_mul` method. For example,
1974 /// [`u32::wrapping_mul`]
1975 #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")]
1976 #[rustc_safe_intrinsic]
1977 pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T;
1979 /// Computes `a + b`, saturating at numeric bounds.
1981 /// Note that, unlike most intrinsics, this is safe to call;
1982 /// it does not require an `unsafe` block.
1983 /// Therefore, implementations must not require the user to uphold
1984 /// any safety invariants.
1986 /// The stabilized versions of this intrinsic are available on the integer
1987 /// primitives via the `saturating_add` method. For example,
1988 /// [`u32::saturating_add`]
1989 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
1990 #[rustc_safe_intrinsic]
1991 pub fn saturating_add<T: Copy>(a: T, b: T) -> T;
1992 /// Computes `a - b`, saturating at numeric bounds.
1994 /// Note that, unlike most intrinsics, this is safe to call;
1995 /// it does not require an `unsafe` block.
1996 /// Therefore, implementations must not require the user to uphold
1997 /// any safety invariants.
1999 /// The stabilized versions of this intrinsic are available on the integer
2000 /// primitives via the `saturating_sub` method. For example,
2001 /// [`u32::saturating_sub`]
2002 #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")]
2003 #[rustc_safe_intrinsic]
2004 pub fn saturating_sub<T: Copy>(a: T, b: T) -> T;
2006 /// Returns the value of the discriminant for the variant in 'v';
2007 /// if `T` has no discriminant, returns `0`.
2009 /// Note that, unlike most intrinsics, this is safe to call;
2010 /// it does not require an `unsafe` block.
2011 /// Therefore, implementations must not require the user to uphold
2012 /// any safety invariants.
2014 /// The stabilized version of this intrinsic is [`core::mem::discriminant`].
2015 #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")]
2016 #[rustc_safe_intrinsic]
2017 pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant;
2019 /// Returns the number of variants of the type `T` cast to a `usize`;
2020 /// if `T` has no variants, returns `0`. Uninhabited variants will be counted.
2022 /// Note that, unlike most intrinsics, this is safe to call;
2023 /// it does not require an `unsafe` block.
2024 /// Therefore, implementations must not require the user to uphold
2025 /// any safety invariants.
2027 /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`].
2028 #[rustc_const_unstable(feature = "variant_count", issue = "73662")]
2029 #[rustc_safe_intrinsic]
2030 pub fn variant_count<T>() -> usize;
2032 /// Rust's "try catch" construct which invokes the function pointer `try_fn`
2033 /// with the data pointer `data`.
2035 /// The third argument is a function called if a panic occurs. This function
2036 /// takes the data pointer and a pointer to the target-specific exception
2037 /// object that was caught. For more information see the compiler's
2038 /// source as well as std's catch implementation.
2039 pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32;
2041 /// Emits a `!nontemporal` store according to LLVM (see their docs).
2042 /// Probably will never become stable.
2043 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
2045 /// See documentation of `<*const T>::offset_from` for details.
2046 #[rustc_const_stable(feature = "const_ptr_offset_from", since = "1.65.0")]
2047 pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize;
2049 /// See documentation of `<*const T>::sub_ptr` for details.
2050 #[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")]
2051 pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize;
2053 /// See documentation of `<*const T>::guaranteed_eq` for details.
2054 /// Returns `2` if the result is unknown.
2055 /// Returns `1` if the pointers are guaranteed equal
2056 /// Returns `0` if the pointers are guaranteed inequal
2058 /// Note that, unlike most intrinsics, this is safe to call;
2059 /// it does not require an `unsafe` block.
2060 /// Therefore, implementations must not require the user to uphold
2061 /// any safety invariants.
2062 #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")]
2063 #[rustc_safe_intrinsic]
2064 pub fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8;
2066 /// Allocates a block of memory at compile time.
2067 /// At runtime, just returns a null pointer.
2071 /// - The `align` argument must be a power of two.
2072 /// - At compile time, a compile error occurs if this constraint is violated.
2073 /// - At runtime, it is not checked.
2074 #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2075 pub fn const_allocate(size: usize, align: usize) -> *mut u8;
2077 /// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time.
2078 /// At runtime, does nothing.
2082 /// - The `align` argument must be a power of two.
2083 /// - At compile time, a compile error occurs if this constraint is violated.
2084 /// - At runtime, it is not checked.
2085 /// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it.
2086 /// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it.
2087 #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
2088 pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize);
2090 /// Determines whether the raw bytes of the two values are equal.
2092 /// This is particularly handy for arrays, since it allows things like just
2093 /// comparing `i96`s instead of forcing `alloca`s for `[6 x i16]`.
2095 /// Above some backend-decided threshold this will emit calls to `memcmp`,
2096 /// like slice equality does, instead of causing massive code size.
2100 /// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized or carry a
2102 /// Note that this is a stricter criterion than just the *values* being
2103 /// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
2105 /// (The implementation is allowed to branch on the results of comparisons,
2106 /// which is UB if any of their inputs are `undef`.)
2107 #[rustc_const_unstable(feature = "const_intrinsic_raw_eq", issue = "none")]
2108 pub fn raw_eq<T>(a: &T, b: &T) -> bool;
2110 /// See documentation of [`std::hint::black_box`] for details.
2112 /// [`std::hint::black_box`]: crate::hint::black_box
2113 #[rustc_const_unstable(feature = "const_black_box", issue = "none")]
2114 #[rustc_safe_intrinsic]
2115 pub fn black_box<T>(dummy: T) -> T;
2117 /// `ptr` must point to a vtable.
2118 /// The intrinsic will return the size stored in that vtable.
2119 pub fn vtable_size(ptr: *const ()) -> usize;
2121 /// `ptr` must point to a vtable.
2122 /// The intrinsic will return the alignment stored in that vtable.
2123 pub fn vtable_align(ptr: *const ()) -> usize;
2125 /// Selects which function to call depending on the context.
2127 /// If this function is evaluated at compile-time, then a call to this
2128 /// intrinsic will be replaced with a call to `called_in_const`. It gets
2129 /// replaced with a call to `called_at_rt` otherwise.
2131 /// # Type Requirements
2133 /// The two functions must be both function items. They cannot be function
2134 /// pointers or closures. The first function must be a `const fn`.
2136 /// `arg` will be the tupled arguments that will be passed to either one of
2137 /// the two functions, therefore, both functions must accept the same type of
2138 /// arguments. Both functions must return RET.
2142 /// The two functions must behave observably equivalent. Safe code in other
2143 /// crates may assume that calling a `const fn` at compile-time and at run-time
2144 /// produces the same result. A function that produces a different result when
2145 /// evaluated at run-time, or has any other observable side-effects, is
2148 /// Here is an example of how this could cause a problem:
2150 /// #![feature(const_eval_select)]
2151 /// #![feature(core_intrinsics)]
2152 /// use std::hint::unreachable_unchecked;
2153 /// use std::intrinsics::const_eval_select;
2156 /// pub const fn inconsistent() -> i32 {
2157 /// fn runtime() -> i32 { 1 }
2158 /// const fn compiletime() -> i32 { 2 }
2161 // // ⚠ This code violates the required equivalence of `compiletime`
2162 /// // and `runtime`.
2163 /// const_eval_select((), compiletime, runtime)
2168 /// const X: i32 = inconsistent();
2169 /// let x = inconsistent();
2170 /// if x != X { unsafe { unreachable_unchecked(); }}
2173 /// This code causes Undefined Behavior when being run, since the
2174 /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2175 /// which violates the principle that a `const fn` must behave the same at
2176 /// compile-time and at run-time. The unsafe code in crate B is fine.
2178 #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2179 pub fn const_eval_select<ARG, F, G, RET>(arg: ARG, called_in_const: F, called_at_rt: G) -> RET
2181 G: FnOnce<ARG, Output = RET>,
2182 F: FnOnce<ARG, Output = RET>;
2184 /// Selects which function to call depending on the context.
2186 /// If this function is evaluated at compile-time, then a call to this
2187 /// intrinsic will be replaced with a call to `called_in_const`. It gets
2188 /// replaced with a call to `called_at_rt` otherwise.
2190 /// # Type Requirements
2192 /// The two functions must be both function items. They cannot be function
2193 /// pointers or closures. The first function must be a `const fn`.
2195 /// `arg` will be the tupled arguments that will be passed to either one of
2196 /// the two functions, therefore, both functions must accept the same type of
2197 /// arguments. Both functions must return RET.
2201 /// The two functions must behave observably equivalent. Safe code in other
2202 /// crates may assume that calling a `const fn` at compile-time and at run-time
2203 /// produces the same result. A function that produces a different result when
2204 /// evaluated at run-time, or has any other observable side-effects, is
2207 /// Here is an example of how this could cause a problem:
2209 /// #![feature(const_eval_select)]
2210 /// #![feature(core_intrinsics)]
2211 /// use std::hint::unreachable_unchecked;
2212 /// use std::intrinsics::const_eval_select;
2215 /// pub const fn inconsistent() -> i32 {
2216 /// fn runtime() -> i32 { 1 }
2217 /// const fn compiletime() -> i32 { 2 }
2220 // // ⚠ This code violates the required equivalence of `compiletime`
2221 /// // and `runtime`.
2222 /// const_eval_select((), compiletime, runtime)
2227 /// const X: i32 = inconsistent();
2228 /// let x = inconsistent();
2229 /// if x != X { unsafe { unreachable_unchecked(); }}
2232 /// This code causes Undefined Behavior when being run, since the
2233 /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*,
2234 /// which violates the principle that a `const fn` must behave the same at
2235 /// compile-time and at run-time. The unsafe code in crate B is fine.
2236 #[cfg(not(bootstrap))]
2237 #[rustc_const_unstable(feature = "const_eval_select", issue = "none")]
2238 pub fn const_eval_select<ARG: Tuple, F, G, RET>(
2244 G: FnOnce<ARG, Output = RET>,
2245 F: FnOnce<ARG, Output = RET>;
2248 // Some functions are defined here because they accidentally got made
2249 // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>.
2250 // (`transmute` also falls into this category, but it cannot be wrapped due to the
2251 // check that `T` and `U` have the same size.)
2253 /// Check that the preconditions of an unsafe function are followed, if debug_assertions are on,
2254 /// and only at runtime.
2256 /// This macro should be called as `assert_unsafe_precondition!([Generics](name: Type) => Expression)`
2257 /// where the names specified will be moved into the macro as captured variables, and defines an item
2258 /// to call `const_eval_select` on. The tokens inside the square brackets are used to denote generics
2259 /// for the function declaractions and can be omitted if there is no generics.
2263 /// Invoking this macro is only sound if the following code is already UB when the passed
2264 /// expression evaluates to false.
2266 /// This macro expands to a check at runtime if debug_assertions is set. It has no effect at
2267 /// compile time, but the semantics of the contained `const_eval_select` must be the same at
2268 /// runtime and at compile time. Thus if the expression evaluates to false, this macro produces
2269 /// different behavior at compile time and at runtime, and invoking it is incorrect.
2271 /// So in a sense it is UB if this macro is useful, but we expect callers of `unsafe fn` to make
2272 /// the occasional mistake, and this check should help them figure things out.
2273 #[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn
2274 macro_rules! assert_unsafe_precondition {
2275 ($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr) => {
2276 if cfg!(debug_assertions) {
2277 // allow non_snake_case to allow capturing const generics
2278 #[allow(non_snake_case)]
2280 fn runtime$(<$($tt)*>)?($($i:$ty),*) {
2282 // don't unwind to reduce impact on code size
2283 ::core::panicking::panic_str_nounwind(
2284 concat!("unsafe precondition(s) violated: ", $name)
2288 #[allow(non_snake_case)]
2289 const fn comptime$(<$($tt)*>)?($(_:$ty),*) {}
2291 ::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime);
2295 pub(crate) use assert_unsafe_precondition;
2297 /// Checks whether `ptr` is properly aligned with respect to
2298 /// `align_of::<T>()`.
2299 pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
2300 !ptr.is_null() && ptr.is_aligned()
2303 /// Checks whether an allocation of `len` instances of `T` exceeds
2304 /// the maximum allowed allocation size.
2305 pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool {
2306 let max_len = const {
2307 let size = crate::mem::size_of::<T>();
2308 if size == 0 { usize::MAX } else { isize::MAX as usize / size }
2313 /// Checks whether the regions of memory starting at `src` and `dst` of size
2314 /// `count * size_of::<T>()` do *not* overlap.
2315 pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
2316 let src_usize = src.addr();
2317 let dst_usize = dst.addr();
2318 let size = mem::size_of::<T>().checked_mul(count).unwrap();
2319 let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
2320 // If the absolute distance between the ptrs is at least as big as the size of the buffer,
2321 // they do not overlap.
2325 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2326 /// and destination must *not* overlap.
2328 /// For regions of memory which might overlap, use [`copy`] instead.
2330 /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but
2331 /// with the argument order swapped.
2333 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2334 /// requirements of `T`. The initialization state is preserved exactly.
2336 /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy
2340 /// Behavior is undefined if any of the following conditions are violated:
2342 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2344 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2346 /// * Both `src` and `dst` must be properly aligned.
2348 /// * The region of memory beginning at `src` with a size of `count *
2349 /// size_of::<T>()` bytes must *not* overlap with the region of memory
2350 /// beginning at `dst` with the same size.
2352 /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of
2353 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values
2354 /// in the region beginning at `*src` and the region beginning at `*dst` can
2355 /// [violate memory safety][read-ownership].
2357 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2358 /// `0`, the pointers must be non-null and properly aligned.
2360 /// [`read`]: crate::ptr::read
2361 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2362 /// [valid]: crate::ptr#safety
2366 /// Manually implement [`Vec::append`]:
2371 /// /// Moves all the elements of `src` into `dst`, leaving `src` empty.
2372 /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) {
2373 /// let src_len = src.len();
2374 /// let dst_len = dst.len();
2376 /// // Ensure that `dst` has enough capacity to hold all of `src`.
2377 /// dst.reserve(src_len);
2380 /// // The call to add is always safe because `Vec` will never
2381 /// // allocate more than `isize::MAX` bytes.
2382 /// let dst_ptr = dst.as_mut_ptr().add(dst_len);
2383 /// let src_ptr = src.as_ptr();
2385 /// // Truncate `src` without dropping its contents. We do this first,
2386 /// // to avoid problems in case something further down panics.
2389 /// // The two regions cannot overlap because mutable references do
2390 /// // not alias, and two different vectors cannot own the same
2392 /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len);
2394 /// // Notify `dst` that it now holds the contents of `src`.
2395 /// dst.set_len(dst_len + src_len);
2399 /// let mut a = vec!['r'];
2400 /// let mut b = vec!['u', 's', 't'];
2402 /// append(&mut a, &mut b);
2404 /// assert_eq!(a, &['r', 'u', 's', 't']);
2405 /// assert!(b.is_empty());
2408 /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append
2409 #[doc(alias = "memcpy")]
2410 #[stable(feature = "rust1", since = "1.0.0")]
2411 #[rustc_allowed_through_unstable_modules]
2412 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2414 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2415 pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
2416 extern "rust-intrinsic" {
2417 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2418 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
2421 // SAFETY: the safety contract for `copy_nonoverlapping` must be
2422 // upheld by the caller.
2424 assert_unsafe_precondition!(
2425 "ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
2426 and the specified memory ranges do not overlap",
2427 [T](src: *const T, dst: *mut T, count: usize) =>
2428 is_aligned_and_not_null(src)
2429 && is_aligned_and_not_null(dst)
2430 && is_nonoverlapping(src, dst, count)
2432 copy_nonoverlapping(src, dst, count)
2436 /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
2437 /// and destination may overlap.
2439 /// If the source and destination will *never* overlap,
2440 /// [`copy_nonoverlapping`] can be used instead.
2442 /// `copy` is semantically equivalent to C's [`memmove`], but with the argument
2443 /// order swapped. Copying takes place as if the bytes were copied from `src`
2444 /// to a temporary array and then copied from the array to `dst`.
2446 /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the
2447 /// requirements of `T`. The initialization state is preserved exactly.
2449 /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove
2453 /// Behavior is undefined if any of the following conditions are violated:
2455 /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes.
2457 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2459 /// * Both `src` and `dst` must be properly aligned.
2461 /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of
2462 /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values
2463 /// in the region beginning at `*src` and the region beginning at `*dst` can
2464 /// [violate memory safety][read-ownership].
2466 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2467 /// `0`, the pointers must be non-null and properly aligned.
2469 /// [`read`]: crate::ptr::read
2470 /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value
2471 /// [valid]: crate::ptr#safety
2475 /// Efficiently create a Rust vector from an unsafe buffer:
2482 /// /// * `ptr` must be correctly aligned for its type and non-zero.
2483 /// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`.
2484 /// /// * Those elements must not be used after calling this function unless `T: Copy`.
2485 /// # #[allow(dead_code)]
2486 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
2487 /// let mut dst = Vec::with_capacity(elts);
2489 /// // SAFETY: Our precondition ensures the source is aligned and valid,
2490 /// // and `Vec::with_capacity` ensures that we have usable space to write them.
2491 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
2493 /// // SAFETY: We created it with this much capacity earlier,
2494 /// // and the previous `copy` has initialized these elements.
2495 /// dst.set_len(elts);
2499 #[doc(alias = "memmove")]
2500 #[stable(feature = "rust1", since = "1.0.0")]
2501 #[rustc_allowed_through_unstable_modules]
2502 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2504 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2505 pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
2506 extern "rust-intrinsic" {
2507 #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
2508 fn copy<T>(src: *const T, dst: *mut T, count: usize);
2511 // SAFETY: the safety contract for `copy` must be upheld by the caller.
2513 assert_unsafe_precondition!(
2514 "ptr::copy requires that both pointer arguments are aligned aligned and non-null",
2515 [T](src: *const T, dst: *mut T) =>
2516 is_aligned_and_not_null(src) && is_aligned_and_not_null(dst)
2518 copy(src, dst, count)
2522 /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to
2525 /// `write_bytes` is similar to C's [`memset`], but sets `count *
2526 /// size_of::<T>()` bytes to `val`.
2528 /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset
2532 /// Behavior is undefined if any of the following conditions are violated:
2534 /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes.
2536 /// * `dst` must be properly aligned.
2538 /// Note that even if the effectively copied size (`count * size_of::<T>()`) is
2539 /// `0`, the pointer must be non-null and properly aligned.
2541 /// Additionally, note that changing `*dst` in this way can easily lead to undefined behavior (UB)
2542 /// later if the written bytes are not a valid representation of some `T`. For instance, the
2543 /// following is an **incorrect** use of this function:
2547 /// let mut value: u8 = 0;
2548 /// let ptr: *mut bool = &mut value as *mut u8 as *mut bool;
2549 /// let _bool = ptr.read(); // This is fine, `ptr` points to a valid `bool`.
2550 /// ptr.write_bytes(42u8, 1); // This function itself does not cause UB...
2551 /// let _bool = ptr.read(); // ...but it makes this operation UB! ⚠️
2555 /// [valid]: crate::ptr#safety
2564 /// let mut vec = vec![0u32; 4];
2566 /// let vec_ptr = vec.as_mut_ptr();
2567 /// ptr::write_bytes(vec_ptr, 0xfe, 2);
2569 /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);
2571 #[doc(alias = "memset")]
2572 #[stable(feature = "rust1", since = "1.0.0")]
2573 #[rustc_allowed_through_unstable_modules]
2574 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2576 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
2577 pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
2578 extern "rust-intrinsic" {
2579 #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
2580 fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
2583 // SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
2585 assert_unsafe_precondition!(
2586 "ptr::write_bytes requires that the destination pointer is aligned and non-null",
2587 [T](dst: *mut T) => is_aligned_and_not_null(dst)
2589 write_bytes(dst, val, count)