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