Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1 | //! Compiler intrinsics. |
| 2 | //! |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 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>. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 5 | //! |
| 6 | //! # Const intrinsics |
| 7 | //! |
| 8 | //! Note: any changes to the constness of intrinsics should be discussed with the language team. |
| 9 | //! This includes changes in the stability of the constness. |
| 10 | //! |
| 11 | //! In order to make an intrinsic usable at compile-time, one needs to copy the implementation |
Thiébaud Weksteen | 5bd94c1 | 2021-01-06 15:18:42 +0100 | [diff] [blame] | 12 | //! from <https://github.com/rust-lang/miri/blob/master/src/shims/intrinsics.rs> to |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 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. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 15 | //! |
| 16 | //! If an intrinsic is supposed to be used from a `const fn` with a `rustc_const_stable` attribute, |
| 17 | //! the intrinsic's attribute must be `rustc_const_stable`, too. Such a change should not be done |
| 18 | //! without T-lang consultation, because it bakes a feature into the language that cannot be |
| 19 | //! replicated in user code without compiler support. |
| 20 | //! |
| 21 | //! # Volatiles |
| 22 | //! |
| 23 | //! The volatile intrinsics provide operations intended to act on I/O |
| 24 | //! memory, which are guaranteed to not be reordered by the compiler |
| 25 | //! across other volatile intrinsics. See the LLVM documentation on |
| 26 | //! [[volatile]]. |
| 27 | //! |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 28 | //! [volatile]: https://llvm.org/docs/LangRef.html#volatile-memory-accesses |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 29 | //! |
| 30 | //! # Atomics |
| 31 | //! |
| 32 | //! The atomic intrinsics provide common atomic operations on machine |
| 33 | //! words, with multiple possible memory orderings. They obey the same |
| 34 | //! semantics as C++11. See the LLVM documentation on [[atomics]]. |
| 35 | //! |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 36 | //! [atomics]: https://llvm.org/docs/Atomics.html |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 37 | //! |
| 38 | //! A quick refresher on memory ordering: |
| 39 | //! |
| 40 | //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes |
| 41 | //! take place after the barrier. |
| 42 | //! * Release - a barrier for releasing a lock. Preceding reads and writes |
| 43 | //! take place before the barrier. |
| 44 | //! * Sequentially consistent - sequentially consistent operations are |
| 45 | //! guaranteed to happen in order. This is the standard mode for working |
| 46 | //! with atomic types and is equivalent to Java's `volatile`. |
| 47 | |
| 48 | #![unstable( |
| 49 | feature = "core_intrinsics", |
| 50 | reason = "intrinsics are unlikely to ever be stabilized, instead \ |
| 51 | they should be used through stabilized interfaces \ |
| 52 | in the rest of the standard library", |
| 53 | issue = "none" |
| 54 | )] |
| 55 | #![allow(missing_docs)] |
| 56 | |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 57 | use crate::marker::DiscriminantKind; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 58 | use crate::mem; |
| 59 | |
| 60 | // These imports are used for simplifying intra-doc links |
| 61 | #[allow(unused_imports)] |
| 62 | #[cfg(all(target_has_atomic = "8", target_has_atomic = "32", target_has_atomic = "ptr"))] |
| 63 | use crate::sync::atomic::{self, AtomicBool, AtomicI32, AtomicIsize, AtomicU32, Ordering}; |
| 64 | |
| 65 | #[stable(feature = "drop_in_place", since = "1.8.0")] |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 66 | #[rustc_allowed_through_unstable_modules] |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 67 | #[deprecated(note = "no longer an intrinsic - use `ptr::drop_in_place` directly", since = "1.52.0")] |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 68 | #[inline] |
| 69 | pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) { |
| 70 | // SAFETY: see `ptr::drop_in_place` |
| 71 | unsafe { crate::ptr::drop_in_place(to_drop) } |
| 72 | } |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 73 | |
| 74 | extern "rust-intrinsic" { |
| 75 | // N.B., these intrinsics take raw pointers because they mutate aliased |
| 76 | // memory, which is not valid for either `&` or `&mut`. |
| 77 | |
| 78 | /// Stores a value if the current value is the same as the `old` value. |
| 79 | /// |
| 80 | /// The stabilized version of this intrinsic is available on the |
| 81 | /// [`atomic`] types via the `compare_exchange` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 82 | /// [`Ordering::Relaxed`] as both the success and failure parameters. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 83 | /// For example, [`AtomicBool::compare_exchange`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 84 | pub fn atomic_cxchg_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 85 | /// Stores a value if the current value is the same as the `old` value. |
| 86 | /// |
| 87 | /// The stabilized version of this intrinsic is available on the |
| 88 | /// [`atomic`] types via the `compare_exchange` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 89 | /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 90 | /// For example, [`AtomicBool::compare_exchange`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 91 | pub fn atomic_cxchg_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 92 | /// Stores a value if the current value is the same as the `old` value. |
| 93 | /// |
| 94 | /// The stabilized version of this intrinsic is available on the |
| 95 | /// [`atomic`] types via the `compare_exchange` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 96 | /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 97 | /// For example, [`AtomicBool::compare_exchange`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 98 | pub fn atomic_cxchg_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 99 | /// Stores a value if the current value is the same as the `old` value. |
| 100 | /// |
| 101 | /// The stabilized version of this intrinsic is available on the |
| 102 | /// [`atomic`] types via the `compare_exchange` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 103 | /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters. |
| 104 | /// For example, [`AtomicBool::compare_exchange`]. |
| 105 | pub fn atomic_cxchg_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 106 | /// Stores a value if the current value is the same as the `old` value. |
| 107 | /// |
| 108 | /// The stabilized version of this intrinsic is available on the |
| 109 | /// [`atomic`] types via the `compare_exchange` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 110 | /// [`Ordering::Acquire`] as both the success and failure parameters. |
| 111 | /// For example, [`AtomicBool::compare_exchange`]. |
| 112 | pub fn atomic_cxchg_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 113 | /// Stores a value if the current value is the same as the `old` value. |
| 114 | /// |
| 115 | /// The stabilized version of this intrinsic is available on the |
| 116 | /// [`atomic`] types via the `compare_exchange` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 117 | /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters. |
| 118 | /// For example, [`AtomicBool::compare_exchange`]. |
| 119 | pub fn atomic_cxchg_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 120 | /// Stores a value if the current value is the same as the `old` value. |
| 121 | /// |
| 122 | /// The stabilized version of this intrinsic is available on the |
| 123 | /// [`atomic`] types via the `compare_exchange` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 124 | /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters. |
| 125 | /// For example, [`AtomicBool::compare_exchange`]. |
| 126 | pub fn atomic_cxchg_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 127 | /// Stores a value if the current value is the same as the `old` value. |
| 128 | /// |
| 129 | /// The stabilized version of this intrinsic is available on the |
| 130 | /// [`atomic`] types via the `compare_exchange` method by passing |
| 131 | /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters. |
| 132 | /// For example, [`AtomicBool::compare_exchange`]. |
| 133 | pub fn atomic_cxchg_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 134 | /// Stores a value if the current value is the same as the `old` value. |
| 135 | /// |
| 136 | /// The stabilized version of this intrinsic is available on the |
| 137 | /// [`atomic`] types via the `compare_exchange` method by passing |
| 138 | /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters. |
| 139 | /// For example, [`AtomicBool::compare_exchange`]. |
| 140 | pub fn atomic_cxchg_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 141 | /// Stores a value if the current value is the same as the `old` value. |
| 142 | /// |
| 143 | /// The stabilized version of this intrinsic is available on the |
| 144 | /// [`atomic`] types via the `compare_exchange` method by passing |
| 145 | /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters. |
| 146 | /// For example, [`AtomicBool::compare_exchange`]. |
| 147 | pub fn atomic_cxchg_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 148 | /// Stores a value if the current value is the same as the `old` value. |
| 149 | /// |
| 150 | /// The stabilized version of this intrinsic is available on the |
| 151 | /// [`atomic`] types via the `compare_exchange` method by passing |
| 152 | /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters. |
| 153 | /// For example, [`AtomicBool::compare_exchange`]. |
| 154 | pub fn atomic_cxchg_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 155 | /// Stores a value if the current value is the same as the `old` value. |
| 156 | /// |
| 157 | /// The stabilized version of this intrinsic is available on the |
| 158 | /// [`atomic`] types via the `compare_exchange` method by passing |
| 159 | /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters. |
| 160 | /// For example, [`AtomicBool::compare_exchange`]. |
| 161 | pub fn atomic_cxchg_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 162 | /// Stores a value if the current value is the same as the `old` value. |
| 163 | /// |
| 164 | /// The stabilized version of this intrinsic is available on the |
| 165 | /// [`atomic`] types via the `compare_exchange` method by passing |
| 166 | /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters. |
| 167 | /// For example, [`AtomicBool::compare_exchange`]. |
| 168 | pub fn atomic_cxchg_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 169 | /// Stores a value if the current value is the same as the `old` value. |
| 170 | /// |
| 171 | /// The stabilized version of this intrinsic is available on the |
| 172 | /// [`atomic`] types via the `compare_exchange` method by passing |
| 173 | /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters. |
| 174 | /// For example, [`AtomicBool::compare_exchange`]. |
| 175 | pub fn atomic_cxchg_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 176 | /// Stores a value if the current value is the same as the `old` value. |
| 177 | /// |
| 178 | /// The stabilized version of this intrinsic is available on the |
| 179 | /// [`atomic`] types via the `compare_exchange` method by passing |
| 180 | /// [`Ordering::SeqCst`] as both the success and failure parameters. |
| 181 | /// For example, [`AtomicBool::compare_exchange`]. |
| 182 | pub fn atomic_cxchg_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 183 | |
| 184 | /// Stores a value if the current value is the same as the `old` value. |
| 185 | /// |
| 186 | /// The stabilized version of this intrinsic is available on the |
| 187 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 188 | /// [`Ordering::Relaxed`] as both the success and failure parameters. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 189 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 190 | pub fn atomic_cxchgweak_relaxed_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 191 | /// Stores a value if the current value is the same as the `old` value. |
| 192 | /// |
| 193 | /// The stabilized version of this intrinsic is available on the |
| 194 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 195 | /// [`Ordering::Relaxed`] and [`Ordering::Acquire`] as the success and failure parameters. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 196 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 197 | pub fn atomic_cxchgweak_relaxed_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 198 | /// Stores a value if the current value is the same as the `old` value. |
| 199 | /// |
| 200 | /// The stabilized version of this intrinsic is available on the |
| 201 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 202 | /// [`Ordering::Relaxed`] and [`Ordering::SeqCst`] as the success and failure parameters. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 203 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 204 | pub fn atomic_cxchgweak_relaxed_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 205 | /// Stores a value if the current value is the same as the `old` value. |
| 206 | /// |
| 207 | /// The stabilized version of this intrinsic is available on the |
| 208 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 209 | /// [`Ordering::Acquire`] and [`Ordering::Relaxed`] as the success and failure parameters. |
| 210 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 211 | pub fn atomic_cxchgweak_acquire_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 212 | /// Stores a value if the current value is the same as the `old` value. |
| 213 | /// |
| 214 | /// The stabilized version of this intrinsic is available on the |
| 215 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 216 | /// [`Ordering::Acquire`] as both the success and failure parameters. |
| 217 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 218 | pub fn atomic_cxchgweak_acquire_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 219 | /// Stores a value if the current value is the same as the `old` value. |
| 220 | /// |
| 221 | /// The stabilized version of this intrinsic is available on the |
| 222 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 223 | /// [`Ordering::Acquire`] and [`Ordering::SeqCst`] as the success and failure parameters. |
| 224 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 225 | pub fn atomic_cxchgweak_acquire_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 226 | /// Stores a value if the current value is the same as the `old` value. |
| 227 | /// |
| 228 | /// The stabilized version of this intrinsic is available on the |
| 229 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 230 | /// [`Ordering::Release`] and [`Ordering::Relaxed`] as the success and failure parameters. |
| 231 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 232 | pub fn atomic_cxchgweak_release_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 233 | /// Stores a value if the current value is the same as the `old` value. |
| 234 | /// |
| 235 | /// The stabilized version of this intrinsic is available on the |
| 236 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
| 237 | /// [`Ordering::Release`] and [`Ordering::Acquire`] as the success and failure parameters. |
| 238 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 239 | pub fn atomic_cxchgweak_release_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 240 | /// Stores a value if the current value is the same as the `old` value. |
| 241 | /// |
| 242 | /// The stabilized version of this intrinsic is available on the |
| 243 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
| 244 | /// [`Ordering::Release`] and [`Ordering::SeqCst`] as the success and failure parameters. |
| 245 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 246 | pub fn atomic_cxchgweak_release_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 247 | /// Stores a value if the current value is the same as the `old` value. |
| 248 | /// |
| 249 | /// The stabilized version of this intrinsic is available on the |
| 250 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
| 251 | /// [`Ordering::AcqRel`] and [`Ordering::Relaxed`] as the success and failure parameters. |
| 252 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 253 | pub fn atomic_cxchgweak_acqrel_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 254 | /// Stores a value if the current value is the same as the `old` value. |
| 255 | /// |
| 256 | /// The stabilized version of this intrinsic is available on the |
| 257 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
| 258 | /// [`Ordering::AcqRel`] and [`Ordering::Acquire`] as the success and failure parameters. |
| 259 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 260 | pub fn atomic_cxchgweak_acqrel_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 261 | /// Stores a value if the current value is the same as the `old` value. |
| 262 | /// |
| 263 | /// The stabilized version of this intrinsic is available on the |
| 264 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
| 265 | /// [`Ordering::AcqRel`] and [`Ordering::SeqCst`] as the success and failure parameters. |
| 266 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 267 | pub fn atomic_cxchgweak_acqrel_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 268 | /// Stores a value if the current value is the same as the `old` value. |
| 269 | /// |
| 270 | /// The stabilized version of this intrinsic is available on the |
| 271 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
| 272 | /// [`Ordering::SeqCst`] and [`Ordering::Relaxed`] as the success and failure parameters. |
| 273 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 274 | pub fn atomic_cxchgweak_seqcst_relaxed<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 275 | /// Stores a value if the current value is the same as the `old` value. |
| 276 | /// |
| 277 | /// The stabilized version of this intrinsic is available on the |
| 278 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
| 279 | /// [`Ordering::SeqCst`] and [`Ordering::Acquire`] as the success and failure parameters. |
| 280 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 281 | pub fn atomic_cxchgweak_seqcst_acquire<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
| 282 | /// Stores a value if the current value is the same as the `old` value. |
| 283 | /// |
| 284 | /// The stabilized version of this intrinsic is available on the |
| 285 | /// [`atomic`] types via the `compare_exchange_weak` method by passing |
| 286 | /// [`Ordering::SeqCst`] as both the success and failure parameters. |
| 287 | /// For example, [`AtomicBool::compare_exchange_weak`]. |
| 288 | pub fn atomic_cxchgweak_seqcst_seqcst<T: Copy>(dst: *mut T, old: T, src: T) -> (T, bool); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 289 | |
| 290 | /// Loads the current value of the pointer. |
| 291 | /// |
| 292 | /// The stabilized version of this intrinsic is available on the |
| 293 | /// [`atomic`] types via the `load` method by passing |
| 294 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::load`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 295 | pub fn atomic_load_seqcst<T: Copy>(src: *const T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 296 | /// Loads the current value of the pointer. |
| 297 | /// |
| 298 | /// The stabilized version of this intrinsic is available on the |
| 299 | /// [`atomic`] types via the `load` method by passing |
| 300 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::load`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 301 | pub fn atomic_load_acquire<T: Copy>(src: *const T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 302 | /// Loads the current value of the pointer. |
| 303 | /// |
| 304 | /// The stabilized version of this intrinsic is available on the |
| 305 | /// [`atomic`] types via the `load` method by passing |
| 306 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::load`]. |
| 307 | pub fn atomic_load_relaxed<T: Copy>(src: *const T) -> T; |
| 308 | pub fn atomic_load_unordered<T: Copy>(src: *const T) -> T; |
| 309 | |
| 310 | /// Stores the value at the specified memory location. |
| 311 | /// |
| 312 | /// The stabilized version of this intrinsic is available on the |
| 313 | /// [`atomic`] types via the `store` method by passing |
| 314 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::store`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 315 | pub fn atomic_store_seqcst<T: Copy>(dst: *mut T, val: T); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 316 | /// Stores the value at the specified memory location. |
| 317 | /// |
| 318 | /// The stabilized version of this intrinsic is available on the |
| 319 | /// [`atomic`] types via the `store` method by passing |
| 320 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::store`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 321 | pub fn atomic_store_release<T: Copy>(dst: *mut T, val: T); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 322 | /// Stores the value at the specified memory location. |
| 323 | /// |
| 324 | /// The stabilized version of this intrinsic is available on the |
| 325 | /// [`atomic`] types via the `store` method by passing |
| 326 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::store`]. |
| 327 | pub fn atomic_store_relaxed<T: Copy>(dst: *mut T, val: T); |
| 328 | pub fn atomic_store_unordered<T: Copy>(dst: *mut T, val: T); |
| 329 | |
| 330 | /// Stores the value at the specified memory location, returning the old value. |
| 331 | /// |
| 332 | /// The stabilized version of this intrinsic is available on the |
| 333 | /// [`atomic`] types via the `swap` method by passing |
| 334 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::swap`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 335 | pub fn atomic_xchg_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 336 | /// Stores the value at the specified memory location, returning the old value. |
| 337 | /// |
| 338 | /// The stabilized version of this intrinsic is available on the |
| 339 | /// [`atomic`] types via the `swap` method by passing |
| 340 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::swap`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 341 | pub fn atomic_xchg_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 342 | /// Stores the value at the specified memory location, returning the old value. |
| 343 | /// |
| 344 | /// The stabilized version of this intrinsic is available on the |
| 345 | /// [`atomic`] types via the `swap` method by passing |
| 346 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::swap`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 347 | pub fn atomic_xchg_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 348 | /// Stores the value at the specified memory location, returning the old value. |
| 349 | /// |
| 350 | /// The stabilized version of this intrinsic is available on the |
| 351 | /// [`atomic`] types via the `swap` method by passing |
| 352 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::swap`]. |
| 353 | pub fn atomic_xchg_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 354 | /// Stores the value at the specified memory location, returning the old value. |
| 355 | /// |
| 356 | /// The stabilized version of this intrinsic is available on the |
| 357 | /// [`atomic`] types via the `swap` method by passing |
| 358 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::swap`]. |
| 359 | pub fn atomic_xchg_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 360 | |
| 361 | /// Adds to the current value, returning the previous value. |
| 362 | /// |
| 363 | /// The stabilized version of this intrinsic is available on the |
| 364 | /// [`atomic`] types via the `fetch_add` method by passing |
| 365 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_add`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 366 | pub fn atomic_xadd_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 367 | /// Adds to the current value, returning the previous value. |
| 368 | /// |
| 369 | /// The stabilized version of this intrinsic is available on the |
| 370 | /// [`atomic`] types via the `fetch_add` method by passing |
| 371 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_add`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 372 | pub fn atomic_xadd_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 373 | /// Adds to the current value, returning the previous value. |
| 374 | /// |
| 375 | /// The stabilized version of this intrinsic is available on the |
| 376 | /// [`atomic`] types via the `fetch_add` method by passing |
| 377 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_add`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 378 | pub fn atomic_xadd_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 379 | /// Adds to the current value, returning the previous value. |
| 380 | /// |
| 381 | /// The stabilized version of this intrinsic is available on the |
| 382 | /// [`atomic`] types via the `fetch_add` method by passing |
| 383 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_add`]. |
| 384 | pub fn atomic_xadd_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 385 | /// Adds to the current value, returning the previous value. |
| 386 | /// |
| 387 | /// The stabilized version of this intrinsic is available on the |
| 388 | /// [`atomic`] types via the `fetch_add` method by passing |
| 389 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_add`]. |
| 390 | pub fn atomic_xadd_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 391 | |
| 392 | /// Subtract from the current value, returning the previous value. |
| 393 | /// |
| 394 | /// The stabilized version of this intrinsic is available on the |
| 395 | /// [`atomic`] types via the `fetch_sub` method by passing |
| 396 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicIsize::fetch_sub`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 397 | pub fn atomic_xsub_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 398 | /// Subtract from the current value, returning the previous value. |
| 399 | /// |
| 400 | /// The stabilized version of this intrinsic is available on the |
| 401 | /// [`atomic`] types via the `fetch_sub` method by passing |
| 402 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicIsize::fetch_sub`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 403 | pub fn atomic_xsub_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 404 | /// Subtract from the current value, returning the previous value. |
| 405 | /// |
| 406 | /// The stabilized version of this intrinsic is available on the |
| 407 | /// [`atomic`] types via the `fetch_sub` method by passing |
| 408 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicIsize::fetch_sub`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 409 | pub fn atomic_xsub_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 410 | /// Subtract from the current value, returning the previous value. |
| 411 | /// |
| 412 | /// The stabilized version of this intrinsic is available on the |
| 413 | /// [`atomic`] types via the `fetch_sub` method by passing |
| 414 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicIsize::fetch_sub`]. |
| 415 | pub fn atomic_xsub_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 416 | /// Subtract from the current value, returning the previous value. |
| 417 | /// |
| 418 | /// The stabilized version of this intrinsic is available on the |
| 419 | /// [`atomic`] types via the `fetch_sub` method by passing |
| 420 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicIsize::fetch_sub`]. |
| 421 | pub fn atomic_xsub_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 422 | |
| 423 | /// Bitwise and with the current value, returning the previous value. |
| 424 | /// |
| 425 | /// The stabilized version of this intrinsic is available on the |
| 426 | /// [`atomic`] types via the `fetch_and` method by passing |
| 427 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_and`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 428 | pub fn atomic_and_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 429 | /// Bitwise and with the current value, returning the previous value. |
| 430 | /// |
| 431 | /// The stabilized version of this intrinsic is available on the |
| 432 | /// [`atomic`] types via the `fetch_and` method by passing |
| 433 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_and`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 434 | pub fn atomic_and_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 435 | /// Bitwise and with the current value, returning the previous value. |
| 436 | /// |
| 437 | /// The stabilized version of this intrinsic is available on the |
| 438 | /// [`atomic`] types via the `fetch_and` method by passing |
| 439 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_and`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 440 | pub fn atomic_and_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 441 | /// Bitwise and with the current value, returning the previous value. |
| 442 | /// |
| 443 | /// The stabilized version of this intrinsic is available on the |
| 444 | /// [`atomic`] types via the `fetch_and` method by passing |
| 445 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_and`]. |
| 446 | pub fn atomic_and_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 447 | /// Bitwise and with the current value, returning the previous value. |
| 448 | /// |
| 449 | /// The stabilized version of this intrinsic is available on the |
| 450 | /// [`atomic`] types via the `fetch_and` method by passing |
| 451 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_and`]. |
| 452 | pub fn atomic_and_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 453 | |
| 454 | /// Bitwise nand with the current value, returning the previous value. |
| 455 | /// |
| 456 | /// The stabilized version of this intrinsic is available on the |
| 457 | /// [`AtomicBool`] type via the `fetch_nand` method by passing |
| 458 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_nand`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 459 | pub fn atomic_nand_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 460 | /// Bitwise nand with the current value, returning the previous value. |
| 461 | /// |
| 462 | /// The stabilized version of this intrinsic is available on the |
| 463 | /// [`AtomicBool`] type via the `fetch_nand` method by passing |
| 464 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_nand`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 465 | pub fn atomic_nand_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 466 | /// Bitwise nand with the current value, returning the previous value. |
| 467 | /// |
| 468 | /// The stabilized version of this intrinsic is available on the |
| 469 | /// [`AtomicBool`] type via the `fetch_nand` method by passing |
| 470 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_nand`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 471 | pub fn atomic_nand_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 472 | /// Bitwise nand with the current value, returning the previous value. |
| 473 | /// |
| 474 | /// The stabilized version of this intrinsic is available on the |
| 475 | /// [`AtomicBool`] type via the `fetch_nand` method by passing |
| 476 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_nand`]. |
| 477 | pub fn atomic_nand_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 478 | /// Bitwise nand with the current value, returning the previous value. |
| 479 | /// |
| 480 | /// The stabilized version of this intrinsic is available on the |
| 481 | /// [`AtomicBool`] type via the `fetch_nand` method by passing |
| 482 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_nand`]. |
| 483 | pub fn atomic_nand_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 484 | |
| 485 | /// Bitwise or with the current value, returning the previous value. |
| 486 | /// |
| 487 | /// The stabilized version of this intrinsic is available on the |
| 488 | /// [`atomic`] types via the `fetch_or` method by passing |
| 489 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_or`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 490 | pub fn atomic_or_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 491 | /// Bitwise or with the current value, returning the previous value. |
| 492 | /// |
| 493 | /// The stabilized version of this intrinsic is available on the |
| 494 | /// [`atomic`] types via the `fetch_or` method by passing |
| 495 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_or`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 496 | pub fn atomic_or_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 497 | /// Bitwise or with the current value, returning the previous value. |
| 498 | /// |
| 499 | /// The stabilized version of this intrinsic is available on the |
| 500 | /// [`atomic`] types via the `fetch_or` method by passing |
| 501 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_or`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 502 | pub fn atomic_or_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 503 | /// Bitwise or with the current value, returning the previous value. |
| 504 | /// |
| 505 | /// The stabilized version of this intrinsic is available on the |
| 506 | /// [`atomic`] types via the `fetch_or` method by passing |
| 507 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_or`]. |
| 508 | pub fn atomic_or_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 509 | /// Bitwise or with the current value, returning the previous value. |
| 510 | /// |
| 511 | /// The stabilized version of this intrinsic is available on the |
| 512 | /// [`atomic`] types via the `fetch_or` method by passing |
| 513 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_or`]. |
| 514 | pub fn atomic_or_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 515 | |
| 516 | /// Bitwise xor with the current value, returning the previous value. |
| 517 | /// |
| 518 | /// The stabilized version of this intrinsic is available on the |
| 519 | /// [`atomic`] types via the `fetch_xor` method by passing |
| 520 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicBool::fetch_xor`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 521 | pub fn atomic_xor_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 522 | /// Bitwise xor with the current value, returning the previous value. |
| 523 | /// |
| 524 | /// The stabilized version of this intrinsic is available on the |
| 525 | /// [`atomic`] types via the `fetch_xor` method by passing |
| 526 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicBool::fetch_xor`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 527 | pub fn atomic_xor_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 528 | /// Bitwise xor with the current value, returning the previous value. |
| 529 | /// |
| 530 | /// The stabilized version of this intrinsic is available on the |
| 531 | /// [`atomic`] types via the `fetch_xor` method by passing |
| 532 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicBool::fetch_xor`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 533 | pub fn atomic_xor_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 534 | /// Bitwise xor with the current value, returning the previous value. |
| 535 | /// |
| 536 | /// The stabilized version of this intrinsic is available on the |
| 537 | /// [`atomic`] types via the `fetch_xor` method by passing |
| 538 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicBool::fetch_xor`]. |
| 539 | pub fn atomic_xor_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 540 | /// Bitwise xor with the current value, returning the previous value. |
| 541 | /// |
| 542 | /// The stabilized version of this intrinsic is available on the |
| 543 | /// [`atomic`] types via the `fetch_xor` method by passing |
| 544 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicBool::fetch_xor`]. |
| 545 | pub fn atomic_xor_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 546 | |
| 547 | /// Maximum with the current value using a signed comparison. |
| 548 | /// |
| 549 | /// The stabilized version of this intrinsic is available on the |
| 550 | /// [`atomic`] signed integer types via the `fetch_max` method by passing |
| 551 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_max`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 552 | pub fn atomic_max_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 553 | /// Maximum with the current value using a signed comparison. |
| 554 | /// |
| 555 | /// The stabilized version of this intrinsic is available on the |
| 556 | /// [`atomic`] signed integer types via the `fetch_max` method by passing |
| 557 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_max`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 558 | pub fn atomic_max_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 559 | /// Maximum with the current value using a signed comparison. |
| 560 | /// |
| 561 | /// The stabilized version of this intrinsic is available on the |
| 562 | /// [`atomic`] signed integer types via the `fetch_max` method by passing |
| 563 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_max`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 564 | pub fn atomic_max_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 565 | /// Maximum with the current value using a signed comparison. |
| 566 | /// |
| 567 | /// The stabilized version of this intrinsic is available on the |
| 568 | /// [`atomic`] signed integer types via the `fetch_max` method by passing |
| 569 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_max`]. |
| 570 | pub fn atomic_max_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 571 | /// Maximum with the current value. |
| 572 | /// |
| 573 | /// The stabilized version of this intrinsic is available on the |
| 574 | /// [`atomic`] signed integer types via the `fetch_max` method by passing |
| 575 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_max`]. |
| 576 | pub fn atomic_max_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 577 | |
| 578 | /// Minimum with the current value using a signed comparison. |
| 579 | /// |
| 580 | /// The stabilized version of this intrinsic is available on the |
| 581 | /// [`atomic`] signed integer types via the `fetch_min` method by passing |
| 582 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicI32::fetch_min`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 583 | pub fn atomic_min_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 584 | /// Minimum with the current value using a signed comparison. |
| 585 | /// |
| 586 | /// The stabilized version of this intrinsic is available on the |
| 587 | /// [`atomic`] signed integer types via the `fetch_min` method by passing |
| 588 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicI32::fetch_min`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 589 | pub fn atomic_min_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 590 | /// Minimum with the current value using a signed comparison. |
| 591 | /// |
| 592 | /// The stabilized version of this intrinsic is available on the |
| 593 | /// [`atomic`] signed integer types via the `fetch_min` method by passing |
| 594 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicI32::fetch_min`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 595 | pub fn atomic_min_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 596 | /// Minimum with the current value using a signed comparison. |
| 597 | /// |
| 598 | /// The stabilized version of this intrinsic is available on the |
| 599 | /// [`atomic`] signed integer types via the `fetch_min` method by passing |
| 600 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicI32::fetch_min`]. |
| 601 | pub fn atomic_min_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 602 | /// Minimum with the current value using a signed comparison. |
| 603 | /// |
| 604 | /// The stabilized version of this intrinsic is available on the |
| 605 | /// [`atomic`] signed integer types via the `fetch_min` method by passing |
| 606 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicI32::fetch_min`]. |
| 607 | pub fn atomic_min_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 608 | |
| 609 | /// Minimum with the current value using an unsigned comparison. |
| 610 | /// |
| 611 | /// The stabilized version of this intrinsic is available on the |
| 612 | /// [`atomic`] unsigned integer types via the `fetch_min` method by passing |
| 613 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_min`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 614 | pub fn atomic_umin_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 615 | /// Minimum with the current value using an unsigned comparison. |
| 616 | /// |
| 617 | /// The stabilized version of this intrinsic is available on the |
| 618 | /// [`atomic`] unsigned integer types via the `fetch_min` method by passing |
| 619 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_min`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 620 | pub fn atomic_umin_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 621 | /// Minimum with the current value using an unsigned comparison. |
| 622 | /// |
| 623 | /// The stabilized version of this intrinsic is available on the |
| 624 | /// [`atomic`] unsigned integer types via the `fetch_min` method by passing |
| 625 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_min`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 626 | pub fn atomic_umin_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 627 | /// Minimum with the current value using an unsigned comparison. |
| 628 | /// |
| 629 | /// The stabilized version of this intrinsic is available on the |
| 630 | /// [`atomic`] unsigned integer types via the `fetch_min` method by passing |
| 631 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_min`]. |
| 632 | pub fn atomic_umin_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 633 | /// Minimum with the current value using an unsigned comparison. |
| 634 | /// |
| 635 | /// The stabilized version of this intrinsic is available on the |
| 636 | /// [`atomic`] unsigned integer types via the `fetch_min` method by passing |
| 637 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_min`]. |
| 638 | pub fn atomic_umin_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 639 | |
| 640 | /// Maximum with the current value using an unsigned comparison. |
| 641 | /// |
| 642 | /// The stabilized version of this intrinsic is available on the |
| 643 | /// [`atomic`] unsigned integer types via the `fetch_max` method by passing |
| 644 | /// [`Ordering::SeqCst`] as the `order`. For example, [`AtomicU32::fetch_max`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 645 | pub fn atomic_umax_seqcst<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 646 | /// Maximum with the current value using an unsigned comparison. |
| 647 | /// |
| 648 | /// The stabilized version of this intrinsic is available on the |
| 649 | /// [`atomic`] unsigned integer types via the `fetch_max` method by passing |
| 650 | /// [`Ordering::Acquire`] as the `order`. For example, [`AtomicU32::fetch_max`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 651 | pub fn atomic_umax_acquire<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 652 | /// Maximum with the current value using an unsigned comparison. |
| 653 | /// |
| 654 | /// The stabilized version of this intrinsic is available on the |
| 655 | /// [`atomic`] unsigned integer types via the `fetch_max` method by passing |
| 656 | /// [`Ordering::Release`] as the `order`. For example, [`AtomicU32::fetch_max`]. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 657 | pub fn atomic_umax_release<T: Copy>(dst: *mut T, src: T) -> T; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 658 | /// Maximum with the current value using an unsigned comparison. |
| 659 | /// |
| 660 | /// The stabilized version of this intrinsic is available on the |
| 661 | /// [`atomic`] unsigned integer types via the `fetch_max` method by passing |
| 662 | /// [`Ordering::AcqRel`] as the `order`. For example, [`AtomicU32::fetch_max`]. |
| 663 | pub fn atomic_umax_acqrel<T: Copy>(dst: *mut T, src: T) -> T; |
| 664 | /// Maximum with the current value using an unsigned comparison. |
| 665 | /// |
| 666 | /// The stabilized version of this intrinsic is available on the |
| 667 | /// [`atomic`] unsigned integer types via the `fetch_max` method by passing |
| 668 | /// [`Ordering::Relaxed`] as the `order`. For example, [`AtomicU32::fetch_max`]. |
| 669 | pub fn atomic_umax_relaxed<T: Copy>(dst: *mut T, src: T) -> T; |
| 670 | |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 671 | /// An atomic fence. |
| 672 | /// |
| 673 | /// The stabilized version of this intrinsic is available in |
| 674 | /// [`atomic::fence`] by passing [`Ordering::SeqCst`] |
| 675 | /// as the `order`. |
| 676 | pub fn atomic_fence_seqcst(); |
| 677 | /// An atomic fence. |
| 678 | /// |
| 679 | /// The stabilized version of this intrinsic is available in |
| 680 | /// [`atomic::fence`] by passing [`Ordering::Acquire`] |
| 681 | /// as the `order`. |
| 682 | pub fn atomic_fence_acquire(); |
| 683 | /// An atomic fence. |
| 684 | /// |
| 685 | /// The stabilized version of this intrinsic is available in |
| 686 | /// [`atomic::fence`] by passing [`Ordering::Release`] |
| 687 | /// as the `order`. |
| 688 | pub fn atomic_fence_release(); |
| 689 | /// An atomic fence. |
| 690 | /// |
| 691 | /// The stabilized version of this intrinsic is available in |
| 692 | /// [`atomic::fence`] by passing [`Ordering::AcqRel`] |
| 693 | /// as the `order`. |
| 694 | pub fn atomic_fence_acqrel(); |
| 695 | |
| 696 | /// A compiler-only memory barrier. |
| 697 | /// |
| 698 | /// Memory accesses will never be reordered across this barrier by the |
| 699 | /// compiler, but no instructions will be emitted for it. This is |
| 700 | /// appropriate for operations on the same thread that may be preempted, |
| 701 | /// such as when interacting with signal handlers. |
| 702 | /// |
| 703 | /// The stabilized version of this intrinsic is available in |
| 704 | /// [`atomic::compiler_fence`] by passing [`Ordering::SeqCst`] |
| 705 | /// as the `order`. |
| 706 | pub fn atomic_singlethreadfence_seqcst(); |
| 707 | /// A compiler-only memory barrier. |
| 708 | /// |
| 709 | /// Memory accesses will never be reordered across this barrier by the |
| 710 | /// compiler, but no instructions will be emitted for it. This is |
| 711 | /// appropriate for operations on the same thread that may be preempted, |
| 712 | /// such as when interacting with signal handlers. |
| 713 | /// |
| 714 | /// The stabilized version of this intrinsic is available in |
| 715 | /// [`atomic::compiler_fence`] by passing [`Ordering::Acquire`] |
| 716 | /// as the `order`. |
| 717 | pub fn atomic_singlethreadfence_acquire(); |
| 718 | /// A compiler-only memory barrier. |
| 719 | /// |
| 720 | /// Memory accesses will never be reordered across this barrier by the |
| 721 | /// compiler, but no instructions will be emitted for it. This is |
| 722 | /// appropriate for operations on the same thread that may be preempted, |
| 723 | /// such as when interacting with signal handlers. |
| 724 | /// |
| 725 | /// The stabilized version of this intrinsic is available in |
| 726 | /// [`atomic::compiler_fence`] by passing [`Ordering::Release`] |
| 727 | /// as the `order`. |
| 728 | pub fn atomic_singlethreadfence_release(); |
| 729 | /// A compiler-only memory barrier. |
| 730 | /// |
| 731 | /// Memory accesses will never be reordered across this barrier by the |
| 732 | /// compiler, but no instructions will be emitted for it. This is |
| 733 | /// appropriate for operations on the same thread that may be preempted, |
| 734 | /// such as when interacting with signal handlers. |
| 735 | /// |
| 736 | /// The stabilized version of this intrinsic is available in |
| 737 | /// [`atomic::compiler_fence`] by passing [`Ordering::AcqRel`] |
| 738 | /// as the `order`. |
| 739 | pub fn atomic_singlethreadfence_acqrel(); |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 740 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 741 | /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction |
| 742 | /// if supported; otherwise, it is a no-op. |
| 743 | /// Prefetches have no effect on the behavior of the program but can change its performance |
| 744 | /// characteristics. |
| 745 | /// |
| 746 | /// The `locality` argument must be a constant integer and is a temporal locality specifier |
| 747 | /// ranging from (0) - no locality, to (3) - extremely local keep in cache. |
| 748 | /// |
| 749 | /// This intrinsic does not have a stable counterpart. |
| 750 | pub fn prefetch_read_data<T>(data: *const T, locality: i32); |
| 751 | /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction |
| 752 | /// if supported; otherwise, it is a no-op. |
| 753 | /// Prefetches have no effect on the behavior of the program but can change its performance |
| 754 | /// characteristics. |
| 755 | /// |
| 756 | /// The `locality` argument must be a constant integer and is a temporal locality specifier |
| 757 | /// ranging from (0) - no locality, to (3) - extremely local keep in cache. |
| 758 | /// |
| 759 | /// This intrinsic does not have a stable counterpart. |
| 760 | pub fn prefetch_write_data<T>(data: *const T, locality: i32); |
| 761 | /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction |
| 762 | /// if supported; otherwise, it is a no-op. |
| 763 | /// Prefetches have no effect on the behavior of the program but can change its performance |
| 764 | /// characteristics. |
| 765 | /// |
| 766 | /// The `locality` argument must be a constant integer and is a temporal locality specifier |
| 767 | /// ranging from (0) - no locality, to (3) - extremely local keep in cache. |
| 768 | /// |
| 769 | /// This intrinsic does not have a stable counterpart. |
| 770 | pub fn prefetch_read_instruction<T>(data: *const T, locality: i32); |
| 771 | /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction |
| 772 | /// if supported; otherwise, it is a no-op. |
| 773 | /// Prefetches have no effect on the behavior of the program but can change its performance |
| 774 | /// characteristics. |
| 775 | /// |
| 776 | /// The `locality` argument must be a constant integer and is a temporal locality specifier |
| 777 | /// ranging from (0) - no locality, to (3) - extremely local keep in cache. |
| 778 | /// |
| 779 | /// This intrinsic does not have a stable counterpart. |
| 780 | pub fn prefetch_write_instruction<T>(data: *const T, locality: i32); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 781 | |
| 782 | /// Magic intrinsic that derives its meaning from attributes |
| 783 | /// attached to the function. |
| 784 | /// |
| 785 | /// For example, dataflow uses this to inject static assertions so |
| 786 | /// that `rustc_peek(potentially_uninitialized)` would actually |
| 787 | /// double-check that dataflow did indeed compute that it is |
| 788 | /// uninitialized at that point in the control flow. |
| 789 | /// |
| 790 | /// This intrinsic should not be used outside of the compiler. |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 791 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 792 | pub fn rustc_peek<T>(_: T) -> T; |
| 793 | |
| 794 | /// Aborts the execution of the process. |
| 795 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 796 | /// Note that, unlike most intrinsics, this is safe to call; |
| 797 | /// it does not require an `unsafe` block. |
| 798 | /// Therefore, implementations must not require the user to uphold |
| 799 | /// any safety invariants. |
| 800 | /// |
| 801 | /// [`std::process::abort`](../../std/process/fn.abort.html) is to be preferred if possible, |
| 802 | /// as its behavior is more user-friendly and more stable. |
| 803 | /// |
| 804 | /// The current implementation of `intrinsics::abort` is to invoke an invalid instruction, |
| 805 | /// on most platforms. |
| 806 | /// On Unix, the |
| 807 | /// process will probably terminate with a signal like `SIGABRT`, `SIGILL`, `SIGTRAP`, `SIGSEGV` or |
| 808 | /// `SIGBUS`. The precise behaviour is not guaranteed and not stable. |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 809 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 810 | pub fn abort() -> !; |
| 811 | |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 812 | /// Informs the optimizer that this point in the code is not reachable, |
| 813 | /// enabling further optimizations. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 814 | /// |
| 815 | /// N.B., this is very different from the `unreachable!()` macro: Unlike the |
| 816 | /// macro, which panics when it is executed, it is *undefined behavior* to |
| 817 | /// reach code marked with this function. |
| 818 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 819 | /// The stabilized version of this intrinsic is [`core::hint::unreachable_unchecked`]. |
Chris Wailes | a153842 | 2021-12-02 10:37:12 -0800 | [diff] [blame] | 820 | #[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 821 | pub fn unreachable() -> !; |
| 822 | |
| 823 | /// Informs the optimizer that a condition is always true. |
| 824 | /// If the condition is false, the behavior is undefined. |
| 825 | /// |
| 826 | /// No code is generated for this intrinsic, but the optimizer will try |
| 827 | /// to preserve it (and its condition) between passes, which may interfere |
| 828 | /// with optimization of surrounding code and reduce performance. It should |
| 829 | /// not be used if the invariant can be discovered by the optimizer on its |
| 830 | /// own, or if it does not enable any significant optimizations. |
| 831 | /// |
| 832 | /// This intrinsic does not have a stable counterpart. |
Thiébaud Weksteen | 3b664ca | 2020-11-26 14:41:59 +0100 | [diff] [blame] | 833 | #[rustc_const_unstable(feature = "const_assume", issue = "76972")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 834 | pub fn assume(b: bool); |
| 835 | |
| 836 | /// Hints to the compiler that branch condition is likely to be true. |
| 837 | /// Returns the value passed to it. |
| 838 | /// |
| 839 | /// Any use other than with `if` statements will probably not have an effect. |
| 840 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 841 | /// Note that, unlike most intrinsics, this is safe to call; |
| 842 | /// it does not require an `unsafe` block. |
| 843 | /// Therefore, implementations must not require the user to uphold |
| 844 | /// any safety invariants. |
| 845 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 846 | /// This intrinsic does not have a stable counterpart. |
| 847 | #[rustc_const_unstable(feature = "const_likely", issue = "none")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 848 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 849 | pub fn likely(b: bool) -> bool; |
| 850 | |
| 851 | /// Hints to the compiler that branch condition is likely to be false. |
| 852 | /// Returns the value passed to it. |
| 853 | /// |
| 854 | /// Any use other than with `if` statements will probably not have an effect. |
| 855 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 856 | /// Note that, unlike most intrinsics, this is safe to call; |
| 857 | /// it does not require an `unsafe` block. |
| 858 | /// Therefore, implementations must not require the user to uphold |
| 859 | /// any safety invariants. |
| 860 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 861 | /// This intrinsic does not have a stable counterpart. |
| 862 | #[rustc_const_unstable(feature = "const_likely", issue = "none")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 863 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 864 | pub fn unlikely(b: bool) -> bool; |
| 865 | |
| 866 | /// Executes a breakpoint trap, for inspection by a debugger. |
| 867 | /// |
| 868 | /// This intrinsic does not have a stable counterpart. |
| 869 | pub fn breakpoint(); |
| 870 | |
| 871 | /// The size of a type in bytes. |
| 872 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 873 | /// Note that, unlike most intrinsics, this is safe to call; |
| 874 | /// it does not require an `unsafe` block. |
| 875 | /// Therefore, implementations must not require the user to uphold |
| 876 | /// any safety invariants. |
| 877 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 878 | /// More specifically, this is the offset in bytes between successive |
| 879 | /// items of the same type, including alignment padding. |
| 880 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 881 | /// The stabilized version of this intrinsic is [`core::mem::size_of`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 882 | #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 883 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 884 | pub fn size_of<T>() -> usize; |
| 885 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 886 | /// The minimum alignment of a type. |
| 887 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 888 | /// Note that, unlike most intrinsics, this is safe to call; |
| 889 | /// it does not require an `unsafe` block. |
| 890 | /// Therefore, implementations must not require the user to uphold |
| 891 | /// any safety invariants. |
| 892 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 893 | /// The stabilized version of this intrinsic is [`core::mem::align_of`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 894 | #[rustc_const_stable(feature = "const_min_align_of", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 895 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 896 | pub fn min_align_of<T>() -> usize; |
| 897 | /// The preferred alignment of a type. |
| 898 | /// |
| 899 | /// This intrinsic does not have a stable counterpart. |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 900 | /// It's "tracking issue" is [#91971](https://github.com/rust-lang/rust/issues/91971). |
| 901 | #[rustc_const_unstable(feature = "const_pref_align_of", issue = "91971")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 902 | pub fn pref_align_of<T>() -> usize; |
| 903 | |
| 904 | /// The size of the referenced value in bytes. |
| 905 | /// |
Thiébaud Weksteen | 5bd94c1 | 2021-01-06 15:18:42 +0100 | [diff] [blame] | 906 | /// The stabilized version of this intrinsic is [`mem::size_of_val`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 907 | #[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")] |
| 908 | pub fn size_of_val<T: ?Sized>(_: *const T) -> usize; |
| 909 | /// The required alignment of the referenced value. |
| 910 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 911 | /// The stabilized version of this intrinsic is [`core::mem::align_of_val`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 912 | #[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")] |
| 913 | pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize; |
| 914 | |
| 915 | /// Gets a static string slice containing the name of a type. |
| 916 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 917 | /// Note that, unlike most intrinsics, this is safe to call; |
| 918 | /// it does not require an `unsafe` block. |
| 919 | /// Therefore, implementations must not require the user to uphold |
| 920 | /// any safety invariants. |
| 921 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 922 | /// The stabilized version of this intrinsic is [`core::any::type_name`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 923 | #[rustc_const_unstable(feature = "const_type_name", issue = "63084")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 924 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 925 | pub fn type_name<T: ?Sized>() -> &'static str; |
| 926 | |
| 927 | /// Gets an identifier which is globally unique to the specified type. This |
| 928 | /// function will return the same value for a type regardless of whichever |
| 929 | /// crate it is invoked in. |
| 930 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 931 | /// Note that, unlike most intrinsics, this is safe to call; |
| 932 | /// it does not require an `unsafe` block. |
| 933 | /// Therefore, implementations must not require the user to uphold |
| 934 | /// any safety invariants. |
| 935 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 936 | /// The stabilized version of this intrinsic is [`core::any::TypeId::of`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 937 | #[rustc_const_unstable(feature = "const_type_id", issue = "77125")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 938 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 939 | pub fn type_id<T: ?Sized + 'static>() -> u64; |
| 940 | |
| 941 | /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited: |
| 942 | /// This will statically either panic, or do nothing. |
| 943 | /// |
| 944 | /// This intrinsic does not have a stable counterpart. |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 945 | #[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 946 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 947 | pub fn assert_inhabited<T>(); |
| 948 | |
| 949 | /// A guard for unsafe functions that cannot ever be executed if `T` does not permit |
| 950 | /// zero-initialization: This will statically either panic, or do nothing. |
| 951 | /// |
| 952 | /// This intrinsic does not have a stable counterpart. |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 953 | #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 954 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 955 | pub fn assert_zero_valid<T>(); |
| 956 | |
| 957 | /// A guard for unsafe functions that cannot ever be executed if `T` has invalid |
| 958 | /// bit patterns: This will statically either panic, or do nothing. |
| 959 | /// |
| 960 | /// This intrinsic does not have a stable counterpart. |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 961 | #[rustc_const_unstable(feature = "const_assert_type2", issue = "none")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 962 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 963 | pub fn assert_uninit_valid<T>(); |
| 964 | |
| 965 | /// Gets a reference to a static `Location` indicating where it was called. |
| 966 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 967 | /// Note that, unlike most intrinsics, this is safe to call; |
| 968 | /// it does not require an `unsafe` block. |
| 969 | /// Therefore, implementations must not require the user to uphold |
| 970 | /// any safety invariants. |
| 971 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 972 | /// Consider using [`core::panic::Location::caller`] instead. |
Thiébaud Weksteen | 3b664ca | 2020-11-26 14:41:59 +0100 | [diff] [blame] | 973 | #[rustc_const_unstable(feature = "const_caller_location", issue = "76156")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 974 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 975 | pub fn caller_location() -> &'static crate::panic::Location<'static>; |
| 976 | |
| 977 | /// Moves a value out of scope without running drop glue. |
| 978 | /// |
| 979 | /// This exists solely for [`mem::forget_unsized`]; normal `forget` uses |
| 980 | /// `ManuallyDrop` instead. |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 981 | /// |
| 982 | /// Note that, unlike most intrinsics, this is safe to call; |
| 983 | /// it does not require an `unsafe` block. |
| 984 | /// Therefore, implementations must not require the user to uphold |
| 985 | /// any safety invariants. |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 986 | #[rustc_const_unstable(feature = "const_intrinsic_forget", issue = "none")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 987 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 988 | pub fn forget<T: ?Sized>(_: T); |
| 989 | |
| 990 | /// Reinterprets the bits of a value of one type as another type. |
| 991 | /// |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 992 | /// Both types must have the same size. Compilation will fail if this is not guaranteed. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 993 | /// |
| 994 | /// `transmute` is semantically equivalent to a bitwise move of one type |
| 995 | /// into another. It copies the bits from the source value into the |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 996 | /// destination value, then forgets the original. Note that source and destination |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 997 | /// are passed by-value, which means if `Src` or `Dst` contain padding, that padding |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 998 | /// is *not* guaranteed to be preserved by `transmute`. |
| 999 | /// |
| 1000 | /// Both the argument and the result must be [valid](../../nomicon/what-unsafe-does.html) at |
| 1001 | /// their given type. Violating this condition leads to [undefined behavior][ub]. The compiler |
| 1002 | /// will generate code *assuming that you, the programmer, ensure that there will never be |
| 1003 | /// undefined behavior*. It is therefore your responsibility to guarantee that every value |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1004 | /// passed to `transmute` is valid at both types `Src` and `Dst`. Failing to uphold this condition |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 1005 | /// may lead to unexpected and unstable compilation results. This makes `transmute` **incredibly |
| 1006 | /// unsafe**. `transmute` should be the absolute last resort. |
| 1007 | /// |
| 1008 | /// Transmuting pointers to integers in a `const` context is [undefined behavior][ub]. |
| 1009 | /// Any attempt to use the resulting value for integer operations will abort const-evaluation. |
| 1010 | /// (And even outside `const`, such transmutation is touching on many unspecified aspects of the |
| 1011 | /// Rust memory model and should be avoided. See below for alternatives.) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1012 | /// |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 1013 | /// Because `transmute` is a by-value operation, alignment of the *transmuted values |
| 1014 | /// themselves* is not a concern. As with any other function, the compiler already ensures |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1015 | /// both `Src` and `Dst` are properly aligned. However, when transmuting values that *point |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 1016 | /// elsewhere* (such as pointers, references, boxes…), the caller has to ensure proper |
| 1017 | /// alignment of the pointed-to values. |
| 1018 | /// |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 1019 | /// The [nomicon](../../nomicon/transmutes.html) has additional documentation. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1020 | /// |
| 1021 | /// [ub]: ../../reference/behavior-considered-undefined.html |
| 1022 | /// |
| 1023 | /// # Examples |
| 1024 | /// |
| 1025 | /// There are a few things that `transmute` is really useful for. |
| 1026 | /// |
| 1027 | /// Turning a pointer into a function pointer. This is *not* portable to |
| 1028 | /// machines where function pointers and data pointers have different sizes. |
| 1029 | /// |
| 1030 | /// ``` |
| 1031 | /// fn foo() -> i32 { |
| 1032 | /// 0 |
| 1033 | /// } |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 1034 | /// // Crucially, we `as`-cast to a raw pointer before `transmute`ing to a function pointer. |
| 1035 | /// // This avoids an integer-to-pointer `transmute`, which can be problematic. |
| 1036 | /// // Transmuting between raw pointers and function pointers (i.e., two pointer types) is fine. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1037 | /// let pointer = foo as *const (); |
| 1038 | /// let function = unsafe { |
| 1039 | /// std::mem::transmute::<*const (), fn() -> i32>(pointer) |
| 1040 | /// }; |
| 1041 | /// assert_eq!(function(), 0); |
| 1042 | /// ``` |
| 1043 | /// |
| 1044 | /// Extending a lifetime, or shortening an invariant lifetime. This is |
| 1045 | /// advanced, very unsafe Rust! |
| 1046 | /// |
| 1047 | /// ``` |
| 1048 | /// struct R<'a>(&'a i32); |
| 1049 | /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> { |
| 1050 | /// std::mem::transmute::<R<'b>, R<'static>>(r) |
| 1051 | /// } |
| 1052 | /// |
| 1053 | /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>) |
| 1054 | /// -> &'b mut R<'c> { |
| 1055 | /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r) |
| 1056 | /// } |
| 1057 | /// ``` |
| 1058 | /// |
| 1059 | /// # Alternatives |
| 1060 | /// |
| 1061 | /// Don't despair: many uses of `transmute` can be achieved through other means. |
| 1062 | /// Below are common applications of `transmute` which can be replaced with safer |
| 1063 | /// constructs. |
| 1064 | /// |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 1065 | /// Turning raw bytes (`&[u8]`) into `u32`, `f64`, etc.: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1066 | /// |
| 1067 | /// ``` |
| 1068 | /// let raw_bytes = [0x78, 0x56, 0x34, 0x12]; |
| 1069 | /// |
| 1070 | /// let num = unsafe { |
Thiébaud Weksteen | 3b664ca | 2020-11-26 14:41:59 +0100 | [diff] [blame] | 1071 | /// std::mem::transmute::<[u8; 4], u32>(raw_bytes) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1072 | /// }; |
| 1073 | /// |
| 1074 | /// // use `u32::from_ne_bytes` instead |
| 1075 | /// let num = u32::from_ne_bytes(raw_bytes); |
| 1076 | /// // or use `u32::from_le_bytes` or `u32::from_be_bytes` to specify the endianness |
| 1077 | /// let num = u32::from_le_bytes(raw_bytes); |
| 1078 | /// assert_eq!(num, 0x12345678); |
| 1079 | /// let num = u32::from_be_bytes(raw_bytes); |
| 1080 | /// assert_eq!(num, 0x78563412); |
| 1081 | /// ``` |
| 1082 | /// |
| 1083 | /// Turning a pointer into a `usize`: |
| 1084 | /// |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 1085 | /// ```no_run |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1086 | /// let ptr = &0; |
| 1087 | /// let ptr_num_transmute = unsafe { |
| 1088 | /// std::mem::transmute::<&i32, usize>(ptr) |
| 1089 | /// }; |
| 1090 | /// |
| 1091 | /// // Use an `as` cast instead |
| 1092 | /// let ptr_num_cast = ptr as *const i32 as usize; |
| 1093 | /// ``` |
| 1094 | /// |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 1095 | /// Note that using `transmute` to turn a pointer to a `usize` is (as noted above) [undefined |
| 1096 | /// behavior][ub] in `const` contexts. Also outside of consts, this operation might not behave |
| 1097 | /// as expected -- this is touching on many unspecified aspects of the Rust memory model. |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 1098 | /// Depending on what the code is doing, the following alternatives are preferable to |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 1099 | /// pointer-to-integer transmutation: |
| 1100 | /// - If the code just wants to store data of arbitrary type in some buffer and needs to pick a |
| 1101 | /// type for that buffer, it can use [`MaybeUninit`][mem::MaybeUninit]. |
| 1102 | /// - If the code actually wants to work on the address the pointer points to, it can use `as` |
| 1103 | /// casts or [`ptr.addr()`][pointer::addr]. |
| 1104 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1105 | /// Turning a `*mut T` into an `&mut T`: |
| 1106 | /// |
| 1107 | /// ``` |
| 1108 | /// let ptr: *mut i32 = &mut 0; |
| 1109 | /// let ref_transmuted = unsafe { |
| 1110 | /// std::mem::transmute::<*mut i32, &mut i32>(ptr) |
| 1111 | /// }; |
| 1112 | /// |
| 1113 | /// // Use a reborrow instead |
| 1114 | /// let ref_casted = unsafe { &mut *ptr }; |
| 1115 | /// ``` |
| 1116 | /// |
| 1117 | /// Turning an `&mut T` into an `&mut U`: |
| 1118 | /// |
| 1119 | /// ``` |
| 1120 | /// let ptr = &mut 0; |
| 1121 | /// let val_transmuted = unsafe { |
| 1122 | /// std::mem::transmute::<&mut i32, &mut u32>(ptr) |
| 1123 | /// }; |
| 1124 | /// |
| 1125 | /// // Now, put together `as` and reborrowing - note the chaining of `as` |
| 1126 | /// // `as` is not transitive |
| 1127 | /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) }; |
| 1128 | /// ``` |
| 1129 | /// |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 1130 | /// Turning an `&str` into a `&[u8]`: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1131 | /// |
| 1132 | /// ``` |
| 1133 | /// // this is not a good way to do this. |
| 1134 | /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") }; |
| 1135 | /// assert_eq!(slice, &[82, 117, 115, 116]); |
| 1136 | /// |
| 1137 | /// // You could use `str::as_bytes` |
| 1138 | /// let slice = "Rust".as_bytes(); |
| 1139 | /// assert_eq!(slice, &[82, 117, 115, 116]); |
| 1140 | /// |
| 1141 | /// // Or, just use a byte string, if you have control over the string |
| 1142 | /// // literal |
| 1143 | /// assert_eq!(b"Rust", &[82, 117, 115, 116]); |
| 1144 | /// ``` |
| 1145 | /// |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 1146 | /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`. |
| 1147 | /// |
| 1148 | /// To transmute the inner type of the contents of a container, you must make sure to not |
| 1149 | /// violate any of the container's invariants. For `Vec`, this means that both the size |
| 1150 | /// *and alignment* of the inner types have to match. Other containers might rely on the |
| 1151 | /// size of the type, alignment, or even the `TypeId`, in which case transmuting wouldn't |
| 1152 | /// be possible at all without violating the container invariants. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1153 | /// |
| 1154 | /// ``` |
| 1155 | /// let store = [0, 1, 2, 3]; |
| 1156 | /// let v_orig = store.iter().collect::<Vec<&i32>>(); |
| 1157 | /// |
| 1158 | /// // clone the vector as we will reuse them later |
| 1159 | /// let v_clone = v_orig.clone(); |
| 1160 | /// |
| 1161 | /// // Using transmute: this relies on the unspecified data layout of `Vec`, which is a |
| 1162 | /// // bad idea and could cause Undefined Behavior. |
| 1163 | /// // However, it is no-copy. |
| 1164 | /// let v_transmuted = unsafe { |
| 1165 | /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(v_clone) |
| 1166 | /// }; |
| 1167 | /// |
| 1168 | /// let v_clone = v_orig.clone(); |
| 1169 | /// |
| 1170 | /// // This is the suggested, safe way. |
| 1171 | /// // It does copy the entire vector, though, into a new array. |
| 1172 | /// let v_collected = v_clone.into_iter() |
| 1173 | /// .map(Some) |
| 1174 | /// .collect::<Vec<Option<&i32>>>(); |
| 1175 | /// |
| 1176 | /// let v_clone = v_orig.clone(); |
| 1177 | /// |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 1178 | /// // This is the proper no-copy, unsafe way of "transmuting" a `Vec`, without relying on the |
| 1179 | /// // data layout. Instead of literally calling `transmute`, we perform a pointer cast, but |
| 1180 | /// // in terms of converting the original inner type (`&i32`) to the new one (`Option<&i32>`), |
| 1181 | /// // this has all the same caveats. Besides the information provided above, also consult the |
| 1182 | /// // [`from_raw_parts`] documentation. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1183 | /// let v_from_raw = unsafe { |
| 1184 | // FIXME Update this when vec_into_raw_parts is stabilized |
| 1185 | /// // Ensure the original vector is not dropped. |
| 1186 | /// let mut v_clone = std::mem::ManuallyDrop::new(v_clone); |
| 1187 | /// Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>, |
| 1188 | /// v_clone.len(), |
| 1189 | /// v_clone.capacity()) |
| 1190 | /// }; |
| 1191 | /// ``` |
| 1192 | /// |
| 1193 | /// [`from_raw_parts`]: ../../std/vec/struct.Vec.html#method.from_raw_parts |
| 1194 | /// |
| 1195 | /// Implementing `split_at_mut`: |
| 1196 | /// |
| 1197 | /// ``` |
| 1198 | /// use std::{slice, mem}; |
| 1199 | /// |
| 1200 | /// // There are multiple ways to do this, and there are multiple problems |
| 1201 | /// // with the following (transmute) way. |
| 1202 | /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize) |
| 1203 | /// -> (&mut [T], &mut [T]) { |
| 1204 | /// let len = slice.len(); |
| 1205 | /// assert!(mid <= len); |
| 1206 | /// unsafe { |
| 1207 | /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice); |
| 1208 | /// // first: transmute is not type safe; all it checks is that T and |
| 1209 | /// // U are of the same size. Second, right here, you have two |
| 1210 | /// // mutable references pointing to the same memory. |
| 1211 | /// (&mut slice[0..mid], &mut slice2[mid..len]) |
| 1212 | /// } |
| 1213 | /// } |
| 1214 | /// |
| 1215 | /// // This gets rid of the type safety problems; `&mut *` will *only* give |
| 1216 | /// // you an `&mut T` from an `&mut T` or `*mut T`. |
| 1217 | /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize) |
| 1218 | /// -> (&mut [T], &mut [T]) { |
| 1219 | /// let len = slice.len(); |
| 1220 | /// assert!(mid <= len); |
| 1221 | /// unsafe { |
| 1222 | /// let slice2 = &mut *(slice as *mut [T]); |
| 1223 | /// // however, you still have two mutable references pointing to |
| 1224 | /// // the same memory. |
| 1225 | /// (&mut slice[0..mid], &mut slice2[mid..len]) |
| 1226 | /// } |
| 1227 | /// } |
| 1228 | /// |
| 1229 | /// // This is how the standard library does it. This is the best method, if |
| 1230 | /// // you need to do something like this |
| 1231 | /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize) |
| 1232 | /// -> (&mut [T], &mut [T]) { |
| 1233 | /// let len = slice.len(); |
| 1234 | /// assert!(mid <= len); |
| 1235 | /// unsafe { |
| 1236 | /// let ptr = slice.as_mut_ptr(); |
| 1237 | /// // This now has three mutable references pointing at the same |
| 1238 | /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1. |
| 1239 | /// // `slice` is never used after `let ptr = ...`, and so one can |
| 1240 | /// // treat it as "dead", and therefore, you only have two real |
| 1241 | /// // mutable slices. |
| 1242 | /// (slice::from_raw_parts_mut(ptr, mid), |
| 1243 | /// slice::from_raw_parts_mut(ptr.add(mid), len - mid)) |
| 1244 | /// } |
| 1245 | /// } |
| 1246 | /// ``` |
| 1247 | #[stable(feature = "rust1", since = "1.0.0")] |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 1248 | #[rustc_allowed_through_unstable_modules] |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 1249 | #[rustc_const_stable(feature = "const_transmute", since = "1.56.0")] |
Thiébaud Weksteen | 5bd94c1 | 2021-01-06 15:18:42 +0100 | [diff] [blame] | 1250 | #[rustc_diagnostic_item = "transmute"] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1251 | pub fn transmute<Src, Dst>(src: Src) -> Dst; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1252 | |
| 1253 | /// Returns `true` if the actual type given as `T` requires drop |
| 1254 | /// glue; returns `false` if the actual type provided for `T` |
| 1255 | /// implements `Copy`. |
| 1256 | /// |
| 1257 | /// If the actual type neither requires drop glue nor implements |
| 1258 | /// `Copy`, then the return value of this function is unspecified. |
| 1259 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1260 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1261 | /// it does not require an `unsafe` block. |
| 1262 | /// Therefore, implementations must not require the user to uphold |
| 1263 | /// any safety invariants. |
| 1264 | /// |
Thiébaud Weksteen | 5bd94c1 | 2021-01-06 15:18:42 +0100 | [diff] [blame] | 1265 | /// The stabilized version of this intrinsic is [`mem::needs_drop`](crate::mem::needs_drop). |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1266 | #[rustc_const_stable(feature = "const_needs_drop", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1267 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 1268 | pub fn needs_drop<T: ?Sized>() -> bool; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1269 | |
| 1270 | /// Calculates the offset from a pointer. |
| 1271 | /// |
| 1272 | /// This is implemented as an intrinsic to avoid converting to and from an |
| 1273 | /// integer, since the conversion would throw away aliasing information. |
| 1274 | /// |
| 1275 | /// # Safety |
| 1276 | /// |
| 1277 | /// Both the starting and resulting pointer must be either in bounds or one |
| 1278 | /// byte past the end of an allocated object. If either pointer is out of |
| 1279 | /// bounds or arithmetic overflow occurs then any further use of the |
| 1280 | /// returned value will result in undefined behavior. |
| 1281 | /// |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 1282 | /// The stabilized version of this intrinsic is [`pointer::offset`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1283 | #[must_use = "returns a new pointer rather than modifying its argument"] |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 1284 | #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1285 | pub fn offset<T>(dst: *const T, offset: isize) -> *const T; |
| 1286 | |
| 1287 | /// Calculates the offset from a pointer, potentially wrapping. |
| 1288 | /// |
| 1289 | /// This is implemented as an intrinsic to avoid converting to and from an |
| 1290 | /// integer, since the conversion inhibits certain optimizations. |
| 1291 | /// |
| 1292 | /// # Safety |
| 1293 | /// |
| 1294 | /// Unlike the `offset` intrinsic, this intrinsic does not restrict the |
| 1295 | /// resulting pointer to point into or one byte past the end of an allocated |
| 1296 | /// object, and it wraps with two's complement arithmetic. The resulting |
| 1297 | /// value is not necessarily valid to be used to actually access memory. |
| 1298 | /// |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 1299 | /// The stabilized version of this intrinsic is [`pointer::wrapping_offset`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1300 | #[must_use = "returns a new pointer rather than modifying its argument"] |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 1301 | #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1302 | pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T; |
| 1303 | |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 1304 | /// Masks out bits of the pointer according to a mask. |
| 1305 | /// |
| 1306 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1307 | /// it does not require an `unsafe` block. |
| 1308 | /// Therefore, implementations must not require the user to uphold |
| 1309 | /// any safety invariants. |
| 1310 | /// |
| 1311 | /// Consider using [`pointer::mask`] instead. |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1312 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 1313 | pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T; |
| 1314 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1315 | /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with |
| 1316 | /// a size of `count` * `size_of::<T>()` and an alignment of |
| 1317 | /// `min_align_of::<T>()` |
| 1318 | /// |
| 1319 | /// The volatile parameter is set to `true`, so it will not be optimized out |
| 1320 | /// unless size is equal to zero. |
| 1321 | /// |
| 1322 | /// This intrinsic does not have a stable counterpart. |
| 1323 | pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: usize); |
| 1324 | /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 1325 | /// a size of `count * size_of::<T>()` and an alignment of |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1326 | /// `min_align_of::<T>()` |
| 1327 | /// |
| 1328 | /// The volatile parameter is set to `true`, so it will not be optimized out |
| 1329 | /// unless size is equal to zero. |
| 1330 | /// |
| 1331 | /// This intrinsic does not have a stable counterpart. |
| 1332 | pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize); |
| 1333 | /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 1334 | /// size of `count * size_of::<T>()` and an alignment of |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1335 | /// `min_align_of::<T>()`. |
| 1336 | /// |
| 1337 | /// The volatile parameter is set to `true`, so it will not be optimized out |
| 1338 | /// unless size is equal to zero. |
| 1339 | /// |
| 1340 | /// This intrinsic does not have a stable counterpart. |
| 1341 | pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize); |
| 1342 | |
| 1343 | /// Performs a volatile load from the `src` pointer. |
| 1344 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 1345 | /// The stabilized version of this intrinsic is [`core::ptr::read_volatile`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1346 | pub fn volatile_load<T>(src: *const T) -> T; |
| 1347 | /// Performs a volatile store to the `dst` pointer. |
| 1348 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 1349 | /// The stabilized version of this intrinsic is [`core::ptr::write_volatile`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1350 | pub fn volatile_store<T>(dst: *mut T, val: T); |
| 1351 | |
| 1352 | /// Performs a volatile load from the `src` pointer |
| 1353 | /// The pointer is not required to be aligned. |
| 1354 | /// |
| 1355 | /// This intrinsic does not have a stable counterpart. |
| 1356 | pub fn unaligned_volatile_load<T>(src: *const T) -> T; |
| 1357 | /// Performs a volatile store to the `dst` pointer. |
| 1358 | /// The pointer is not required to be aligned. |
| 1359 | /// |
| 1360 | /// This intrinsic does not have a stable counterpart. |
| 1361 | pub fn unaligned_volatile_store<T>(dst: *mut T, val: T); |
| 1362 | |
| 1363 | /// Returns the square root of an `f32` |
| 1364 | /// |
| 1365 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1366 | /// [`f32::sqrt`](../../std/primitive.f32.html#method.sqrt) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1367 | pub fn sqrtf32(x: f32) -> f32; |
| 1368 | /// Returns the square root of an `f64` |
| 1369 | /// |
| 1370 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1371 | /// [`f64::sqrt`](../../std/primitive.f64.html#method.sqrt) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1372 | pub fn sqrtf64(x: f64) -> f64; |
| 1373 | |
| 1374 | /// Raises an `f32` to an integer power. |
| 1375 | /// |
| 1376 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1377 | /// [`f32::powi`](../../std/primitive.f32.html#method.powi) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1378 | pub fn powif32(a: f32, x: i32) -> f32; |
| 1379 | /// Raises an `f64` to an integer power. |
| 1380 | /// |
| 1381 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1382 | /// [`f64::powi`](../../std/primitive.f64.html#method.powi) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1383 | pub fn powif64(a: f64, x: i32) -> f64; |
| 1384 | |
| 1385 | /// Returns the sine of an `f32`. |
| 1386 | /// |
| 1387 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1388 | /// [`f32::sin`](../../std/primitive.f32.html#method.sin) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1389 | pub fn sinf32(x: f32) -> f32; |
| 1390 | /// Returns the sine of an `f64`. |
| 1391 | /// |
| 1392 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1393 | /// [`f64::sin`](../../std/primitive.f64.html#method.sin) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1394 | pub fn sinf64(x: f64) -> f64; |
| 1395 | |
| 1396 | /// Returns the cosine of an `f32`. |
| 1397 | /// |
| 1398 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1399 | /// [`f32::cos`](../../std/primitive.f32.html#method.cos) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1400 | pub fn cosf32(x: f32) -> f32; |
| 1401 | /// Returns the cosine of an `f64`. |
| 1402 | /// |
| 1403 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1404 | /// [`f64::cos`](../../std/primitive.f64.html#method.cos) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1405 | pub fn cosf64(x: f64) -> f64; |
| 1406 | |
| 1407 | /// Raises an `f32` to an `f32` power. |
| 1408 | /// |
| 1409 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1410 | /// [`f32::powf`](../../std/primitive.f32.html#method.powf) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1411 | pub fn powf32(a: f32, x: f32) -> f32; |
| 1412 | /// Raises an `f64` to an `f64` power. |
| 1413 | /// |
| 1414 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1415 | /// [`f64::powf`](../../std/primitive.f64.html#method.powf) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1416 | pub fn powf64(a: f64, x: f64) -> f64; |
| 1417 | |
| 1418 | /// Returns the exponential of an `f32`. |
| 1419 | /// |
| 1420 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1421 | /// [`f32::exp`](../../std/primitive.f32.html#method.exp) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1422 | pub fn expf32(x: f32) -> f32; |
| 1423 | /// Returns the exponential of an `f64`. |
| 1424 | /// |
| 1425 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1426 | /// [`f64::exp`](../../std/primitive.f64.html#method.exp) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1427 | pub fn expf64(x: f64) -> f64; |
| 1428 | |
| 1429 | /// Returns 2 raised to the power of an `f32`. |
| 1430 | /// |
| 1431 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1432 | /// [`f32::exp2`](../../std/primitive.f32.html#method.exp2) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1433 | pub fn exp2f32(x: f32) -> f32; |
| 1434 | /// Returns 2 raised to the power of an `f64`. |
| 1435 | /// |
| 1436 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1437 | /// [`f64::exp2`](../../std/primitive.f64.html#method.exp2) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1438 | pub fn exp2f64(x: f64) -> f64; |
| 1439 | |
| 1440 | /// Returns the natural logarithm of an `f32`. |
| 1441 | /// |
| 1442 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1443 | /// [`f32::ln`](../../std/primitive.f32.html#method.ln) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1444 | pub fn logf32(x: f32) -> f32; |
| 1445 | /// Returns the natural logarithm of an `f64`. |
| 1446 | /// |
| 1447 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1448 | /// [`f64::ln`](../../std/primitive.f64.html#method.ln) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1449 | pub fn logf64(x: f64) -> f64; |
| 1450 | |
| 1451 | /// Returns the base 10 logarithm of an `f32`. |
| 1452 | /// |
| 1453 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1454 | /// [`f32::log10`](../../std/primitive.f32.html#method.log10) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1455 | pub fn log10f32(x: f32) -> f32; |
| 1456 | /// Returns the base 10 logarithm of an `f64`. |
| 1457 | /// |
| 1458 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1459 | /// [`f64::log10`](../../std/primitive.f64.html#method.log10) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1460 | pub fn log10f64(x: f64) -> f64; |
| 1461 | |
| 1462 | /// Returns the base 2 logarithm of an `f32`. |
| 1463 | /// |
| 1464 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1465 | /// [`f32::log2`](../../std/primitive.f32.html#method.log2) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1466 | pub fn log2f32(x: f32) -> f32; |
| 1467 | /// Returns the base 2 logarithm of an `f64`. |
| 1468 | /// |
| 1469 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1470 | /// [`f64::log2`](../../std/primitive.f64.html#method.log2) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1471 | pub fn log2f64(x: f64) -> f64; |
| 1472 | |
| 1473 | /// Returns `a * b + c` for `f32` values. |
| 1474 | /// |
| 1475 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1476 | /// [`f32::mul_add`](../../std/primitive.f32.html#method.mul_add) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1477 | pub fn fmaf32(a: f32, b: f32, c: f32) -> f32; |
| 1478 | /// Returns `a * b + c` for `f64` values. |
| 1479 | /// |
| 1480 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1481 | /// [`f64::mul_add`](../../std/primitive.f64.html#method.mul_add) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1482 | pub fn fmaf64(a: f64, b: f64, c: f64) -> f64; |
| 1483 | |
| 1484 | /// Returns the absolute value of an `f32`. |
| 1485 | /// |
| 1486 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1487 | /// [`f32::abs`](../../std/primitive.f32.html#method.abs) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1488 | pub fn fabsf32(x: f32) -> f32; |
| 1489 | /// Returns the absolute value of an `f64`. |
| 1490 | /// |
| 1491 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1492 | /// [`f64::abs`](../../std/primitive.f64.html#method.abs) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1493 | pub fn fabsf64(x: f64) -> f64; |
| 1494 | |
| 1495 | /// Returns the minimum of two `f32` values. |
| 1496 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1497 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1498 | /// it does not require an `unsafe` block. |
| 1499 | /// Therefore, implementations must not require the user to uphold |
| 1500 | /// any safety invariants. |
| 1501 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1502 | /// The stabilized version of this intrinsic is |
| 1503 | /// [`f32::min`] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1504 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1505 | pub fn minnumf32(x: f32, y: f32) -> f32; |
| 1506 | /// Returns the minimum of two `f64` values. |
| 1507 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1508 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1509 | /// it does not require an `unsafe` block. |
| 1510 | /// Therefore, implementations must not require the user to uphold |
| 1511 | /// any safety invariants. |
| 1512 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1513 | /// The stabilized version of this intrinsic is |
| 1514 | /// [`f64::min`] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1515 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1516 | pub fn minnumf64(x: f64, y: f64) -> f64; |
| 1517 | /// Returns the maximum of two `f32` values. |
| 1518 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1519 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1520 | /// it does not require an `unsafe` block. |
| 1521 | /// Therefore, implementations must not require the user to uphold |
| 1522 | /// any safety invariants. |
| 1523 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1524 | /// The stabilized version of this intrinsic is |
| 1525 | /// [`f32::max`] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1526 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1527 | pub fn maxnumf32(x: f32, y: f32) -> f32; |
| 1528 | /// Returns the maximum of two `f64` values. |
| 1529 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1530 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1531 | /// it does not require an `unsafe` block. |
| 1532 | /// Therefore, implementations must not require the user to uphold |
| 1533 | /// any safety invariants. |
| 1534 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1535 | /// The stabilized version of this intrinsic is |
| 1536 | /// [`f64::max`] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1537 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1538 | pub fn maxnumf64(x: f64, y: f64) -> f64; |
| 1539 | |
| 1540 | /// Copies the sign from `y` to `x` for `f32` values. |
| 1541 | /// |
| 1542 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1543 | /// [`f32::copysign`](../../std/primitive.f32.html#method.copysign) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1544 | pub fn copysignf32(x: f32, y: f32) -> f32; |
| 1545 | /// Copies the sign from `y` to `x` for `f64` values. |
| 1546 | /// |
| 1547 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1548 | /// [`f64::copysign`](../../std/primitive.f64.html#method.copysign) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1549 | pub fn copysignf64(x: f64, y: f64) -> f64; |
| 1550 | |
| 1551 | /// Returns the largest integer less than or equal to an `f32`. |
| 1552 | /// |
| 1553 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1554 | /// [`f32::floor`](../../std/primitive.f32.html#method.floor) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1555 | pub fn floorf32(x: f32) -> f32; |
| 1556 | /// Returns the largest integer less than or equal to an `f64`. |
| 1557 | /// |
| 1558 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1559 | /// [`f64::floor`](../../std/primitive.f64.html#method.floor) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1560 | pub fn floorf64(x: f64) -> f64; |
| 1561 | |
| 1562 | /// Returns the smallest integer greater than or equal to an `f32`. |
| 1563 | /// |
| 1564 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1565 | /// [`f32::ceil`](../../std/primitive.f32.html#method.ceil) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1566 | pub fn ceilf32(x: f32) -> f32; |
| 1567 | /// Returns the smallest integer greater than or equal to an `f64`. |
| 1568 | /// |
| 1569 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1570 | /// [`f64::ceil`](../../std/primitive.f64.html#method.ceil) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1571 | pub fn ceilf64(x: f64) -> f64; |
| 1572 | |
| 1573 | /// Returns the integer part of an `f32`. |
| 1574 | /// |
| 1575 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1576 | /// [`f32::trunc`](../../std/primitive.f32.html#method.trunc) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1577 | pub fn truncf32(x: f32) -> f32; |
| 1578 | /// Returns the integer part of an `f64`. |
| 1579 | /// |
| 1580 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1581 | /// [`f64::trunc`](../../std/primitive.f64.html#method.trunc) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1582 | pub fn truncf64(x: f64) -> f64; |
| 1583 | |
| 1584 | /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception |
| 1585 | /// if the argument is not an integer. |
| 1586 | pub fn rintf32(x: f32) -> f32; |
| 1587 | /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception |
| 1588 | /// if the argument is not an integer. |
| 1589 | pub fn rintf64(x: f64) -> f64; |
| 1590 | |
| 1591 | /// Returns the nearest integer to an `f32`. |
| 1592 | /// |
| 1593 | /// This intrinsic does not have a stable counterpart. |
| 1594 | pub fn nearbyintf32(x: f32) -> f32; |
| 1595 | /// Returns the nearest integer to an `f64`. |
| 1596 | /// |
| 1597 | /// This intrinsic does not have a stable counterpart. |
| 1598 | pub fn nearbyintf64(x: f64) -> f64; |
| 1599 | |
| 1600 | /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero. |
| 1601 | /// |
| 1602 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1603 | /// [`f32::round`](../../std/primitive.f32.html#method.round) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1604 | pub fn roundf32(x: f32) -> f32; |
| 1605 | /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero. |
| 1606 | /// |
| 1607 | /// The stabilized version of this intrinsic is |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1608 | /// [`f64::round`](../../std/primitive.f64.html#method.round) |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1609 | pub fn roundf64(x: f64) -> f64; |
| 1610 | |
| 1611 | /// Float addition that allows optimizations based on algebraic rules. |
| 1612 | /// May assume inputs are finite. |
| 1613 | /// |
| 1614 | /// This intrinsic does not have a stable counterpart. |
| 1615 | pub fn fadd_fast<T: Copy>(a: T, b: T) -> T; |
| 1616 | |
| 1617 | /// Float subtraction that allows optimizations based on algebraic rules. |
| 1618 | /// May assume inputs are finite. |
| 1619 | /// |
| 1620 | /// This intrinsic does not have a stable counterpart. |
| 1621 | pub fn fsub_fast<T: Copy>(a: T, b: T) -> T; |
| 1622 | |
| 1623 | /// Float multiplication that allows optimizations based on algebraic rules. |
| 1624 | /// May assume inputs are finite. |
| 1625 | /// |
| 1626 | /// This intrinsic does not have a stable counterpart. |
| 1627 | pub fn fmul_fast<T: Copy>(a: T, b: T) -> T; |
| 1628 | |
| 1629 | /// Float division that allows optimizations based on algebraic rules. |
| 1630 | /// May assume inputs are finite. |
| 1631 | /// |
| 1632 | /// This intrinsic does not have a stable counterpart. |
| 1633 | pub fn fdiv_fast<T: Copy>(a: T, b: T) -> T; |
| 1634 | |
| 1635 | /// Float remainder that allows optimizations based on algebraic rules. |
| 1636 | /// May assume inputs are finite. |
| 1637 | /// |
| 1638 | /// This intrinsic does not have a stable counterpart. |
| 1639 | pub fn frem_fast<T: Copy>(a: T, b: T) -> T; |
| 1640 | |
| 1641 | /// Convert with LLVM’s fptoui/fptosi, which may return undef for values out of range |
| 1642 | /// (<https://github.com/rust-lang/rust/issues/10184>) |
| 1643 | /// |
| 1644 | /// Stabilized as [`f32::to_int_unchecked`] and [`f64::to_int_unchecked`]. |
| 1645 | pub fn float_to_int_unchecked<Float: Copy, Int: Copy>(value: Float) -> Int; |
| 1646 | |
| 1647 | /// Returns the number of bits set in an integer type `T` |
| 1648 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1649 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1650 | /// it does not require an `unsafe` block. |
| 1651 | /// Therefore, implementations must not require the user to uphold |
| 1652 | /// any safety invariants. |
| 1653 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1654 | /// The stabilized versions of this intrinsic are available on the integer |
| 1655 | /// primitives via the `count_ones` method. For example, |
| 1656 | /// [`u32::count_ones`] |
| 1657 | #[rustc_const_stable(feature = "const_ctpop", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1658 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1659 | pub fn ctpop<T: Copy>(x: T) -> T; |
| 1660 | |
| 1661 | /// Returns the number of leading unset bits (zeroes) in an integer type `T`. |
| 1662 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1663 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1664 | /// it does not require an `unsafe` block. |
| 1665 | /// Therefore, implementations must not require the user to uphold |
| 1666 | /// any safety invariants. |
| 1667 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1668 | /// The stabilized versions of this intrinsic are available on the integer |
| 1669 | /// primitives via the `leading_zeros` method. For example, |
| 1670 | /// [`u32::leading_zeros`] |
| 1671 | /// |
| 1672 | /// # Examples |
| 1673 | /// |
| 1674 | /// ``` |
| 1675 | /// #![feature(core_intrinsics)] |
| 1676 | /// |
| 1677 | /// use std::intrinsics::ctlz; |
| 1678 | /// |
| 1679 | /// let x = 0b0001_1100_u8; |
| 1680 | /// let num_leading = ctlz(x); |
| 1681 | /// assert_eq!(num_leading, 3); |
| 1682 | /// ``` |
| 1683 | /// |
| 1684 | /// An `x` with value `0` will return the bit width of `T`. |
| 1685 | /// |
| 1686 | /// ``` |
| 1687 | /// #![feature(core_intrinsics)] |
| 1688 | /// |
| 1689 | /// use std::intrinsics::ctlz; |
| 1690 | /// |
| 1691 | /// let x = 0u16; |
| 1692 | /// let num_leading = ctlz(x); |
| 1693 | /// assert_eq!(num_leading, 16); |
| 1694 | /// ``` |
| 1695 | #[rustc_const_stable(feature = "const_ctlz", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1696 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1697 | pub fn ctlz<T: Copy>(x: T) -> T; |
| 1698 | |
| 1699 | /// Like `ctlz`, but extra-unsafe as it returns `undef` when |
| 1700 | /// given an `x` with value `0`. |
| 1701 | /// |
| 1702 | /// This intrinsic does not have a stable counterpart. |
| 1703 | /// |
| 1704 | /// # Examples |
| 1705 | /// |
| 1706 | /// ``` |
| 1707 | /// #![feature(core_intrinsics)] |
| 1708 | /// |
| 1709 | /// use std::intrinsics::ctlz_nonzero; |
| 1710 | /// |
| 1711 | /// let x = 0b0001_1100_u8; |
| 1712 | /// let num_leading = unsafe { ctlz_nonzero(x) }; |
| 1713 | /// assert_eq!(num_leading, 3); |
| 1714 | /// ``` |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 1715 | #[rustc_const_stable(feature = "constctlz", since = "1.50.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1716 | pub fn ctlz_nonzero<T: Copy>(x: T) -> T; |
| 1717 | |
| 1718 | /// Returns the number of trailing unset bits (zeroes) in an integer type `T`. |
| 1719 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1720 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1721 | /// it does not require an `unsafe` block. |
| 1722 | /// Therefore, implementations must not require the user to uphold |
| 1723 | /// any safety invariants. |
| 1724 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1725 | /// The stabilized versions of this intrinsic are available on the integer |
| 1726 | /// primitives via the `trailing_zeros` method. For example, |
| 1727 | /// [`u32::trailing_zeros`] |
| 1728 | /// |
| 1729 | /// # Examples |
| 1730 | /// |
| 1731 | /// ``` |
| 1732 | /// #![feature(core_intrinsics)] |
| 1733 | /// |
| 1734 | /// use std::intrinsics::cttz; |
| 1735 | /// |
| 1736 | /// let x = 0b0011_1000_u8; |
| 1737 | /// let num_trailing = cttz(x); |
| 1738 | /// assert_eq!(num_trailing, 3); |
| 1739 | /// ``` |
| 1740 | /// |
| 1741 | /// An `x` with value `0` will return the bit width of `T`: |
| 1742 | /// |
| 1743 | /// ``` |
| 1744 | /// #![feature(core_intrinsics)] |
| 1745 | /// |
| 1746 | /// use std::intrinsics::cttz; |
| 1747 | /// |
| 1748 | /// let x = 0u16; |
| 1749 | /// let num_trailing = cttz(x); |
| 1750 | /// assert_eq!(num_trailing, 16); |
| 1751 | /// ``` |
| 1752 | #[rustc_const_stable(feature = "const_cttz", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1753 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1754 | pub fn cttz<T: Copy>(x: T) -> T; |
| 1755 | |
| 1756 | /// Like `cttz`, but extra-unsafe as it returns `undef` when |
| 1757 | /// given an `x` with value `0`. |
| 1758 | /// |
| 1759 | /// This intrinsic does not have a stable counterpart. |
| 1760 | /// |
| 1761 | /// # Examples |
| 1762 | /// |
| 1763 | /// ``` |
| 1764 | /// #![feature(core_intrinsics)] |
| 1765 | /// |
| 1766 | /// use std::intrinsics::cttz_nonzero; |
| 1767 | /// |
| 1768 | /// let x = 0b0011_1000_u8; |
| 1769 | /// let num_trailing = unsafe { cttz_nonzero(x) }; |
| 1770 | /// assert_eq!(num_trailing, 3); |
| 1771 | /// ``` |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 1772 | #[rustc_const_stable(feature = "const_cttz_nonzero", since = "1.53.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1773 | pub fn cttz_nonzero<T: Copy>(x: T) -> T; |
| 1774 | |
| 1775 | /// Reverses the bytes in an integer type `T`. |
| 1776 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1777 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1778 | /// it does not require an `unsafe` block. |
| 1779 | /// Therefore, implementations must not require the user to uphold |
| 1780 | /// any safety invariants. |
| 1781 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1782 | /// The stabilized versions of this intrinsic are available on the integer |
| 1783 | /// primitives via the `swap_bytes` method. For example, |
| 1784 | /// [`u32::swap_bytes`] |
| 1785 | #[rustc_const_stable(feature = "const_bswap", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1786 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1787 | pub fn bswap<T: Copy>(x: T) -> T; |
| 1788 | |
| 1789 | /// Reverses the bits in an integer type `T`. |
| 1790 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1791 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1792 | /// it does not require an `unsafe` block. |
| 1793 | /// Therefore, implementations must not require the user to uphold |
| 1794 | /// any safety invariants. |
| 1795 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1796 | /// The stabilized versions of this intrinsic are available on the integer |
| 1797 | /// primitives via the `reverse_bits` method. For example, |
| 1798 | /// [`u32::reverse_bits`] |
| 1799 | #[rustc_const_stable(feature = "const_bitreverse", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1800 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1801 | pub fn bitreverse<T: Copy>(x: T) -> T; |
| 1802 | |
| 1803 | /// Performs checked integer addition. |
| 1804 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1805 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1806 | /// it does not require an `unsafe` block. |
| 1807 | /// Therefore, implementations must not require the user to uphold |
| 1808 | /// any safety invariants. |
| 1809 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1810 | /// The stabilized versions of this intrinsic are available on the integer |
| 1811 | /// primitives via the `overflowing_add` method. For example, |
| 1812 | /// [`u32::overflowing_add`] |
| 1813 | #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1814 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1815 | pub fn add_with_overflow<T: Copy>(x: T, y: T) -> (T, bool); |
| 1816 | |
| 1817 | /// Performs checked integer subtraction |
| 1818 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1819 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1820 | /// it does not require an `unsafe` block. |
| 1821 | /// Therefore, implementations must not require the user to uphold |
| 1822 | /// any safety invariants. |
| 1823 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1824 | /// The stabilized versions of this intrinsic are available on the integer |
| 1825 | /// primitives via the `overflowing_sub` method. For example, |
| 1826 | /// [`u32::overflowing_sub`] |
| 1827 | #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1828 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1829 | pub fn sub_with_overflow<T: Copy>(x: T, y: T) -> (T, bool); |
| 1830 | |
| 1831 | /// Performs checked integer multiplication |
| 1832 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1833 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1834 | /// it does not require an `unsafe` block. |
| 1835 | /// Therefore, implementations must not require the user to uphold |
| 1836 | /// any safety invariants. |
| 1837 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1838 | /// The stabilized versions of this intrinsic are available on the integer |
| 1839 | /// primitives via the `overflowing_mul` method. For example, |
| 1840 | /// [`u32::overflowing_mul`] |
| 1841 | #[rustc_const_stable(feature = "const_int_overflow", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1842 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1843 | pub fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool); |
| 1844 | |
| 1845 | /// Performs an exact division, resulting in undefined behavior where |
| 1846 | /// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1` |
| 1847 | /// |
| 1848 | /// This intrinsic does not have a stable counterpart. |
| 1849 | pub fn exact_div<T: Copy>(x: T, y: T) -> T; |
| 1850 | |
| 1851 | /// Performs an unchecked division, resulting in undefined behavior |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 1852 | /// where `y == 0` or `x == T::MIN && y == -1` |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1853 | /// |
| 1854 | /// Safe wrappers for this intrinsic are available on the integer |
| 1855 | /// primitives via the `checked_div` method. For example, |
| 1856 | /// [`u32::checked_div`] |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 1857 | #[rustc_const_stable(feature = "const_int_unchecked_div", since = "1.52.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1858 | pub fn unchecked_div<T: Copy>(x: T, y: T) -> T; |
| 1859 | /// Returns the remainder of an unchecked division, resulting in |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 1860 | /// undefined behavior when `y == 0` or `x == T::MIN && y == -1` |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1861 | /// |
| 1862 | /// Safe wrappers for this intrinsic are available on the integer |
| 1863 | /// primitives via the `checked_rem` method. For example, |
| 1864 | /// [`u32::checked_rem`] |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 1865 | #[rustc_const_stable(feature = "const_int_unchecked_rem", since = "1.52.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1866 | pub fn unchecked_rem<T: Copy>(x: T, y: T) -> T; |
| 1867 | |
| 1868 | /// Performs an unchecked left shift, resulting in undefined behavior when |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 1869 | /// `y < 0` or `y >= N`, where N is the width of T in bits. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1870 | /// |
| 1871 | /// Safe wrappers for this intrinsic are available on the integer |
| 1872 | /// primitives via the `checked_shl` method. For example, |
| 1873 | /// [`u32::checked_shl`] |
| 1874 | #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")] |
| 1875 | pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T; |
| 1876 | /// Performs an unchecked right shift, resulting in undefined behavior when |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 1877 | /// `y < 0` or `y >= N`, where N is the width of T in bits. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1878 | /// |
| 1879 | /// Safe wrappers for this intrinsic are available on the integer |
| 1880 | /// primitives via the `checked_shr` method. For example, |
| 1881 | /// [`u32::checked_shr`] |
| 1882 | #[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")] |
| 1883 | pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T; |
| 1884 | |
| 1885 | /// Returns the result of an unchecked addition, resulting in |
| 1886 | /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`. |
| 1887 | /// |
| 1888 | /// This intrinsic does not have a stable counterpart. |
| 1889 | #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")] |
| 1890 | pub fn unchecked_add<T: Copy>(x: T, y: T) -> T; |
| 1891 | |
| 1892 | /// Returns the result of an unchecked subtraction, resulting in |
| 1893 | /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`. |
| 1894 | /// |
| 1895 | /// This intrinsic does not have a stable counterpart. |
| 1896 | #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")] |
| 1897 | pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T; |
| 1898 | |
| 1899 | /// Returns the result of an unchecked multiplication, resulting in |
| 1900 | /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`. |
| 1901 | /// |
| 1902 | /// This intrinsic does not have a stable counterpart. |
| 1903 | #[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")] |
| 1904 | pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T; |
| 1905 | |
| 1906 | /// Performs rotate left. |
| 1907 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1908 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1909 | /// it does not require an `unsafe` block. |
| 1910 | /// Therefore, implementations must not require the user to uphold |
| 1911 | /// any safety invariants. |
| 1912 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1913 | /// The stabilized versions of this intrinsic are available on the integer |
| 1914 | /// primitives via the `rotate_left` method. For example, |
| 1915 | /// [`u32::rotate_left`] |
| 1916 | #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1917 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1918 | pub fn rotate_left<T: Copy>(x: T, y: T) -> T; |
| 1919 | |
| 1920 | /// Performs rotate right. |
| 1921 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1922 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1923 | /// it does not require an `unsafe` block. |
| 1924 | /// Therefore, implementations must not require the user to uphold |
| 1925 | /// any safety invariants. |
| 1926 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1927 | /// The stabilized versions of this intrinsic are available on the integer |
| 1928 | /// primitives via the `rotate_right` method. For example, |
| 1929 | /// [`u32::rotate_right`] |
| 1930 | #[rustc_const_stable(feature = "const_int_rotate", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1931 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1932 | pub fn rotate_right<T: Copy>(x: T, y: T) -> T; |
| 1933 | |
| 1934 | /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits. |
| 1935 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1936 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1937 | /// it does not require an `unsafe` block. |
| 1938 | /// Therefore, implementations must not require the user to uphold |
| 1939 | /// any safety invariants. |
| 1940 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1941 | /// The stabilized versions of this intrinsic are available on the integer |
Thiébaud Weksteen | 5bd94c1 | 2021-01-06 15:18:42 +0100 | [diff] [blame] | 1942 | /// primitives via the `wrapping_add` method. For example, |
| 1943 | /// [`u32::wrapping_add`] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1944 | #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1945 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1946 | pub fn wrapping_add<T: Copy>(a: T, b: T) -> T; |
| 1947 | /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits. |
| 1948 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1949 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1950 | /// it does not require an `unsafe` block. |
| 1951 | /// Therefore, implementations must not require the user to uphold |
| 1952 | /// any safety invariants. |
| 1953 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1954 | /// The stabilized versions of this intrinsic are available on the integer |
Thiébaud Weksteen | 5bd94c1 | 2021-01-06 15:18:42 +0100 | [diff] [blame] | 1955 | /// primitives via the `wrapping_sub` method. For example, |
| 1956 | /// [`u32::wrapping_sub`] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1957 | #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1958 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1959 | pub fn wrapping_sub<T: Copy>(a: T, b: T) -> T; |
| 1960 | /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits. |
| 1961 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1962 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1963 | /// it does not require an `unsafe` block. |
| 1964 | /// Therefore, implementations must not require the user to uphold |
| 1965 | /// any safety invariants. |
| 1966 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1967 | /// The stabilized versions of this intrinsic are available on the integer |
Thiébaud Weksteen | 5bd94c1 | 2021-01-06 15:18:42 +0100 | [diff] [blame] | 1968 | /// primitives via the `wrapping_mul` method. For example, |
| 1969 | /// [`u32::wrapping_mul`] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1970 | #[rustc_const_stable(feature = "const_int_wrapping", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1971 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1972 | pub fn wrapping_mul<T: Copy>(a: T, b: T) -> T; |
| 1973 | |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 1974 | /// Computes `a + b`, saturating at numeric bounds. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1975 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1976 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1977 | /// it does not require an `unsafe` block. |
| 1978 | /// Therefore, implementations must not require the user to uphold |
| 1979 | /// any safety invariants. |
| 1980 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1981 | /// The stabilized versions of this intrinsic are available on the integer |
| 1982 | /// primitives via the `saturating_add` method. For example, |
| 1983 | /// [`u32::saturating_add`] |
| 1984 | #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1985 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1986 | pub fn saturating_add<T: Copy>(a: T, b: T) -> T; |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 1987 | /// Computes `a - b`, saturating at numeric bounds. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1988 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 1989 | /// Note that, unlike most intrinsics, this is safe to call; |
| 1990 | /// it does not require an `unsafe` block. |
| 1991 | /// Therefore, implementations must not require the user to uphold |
| 1992 | /// any safety invariants. |
| 1993 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1994 | /// The stabilized versions of this intrinsic are available on the integer |
| 1995 | /// primitives via the `saturating_sub` method. For example, |
| 1996 | /// [`u32::saturating_sub`] |
| 1997 | #[rustc_const_stable(feature = "const_int_saturating", since = "1.40.0")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 1998 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 1999 | pub fn saturating_sub<T: Copy>(a: T, b: T) -> T; |
| 2000 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 2001 | /// Returns the value of the discriminant for the variant in 'v'; |
| 2002 | /// if `T` has no discriminant, returns `0`. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2003 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 2004 | /// Note that, unlike most intrinsics, this is safe to call; |
| 2005 | /// it does not require an `unsafe` block. |
| 2006 | /// Therefore, implementations must not require the user to uphold |
| 2007 | /// any safety invariants. |
| 2008 | /// |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 2009 | /// The stabilized version of this intrinsic is [`core::mem::discriminant`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2010 | #[rustc_const_unstable(feature = "const_discriminant", issue = "69821")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2011 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2012 | pub fn discriminant_value<T>(v: &T) -> <T as DiscriminantKind>::Discriminant; |
| 2013 | |
| 2014 | /// Returns the number of variants of the type `T` cast to a `usize`; |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 2015 | /// if `T` has no variants, returns `0`. Uninhabited variants will be counted. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2016 | /// |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 2017 | /// Note that, unlike most intrinsics, this is safe to call; |
| 2018 | /// it does not require an `unsafe` block. |
| 2019 | /// Therefore, implementations must not require the user to uphold |
| 2020 | /// any safety invariants. |
| 2021 | /// |
Thiébaud Weksteen | 5bd94c1 | 2021-01-06 15:18:42 +0100 | [diff] [blame] | 2022 | /// The to-be-stabilized version of this intrinsic is [`mem::variant_count`]. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2023 | #[rustc_const_unstable(feature = "variant_count", issue = "73662")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2024 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2025 | pub fn variant_count<T>() -> usize; |
| 2026 | |
| 2027 | /// Rust's "try catch" construct which invokes the function pointer `try_fn` |
| 2028 | /// with the data pointer `data`. |
| 2029 | /// |
| 2030 | /// The third argument is a function called if a panic occurs. This function |
| 2031 | /// takes the data pointer and a pointer to the target-specific exception |
| 2032 | /// object that was caught. For more information see the compiler's |
| 2033 | /// source as well as std's catch implementation. |
| 2034 | pub fn r#try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32; |
| 2035 | |
| 2036 | /// Emits a `!nontemporal` store according to LLVM (see their docs). |
| 2037 | /// Probably will never become stable. |
| 2038 | pub fn nontemporal_store<T>(ptr: *mut T, val: T); |
| 2039 | |
| 2040 | /// See documentation of `<*const T>::offset_from` for details. |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2041 | #[rustc_const_stable(feature = "const_ptr_offset_from", since = "1.65.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2042 | pub fn ptr_offset_from<T>(ptr: *const T, base: *const T) -> isize; |
| 2043 | |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 2044 | /// See documentation of `<*const T>::sub_ptr` for details. |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2045 | #[rustc_const_unstable(feature = "const_ptr_sub_ptr", issue = "95892")] |
Charisee | 9cf6780 | 2022-06-30 20:04:09 +0000 | [diff] [blame] | 2046 | pub fn ptr_offset_from_unsigned<T>(ptr: *const T, base: *const T) -> usize; |
| 2047 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2048 | /// See documentation of `<*const T>::guaranteed_eq` for details. |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2049 | /// Returns `2` if the result is unknown. |
| 2050 | /// Returns `1` if the pointers are guaranteed equal |
| 2051 | /// Returns `0` if the pointers are guaranteed inequal |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 2052 | /// |
| 2053 | /// Note that, unlike most intrinsics, this is safe to call; |
| 2054 | /// it does not require an `unsafe` block. |
| 2055 | /// Therefore, implementations must not require the user to uphold |
| 2056 | /// any safety invariants. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2057 | #[rustc_const_unstable(feature = "const_raw_ptr_comparison", issue = "53020")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2058 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2059 | pub fn ptr_guaranteed_cmp<T>(ptr: *const T, other: *const T) -> u8; |
| 2060 | |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 2061 | /// Allocates a block of memory at compile time. |
| 2062 | /// At runtime, just returns a null pointer. |
| 2063 | /// |
| 2064 | /// # Safety |
| 2065 | /// |
| 2066 | /// - The `align` argument must be a power of two. |
| 2067 | /// - At compile time, a compile error occurs if this constraint is violated. |
| 2068 | /// - At runtime, it is not checked. |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 2069 | #[rustc_const_unstable(feature = "const_heap", issue = "79597")] |
Jeff Vander Stoep | d59a287 | 2021-02-15 10:22:21 +0100 | [diff] [blame] | 2070 | pub fn const_allocate(size: usize, align: usize) -> *mut u8; |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 2071 | |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 2072 | /// Deallocates a memory which allocated by `intrinsics::const_allocate` at compile time. |
| 2073 | /// At runtime, does nothing. |
| 2074 | /// |
| 2075 | /// # Safety |
| 2076 | /// |
| 2077 | /// - The `align` argument must be a power of two. |
| 2078 | /// - At compile time, a compile error occurs if this constraint is violated. |
| 2079 | /// - At runtime, it is not checked. |
| 2080 | /// - If the `ptr` is created in an another const, this intrinsic doesn't deallocate it. |
| 2081 | /// - If the `ptr` is pointing to a local variable, this intrinsic doesn't deallocate it. |
| 2082 | #[rustc_const_unstable(feature = "const_heap", issue = "79597")] |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 2083 | pub fn const_deallocate(ptr: *mut u8, size: usize, align: usize); |
| 2084 | |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 2085 | /// Determines whether the raw bytes of the two values are equal. |
| 2086 | /// |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 2087 | /// This is particularly handy for arrays, since it allows things like just |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 2088 | /// comparing `i96`s instead of forcing `alloca`s for `[6 x i16]`. |
| 2089 | /// |
| 2090 | /// Above some backend-decided threshold this will emit calls to `memcmp`, |
| 2091 | /// like slice equality does, instead of causing massive code size. |
| 2092 | /// |
| 2093 | /// # Safety |
| 2094 | /// |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2095 | /// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized or carry a |
| 2096 | /// pointer value. |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 2097 | /// Note that this is a stricter criterion than just the *values* being |
| 2098 | /// fully-initialized: if `T` has padding, it's UB to call this intrinsic. |
| 2099 | /// |
| 2100 | /// (The implementation is allowed to branch on the results of comparisons, |
| 2101 | /// which is UB if any of their inputs are `undef`.) |
Chris Wailes | 54272ac | 2021-09-09 16:08:13 -0700 | [diff] [blame] | 2102 | #[rustc_const_unstable(feature = "const_intrinsic_raw_eq", issue = "none")] |
| 2103 | pub fn raw_eq<T>(a: &T, b: &T) -> bool; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 2104 | |
| 2105 | /// See documentation of [`std::hint::black_box`] for details. |
| 2106 | /// |
| 2107 | /// [`std::hint::black_box`]: crate::hint::black_box |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 2108 | #[rustc_const_unstable(feature = "const_black_box", issue = "none")] |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2109 | #[cfg_attr(not(bootstrap), rustc_safe_intrinsic)] |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 2110 | pub fn black_box<T>(dummy: T) -> T; |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 2111 | |
| 2112 | /// `ptr` must point to a vtable. |
| 2113 | /// The intrinsic will return the size stored in that vtable. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 2114 | pub fn vtable_size(ptr: *const ()) -> usize; |
| 2115 | |
| 2116 | /// `ptr` must point to a vtable. |
| 2117 | /// The intrinsic will return the alignment stored in that vtable. |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 2118 | pub fn vtable_align(ptr: *const ()) -> usize; |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2119 | |
| 2120 | /// Selects which function to call depending on the context. |
| 2121 | /// |
| 2122 | /// If this function is evaluated at compile-time, then a call to this |
| 2123 | /// intrinsic will be replaced with a call to `called_in_const`. It gets |
| 2124 | /// replaced with a call to `called_at_rt` otherwise. |
| 2125 | /// |
| 2126 | /// # Type Requirements |
| 2127 | /// |
| 2128 | /// The two functions must be both function items. They cannot be function |
| 2129 | /// pointers or closures. The first function must be a `const fn`. |
| 2130 | /// |
| 2131 | /// `arg` will be the tupled arguments that will be passed to either one of |
| 2132 | /// the two functions, therefore, both functions must accept the same type of |
| 2133 | /// arguments. Both functions must return RET. |
| 2134 | /// |
| 2135 | /// # Safety |
| 2136 | /// |
| 2137 | /// The two functions must behave observably equivalent. Safe code in other |
| 2138 | /// crates may assume that calling a `const fn` at compile-time and at run-time |
| 2139 | /// produces the same result. A function that produces a different result when |
| 2140 | /// evaluated at run-time, or has any other observable side-effects, is |
| 2141 | /// *unsound*. |
| 2142 | /// |
| 2143 | /// Here is an example of how this could cause a problem: |
| 2144 | /// ```no_run |
| 2145 | /// #![feature(const_eval_select)] |
| 2146 | /// #![feature(core_intrinsics)] |
| 2147 | /// use std::hint::unreachable_unchecked; |
| 2148 | /// use std::intrinsics::const_eval_select; |
| 2149 | /// |
| 2150 | /// // Crate A |
| 2151 | /// pub const fn inconsistent() -> i32 { |
| 2152 | /// fn runtime() -> i32 { 1 } |
| 2153 | /// const fn compiletime() -> i32 { 2 } |
| 2154 | /// |
| 2155 | /// unsafe { |
| 2156 | // // ⚠ This code violates the required equivalence of `compiletime` |
| 2157 | /// // and `runtime`. |
| 2158 | /// const_eval_select((), compiletime, runtime) |
| 2159 | /// } |
| 2160 | /// } |
| 2161 | /// |
| 2162 | /// // Crate B |
| 2163 | /// const X: i32 = inconsistent(); |
| 2164 | /// let x = inconsistent(); |
| 2165 | /// if x != X { unsafe { unreachable_unchecked(); }} |
| 2166 | /// ``` |
| 2167 | /// |
| 2168 | /// This code causes Undefined Behavior when being run, since the |
| 2169 | /// `unreachable_unchecked` is actually being reached. The bug is in *crate A*, |
| 2170 | /// which violates the principle that a `const fn` must behave the same at |
| 2171 | /// compile-time and at run-time. The unsafe code in crate B is fine. |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2172 | #[rustc_const_unstable(feature = "const_eval_select", issue = "none")] |
| 2173 | pub fn const_eval_select<ARG, F, G, RET>(arg: ARG, called_in_const: F, called_at_rt: G) -> RET |
| 2174 | where |
| 2175 | G: FnOnce<ARG, Output = RET>, |
| 2176 | F: FnOnce<ARG, Output = RET>; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | // Some functions are defined here because they accidentally got made |
| 2180 | // available in this module on stable. See <https://github.com/rust-lang/rust/issues/15702>. |
| 2181 | // (`transmute` also falls into this category, but it cannot be wrapped due to the |
| 2182 | // check that `T` and `U` have the same size.) |
| 2183 | |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2184 | /// Check that the preconditions of an unsafe function are followed, if debug_assertions are on, |
| 2185 | /// and only at runtime. |
| 2186 | /// |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2187 | /// This macro should be called as `assert_unsafe_precondition!([Generics](name: Type) => Expression)` |
| 2188 | /// where the names specified will be moved into the macro as captured variables, and defines an item |
| 2189 | /// to call `const_eval_select` on. The tokens inside the square brackets are used to denote generics |
| 2190 | /// for the function declaractions and can be omitted if there is no generics. |
| 2191 | /// |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2192 | /// # Safety |
| 2193 | /// |
| 2194 | /// Invoking this macro is only sound if the following code is already UB when the passed |
| 2195 | /// expression evaluates to false. |
| 2196 | /// |
| 2197 | /// This macro expands to a check at runtime if debug_assertions is set. It has no effect at |
| 2198 | /// compile time, but the semantics of the contained `const_eval_select` must be the same at |
| 2199 | /// runtime and at compile time. Thus if the expression evaluates to false, this macro produces |
| 2200 | /// different behavior at compile time and at runtime, and invoking it is incorrect. |
| 2201 | /// |
| 2202 | /// So in a sense it is UB if this macro is useful, but we expect callers of `unsafe fn` to make |
| 2203 | /// the occasional mistake, and this check should help them figure things out. |
| 2204 | #[allow_internal_unstable(const_eval_select)] // permit this to be called in stably-const fn |
| 2205 | macro_rules! assert_unsafe_precondition { |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2206 | ($name:expr, $([$($tt:tt)*])?($($i:ident:$ty:ty),*$(,)?) => $e:expr) => { |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2207 | if cfg!(debug_assertions) { |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2208 | // allow non_snake_case to allow capturing const generics |
| 2209 | #[allow(non_snake_case)] |
| 2210 | #[inline(always)] |
| 2211 | fn runtime$(<$($tt)*>)?($($i:$ty),*) { |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2212 | if !$e { |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2213 | // don't unwind to reduce impact on code size |
| 2214 | ::core::panicking::panic_str_nounwind( |
| 2215 | concat!("unsafe precondition(s) violated: ", $name) |
| 2216 | ); |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2217 | } |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2218 | } |
| 2219 | #[allow(non_snake_case)] |
| 2220 | const fn comptime$(<$($tt)*>)?($(_:$ty),*) {} |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2221 | |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2222 | ::core::intrinsics::const_eval_select(($($i,)*), comptime, runtime); |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2223 | } |
| 2224 | }; |
| 2225 | } |
| 2226 | pub(crate) use assert_unsafe_precondition; |
| 2227 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2228 | /// Checks whether `ptr` is properly aligned with respect to |
| 2229 | /// `align_of::<T>()`. |
| 2230 | pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool { |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2231 | !ptr.is_null() && ptr.is_aligned() |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2234 | /// Checks whether an allocation of `len` instances of `T` exceeds |
| 2235 | /// the maximum allowed allocation size. |
| 2236 | pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool { |
| 2237 | let max_len = const { |
| 2238 | let size = crate::mem::size_of::<T>(); |
| 2239 | if size == 0 { usize::MAX } else { isize::MAX as usize / size } |
| 2240 | }; |
| 2241 | len <= max_len |
| 2242 | } |
| 2243 | |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 2244 | /// Checks whether the regions of memory starting at `src` and `dst` of size |
| 2245 | /// `count * size_of::<T>()` do *not* overlap. |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 2246 | pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool { |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2247 | let src_usize = src.addr(); |
| 2248 | let dst_usize = dst.addr(); |
Chris Wailes | 356b57e | 2022-01-13 10:08:24 -0800 | [diff] [blame] | 2249 | let size = mem::size_of::<T>().checked_mul(count).unwrap(); |
| 2250 | let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize }; |
| 2251 | // If the absolute distance between the ptrs is at least as big as the size of the buffer, |
| 2252 | // they do not overlap. |
| 2253 | diff >= size |
| 2254 | } |
| 2255 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2256 | /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source |
| 2257 | /// and destination must *not* overlap. |
| 2258 | /// |
| 2259 | /// For regions of memory which might overlap, use [`copy`] instead. |
| 2260 | /// |
| 2261 | /// `copy_nonoverlapping` is semantically equivalent to C's [`memcpy`], but |
| 2262 | /// with the argument order swapped. |
| 2263 | /// |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 2264 | /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the |
| 2265 | /// requirements of `T`. The initialization state is preserved exactly. |
| 2266 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2267 | /// [`memcpy`]: https://en.cppreference.com/w/c/string/byte/memcpy |
| 2268 | /// |
| 2269 | /// # Safety |
| 2270 | /// |
| 2271 | /// Behavior is undefined if any of the following conditions are violated: |
| 2272 | /// |
| 2273 | /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes. |
| 2274 | /// |
| 2275 | /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes. |
| 2276 | /// |
| 2277 | /// * Both `src` and `dst` must be properly aligned. |
| 2278 | /// |
| 2279 | /// * The region of memory beginning at `src` with a size of `count * |
| 2280 | /// size_of::<T>()` bytes must *not* overlap with the region of memory |
| 2281 | /// beginning at `dst` with the same size. |
| 2282 | /// |
| 2283 | /// Like [`read`], `copy_nonoverlapping` creates a bitwise copy of `T`, regardless of |
| 2284 | /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using *both* the values |
| 2285 | /// in the region beginning at `*src` and the region beginning at `*dst` can |
| 2286 | /// [violate memory safety][read-ownership]. |
| 2287 | /// |
| 2288 | /// Note that even if the effectively copied size (`count * size_of::<T>()`) is |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 2289 | /// `0`, the pointers must be non-null and properly aligned. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2290 | /// |
| 2291 | /// [`read`]: crate::ptr::read |
| 2292 | /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value |
| 2293 | /// [valid]: crate::ptr#safety |
| 2294 | /// |
| 2295 | /// # Examples |
| 2296 | /// |
| 2297 | /// Manually implement [`Vec::append`]: |
| 2298 | /// |
| 2299 | /// ``` |
| 2300 | /// use std::ptr; |
| 2301 | /// |
| 2302 | /// /// Moves all the elements of `src` into `dst`, leaving `src` empty. |
| 2303 | /// fn append<T>(dst: &mut Vec<T>, src: &mut Vec<T>) { |
| 2304 | /// let src_len = src.len(); |
| 2305 | /// let dst_len = dst.len(); |
| 2306 | /// |
| 2307 | /// // Ensure that `dst` has enough capacity to hold all of `src`. |
| 2308 | /// dst.reserve(src_len); |
| 2309 | /// |
| 2310 | /// unsafe { |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2311 | /// // The call to add is always safe because `Vec` will never |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2312 | /// // allocate more than `isize::MAX` bytes. |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2313 | /// let dst_ptr = dst.as_mut_ptr().add(dst_len); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2314 | /// let src_ptr = src.as_ptr(); |
| 2315 | /// |
| 2316 | /// // Truncate `src` without dropping its contents. We do this first, |
| 2317 | /// // to avoid problems in case something further down panics. |
| 2318 | /// src.set_len(0); |
| 2319 | /// |
| 2320 | /// // The two regions cannot overlap because mutable references do |
| 2321 | /// // not alias, and two different vectors cannot own the same |
| 2322 | /// // memory. |
| 2323 | /// ptr::copy_nonoverlapping(src_ptr, dst_ptr, src_len); |
| 2324 | /// |
| 2325 | /// // Notify `dst` that it now holds the contents of `src`. |
| 2326 | /// dst.set_len(dst_len + src_len); |
| 2327 | /// } |
| 2328 | /// } |
| 2329 | /// |
| 2330 | /// let mut a = vec!['r']; |
| 2331 | /// let mut b = vec!['u', 's', 't']; |
| 2332 | /// |
| 2333 | /// append(&mut a, &mut b); |
| 2334 | /// |
| 2335 | /// assert_eq!(a, &['r', 'u', 's', 't']); |
| 2336 | /// assert!(b.is_empty()); |
| 2337 | /// ``` |
| 2338 | /// |
| 2339 | /// [`Vec::append`]: ../../std/vec/struct.Vec.html#method.append |
| 2340 | #[doc(alias = "memcpy")] |
| 2341 | #[stable(feature = "rust1", since = "1.0.0")] |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2342 | #[rustc_allowed_through_unstable_modules] |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 2343 | #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2344 | #[inline] |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 2345 | #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 2346 | pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) { |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2347 | extern "rust-intrinsic" { |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 2348 | #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")] |
Chris Wailes | 32f7835 | 2021-07-20 14:04:55 -0700 | [diff] [blame] | 2349 | pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2352 | // SAFETY: the safety contract for `copy_nonoverlapping` must be |
| 2353 | // upheld by the caller. |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2354 | unsafe { |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2355 | assert_unsafe_precondition!( |
| 2356 | "ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \ |
| 2357 | and the specified memory ranges do not overlap", |
| 2358 | [T](src: *const T, dst: *mut T, count: usize) => |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2359 | is_aligned_and_not_null(src) |
| 2360 | && is_aligned_and_not_null(dst) |
| 2361 | && is_nonoverlapping(src, dst, count) |
| 2362 | ); |
| 2363 | copy_nonoverlapping(src, dst, count) |
| 2364 | } |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | /// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source |
| 2368 | /// and destination may overlap. |
| 2369 | /// |
| 2370 | /// If the source and destination will *never* overlap, |
| 2371 | /// [`copy_nonoverlapping`] can be used instead. |
| 2372 | /// |
| 2373 | /// `copy` is semantically equivalent to C's [`memmove`], but with the argument |
| 2374 | /// order swapped. Copying takes place as if the bytes were copied from `src` |
| 2375 | /// to a temporary array and then copied from the array to `dst`. |
| 2376 | /// |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 2377 | /// The copy is "untyped" in the sense that data may be uninitialized or otherwise violate the |
| 2378 | /// requirements of `T`. The initialization state is preserved exactly. |
| 2379 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2380 | /// [`memmove`]: https://en.cppreference.com/w/c/string/byte/memmove |
| 2381 | /// |
| 2382 | /// # Safety |
| 2383 | /// |
| 2384 | /// Behavior is undefined if any of the following conditions are violated: |
| 2385 | /// |
| 2386 | /// * `src` must be [valid] for reads of `count * size_of::<T>()` bytes. |
| 2387 | /// |
| 2388 | /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes. |
| 2389 | /// |
| 2390 | /// * Both `src` and `dst` must be properly aligned. |
| 2391 | /// |
| 2392 | /// Like [`read`], `copy` creates a bitwise copy of `T`, regardless of |
| 2393 | /// whether `T` is [`Copy`]. If `T` is not [`Copy`], using both the values |
| 2394 | /// in the region beginning at `*src` and the region beginning at `*dst` can |
| 2395 | /// [violate memory safety][read-ownership]. |
| 2396 | /// |
| 2397 | /// Note that even if the effectively copied size (`count * size_of::<T>()`) is |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 2398 | /// `0`, the pointers must be non-null and properly aligned. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2399 | /// |
| 2400 | /// [`read`]: crate::ptr::read |
| 2401 | /// [read-ownership]: crate::ptr::read#ownership-of-the-returned-value |
| 2402 | /// [valid]: crate::ptr#safety |
| 2403 | /// |
| 2404 | /// # Examples |
| 2405 | /// |
| 2406 | /// Efficiently create a Rust vector from an unsafe buffer: |
| 2407 | /// |
| 2408 | /// ``` |
| 2409 | /// use std::ptr; |
| 2410 | /// |
Thiébaud Weksteen | 3b664ca | 2020-11-26 14:41:59 +0100 | [diff] [blame] | 2411 | /// /// # Safety |
| 2412 | /// /// |
| 2413 | /// /// * `ptr` must be correctly aligned for its type and non-zero. |
| 2414 | /// /// * `ptr` must be valid for reads of `elts` contiguous elements of type `T`. |
| 2415 | /// /// * Those elements must not be used after calling this function unless `T: Copy`. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2416 | /// # #[allow(dead_code)] |
| 2417 | /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> { |
| 2418 | /// let mut dst = Vec::with_capacity(elts); |
Thiébaud Weksteen | 3b664ca | 2020-11-26 14:41:59 +0100 | [diff] [blame] | 2419 | /// |
| 2420 | /// // SAFETY: Our precondition ensures the source is aligned and valid, |
| 2421 | /// // and `Vec::with_capacity` ensures that we have usable space to write them. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2422 | /// ptr::copy(ptr, dst.as_mut_ptr(), elts); |
Thiébaud Weksteen | 3b664ca | 2020-11-26 14:41:59 +0100 | [diff] [blame] | 2423 | /// |
| 2424 | /// // SAFETY: We created it with this much capacity earlier, |
| 2425 | /// // and the previous `copy` has initialized these elements. |
| 2426 | /// dst.set_len(elts); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2427 | /// dst |
| 2428 | /// } |
| 2429 | /// ``` |
| 2430 | #[doc(alias = "memmove")] |
| 2431 | #[stable(feature = "rust1", since = "1.0.0")] |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2432 | #[rustc_allowed_through_unstable_modules] |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 2433 | #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2434 | #[inline] |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 2435 | #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
Jeff Vander Stoep | 59fbe18 | 2021-03-29 10:17:52 +0200 | [diff] [blame] | 2436 | pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) { |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2437 | extern "rust-intrinsic" { |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 2438 | #[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2439 | fn copy<T>(src: *const T, dst: *mut T, count: usize); |
| 2440 | } |
| 2441 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2442 | // SAFETY: the safety contract for `copy` must be upheld by the caller. |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2443 | unsafe { |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2444 | assert_unsafe_precondition!( |
| 2445 | "ptr::copy requires that both pointer arguments are aligned aligned and non-null", |
| 2446 | [T](src: *const T, dst: *mut T) => |
| 2447 | is_aligned_and_not_null(src) && is_aligned_and_not_null(dst) |
| 2448 | ); |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2449 | copy(src, dst, count) |
| 2450 | } |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | /// Sets `count * size_of::<T>()` bytes of memory starting at `dst` to |
| 2454 | /// `val`. |
| 2455 | /// |
| 2456 | /// `write_bytes` is similar to C's [`memset`], but sets `count * |
| 2457 | /// size_of::<T>()` bytes to `val`. |
| 2458 | /// |
| 2459 | /// [`memset`]: https://en.cppreference.com/w/c/string/byte/memset |
| 2460 | /// |
| 2461 | /// # Safety |
| 2462 | /// |
| 2463 | /// Behavior is undefined if any of the following conditions are violated: |
| 2464 | /// |
| 2465 | /// * `dst` must be [valid] for writes of `count * size_of::<T>()` bytes. |
| 2466 | /// |
| 2467 | /// * `dst` must be properly aligned. |
| 2468 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2469 | /// Note that even if the effectively copied size (`count * size_of::<T>()`) is |
Chris Wailes | 2f3fdfe | 2021-07-29 10:56:18 -0700 | [diff] [blame] | 2470 | /// `0`, the pointer must be non-null and properly aligned. |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2471 | /// |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 2472 | /// Additionally, note that changing `*dst` in this way can easily lead to undefined behavior (UB) |
| 2473 | /// later if the written bytes are not a valid representation of some `T`. For instance, the |
| 2474 | /// following is an **incorrect** use of this function: |
| 2475 | /// |
| 2476 | /// ```rust,no_run |
| 2477 | /// unsafe { |
| 2478 | /// let mut value: u8 = 0; |
| 2479 | /// let ptr: *mut bool = &mut value as *mut u8 as *mut bool; |
| 2480 | /// let _bool = ptr.read(); // This is fine, `ptr` points to a valid `bool`. |
| 2481 | /// ptr.write_bytes(42u8, 1); // This function itself does not cause UB... |
| 2482 | /// let _bool = ptr.read(); // ...but it makes this operation UB! ⚠️ |
| 2483 | /// } |
| 2484 | /// ``` |
| 2485 | /// |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2486 | /// [valid]: crate::ptr#safety |
| 2487 | /// |
| 2488 | /// # Examples |
| 2489 | /// |
| 2490 | /// Basic usage: |
| 2491 | /// |
| 2492 | /// ``` |
| 2493 | /// use std::ptr; |
| 2494 | /// |
| 2495 | /// let mut vec = vec![0u32; 4]; |
| 2496 | /// unsafe { |
| 2497 | /// let vec_ptr = vec.as_mut_ptr(); |
| 2498 | /// ptr::write_bytes(vec_ptr, 0xfe, 2); |
| 2499 | /// } |
| 2500 | /// assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]); |
| 2501 | /// ``` |
Chris Wailes | 6572058 | 2022-08-11 09:53:28 -0700 | [diff] [blame] | 2502 | #[doc(alias = "memset")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2503 | #[stable(feature = "rust1", since = "1.0.0")] |
Chris Wailes | 2f380c1 | 2022-11-09 13:04:22 -0800 | [diff] [blame] | 2504 | #[rustc_allowed_through_unstable_modules] |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 2505 | #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2506 | #[inline] |
Charisee | b1d3280 | 2022-09-22 15:38:41 +0000 | [diff] [blame] | 2507 | #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 2508 | pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) { |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2509 | extern "rust-intrinsic" { |
Charisee | 7878d54 | 2022-02-24 18:21:36 +0000 | [diff] [blame] | 2510 | #[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")] |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2511 | fn write_bytes<T>(dst: *mut T, val: u8, count: usize); |
| 2512 | } |
| 2513 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2514 | // SAFETY: the safety contract for `write_bytes` must be upheld by the caller. |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2515 | unsafe { |
Charisee | 89a0a0c | 2023-01-24 17:33:30 +0000 | [diff] [blame^] | 2516 | assert_unsafe_precondition!( |
| 2517 | "ptr::write_bytes requires that the destination pointer is aligned and non-null", |
| 2518 | [T](dst: *mut T) => is_aligned_and_not_null(dst) |
| 2519 | ); |
Charisee | 341341c | 2022-05-20 05:14:50 +0000 | [diff] [blame] | 2520 | write_bytes(dst, val, count) |
| 2521 | } |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 2522 | } |