]> git.proxmox.com Git - rustc.git/blame - src/libcore/intrinsics.rs
New upstream version 1.26.2+dfsg1
[rustc.git] / src / libcore / intrinsics.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! rustc compiler intrinsics.
12//!
3157f602 13//! The corresponding definitions are in librustc_trans/intrinsic.rs.
1a4d82fc
JJ
14//!
15//! # Volatiles
16//!
17//! The volatile intrinsics provide operations intended to act on I/O
18//! memory, which are guaranteed to not be reordered by the compiler
19//! across other volatile intrinsics. See the LLVM documentation on
20//! [[volatile]].
21//!
22//! [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
23//!
24//! # Atomics
25//!
26//! The atomic intrinsics provide common atomic operations on machine
27//! words, with multiple possible memory orderings. They obey the same
28//! semantics as C++11. See the LLVM documentation on [[atomics]].
29//!
30//! [atomics]: http://llvm.org/docs/Atomics.html
31//!
32//! A quick refresher on memory ordering:
33//!
34//! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
35//! take place after the barrier.
36//! * Release - a barrier for releasing a lock. Preceding reads and writes
37//! take place before the barrier.
38//! * Sequentially consistent - sequentially consistent operations are
39//! guaranteed to happen in order. This is the standard mode for working
40//! with atomic types and is equivalent to Java's `volatile`.
41
62682a34
SL
42#![unstable(feature = "core_intrinsics",
43 reason = "intrinsics are unlikely to ever be stabilized, instead \
44 they should be used through stabilized interfaces \
e9174d1e
SL
45 in the rest of the standard library",
46 issue = "0")]
1a4d82fc
JJ
47#![allow(missing_docs)]
48
cc61c64b
XL
49#[stable(feature = "drop_in_place", since = "1.8.0")]
50#[rustc_deprecated(reason = "no longer an intrinsic - use `ptr::drop_in_place` directly",
51 since = "1.18.0")]
52pub use ptr::drop_in_place;
1a4d82fc 53
cc61c64b 54extern "rust-intrinsic" {
62682a34 55 // NB: These intrinsics take raw pointers because they mutate aliased
1a4d82fc
JJ
56 // memory, which is not valid for either `&` or `&mut`.
57
476ff2be
SL
58 /// Stores a value if the current value is the same as the `old` value.
59 /// The stabilized version of this intrinsic is available on the
60 /// `std::sync::atomic` types via the `compare_exchange` method by passing
61 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
62 /// as both the `success` and `failure` parameters. For example,
cc61c64b
XL
63 /// [`AtomicBool::compare_exchange`][compare_exchange].
64 ///
65 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
54a0048b 66 pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
67 /// Stores a value if the current value is the same as the `old` value.
68 /// The stabilized version of this intrinsic is available on the
69 /// `std::sync::atomic` types via the `compare_exchange` method by passing
70 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
71 /// as both the `success` and `failure` parameters. For example,
cc61c64b
XL
72 /// [`AtomicBool::compare_exchange`][compare_exchange].
73 ///
74 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
54a0048b 75 pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
76 /// Stores a value if the current value is the same as the `old` value.
77 /// The stabilized version of this intrinsic is available on the
78 /// `std::sync::atomic` types via the `compare_exchange` method by passing
79 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
80 /// as the `success` and
81 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
82 /// as the `failure` parameters. For example,
cc61c64b
XL
83 /// [`AtomicBool::compare_exchange`][compare_exchange].
84 ///
85 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
54a0048b 86 pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
87 /// Stores a value if the current value is the same as the `old` value.
88 /// The stabilized version of this intrinsic is available on the
89 /// `std::sync::atomic` types via the `compare_exchange` method by passing
90 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
91 /// as the `success` and
92 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
93 /// as the `failure` parameters. For example,
cc61c64b
XL
94 /// [`AtomicBool::compare_exchange`][compare_exchange].
95 ///
96 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
54a0048b 97 pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
98 /// Stores a value if the current value is the same as the `old` value.
99 /// The stabilized version of this intrinsic is available on the
100 /// `std::sync::atomic` types via the `compare_exchange` method by passing
101 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
102 /// as both the `success` and `failure` parameters. For example,
cc61c64b
XL
103 /// [`AtomicBool::compare_exchange`][compare_exchange].
104 ///
105 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
54a0048b 106 pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
107 /// Stores a value if the current value is the same as the `old` value.
108 /// The stabilized version of this intrinsic is available on the
109 /// `std::sync::atomic` types via the `compare_exchange` method by passing
110 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
111 /// as the `success` and
112 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
113 /// as the `failure` parameters. For example,
cc61c64b
XL
114 /// [`AtomicBool::compare_exchange`][compare_exchange].
115 ///
116 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
54a0048b 117 pub fn atomic_cxchg_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
118 /// Stores a value if the current value is the same as the `old` value.
119 /// The stabilized version of this intrinsic is available on the
120 /// `std::sync::atomic` types via the `compare_exchange` method by passing
121 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
122 /// as the `success` and
123 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
124 /// as the `failure` parameters. For example,
cc61c64b
XL
125 /// [`AtomicBool::compare_exchange`][compare_exchange].
126 ///
127 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
54a0048b 128 pub fn atomic_cxchg_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
129 /// Stores a value if the current value is the same as the `old` value.
130 /// The stabilized version of this intrinsic is available on the
131 /// `std::sync::atomic` types via the `compare_exchange` method by passing
132 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
133 /// as the `success` and
134 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
135 /// as the `failure` parameters. For example,
cc61c64b
XL
136 /// [`AtomicBool::compare_exchange`][compare_exchange].
137 ///
138 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
54a0048b 139 pub fn atomic_cxchg_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
140 /// Stores a value if the current value is the same as the `old` value.
141 /// The stabilized version of this intrinsic is available on the
142 /// `std::sync::atomic` types via the `compare_exchange` method by passing
143 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
144 /// as the `success` and
145 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
146 /// as the `failure` parameters. For example,
cc61c64b
XL
147 /// [`AtomicBool::compare_exchange`][compare_exchange].
148 ///
149 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
54a0048b
SL
150 pub fn atomic_cxchg_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
151
476ff2be
SL
152 /// Stores a value if the current value is the same as the `old` value.
153 /// The stabilized version of this intrinsic is available on the
154 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
155 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
156 /// as both the `success` and `failure` parameters. For example,
cc61c64b
XL
157 /// [`AtomicBool::compare_exchange_weak`][cew].
158 ///
159 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
7453a54e 160 pub fn atomic_cxchgweak<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
161 /// Stores a value if the current value is the same as the `old` value.
162 /// The stabilized version of this intrinsic is available on the
163 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
164 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
165 /// as both the `success` and `failure` parameters. For example,
cc61c64b
XL
166 /// [`AtomicBool::compare_exchange_weak`][cew].
167 ///
168 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
7453a54e 169 pub fn atomic_cxchgweak_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
170 /// Stores a value if the current value is the same as the `old` value.
171 /// The stabilized version of this intrinsic is available on the
172 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
173 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
174 /// as the `success` and
175 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
176 /// as the `failure` parameters. For example,
cc61c64b
XL
177 /// [`AtomicBool::compare_exchange_weak`][cew].
178 ///
179 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
7453a54e 180 pub fn atomic_cxchgweak_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
181 /// Stores a value if the current value is the same as the `old` value.
182 /// The stabilized version of this intrinsic is available on the
183 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
184 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
185 /// as the `success` and
186 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
187 /// as the `failure` parameters. For example,
cc61c64b
XL
188 /// [`AtomicBool::compare_exchange_weak`][cew].
189 ///
190 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
7453a54e 191 pub fn atomic_cxchgweak_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
192 /// Stores a value if the current value is the same as the `old` value.
193 /// The stabilized version of this intrinsic is available on the
194 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
195 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
196 /// as both the `success` and `failure` parameters. For example,
cc61c64b
XL
197 /// [`AtomicBool::compare_exchange_weak`][cew].
198 ///
199 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
7453a54e 200 pub fn atomic_cxchgweak_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
201 /// Stores a value if the current value is the same as the `old` value.
202 /// The stabilized version of this intrinsic is available on the
203 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
204 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
205 /// as the `success` and
206 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
207 /// as the `failure` parameters. For example,
cc61c64b
XL
208 /// [`AtomicBool::compare_exchange_weak`][cew].
209 ///
210 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
7453a54e 211 pub fn atomic_cxchgweak_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
212 /// Stores a value if the current value is the same as the `old` value.
213 /// The stabilized version of this intrinsic is available on the
214 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
215 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
216 /// as the `success` and
217 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
218 /// as the `failure` parameters. For example,
cc61c64b
XL
219 /// [`AtomicBool::compare_exchange_weak`][cew].
220 ///
221 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
7453a54e 222 pub fn atomic_cxchgweak_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
223 /// Stores a value if the current value is the same as the `old` value.
224 /// The stabilized version of this intrinsic is available on the
225 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
226 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
227 /// as the `success` and
228 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
229 /// as the `failure` parameters. For example,
cc61c64b
XL
230 /// [`AtomicBool::compare_exchange_weak`][cew].
231 ///
232 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
7453a54e 233 pub fn atomic_cxchgweak_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
476ff2be
SL
234 /// Stores a value if the current value is the same as the `old` value.
235 /// The stabilized version of this intrinsic is available on the
236 /// `std::sync::atomic` types via the `compare_exchange_weak` method by passing
237 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
238 /// as the `success` and
239 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
240 /// as the `failure` parameters. For example,
cc61c64b
XL
241 /// [`AtomicBool::compare_exchange_weak`][cew].
242 ///
243 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
7453a54e 244 pub fn atomic_cxchgweak_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
1a4d82fc 245
476ff2be
SL
246 /// Loads the current value of the pointer.
247 /// The stabilized version of this intrinsic is available on the
248 /// `std::sync::atomic` types via the `load` method by passing
249 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
250 /// as the `order`. For example,
251 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
1a4d82fc 252 pub fn atomic_load<T>(src: *const T) -> T;
476ff2be
SL
253 /// Loads the current value of the pointer.
254 /// The stabilized version of this intrinsic is available on the
255 /// `std::sync::atomic` types via the `load` method by passing
256 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
257 /// as the `order`. For example,
258 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
1a4d82fc 259 pub fn atomic_load_acq<T>(src: *const T) -> T;
476ff2be
SL
260 /// Loads the current value of the pointer.
261 /// The stabilized version of this intrinsic is available on the
262 /// `std::sync::atomic` types via the `load` method by passing
263 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
264 /// as the `order`. For example,
265 /// [`AtomicBool::load`](../../std/sync/atomic/struct.AtomicBool.html#method.load).
1a4d82fc
JJ
266 pub fn atomic_load_relaxed<T>(src: *const T) -> T;
267 pub fn atomic_load_unordered<T>(src: *const T) -> T;
268
476ff2be
SL
269 /// Stores the value at the specified memory location.
270 /// The stabilized version of this intrinsic is available on the
271 /// `std::sync::atomic` types via the `store` method by passing
272 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
273 /// as the `order`. For example,
274 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
1a4d82fc 275 pub fn atomic_store<T>(dst: *mut T, val: T);
476ff2be
SL
276 /// Stores the value at the specified memory location.
277 /// The stabilized version of this intrinsic is available on the
278 /// `std::sync::atomic` types via the `store` method by passing
279 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
280 /// as the `order`. For example,
281 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
1a4d82fc 282 pub fn atomic_store_rel<T>(dst: *mut T, val: T);
476ff2be
SL
283 /// Stores the value at the specified memory location.
284 /// The stabilized version of this intrinsic is available on the
285 /// `std::sync::atomic` types via the `store` method by passing
286 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
287 /// as the `order`. For example,
288 /// [`AtomicBool::store`](../../std/sync/atomic/struct.AtomicBool.html#method.store).
1a4d82fc
JJ
289 pub fn atomic_store_relaxed<T>(dst: *mut T, val: T);
290 pub fn atomic_store_unordered<T>(dst: *mut T, val: T);
291
476ff2be
SL
292 /// Stores the value at the specified memory location, returning the old value.
293 /// The stabilized version of this intrinsic is available on the
294 /// `std::sync::atomic` types via the `swap` method by passing
295 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
296 /// as the `order`. For example,
297 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
1a4d82fc 298 pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
299 /// Stores the value at the specified memory location, returning the old value.
300 /// The stabilized version of this intrinsic is available on the
301 /// `std::sync::atomic` types via the `swap` method by passing
302 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
303 /// as the `order`. For example,
304 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
1a4d82fc 305 pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
306 /// Stores the value at the specified memory location, returning the old value.
307 /// The stabilized version of this intrinsic is available on the
308 /// `std::sync::atomic` types via the `swap` method by passing
309 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
310 /// as the `order`. For example,
311 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
1a4d82fc 312 pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
313 /// Stores the value at the specified memory location, returning the old value.
314 /// The stabilized version of this intrinsic is available on the
315 /// `std::sync::atomic` types via the `swap` method by passing
316 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
317 /// as the `order`. For example,
318 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
1a4d82fc 319 pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
320 /// Stores the value at the specified memory location, returning the old value.
321 /// The stabilized version of this intrinsic is available on the
322 /// `std::sync::atomic` types via the `swap` method by passing
323 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
324 /// as the `order`. For example,
325 /// [`AtomicBool::swap`](../../std/sync/atomic/struct.AtomicBool.html#method.swap).
1a4d82fc
JJ
326 pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T;
327
476ff2be
SL
328 /// Add to the current value, returning the previous value.
329 /// The stabilized version of this intrinsic is available on the
330 /// `std::sync::atomic` types via the `fetch_add` method by passing
331 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
332 /// as the `order`. For example,
333 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
1a4d82fc 334 pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
335 /// Add to the current value, returning the previous value.
336 /// The stabilized version of this intrinsic is available on the
337 /// `std::sync::atomic` types via the `fetch_add` method by passing
338 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
339 /// as the `order`. For example,
340 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
1a4d82fc 341 pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
342 /// Add to the current value, returning the previous value.
343 /// The stabilized version of this intrinsic is available on the
344 /// `std::sync::atomic` types via the `fetch_add` method by passing
345 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
346 /// as the `order`. For example,
347 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
1a4d82fc 348 pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
349 /// Add to the current value, returning the previous value.
350 /// The stabilized version of this intrinsic is available on the
351 /// `std::sync::atomic` types via the `fetch_add` method by passing
352 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
353 /// as the `order`. For example,
354 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
1a4d82fc 355 pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
356 /// Add to the current value, returning the previous value.
357 /// The stabilized version of this intrinsic is available on the
358 /// `std::sync::atomic` types via the `fetch_add` method by passing
359 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
360 /// as the `order`. For example,
361 /// [`AtomicIsize::fetch_add`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_add).
1a4d82fc
JJ
362 pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T;
363
476ff2be
SL
364 /// Subtract from the current value, returning the previous value.
365 /// The stabilized version of this intrinsic is available on the
366 /// `std::sync::atomic` types via the `fetch_sub` method by passing
367 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
368 /// as the `order`. For example,
369 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
1a4d82fc 370 pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
371 /// Subtract from the current value, returning the previous value.
372 /// The stabilized version of this intrinsic is available on the
373 /// `std::sync::atomic` types via the `fetch_sub` method by passing
374 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
375 /// as the `order`. For example,
376 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
1a4d82fc 377 pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
378 /// Subtract from the current value, returning the previous value.
379 /// The stabilized version of this intrinsic is available on the
380 /// `std::sync::atomic` types via the `fetch_sub` method by passing
381 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
382 /// as the `order`. For example,
383 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
1a4d82fc 384 pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
385 /// Subtract from the current value, returning the previous value.
386 /// The stabilized version of this intrinsic is available on the
387 /// `std::sync::atomic` types via the `fetch_sub` method by passing
388 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
389 /// as the `order`. For example,
390 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
1a4d82fc 391 pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
392 /// Subtract from the current value, returning the previous value.
393 /// The stabilized version of this intrinsic is available on the
394 /// `std::sync::atomic` types via the `fetch_sub` method by passing
395 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
396 /// as the `order`. For example,
397 /// [`AtomicIsize::fetch_sub`](../../std/sync/atomic/struct.AtomicIsize.html#method.fetch_sub).
1a4d82fc
JJ
398 pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T;
399
476ff2be
SL
400 /// Bitwise and with the current value, returning the previous value.
401 /// The stabilized version of this intrinsic is available on the
402 /// `std::sync::atomic` types via the `fetch_and` method by passing
403 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
404 /// as the `order`. For example,
405 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
1a4d82fc 406 pub fn atomic_and<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
407 /// Bitwise and with the current value, returning the previous value.
408 /// The stabilized version of this intrinsic is available on the
409 /// `std::sync::atomic` types via the `fetch_and` method by passing
410 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
411 /// as the `order`. For example,
412 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
1a4d82fc 413 pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
414 /// Bitwise and with the current value, returning the previous value.
415 /// The stabilized version of this intrinsic is available on the
416 /// `std::sync::atomic` types via the `fetch_and` method by passing
417 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
418 /// as the `order`. For example,
419 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
1a4d82fc 420 pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
421 /// Bitwise and with the current value, returning the previous value.
422 /// The stabilized version of this intrinsic is available on the
423 /// `std::sync::atomic` types via the `fetch_and` method by passing
424 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
425 /// as the `order`. For example,
426 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
1a4d82fc 427 pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
428 /// Bitwise and with the current value, returning the previous value.
429 /// The stabilized version of this intrinsic is available on the
430 /// `std::sync::atomic` types via the `fetch_and` method by passing
431 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
432 /// as the `order`. For example,
433 /// [`AtomicBool::fetch_and`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_and).
1a4d82fc
JJ
434 pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T;
435
476ff2be
SL
436 /// Bitwise nand with the current value, returning the previous value.
437 /// The stabilized version of this intrinsic is available on the
438 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
439 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
440 /// as the `order`. For example,
441 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
1a4d82fc 442 pub fn atomic_nand<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
443 /// Bitwise nand with the current value, returning the previous value.
444 /// The stabilized version of this intrinsic is available on the
445 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
446 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
447 /// as the `order`. For example,
448 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
1a4d82fc 449 pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
450 /// Bitwise nand with the current value, returning the previous value.
451 /// The stabilized version of this intrinsic is available on the
452 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
453 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
454 /// as the `order`. For example,
455 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
1a4d82fc 456 pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
457 /// Bitwise nand with the current value, returning the previous value.
458 /// The stabilized version of this intrinsic is available on the
459 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
460 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
461 /// as the `order`. For example,
462 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
1a4d82fc 463 pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
464 /// Bitwise nand with the current value, returning the previous value.
465 /// The stabilized version of this intrinsic is available on the
466 /// `std::sync::atomic::AtomicBool` type via the `fetch_nand` method by passing
467 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
468 /// as the `order`. For example,
469 /// [`AtomicBool::fetch_nand`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_nand).
1a4d82fc
JJ
470 pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T;
471
476ff2be
SL
472 /// Bitwise or with the current value, returning the previous value.
473 /// The stabilized version of this intrinsic is available on the
474 /// `std::sync::atomic` types via the `fetch_or` method by passing
475 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
476 /// as the `order`. For example,
477 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
1a4d82fc 478 pub fn atomic_or<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
479 /// Bitwise or with the current value, returning the previous value.
480 /// The stabilized version of this intrinsic is available on the
481 /// `std::sync::atomic` types via the `fetch_or` method by passing
482 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
483 /// as the `order`. For example,
484 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
1a4d82fc 485 pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
486 /// Bitwise or with the current value, returning the previous value.
487 /// The stabilized version of this intrinsic is available on the
488 /// `std::sync::atomic` types via the `fetch_or` method by passing
489 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
490 /// as the `order`. For example,
491 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
1a4d82fc 492 pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
493 /// Bitwise or with the current value, returning the previous value.
494 /// The stabilized version of this intrinsic is available on the
495 /// `std::sync::atomic` types via the `fetch_or` method by passing
496 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
497 /// as the `order`. For example,
498 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
1a4d82fc 499 pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
500 /// Bitwise or with the current value, returning the previous value.
501 /// The stabilized version of this intrinsic is available on the
502 /// `std::sync::atomic` types via the `fetch_or` method by passing
503 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
504 /// as the `order`. For example,
505 /// [`AtomicBool::fetch_or`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_or).
1a4d82fc
JJ
506 pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T;
507
476ff2be
SL
508 /// Bitwise xor with the current value, returning the previous value.
509 /// The stabilized version of this intrinsic is available on the
510 /// `std::sync::atomic` types via the `fetch_xor` method by passing
511 /// [`Ordering::SeqCst`](../../std/sync/atomic/enum.Ordering.html)
512 /// as the `order`. For example,
513 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
1a4d82fc 514 pub fn atomic_xor<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
515 /// Bitwise xor with the current value, returning the previous value.
516 /// The stabilized version of this intrinsic is available on the
517 /// `std::sync::atomic` types via the `fetch_xor` method by passing
518 /// [`Ordering::Acquire`](../../std/sync/atomic/enum.Ordering.html)
519 /// as the `order`. For example,
520 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
1a4d82fc 521 pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
522 /// Bitwise xor with the current value, returning the previous value.
523 /// The stabilized version of this intrinsic is available on the
524 /// `std::sync::atomic` types via the `fetch_xor` method by passing
525 /// [`Ordering::Release`](../../std/sync/atomic/enum.Ordering.html)
526 /// as the `order`. For example,
527 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
1a4d82fc 528 pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
529 /// Bitwise xor with the current value, returning the previous value.
530 /// The stabilized version of this intrinsic is available on the
531 /// `std::sync::atomic` types via the `fetch_xor` method by passing
532 /// [`Ordering::AcqRel`](../../std/sync/atomic/enum.Ordering.html)
533 /// as the `order`. For example,
534 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
1a4d82fc 535 pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T;
476ff2be
SL
536 /// Bitwise xor with the current value, returning the previous value.
537 /// The stabilized version of this intrinsic is available on the
538 /// `std::sync::atomic` types via the `fetch_xor` method by passing
539 /// [`Ordering::Relaxed`](../../std/sync/atomic/enum.Ordering.html)
540 /// as the `order`. For example,
541 /// [`AtomicBool::fetch_xor`](../../std/sync/atomic/struct.AtomicBool.html#method.fetch_xor).
1a4d82fc
JJ
542 pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T;
543
544 pub fn atomic_max<T>(dst: *mut T, src: T) -> T;
545 pub fn atomic_max_acq<T>(dst: *mut T, src: T) -> T;
546 pub fn atomic_max_rel<T>(dst: *mut T, src: T) -> T;
547 pub fn atomic_max_acqrel<T>(dst: *mut T, src: T) -> T;
548 pub fn atomic_max_relaxed<T>(dst: *mut T, src: T) -> T;
549
550 pub fn atomic_min<T>(dst: *mut T, src: T) -> T;
551 pub fn atomic_min_acq<T>(dst: *mut T, src: T) -> T;
552 pub fn atomic_min_rel<T>(dst: *mut T, src: T) -> T;
553 pub fn atomic_min_acqrel<T>(dst: *mut T, src: T) -> T;
554 pub fn atomic_min_relaxed<T>(dst: *mut T, src: T) -> T;
555
556 pub fn atomic_umin<T>(dst: *mut T, src: T) -> T;
557 pub fn atomic_umin_acq<T>(dst: *mut T, src: T) -> T;
558 pub fn atomic_umin_rel<T>(dst: *mut T, src: T) -> T;
559 pub fn atomic_umin_acqrel<T>(dst: *mut T, src: T) -> T;
560 pub fn atomic_umin_relaxed<T>(dst: *mut T, src: T) -> T;
561
562 pub fn atomic_umax<T>(dst: *mut T, src: T) -> T;
563 pub fn atomic_umax_acq<T>(dst: *mut T, src: T) -> T;
564 pub fn atomic_umax_rel<T>(dst: *mut T, src: T) -> T;
565 pub fn atomic_umax_acqrel<T>(dst: *mut T, src: T) -> T;
566 pub fn atomic_umax_relaxed<T>(dst: *mut T, src: T) -> T;
7cac9316
XL
567
568 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
569 /// if supported; otherwise, it is a noop.
570 /// Prefetches have no effect on the behavior of the program but can change its performance
571 /// characteristics.
572 ///
573 /// The `locality` argument must be a constant integer and is a temporal locality specifier
574 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
7cac9316
XL
575 pub fn prefetch_read_data<T>(data: *const T, locality: i32);
576 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
577 /// if supported; otherwise, it is a noop.
578 /// Prefetches have no effect on the behavior of the program but can change its performance
579 /// characteristics.
580 ///
581 /// The `locality` argument must be a constant integer and is a temporal locality specifier
582 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
7cac9316
XL
583 pub fn prefetch_write_data<T>(data: *const T, locality: i32);
584 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
585 /// if supported; otherwise, it is a noop.
586 /// Prefetches have no effect on the behavior of the program but can change its performance
587 /// characteristics.
588 ///
589 /// The `locality` argument must be a constant integer and is a temporal locality specifier
590 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
7cac9316
XL
591 pub fn prefetch_read_instruction<T>(data: *const T, locality: i32);
592 /// The `prefetch` intrinsic is a hint to the code generator to insert a prefetch instruction
593 /// if supported; otherwise, it is a noop.
594 /// Prefetches have no effect on the behavior of the program but can change its performance
595 /// characteristics.
596 ///
597 /// The `locality` argument must be a constant integer and is a temporal locality specifier
598 /// ranging from (0) - no locality, to (3) - extremely local keep in cache
7cac9316 599 pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
1a4d82fc
JJ
600}
601
602extern "rust-intrinsic" {
603
604 pub fn atomic_fence();
605 pub fn atomic_fence_acq();
606 pub fn atomic_fence_rel();
607 pub fn atomic_fence_acqrel();
608
d9579d0f
AL
609 /// A compiler-only memory barrier.
610 ///
62682a34
SL
611 /// Memory accesses will never be reordered across this barrier by the
612 /// compiler, but no instructions will be emitted for it. This is
613 /// appropriate for operations on the same thread that may be preempted,
614 /// such as when interacting with signal handlers.
d9579d0f 615 pub fn atomic_singlethreadfence();
d9579d0f 616 pub fn atomic_singlethreadfence_acq();
d9579d0f 617 pub fn atomic_singlethreadfence_rel();
d9579d0f
AL
618 pub fn atomic_singlethreadfence_acqrel();
619
3157f602
XL
620 /// Magic intrinsic that derives its meaning from attributes
621 /// attached to the function.
622 ///
623 /// For example, dataflow uses this to inject static assertions so
624 /// that `rustc_peek(potentially_uninitialized)` would actually
625 /// double-check that dataflow did indeed compute that it is
626 /// uninitialized at that point in the control flow.
3157f602
XL
627 pub fn rustc_peek<T>(_: T) -> T;
628
9346a6ac 629 /// Aborts the execution of the process.
abe05a73
XL
630 ///
631 /// The stabilized version of this intrinsic is
632 /// [`std::process::abort`](../../std/process/fn.abort.html)
1a4d82fc
JJ
633 pub fn abort() -> !;
634
3b2f2976
XL
635 /// Tells LLVM that this point in the code is not reachable, enabling
636 /// further optimizations.
1a4d82fc 637 ///
3b2f2976
XL
638 /// NB: This is very different from the `unreachable!()` macro: Unlike the
639 /// macro, which panics when it is executed, it is *undefined behavior* to
640 /// reach code marked with this function.
1a4d82fc
JJ
641 pub fn unreachable() -> !;
642
9346a6ac 643 /// Informs the optimizer that a condition is always true.
1a4d82fc
JJ
644 /// If the condition is false, the behavior is undefined.
645 ///
646 /// No code is generated for this intrinsic, but the optimizer will try
647 /// to preserve it (and its condition) between passes, which may interfere
648 /// with optimization of surrounding code and reduce performance. It should
649 /// not be used if the invariant can be discovered by the optimizer on its
650 /// own, or if it does not enable any significant optimizations.
651 pub fn assume(b: bool);
652
9e0c209e
SL
653 /// Hints to the compiler that branch condition is likely to be true.
654 /// Returns the value passed to it.
655 ///
656 /// Any use other than with `if` statements will probably not have an effect.
657 pub fn likely(b: bool) -> bool;
658
9e0c209e
SL
659 /// Hints to the compiler that branch condition is likely to be false.
660 /// Returns the value passed to it.
661 ///
662 /// Any use other than with `if` statements will probably not have an effect.
663 pub fn unlikely(b: bool) -> bool;
664
9346a6ac 665 /// Executes a breakpoint trap, for inspection by a debugger.
1a4d82fc
JJ
666 pub fn breakpoint();
667
668 /// The size of a type in bytes.
669 ///
a7813a04
XL
670 /// More specifically, this is the offset in bytes between successive
671 /// items of the same type, including alignment padding.
85aaf69f 672 pub fn size_of<T>() -> usize;
1a4d82fc 673
9346a6ac 674 /// Moves a value to an uninitialized memory location.
1a4d82fc
JJ
675 ///
676 /// Drop glue is not run on the destination.
c1a9b12d 677 pub fn move_val_init<T>(dst: *mut T, src: T);
1a4d82fc 678
85aaf69f
SL
679 pub fn min_align_of<T>() -> usize;
680 pub fn pref_align_of<T>() -> usize;
1a4d82fc 681
abe05a73
XL
682 /// The size of the referenced value in bytes.
683 ///
684 /// The stabilized version of this intrinsic is
685 /// [`std::mem::size_of_val`](../../std/mem/fn.size_of_val.html).
d9579d0f 686 pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
d9579d0f 687 pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
92a42be0 688
c34b1796
AL
689 /// Gets a static string slice containing the name of a type.
690 pub fn type_name<T: ?Sized>() -> &'static str;
1a4d82fc
JJ
691
692 /// Gets an identifier which is globally unique to the specified type. This
693 /// function will return the same value for a type regardless of whichever
694 /// crate it is invoked in.
85aaf69f 695 pub fn type_id<T: ?Sized + 'static>() -> u64;
1a4d82fc 696
9346a6ac 697 /// Creates a value initialized to zero.
1a4d82fc
JJ
698 ///
699 /// `init` is unsafe because it returns a zeroed-out datum,
c34b1796
AL
700 /// which is unsafe unless T is `Copy`. Also, even if T is
701 /// `Copy`, an all-zero value may not correspond to any legitimate
702 /// state for the type in question.
1a4d82fc
JJ
703 pub fn init<T>() -> T;
704
9346a6ac 705 /// Creates an uninitialized value.
c34b1796
AL
706 ///
707 /// `uninit` is unsafe because there is no guarantee of what its
708 /// contents are. In particular its drop-flag may be set to any
709 /// state, which means it may claim either dropped or
710 /// undropped. In the general case one must use `ptr::write` to
711 /// initialize memory previous set to the result of `uninit`.
1a4d82fc
JJ
712 pub fn uninit<T>() -> T;
713
9e0c209e
SL
714 /// Reinterprets the bits of a value of one type as another type.
715 ///
716 /// Both types must have the same size. Neither the original, nor the result,
717 /// may be an [invalid value](../../nomicon/meet-safe-and-unsafe.html).
1a4d82fc 718 ///
5bcae85e 719 /// `transmute` is semantically equivalent to a bitwise move of one type
9e0c209e
SL
720 /// into another. It copies the bits from the source value into the
721 /// destination value, then forgets the original. It's equivalent to C's
722 /// `memcpy` under the hood, just like `transmute_copy`.
5bcae85e 723 ///
9e0c209e
SL
724 /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
725 /// cause [undefined behavior][ub] with this function. `transmute` should be
5bcae85e
SL
726 /// the absolute last resort.
727 ///
728 /// The [nomicon](../../nomicon/transmutes.html) has additional
729 /// documentation.
1a4d82fc 730 ///
8bb4bdeb 731 /// [ub]: ../../reference/behavior-considered-undefined.html
9e0c209e 732 ///
1a4d82fc
JJ
733 /// # Examples
734 ///
5bcae85e
SL
735 /// There are a few things that `transmute` is really useful for.
736 ///
737 /// Getting the bitpattern of a floating point type (or, more generally,
738 /// type punning, when `T` and `U` aren't pointers):
739 ///
740 /// ```
741 /// let bitpattern = unsafe {
742 /// std::mem::transmute::<f32, u32>(1.0)
743 /// };
744 /// assert_eq!(bitpattern, 0x3F800000);
745 /// ```
746 ///
9e0c209e
SL
747 /// Turning a pointer into a function pointer. This is *not* portable to
748 /// machines where function pointers and data pointers have different sizes.
5bcae85e
SL
749 ///
750 /// ```
751 /// fn foo() -> i32 {
752 /// 0
753 /// }
754 /// let pointer = foo as *const ();
755 /// let function = unsafe {
756 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
757 /// };
758 /// assert_eq!(function(), 0);
759 /// ```
760 ///
9e0c209e
SL
761 /// Extending a lifetime, or shortening an invariant lifetime. This is
762 /// advanced, very unsafe Rust!
5bcae85e
SL
763 ///
764 /// ```
765 /// struct R<'a>(&'a i32);
766 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
767 /// std::mem::transmute::<R<'b>, R<'static>>(r)
768 /// }
769 ///
770 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
771 /// -> &'b mut R<'c> {
772 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
773 /// }
774 /// ```
775 ///
776 /// # Alternatives
777 ///
9e0c209e
SL
778 /// Don't despair: many uses of `transmute` can be achieved through other means.
779 /// Below are common applications of `transmute` which can be replaced with safer
780 /// constructs.
5bcae85e
SL
781 ///
782 /// Turning a pointer into a `usize`:
783 ///
784 /// ```
785 /// let ptr = &0;
786 /// let ptr_num_transmute = unsafe {
787 /// std::mem::transmute::<&i32, usize>(ptr)
788 /// };
9e0c209e 789 ///
5bcae85e
SL
790 /// // Use an `as` cast instead
791 /// let ptr_num_cast = ptr as *const i32 as usize;
792 /// ```
793 ///
794 /// Turning a `*mut T` into an `&mut T`:
795 ///
796 /// ```
797 /// let ptr: *mut i32 = &mut 0;
798 /// let ref_transmuted = unsafe {
799 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
800 /// };
9e0c209e 801 ///
5bcae85e
SL
802 /// // Use a reborrow instead
803 /// let ref_casted = unsafe { &mut *ptr };
804 /// ```
805 ///
806 /// Turning an `&mut T` into an `&mut U`:
807 ///
808 /// ```
809 /// let ptr = &mut 0;
810 /// let val_transmuted = unsafe {
811 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
812 /// };
9e0c209e 813 ///
5bcae85e
SL
814 /// // Now, put together `as` and reborrowing - note the chaining of `as`
815 /// // `as` is not transitive
816 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
817 /// ```
818 ///
819 /// Turning an `&str` into an `&[u8]`:
820 ///
821 /// ```
822 /// // this is not a good way to do this.
823 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
824 /// assert_eq!(slice, &[82, 117, 115, 116]);
9e0c209e 825 ///
5bcae85e
SL
826 /// // You could use `str::as_bytes`
827 /// let slice = "Rust".as_bytes();
828 /// assert_eq!(slice, &[82, 117, 115, 116]);
9e0c209e 829 ///
5bcae85e
SL
830 /// // Or, just use a byte string, if you have control over the string
831 /// // literal
832 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
833 /// ```
834 ///
835 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
836 ///
85aaf69f 837 /// ```
5bcae85e
SL
838 /// let store = [0, 1, 2, 3];
839 /// let mut v_orig = store.iter().collect::<Vec<&i32>>();
9e0c209e 840 ///
5bcae85e
SL
841 /// // Using transmute: this is Undefined Behavior, and a bad idea.
842 /// // However, it is no-copy.
843 /// let v_transmuted = unsafe {
844 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(
845 /// v_orig.clone())
846 /// };
9e0c209e 847 ///
5bcae85e 848 /// // This is the suggested, safe way.
9e0c209e 849 /// // It does copy the entire vector, though, into a new array.
5bcae85e
SL
850 /// let v_collected = v_orig.clone()
851 /// .into_iter()
852 /// .map(|r| Some(r))
853 /// .collect::<Vec<Option<&i32>>>();
9e0c209e 854 ///
5bcae85e
SL
855 /// // The no-copy, unsafe way, still using transmute, but not UB.
856 /// // This is equivalent to the original, but safer, and reuses the
857 /// // same Vec internals. Therefore the new inner type must have the
ea8adc8c
XL
858 /// // exact same size, and the same alignment, as the old type.
859 /// // The same caveats exist for this method as transmute, for
5bcae85e
SL
860 /// // the original inner type (`&i32`) to the converted inner type
861 /// // (`Option<&i32>`), so read the nomicon pages linked above.
862 /// let v_from_raw = unsafe {
ea8adc8c 863 /// Vec::from_raw_parts(v_orig.as_mut_ptr() as *mut Option<&i32>,
5bcae85e
SL
864 /// v_orig.len(),
865 /// v_orig.capacity())
866 /// };
867 /// std::mem::forget(v_orig);
868 /// ```
869 ///
870 /// Implementing `split_at_mut`:
1a4d82fc 871 ///
5bcae85e
SL
872 /// ```
873 /// use std::{slice, mem};
9e0c209e 874 ///
5bcae85e
SL
875 /// // There are multiple ways to do this; and there are multiple problems
876 /// // with the following, transmute, way.
877 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
878 /// -> (&mut [T], &mut [T]) {
879 /// let len = slice.len();
880 /// assert!(mid <= len);
881 /// unsafe {
882 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
883 /// // first: transmute is not typesafe; all it checks is that T and
884 /// // U are of the same size. Second, right here, you have two
885 /// // mutable references pointing to the same memory.
886 /// (&mut slice[0..mid], &mut slice2[mid..len])
887 /// }
888 /// }
9e0c209e 889 ///
5bcae85e
SL
890 /// // This gets rid of the typesafety problems; `&mut *` will *only* give
891 /// // you an `&mut T` from an `&mut T` or `*mut T`.
892 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
893 /// -> (&mut [T], &mut [T]) {
894 /// let len = slice.len();
895 /// assert!(mid <= len);
896 /// unsafe {
897 /// let slice2 = &mut *(slice as *mut [T]);
898 /// // however, you still have two mutable references pointing to
899 /// // the same memory.
900 /// (&mut slice[0..mid], &mut slice2[mid..len])
901 /// }
902 /// }
9e0c209e 903 ///
5bcae85e
SL
904 /// // This is how the standard library does it. This is the best method, if
905 /// // you need to do something like this
906 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
907 /// -> (&mut [T], &mut [T]) {
908 /// let len = slice.len();
909 /// assert!(mid <= len);
910 /// unsafe {
911 /// let ptr = slice.as_mut_ptr();
912 /// // This now has three mutable references pointing at the same
913 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
914 /// // `slice` is never used after `let ptr = ...`, and so one can
915 /// // treat it as "dead", and therefore, you only have two real
916 /// // mutable slices.
917 /// (slice::from_raw_parts_mut(ptr, mid),
918 /// slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
919 /// }
920 /// }
1a4d82fc 921 /// ```
85aaf69f 922 #[stable(feature = "rust1", since = "1.0.0")]
e9174d1e 923 pub fn transmute<T, U>(e: T) -> U;
1a4d82fc 924
c34b1796
AL
925 /// Returns `true` if the actual type given as `T` requires drop
926 /// glue; returns `false` if the actual type provided for `T`
927 /// implements `Copy`.
928 ///
929 /// If the actual type neither requires drop glue nor implements
930 /// `Copy`, then may return `true` or `false`.
abe05a73
XL
931 ///
932 /// The stabilized version of this intrinsic is
933 /// [`std::mem::needs_drop`](../../std/mem/fn.needs_drop.html).
1a4d82fc
JJ
934 pub fn needs_drop<T>() -> bool;
935
d9579d0f 936 /// Calculates the offset from a pointer.
1a4d82fc
JJ
937 ///
938 /// This is implemented as an intrinsic to avoid converting to and from an
939 /// integer, since the conversion would throw away aliasing information.
d9579d0f
AL
940 ///
941 /// # Safety
942 ///
943 /// Both the starting and resulting pointer must be either in bounds or one
944 /// byte past the end of an allocated object. If either pointer is out of
945 /// bounds or arithmetic overflow occurs then any further use of the
946 /// returned value will result in undefined behavior.
85aaf69f 947 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
1a4d82fc 948
62682a34
SL
949 /// Calculates the offset from a pointer, potentially wrapping.
950 ///
951 /// This is implemented as an intrinsic to avoid converting to and from an
952 /// integer, since the conversion inhibits certain optimizations.
953 ///
954 /// # Safety
955 ///
956 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
957 /// resulting pointer to point into or one byte past the end of an allocated
958 /// object, and it wraps with two's complement arithmetic. The resulting
959 /// value is not necessarily valid to be used to actually access memory.
960 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
961
1a4d82fc
JJ
962 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
963 /// and destination may *not* overlap.
964 ///
c34b1796 965 /// `copy_nonoverlapping` is semantically equivalent to C's `memcpy`.
1a4d82fc
JJ
966 ///
967 /// # Safety
968 ///
85aaf69f 969 /// Beyond requiring that the program must be allowed to access both regions
b039eaaf 970 /// of memory, it is Undefined Behavior for source and destination to
85aaf69f
SL
971 /// overlap. Care must also be taken with the ownership of `src` and
972 /// `dst`. This method semantically moves the values of `src` into `dst`.
973 /// However it does not drop the contents of `dst`, or prevent the contents
974 /// of `src` from being dropped or used.
1a4d82fc
JJ
975 ///
976 /// # Examples
977 ///
978 /// A safe swap function:
979 ///
980 /// ```
981 /// use std::mem;
982 /// use std::ptr;
983 ///
92a42be0 984 /// # #[allow(dead_code)]
1a4d82fc
JJ
985 /// fn swap<T>(x: &mut T, y: &mut T) {
986 /// unsafe {
987 /// // Give ourselves some scratch space to work with
988 /// let mut t: T = mem::uninitialized();
989 ///
990 /// // Perform the swap, `&mut` pointers never alias
c34b1796
AL
991 /// ptr::copy_nonoverlapping(x, &mut t, 1);
992 /// ptr::copy_nonoverlapping(y, x, 1);
993 /// ptr::copy_nonoverlapping(&t, y, 1);
1a4d82fc 994 ///
2c00a5a8 995 /// // y and t now point to the same thing, but we need to completely forget `t`
1a4d82fc
JJ
996 /// // because it's no longer relevant.
997 /// mem::forget(t);
998 /// }
999 /// }
1000 /// ```
c34b1796 1001 #[stable(feature = "rust1", since = "1.0.0")]
c34b1796
AL
1002 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1003
1a4d82fc
JJ
1004 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
1005 /// and destination may overlap.
1006 ///
c34b1796 1007 /// `copy` is semantically equivalent to C's `memmove`.
1a4d82fc
JJ
1008 ///
1009 /// # Safety
1010 ///
1011 /// Care must be taken with the ownership of `src` and `dst`.
1012 /// This method semantically moves the values of `src` into `dst`.
1013 /// However it does not drop the contents of `dst`, or prevent the contents of `src`
1014 /// from being dropped or used.
1015 ///
1016 /// # Examples
1017 ///
1018 /// Efficiently create a Rust vector from an unsafe buffer:
1019 ///
1020 /// ```
1021 /// use std::ptr;
1022 ///
92a42be0 1023 /// # #[allow(dead_code)]
c34b1796 1024 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1a4d82fc
JJ
1025 /// let mut dst = Vec::with_capacity(elts);
1026 /// dst.set_len(elts);
c34b1796 1027 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1a4d82fc
JJ
1028 /// dst
1029 /// }
1030 /// ```
1031 ///
c34b1796 1032 #[stable(feature = "rust1", since = "1.0.0")]
c34b1796
AL
1033 pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
1034
1a4d82fc 1035 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
92a42be0 1036 /// bytes of memory starting at `dst` to `val`.
c30ab7b3
SL
1037 ///
1038 /// # Examples
1039 ///
1040 /// ```
1041 /// use std::ptr;
1042 ///
1043 /// let mut vec = vec![0; 4];
1044 /// unsafe {
1045 /// let vec_ptr = vec.as_mut_ptr();
1046 /// ptr::write_bytes(vec_ptr, b'a', 2);
1047 /// }
1048 /// assert_eq!(vec, [b'a', b'a', 0, 0]);
1049 /// ```
c34b1796
AL
1050 #[stable(feature = "rust1", since = "1.0.0")]
1051 pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1a4d82fc
JJ
1052
1053 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1054 /// a size of `count` * `size_of::<T>()` and an alignment of
1055 /// `min_align_of::<T>()`
1056 ///
3b2f2976
XL
1057 /// The volatile parameter is set to `true`, so it will not be optimized out
1058 /// unless size is equal to zero.
1a4d82fc 1059 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
85aaf69f 1060 count: usize);
1a4d82fc
JJ
1061 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1062 /// a size of `count` * `size_of::<T>()` and an alignment of
1063 /// `min_align_of::<T>()`
1064 ///
3b2f2976
XL
1065 /// The volatile parameter is set to `true`, so it will not be optimized out
1066 /// unless size is equal to zero..
85aaf69f 1067 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1a4d82fc
JJ
1068 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1069 /// size of `count` * `size_of::<T>()` and an alignment of
1070 /// `min_align_of::<T>()`.
1071 ///
3b2f2976
XL
1072 /// The volatile parameter is set to `true`, so it will not be optimized out
1073 /// unless size is equal to zero.
85aaf69f 1074 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1a4d82fc
JJ
1075
1076 /// Perform a volatile load from the `src` pointer.
476ff2be
SL
1077 /// The stabilized version of this intrinsic is
1078 /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1a4d82fc
JJ
1079 pub fn volatile_load<T>(src: *const T) -> T;
1080 /// Perform a volatile store to the `dst` pointer.
476ff2be
SL
1081 /// The stabilized version of this intrinsic is
1082 /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1a4d82fc
JJ
1083 pub fn volatile_store<T>(dst: *mut T, val: T);
1084
1085 /// Returns the square root of an `f32`
1086 pub fn sqrtf32(x: f32) -> f32;
1087 /// Returns the square root of an `f64`
1088 pub fn sqrtf64(x: f64) -> f64;
1089
1090 /// Raises an `f32` to an integer power.
1091 pub fn powif32(a: f32, x: i32) -> f32;
1092 /// Raises an `f64` to an integer power.
1093 pub fn powif64(a: f64, x: i32) -> f64;
1094
1095 /// Returns the sine of an `f32`.
1096 pub fn sinf32(x: f32) -> f32;
1097 /// Returns the sine of an `f64`.
1098 pub fn sinf64(x: f64) -> f64;
1099
1100 /// Returns the cosine of an `f32`.
1101 pub fn cosf32(x: f32) -> f32;
1102 /// Returns the cosine of an `f64`.
1103 pub fn cosf64(x: f64) -> f64;
1104
1105 /// Raises an `f32` to an `f32` power.
1106 pub fn powf32(a: f32, x: f32) -> f32;
1107 /// Raises an `f64` to an `f64` power.
1108 pub fn powf64(a: f64, x: f64) -> f64;
1109
1110 /// Returns the exponential of an `f32`.
1111 pub fn expf32(x: f32) -> f32;
1112 /// Returns the exponential of an `f64`.
1113 pub fn expf64(x: f64) -> f64;
1114
1115 /// Returns 2 raised to the power of an `f32`.
1116 pub fn exp2f32(x: f32) -> f32;
1117 /// Returns 2 raised to the power of an `f64`.
1118 pub fn exp2f64(x: f64) -> f64;
1119
1120 /// Returns the natural logarithm of an `f32`.
1121 pub fn logf32(x: f32) -> f32;
1122 /// Returns the natural logarithm of an `f64`.
1123 pub fn logf64(x: f64) -> f64;
1124
1125 /// Returns the base 10 logarithm of an `f32`.
1126 pub fn log10f32(x: f32) -> f32;
1127 /// Returns the base 10 logarithm of an `f64`.
1128 pub fn log10f64(x: f64) -> f64;
1129
1130 /// Returns the base 2 logarithm of an `f32`.
1131 pub fn log2f32(x: f32) -> f32;
1132 /// Returns the base 2 logarithm of an `f64`.
1133 pub fn log2f64(x: f64) -> f64;
1134
1135 /// Returns `a * b + c` for `f32` values.
1136 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1137 /// Returns `a * b + c` for `f64` values.
1138 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1139
1140 /// Returns the absolute value of an `f32`.
1141 pub fn fabsf32(x: f32) -> f32;
1142 /// Returns the absolute value of an `f64`.
1143 pub fn fabsf64(x: f64) -> f64;
1144
1145 /// Copies the sign from `y` to `x` for `f32` values.
1146 pub fn copysignf32(x: f32, y: f32) -> f32;
1147 /// Copies the sign from `y` to `x` for `f64` values.
1148 pub fn copysignf64(x: f64, y: f64) -> f64;
1149
1150 /// Returns the largest integer less than or equal to an `f32`.
1151 pub fn floorf32(x: f32) -> f32;
1152 /// Returns the largest integer less than or equal to an `f64`.
1153 pub fn floorf64(x: f64) -> f64;
1154
1155 /// Returns the smallest integer greater than or equal to an `f32`.
1156 pub fn ceilf32(x: f32) -> f32;
1157 /// Returns the smallest integer greater than or equal to an `f64`.
1158 pub fn ceilf64(x: f64) -> f64;
1159
1160 /// Returns the integer part of an `f32`.
1161 pub fn truncf32(x: f32) -> f32;
1162 /// Returns the integer part of an `f64`.
1163 pub fn truncf64(x: f64) -> f64;
1164
1165 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1166 /// if the argument is not an integer.
1167 pub fn rintf32(x: f32) -> f32;
1168 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1169 /// if the argument is not an integer.
1170 pub fn rintf64(x: f64) -> f64;
1171
1172 /// Returns the nearest integer to an `f32`.
1173 pub fn nearbyintf32(x: f32) -> f32;
1174 /// Returns the nearest integer to an `f64`.
1175 pub fn nearbyintf64(x: f64) -> f64;
1176
1177 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1178 pub fn roundf32(x: f32) -> f32;
1179 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1180 pub fn roundf64(x: f64) -> f64;
1181
54a0048b
SL
1182 /// Float addition that allows optimizations based on algebraic rules.
1183 /// May assume inputs are finite.
54a0048b
SL
1184 pub fn fadd_fast<T>(a: T, b: T) -> T;
1185
1186 /// Float subtraction that allows optimizations based on algebraic rules.
1187 /// May assume inputs are finite.
54a0048b
SL
1188 pub fn fsub_fast<T>(a: T, b: T) -> T;
1189
1190 /// Float multiplication that allows optimizations based on algebraic rules.
1191 /// May assume inputs are finite.
54a0048b
SL
1192 pub fn fmul_fast<T>(a: T, b: T) -> T;
1193
1194 /// Float division that allows optimizations based on algebraic rules.
1195 /// May assume inputs are finite.
54a0048b
SL
1196 pub fn fdiv_fast<T>(a: T, b: T) -> T;
1197
1198 /// Float remainder that allows optimizations based on algebraic rules.
1199 /// May assume inputs are finite.
54a0048b
SL
1200 pub fn frem_fast<T>(a: T, b: T) -> T;
1201
1202
92a42be0 1203 /// Returns the number of bits set in an integer type `T`
92a42be0 1204 pub fn ctpop<T>(x: T) -> T;
1a4d82fc 1205
32a655c1
SL
1206 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1207 ///
1208 /// # Examples
1209 ///
1210 /// ```
1211 /// #![feature(core_intrinsics)]
1212 ///
1213 /// use std::intrinsics::ctlz;
1214 ///
1215 /// let x = 0b0001_1100_u8;
1216 /// let num_leading = unsafe { ctlz(x) };
1217 /// assert_eq!(num_leading, 3);
1218 /// ```
1219 ///
1220 /// An `x` with value `0` will return the bit width of `T`.
1221 ///
1222 /// ```
1223 /// #![feature(core_intrinsics)]
1224 ///
1225 /// use std::intrinsics::ctlz;
1226 ///
1227 /// let x = 0u16;
1228 /// let num_leading = unsafe { ctlz(x) };
1229 /// assert_eq!(num_leading, 16);
1230 /// ```
92a42be0 1231 pub fn ctlz<T>(x: T) -> T;
1a4d82fc 1232
041b39d2
XL
1233 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1234 /// given an `x` with value `0`.
1235 ///
1236 /// # Examples
1237 ///
1238 /// ```
1239 /// #![feature(core_intrinsics)]
1240 ///
1241 /// use std::intrinsics::ctlz_nonzero;
1242 ///
1243 /// let x = 0b0001_1100_u8;
1244 /// let num_leading = unsafe { ctlz_nonzero(x) };
1245 /// assert_eq!(num_leading, 3);
1246 /// ```
041b39d2
XL
1247 pub fn ctlz_nonzero<T>(x: T) -> T;
1248
32a655c1
SL
1249 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1250 ///
1251 /// # Examples
1252 ///
1253 /// ```
1254 /// #![feature(core_intrinsics)]
1255 ///
1256 /// use std::intrinsics::cttz;
1257 ///
1258 /// let x = 0b0011_1000_u8;
1259 /// let num_trailing = unsafe { cttz(x) };
1260 /// assert_eq!(num_trailing, 3);
1261 /// ```
1262 ///
1263 /// An `x` with value `0` will return the bit width of `T`:
1264 ///
1265 /// ```
1266 /// #![feature(core_intrinsics)]
1267 ///
1268 /// use std::intrinsics::cttz;
1269 ///
1270 /// let x = 0u16;
1271 /// let num_trailing = unsafe { cttz(x) };
1272 /// assert_eq!(num_trailing, 16);
1273 /// ```
92a42be0 1274 pub fn cttz<T>(x: T) -> T;
1a4d82fc 1275
041b39d2
XL
1276 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1277 /// given an `x` with value `0`.
1278 ///
1279 /// # Examples
1280 ///
1281 /// ```
1282 /// #![feature(core_intrinsics)]
1283 ///
1284 /// use std::intrinsics::cttz_nonzero;
1285 ///
1286 /// let x = 0b0011_1000_u8;
1287 /// let num_trailing = unsafe { cttz_nonzero(x) };
1288 /// assert_eq!(num_trailing, 3);
1289 /// ```
041b39d2
XL
1290 pub fn cttz_nonzero<T>(x: T) -> T;
1291
92a42be0 1292 /// Reverses the bytes in an integer type `T`.
92a42be0 1293 pub fn bswap<T>(x: T) -> T;
1a4d82fc 1294
0531ce1d
XL
1295 /// Reverses the bits in an integer type `T`.
1296 #[cfg(not(stage0))]
1297 pub fn bitreverse<T>(x: T) -> T;
1298
92a42be0 1299 /// Performs checked integer addition.
476ff2be
SL
1300 /// The stabilized versions of this intrinsic are available on the integer
1301 /// primitives via the `overflowing_add` method. For example,
1302 /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
92a42be0
SL
1303 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
1304
92a42be0 1305 /// Performs checked integer subtraction
476ff2be
SL
1306 /// The stabilized versions of this intrinsic are available on the integer
1307 /// primitives via the `overflowing_sub` method. For example,
1308 /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
92a42be0
SL
1309 pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
1310
92a42be0 1311 /// Performs checked integer multiplication
476ff2be
SL
1312 /// The stabilized versions of this intrinsic are available on the integer
1313 /// primitives via the `overflowing_mul` method. For example,
1314 /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
92a42be0
SL
1315 pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
1316
0531ce1d
XL
1317 /// Performs an exact division, resulting in undefined behavior where
1318 /// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
1319 #[cfg(not(stage0))]
1320 pub fn exact_div<T>(x: T, y: T) -> T;
1321
92a42be0
SL
1322 /// Performs an unchecked division, resulting in undefined behavior
1323 /// where y = 0 or x = `T::min_value()` and y = -1
92a42be0
SL
1324 pub fn unchecked_div<T>(x: T, y: T) -> T;
1325 /// Returns the remainder of an unchecked division, resulting in
1326 /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
92a42be0
SL
1327 pub fn unchecked_rem<T>(x: T, y: T) -> T;
1328
cc61c64b
XL
1329 /// Performs an unchecked left shift, resulting in undefined behavior when
1330 /// y < 0 or y >= N, where N is the width of T in bits.
cc61c64b
XL
1331 pub fn unchecked_shl<T>(x: T, y: T) -> T;
1332 /// Performs an unchecked right shift, resulting in undefined behavior when
1333 /// y < 0 or y >= N, where N is the width of T in bits.
cc61c64b
XL
1334 pub fn unchecked_shr<T>(x: T, y: T) -> T;
1335
1336 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
476ff2be
SL
1337 /// The stabilized versions of this intrinsic are available on the integer
1338 /// primitives via the `wrapping_add` method. For example,
1339 /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
c34b1796 1340 pub fn overflowing_add<T>(a: T, b: T) -> T;
cc61c64b 1341 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
476ff2be
SL
1342 /// The stabilized versions of this intrinsic are available on the integer
1343 /// primitives via the `wrapping_sub` method. For example,
1344 /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
c34b1796 1345 pub fn overflowing_sub<T>(a: T, b: T) -> T;
cc61c64b 1346 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
476ff2be
SL
1347 /// The stabilized versions of this intrinsic are available on the integer
1348 /// primitives via the `wrapping_mul` method. For example,
1349 /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
c34b1796 1350 pub fn overflowing_mul<T>(a: T, b: T) -> T;
9346a6ac 1351
62682a34
SL
1352 /// Returns the value of the discriminant for the variant in 'v',
1353 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1354 pub fn discriminant_value<T>(v: &T) -> u64;
c1a9b12d
SL
1355
1356 /// Rust's "try catch" construct which invokes the function pointer `f` with
7453a54e
SL
1357 /// the data pointer `data`.
1358 ///
1359 /// The third pointer is a target-specific data pointer which is filled in
1360 /// with the specifics of the exception that occurred. For examples on Unix
1361 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1362 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1363 /// source as well as std's catch implementation.
1364 pub fn try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
ea8adc8c
XL
1365
1366 /// Computes the byte offset that needs to be applied to `ptr` in order to
1367 /// make it aligned to `align`.
1368 /// If it is not possible to align `ptr`, the implementation returns
1369 /// `usize::max_value()`.
1370 ///
1371 /// There are no guarantees whatsover that offsetting the pointer will not
1372 /// overflow or go beyond the allocation that `ptr` points into.
1373 /// It is up to the caller to ensure that the returned offset is correct
1374 /// in all terms other than alignment.
1375 ///
1376 /// # Examples
1377 ///
1378 /// Accessing adjacent `u8` as `u16`
1379 ///
1380 /// ```
1381 /// # #![feature(core_intrinsics)]
1382 /// # fn foo(n: usize) {
1383 /// # use std::intrinsics::align_offset;
1384 /// # use std::mem::align_of;
1385 /// # unsafe {
1386 /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
1387 /// let ptr = &x[n] as *const u8;
1388 /// let offset = align_offset(ptr as *const (), align_of::<u16>());
1389 /// if offset < x.len() - n - 1 {
1390 /// let u16_ptr = ptr.offset(offset as isize) as *const u16;
1391 /// assert_ne!(*u16_ptr, 500);
1392 /// } else {
1393 /// // while the pointer can be aligned via `offset`, it would point
1394 /// // outside the allocation
1395 /// }
1396 /// # } }
1397 /// ```
ea8adc8c 1398 pub fn align_offset(ptr: *const (), align: usize) -> usize;
ff7c6d11
XL
1399
1400 /// Emits a `!nontemporal` store according to LLVM (see their docs).
1401 /// Probably will never become stable.
ff7c6d11 1402 pub fn nontemporal_store<T>(ptr: *mut T, val: T);
ea8adc8c 1403}
0531ce1d
XL
1404
1405#[cfg(stage0)]
1406pub unsafe fn exact_div<T>(a: T, b: T) -> T {
1407 unchecked_div(a, b)
1408}