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.
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.
11 //! rustc compiler intrinsics.
13 //! The corresponding definitions are in librustc_trans/intrinsic.rs.
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
22 //! [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
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]].
30 //! [atomics]: http://llvm.org/docs/Atomics.html
32 //! A quick refresher on memory ordering:
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`.
42 #![unstable(feature = "core_intrinsics",
43 reason = "intrinsics are unlikely to ever be stabilized, instead \
44 they should be used through stabilized interfaces \
45 in the rest of the standard library",
47 #![allow(missing_docs)]
49 #[stable(feature = "drop_in_place", since = "1.8.0")]
50 #[rustc_deprecated(reason = "no longer an intrinsic - use `ptr::drop_in_place` directly",
52 pub use ptr::drop_in_place;
54 extern "rust-intrinsic" {
55 // NB: These intrinsics take raw pointers because they mutate aliased
56 // memory, which is not valid for either `&` or `&mut`.
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,
63 /// [`AtomicBool::compare_exchange`][compare_exchange].
65 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
66 pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
72 /// [`AtomicBool::compare_exchange`][compare_exchange].
74 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
75 pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
83 /// [`AtomicBool::compare_exchange`][compare_exchange].
85 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
86 pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
94 /// [`AtomicBool::compare_exchange`][compare_exchange].
96 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
97 pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
103 /// [`AtomicBool::compare_exchange`][compare_exchange].
105 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
106 pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
114 /// [`AtomicBool::compare_exchange`][compare_exchange].
116 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
117 pub fn atomic_cxchg_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
125 /// [`AtomicBool::compare_exchange`][compare_exchange].
127 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
128 pub fn atomic_cxchg_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
136 /// [`AtomicBool::compare_exchange`][compare_exchange].
138 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
139 pub fn atomic_cxchg_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
147 /// [`AtomicBool::compare_exchange`][compare_exchange].
149 /// [compare_exchange]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange
150 pub fn atomic_cxchg_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
157 /// [`AtomicBool::compare_exchange_weak`][cew].
159 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
160 pub fn atomic_cxchgweak<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
166 /// [`AtomicBool::compare_exchange_weak`][cew].
168 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
169 pub fn atomic_cxchgweak_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
177 /// [`AtomicBool::compare_exchange_weak`][cew].
179 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
180 pub fn atomic_cxchgweak_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
181 /// Stores a value if the current value is the same as the `old` value.
182 /// 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,
188 /// [`AtomicBool::compare_exchange_weak`][cew].
190 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
191 pub fn atomic_cxchgweak_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
197 /// [`AtomicBool::compare_exchange_weak`][cew].
199 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
200 pub fn atomic_cxchgweak_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
208 /// [`AtomicBool::compare_exchange_weak`][cew].
210 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
211 pub fn atomic_cxchgweak_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
219 /// [`AtomicBool::compare_exchange_weak`][cew].
221 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
222 pub fn atomic_cxchgweak_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
230 /// [`AtomicBool::compare_exchange_weak`][cew].
232 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
233 pub fn atomic_cxchgweak_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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,
241 /// [`AtomicBool::compare_exchange_weak`][cew].
243 /// [cew]: ../../std/sync/atomic/struct.AtomicBool.html#method.compare_exchange_weak
244 pub fn atomic_cxchgweak_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
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).
252 pub fn atomic_load<T>(src: *const T) -> T;
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).
259 pub fn atomic_load_acq<T>(src: *const T) -> T;
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).
266 pub fn atomic_load_relaxed<T>(src: *const T) -> T;
267 pub fn atomic_load_unordered<T>(src: *const T) -> T;
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).
275 pub fn atomic_store<T>(dst: *mut T, val: T);
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).
282 pub fn atomic_store_rel<T>(dst: *mut T, val: T);
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).
289 pub fn atomic_store_relaxed<T>(dst: *mut T, val: T);
290 pub fn atomic_store_unordered<T>(dst: *mut T, val: T);
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).
298 pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
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).
305 pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
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).
312 pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
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).
319 pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T;
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).
326 pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T;
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).
334 pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
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).
341 pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
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).
348 pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
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).
355 pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T;
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).
362 pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T;
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).
370 pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
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).
377 pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
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).
384 pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
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).
391 pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T;
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).
398 pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T;
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).
406 pub fn atomic_and<T>(dst: *mut T, src: T) -> T;
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).
413 pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T;
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).
420 pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T;
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).
427 pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T;
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).
434 pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T;
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).
442 pub fn atomic_nand<T>(dst: *mut T, src: T) -> T;
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).
449 pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T;
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).
456 pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T;
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).
463 pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T;
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).
470 pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T;
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).
478 pub fn atomic_or<T>(dst: *mut T, src: T) -> T;
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).
485 pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T;
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).
492 pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T;
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).
499 pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T;
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).
506 pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T;
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).
514 pub fn atomic_xor<T>(dst: *mut T, src: T) -> T;
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).
521 pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T;
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).
528 pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T;
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).
535 pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T;
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).
542 pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T;
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;
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;
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;
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;
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
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
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
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
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
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
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
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
599 pub fn prefetch_write_instruction<T>(data: *const T, locality: i32);
602 extern "rust-intrinsic" {
604 pub fn atomic_fence();
605 pub fn atomic_fence_acq();
606 pub fn atomic_fence_rel();
607 pub fn atomic_fence_acqrel();
609 /// A compiler-only memory barrier.
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.
615 pub fn atomic_singlethreadfence();
616 pub fn atomic_singlethreadfence_acq();
617 pub fn atomic_singlethreadfence_rel();
618 pub fn atomic_singlethreadfence_acqrel();
620 /// Magic intrinsic that derives its meaning from attributes
621 /// attached to the function.
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.
627 pub fn rustc_peek<T>(_: T) -> T;
629 /// Aborts the execution of the process.
632 /// Tells LLVM that this point in the code is not reachable, enabling
633 /// further optimizations.
635 /// NB: This is very different from the `unreachable!()` macro: Unlike the
636 /// macro, which panics when it is executed, it is *undefined behavior* to
637 /// reach code marked with this function.
638 pub fn unreachable() -> !;
640 /// Informs the optimizer that a condition is always true.
641 /// If the condition is false, the behavior is undefined.
643 /// No code is generated for this intrinsic, but the optimizer will try
644 /// to preserve it (and its condition) between passes, which may interfere
645 /// with optimization of surrounding code and reduce performance. It should
646 /// not be used if the invariant can be discovered by the optimizer on its
647 /// own, or if it does not enable any significant optimizations.
648 pub fn assume(b: bool);
650 /// Hints to the compiler that branch condition is likely to be true.
651 /// Returns the value passed to it.
653 /// Any use other than with `if` statements will probably not have an effect.
654 pub fn likely(b: bool) -> bool;
656 /// Hints to the compiler that branch condition is likely to be false.
657 /// Returns the value passed to it.
659 /// Any use other than with `if` statements will probably not have an effect.
660 pub fn unlikely(b: bool) -> bool;
662 /// Executes a breakpoint trap, for inspection by a debugger.
665 /// The size of a type in bytes.
667 /// More specifically, this is the offset in bytes between successive
668 /// items of the same type, including alignment padding.
669 pub fn size_of<T>() -> usize;
671 /// Moves a value to an uninitialized memory location.
673 /// Drop glue is not run on the destination.
674 pub fn move_val_init<T>(dst: *mut T, src: T);
676 pub fn min_align_of<T>() -> usize;
677 pub fn pref_align_of<T>() -> usize;
679 pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
680 pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
682 /// Gets a static string slice containing the name of a type.
683 pub fn type_name<T: ?Sized>() -> &'static str;
685 /// Gets an identifier which is globally unique to the specified type. This
686 /// function will return the same value for a type regardless of whichever
687 /// crate it is invoked in.
688 pub fn type_id<T: ?Sized + 'static>() -> u64;
690 /// Creates a value initialized to zero.
692 /// `init` is unsafe because it returns a zeroed-out datum,
693 /// which is unsafe unless T is `Copy`. Also, even if T is
694 /// `Copy`, an all-zero value may not correspond to any legitimate
695 /// state for the type in question.
696 pub fn init<T>() -> T;
698 /// Creates an uninitialized value.
700 /// `uninit` is unsafe because there is no guarantee of what its
701 /// contents are. In particular its drop-flag may be set to any
702 /// state, which means it may claim either dropped or
703 /// undropped. In the general case one must use `ptr::write` to
704 /// initialize memory previous set to the result of `uninit`.
705 pub fn uninit<T>() -> T;
707 /// Reinterprets the bits of a value of one type as another type.
709 /// Both types must have the same size. Neither the original, nor the result,
710 /// may be an [invalid value](../../nomicon/meet-safe-and-unsafe.html).
712 /// `transmute` is semantically equivalent to a bitwise move of one type
713 /// into another. It copies the bits from the source value into the
714 /// destination value, then forgets the original. It's equivalent to C's
715 /// `memcpy` under the hood, just like `transmute_copy`.
717 /// `transmute` is **incredibly** unsafe. There are a vast number of ways to
718 /// cause [undefined behavior][ub] with this function. `transmute` should be
719 /// the absolute last resort.
721 /// The [nomicon](../../nomicon/transmutes.html) has additional
724 /// [ub]: ../../reference/behavior-considered-undefined.html
728 /// There are a few things that `transmute` is really useful for.
730 /// Getting the bitpattern of a floating point type (or, more generally,
731 /// type punning, when `T` and `U` aren't pointers):
734 /// let bitpattern = unsafe {
735 /// std::mem::transmute::<f32, u32>(1.0)
737 /// assert_eq!(bitpattern, 0x3F800000);
740 /// Turning a pointer into a function pointer. This is *not* portable to
741 /// machines where function pointers and data pointers have different sizes.
744 /// fn foo() -> i32 {
747 /// let pointer = foo as *const ();
748 /// let function = unsafe {
749 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
751 /// assert_eq!(function(), 0);
754 /// Extending a lifetime, or shortening an invariant lifetime. This is
755 /// advanced, very unsafe Rust!
758 /// struct R<'a>(&'a i32);
759 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
760 /// std::mem::transmute::<R<'b>, R<'static>>(r)
763 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
764 /// -> &'b mut R<'c> {
765 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
771 /// Don't despair: many uses of `transmute` can be achieved through other means.
772 /// Below are common applications of `transmute` which can be replaced with safer
775 /// Turning a pointer into a `usize`:
779 /// let ptr_num_transmute = unsafe {
780 /// std::mem::transmute::<&i32, usize>(ptr)
783 /// // Use an `as` cast instead
784 /// let ptr_num_cast = ptr as *const i32 as usize;
787 /// Turning a `*mut T` into an `&mut T`:
790 /// let ptr: *mut i32 = &mut 0;
791 /// let ref_transmuted = unsafe {
792 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
795 /// // Use a reborrow instead
796 /// let ref_casted = unsafe { &mut *ptr };
799 /// Turning an `&mut T` into an `&mut U`:
802 /// let ptr = &mut 0;
803 /// let val_transmuted = unsafe {
804 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
807 /// // Now, put together `as` and reborrowing - note the chaining of `as`
808 /// // `as` is not transitive
809 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
812 /// Turning an `&str` into an `&[u8]`:
815 /// // this is not a good way to do this.
816 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
817 /// assert_eq!(slice, &[82, 117, 115, 116]);
819 /// // You could use `str::as_bytes`
820 /// let slice = "Rust".as_bytes();
821 /// assert_eq!(slice, &[82, 117, 115, 116]);
823 /// // Or, just use a byte string, if you have control over the string
825 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
828 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
831 /// let store = [0, 1, 2, 3];
832 /// let mut v_orig = store.iter().collect::<Vec<&i32>>();
834 /// // Using transmute: this is Undefined Behavior, and a bad idea.
835 /// // However, it is no-copy.
836 /// let v_transmuted = unsafe {
837 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(
841 /// // This is the suggested, safe way.
842 /// // It does copy the entire vector, though, into a new array.
843 /// let v_collected = v_orig.clone()
845 /// .map(|r| Some(r))
846 /// .collect::<Vec<Option<&i32>>>();
848 /// // The no-copy, unsafe way, still using transmute, but not UB.
849 /// // This is equivalent to the original, but safer, and reuses the
850 /// // same Vec internals. Therefore the new inner type must have the
851 /// // exact same size, and the same or lesser alignment, as the old
852 /// // type. The same caveats exist for this method as transmute, for
853 /// // the original inner type (`&i32`) to the converted inner type
854 /// // (`Option<&i32>`), so read the nomicon pages linked above.
855 /// let v_from_raw = unsafe {
856 /// Vec::from_raw_parts(v_orig.as_mut_ptr(),
858 /// v_orig.capacity())
860 /// std::mem::forget(v_orig);
863 /// Implementing `split_at_mut`:
866 /// use std::{slice, mem};
868 /// // There are multiple ways to do this; and there are multiple problems
869 /// // with the following, transmute, way.
870 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
871 /// -> (&mut [T], &mut [T]) {
872 /// let len = slice.len();
873 /// assert!(mid <= len);
875 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
876 /// // first: transmute is not typesafe; all it checks is that T and
877 /// // U are of the same size. Second, right here, you have two
878 /// // mutable references pointing to the same memory.
879 /// (&mut slice[0..mid], &mut slice2[mid..len])
883 /// // This gets rid of the typesafety problems; `&mut *` will *only* give
884 /// // you an `&mut T` from an `&mut T` or `*mut T`.
885 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
886 /// -> (&mut [T], &mut [T]) {
887 /// let len = slice.len();
888 /// assert!(mid <= len);
890 /// let slice2 = &mut *(slice as *mut [T]);
891 /// // however, you still have two mutable references pointing to
892 /// // the same memory.
893 /// (&mut slice[0..mid], &mut slice2[mid..len])
897 /// // This is how the standard library does it. This is the best method, if
898 /// // you need to do something like this
899 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
900 /// -> (&mut [T], &mut [T]) {
901 /// let len = slice.len();
902 /// assert!(mid <= len);
904 /// let ptr = slice.as_mut_ptr();
905 /// // This now has three mutable references pointing at the same
906 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
907 /// // `slice` is never used after `let ptr = ...`, and so one can
908 /// // treat it as "dead", and therefore, you only have two real
909 /// // mutable slices.
910 /// (slice::from_raw_parts_mut(ptr, mid),
911 /// slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
915 #[stable(feature = "rust1", since = "1.0.0")]
916 pub fn transmute<T, U>(e: T) -> U;
918 /// Returns `true` if the actual type given as `T` requires drop
919 /// glue; returns `false` if the actual type provided for `T`
920 /// implements `Copy`.
922 /// If the actual type neither requires drop glue nor implements
923 /// `Copy`, then may return `true` or `false`.
924 pub fn needs_drop<T>() -> bool;
926 /// Calculates the offset from a pointer.
928 /// This is implemented as an intrinsic to avoid converting to and from an
929 /// integer, since the conversion would throw away aliasing information.
933 /// Both the starting and resulting pointer must be either in bounds or one
934 /// byte past the end of an allocated object. If either pointer is out of
935 /// bounds or arithmetic overflow occurs then any further use of the
936 /// returned value will result in undefined behavior.
937 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
939 /// Calculates the offset from a pointer, potentially wrapping.
941 /// This is implemented as an intrinsic to avoid converting to and from an
942 /// integer, since the conversion inhibits certain optimizations.
946 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
947 /// resulting pointer to point into or one byte past the end of an allocated
948 /// object, and it wraps with two's complement arithmetic. The resulting
949 /// value is not necessarily valid to be used to actually access memory.
950 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
952 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
953 /// and destination may *not* overlap.
955 /// `copy_nonoverlapping` is semantically equivalent to C's `memcpy`.
959 /// Beyond requiring that the program must be allowed to access both regions
960 /// of memory, it is Undefined Behavior for source and destination to
961 /// overlap. Care must also be taken with the ownership of `src` and
962 /// `dst`. This method semantically moves the values of `src` into `dst`.
963 /// However it does not drop the contents of `dst`, or prevent the contents
964 /// of `src` from being dropped or used.
968 /// A safe swap function:
974 /// # #[allow(dead_code)]
975 /// fn swap<T>(x: &mut T, y: &mut T) {
977 /// // Give ourselves some scratch space to work with
978 /// let mut t: T = mem::uninitialized();
980 /// // Perform the swap, `&mut` pointers never alias
981 /// ptr::copy_nonoverlapping(x, &mut t, 1);
982 /// ptr::copy_nonoverlapping(y, x, 1);
983 /// ptr::copy_nonoverlapping(&t, y, 1);
985 /// // y and t now point to the same thing, but we need to completely forget `tmp`
986 /// // because it's no longer relevant.
991 #[stable(feature = "rust1", since = "1.0.0")]
992 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
994 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
995 /// and destination may overlap.
997 /// `copy` is semantically equivalent to C's `memmove`.
1001 /// Care must be taken with the ownership of `src` and `dst`.
1002 /// This method semantically moves the values of `src` into `dst`.
1003 /// However it does not drop the contents of `dst`, or prevent the contents of `src`
1004 /// from being dropped or used.
1008 /// Efficiently create a Rust vector from an unsafe buffer:
1013 /// # #[allow(dead_code)]
1014 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
1015 /// let mut dst = Vec::with_capacity(elts);
1016 /// dst.set_len(elts);
1017 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
1022 #[stable(feature = "rust1", since = "1.0.0")]
1023 pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
1025 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
1026 /// bytes of memory starting at `dst` to `val`.
1033 /// let mut vec = vec![0; 4];
1035 /// let vec_ptr = vec.as_mut_ptr();
1036 /// ptr::write_bytes(vec_ptr, b'a', 2);
1038 /// assert_eq!(vec, [b'a', b'a', 0, 0]);
1040 #[stable(feature = "rust1", since = "1.0.0")]
1041 pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
1043 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
1044 /// a size of `count` * `size_of::<T>()` and an alignment of
1045 /// `min_align_of::<T>()`
1047 /// The volatile parameter is set to `true`, so it will not be optimized out.
1048 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
1050 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
1051 /// a size of `count` * `size_of::<T>()` and an alignment of
1052 /// `min_align_of::<T>()`
1054 /// The volatile parameter is set to `true`, so it will not be optimized out.
1055 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
1056 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
1057 /// size of `count` * `size_of::<T>()` and an alignment of
1058 /// `min_align_of::<T>()`.
1060 /// The volatile parameter is set to `true`, so it will not be optimized out.
1061 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
1063 /// Perform a volatile load from the `src` pointer.
1064 /// The stabilized version of this intrinsic is
1065 /// [`std::ptr::read_volatile`](../../std/ptr/fn.read_volatile.html).
1066 pub fn volatile_load<T>(src: *const T) -> T;
1067 /// Perform a volatile store to the `dst` pointer.
1068 /// The stabilized version of this intrinsic is
1069 /// [`std::ptr::write_volatile`](../../std/ptr/fn.write_volatile.html).
1070 pub fn volatile_store<T>(dst: *mut T, val: T);
1072 /// Returns the square root of an `f32`
1073 pub fn sqrtf32(x: f32) -> f32;
1074 /// Returns the square root of an `f64`
1075 pub fn sqrtf64(x: f64) -> f64;
1077 /// Raises an `f32` to an integer power.
1078 pub fn powif32(a: f32, x: i32) -> f32;
1079 /// Raises an `f64` to an integer power.
1080 pub fn powif64(a: f64, x: i32) -> f64;
1082 /// Returns the sine of an `f32`.
1083 pub fn sinf32(x: f32) -> f32;
1084 /// Returns the sine of an `f64`.
1085 pub fn sinf64(x: f64) -> f64;
1087 /// Returns the cosine of an `f32`.
1088 pub fn cosf32(x: f32) -> f32;
1089 /// Returns the cosine of an `f64`.
1090 pub fn cosf64(x: f64) -> f64;
1092 /// Raises an `f32` to an `f32` power.
1093 pub fn powf32(a: f32, x: f32) -> f32;
1094 /// Raises an `f64` to an `f64` power.
1095 pub fn powf64(a: f64, x: f64) -> f64;
1097 /// Returns the exponential of an `f32`.
1098 pub fn expf32(x: f32) -> f32;
1099 /// Returns the exponential of an `f64`.
1100 pub fn expf64(x: f64) -> f64;
1102 /// Returns 2 raised to the power of an `f32`.
1103 pub fn exp2f32(x: f32) -> f32;
1104 /// Returns 2 raised to the power of an `f64`.
1105 pub fn exp2f64(x: f64) -> f64;
1107 /// Returns the natural logarithm of an `f32`.
1108 pub fn logf32(x: f32) -> f32;
1109 /// Returns the natural logarithm of an `f64`.
1110 pub fn logf64(x: f64) -> f64;
1112 /// Returns the base 10 logarithm of an `f32`.
1113 pub fn log10f32(x: f32) -> f32;
1114 /// Returns the base 10 logarithm of an `f64`.
1115 pub fn log10f64(x: f64) -> f64;
1117 /// Returns the base 2 logarithm of an `f32`.
1118 pub fn log2f32(x: f32) -> f32;
1119 /// Returns the base 2 logarithm of an `f64`.
1120 pub fn log2f64(x: f64) -> f64;
1122 /// Returns `a * b + c` for `f32` values.
1123 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
1124 /// Returns `a * b + c` for `f64` values.
1125 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
1127 /// Returns the absolute value of an `f32`.
1128 pub fn fabsf32(x: f32) -> f32;
1129 /// Returns the absolute value of an `f64`.
1130 pub fn fabsf64(x: f64) -> f64;
1132 /// Copies the sign from `y` to `x` for `f32` values.
1133 pub fn copysignf32(x: f32, y: f32) -> f32;
1134 /// Copies the sign from `y` to `x` for `f64` values.
1135 pub fn copysignf64(x: f64, y: f64) -> f64;
1137 /// Returns the largest integer less than or equal to an `f32`.
1138 pub fn floorf32(x: f32) -> f32;
1139 /// Returns the largest integer less than or equal to an `f64`.
1140 pub fn floorf64(x: f64) -> f64;
1142 /// Returns the smallest integer greater than or equal to an `f32`.
1143 pub fn ceilf32(x: f32) -> f32;
1144 /// Returns the smallest integer greater than or equal to an `f64`.
1145 pub fn ceilf64(x: f64) -> f64;
1147 /// Returns the integer part of an `f32`.
1148 pub fn truncf32(x: f32) -> f32;
1149 /// Returns the integer part of an `f64`.
1150 pub fn truncf64(x: f64) -> f64;
1152 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
1153 /// if the argument is not an integer.
1154 pub fn rintf32(x: f32) -> f32;
1155 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
1156 /// if the argument is not an integer.
1157 pub fn rintf64(x: f64) -> f64;
1159 /// Returns the nearest integer to an `f32`.
1160 pub fn nearbyintf32(x: f32) -> f32;
1161 /// Returns the nearest integer to an `f64`.
1162 pub fn nearbyintf64(x: f64) -> f64;
1164 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
1165 pub fn roundf32(x: f32) -> f32;
1166 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
1167 pub fn roundf64(x: f64) -> f64;
1169 /// Float addition that allows optimizations based on algebraic rules.
1170 /// May assume inputs are finite.
1171 pub fn fadd_fast<T>(a: T, b: T) -> T;
1173 /// Float subtraction that allows optimizations based on algebraic rules.
1174 /// May assume inputs are finite.
1175 pub fn fsub_fast<T>(a: T, b: T) -> T;
1177 /// Float multiplication that allows optimizations based on algebraic rules.
1178 /// May assume inputs are finite.
1179 pub fn fmul_fast<T>(a: T, b: T) -> T;
1181 /// Float division that allows optimizations based on algebraic rules.
1182 /// May assume inputs are finite.
1183 pub fn fdiv_fast<T>(a: T, b: T) -> T;
1185 /// Float remainder that allows optimizations based on algebraic rules.
1186 /// May assume inputs are finite.
1187 pub fn frem_fast<T>(a: T, b: T) -> T;
1190 /// Returns the number of bits set in an integer type `T`
1191 pub fn ctpop<T>(x: T) -> T;
1193 /// Returns the number of leading unset bits (zeroes) in an integer type `T`.
1198 /// #![feature(core_intrinsics)]
1200 /// use std::intrinsics::ctlz;
1202 /// let x = 0b0001_1100_u8;
1203 /// let num_leading = unsafe { ctlz(x) };
1204 /// assert_eq!(num_leading, 3);
1207 /// An `x` with value `0` will return the bit width of `T`.
1210 /// #![feature(core_intrinsics)]
1212 /// use std::intrinsics::ctlz;
1215 /// let num_leading = unsafe { ctlz(x) };
1216 /// assert_eq!(num_leading, 16);
1218 pub fn ctlz<T>(x: T) -> T;
1220 /// Like `ctlz`, but extra-unsafe as it returns `undef` when
1221 /// given an `x` with value `0`.
1226 /// #![feature(core_intrinsics)]
1228 /// use std::intrinsics::ctlz_nonzero;
1230 /// let x = 0b0001_1100_u8;
1231 /// let num_leading = unsafe { ctlz_nonzero(x) };
1232 /// assert_eq!(num_leading, 3);
1234 pub fn ctlz_nonzero<T>(x: T) -> T;
1236 /// Returns the number of trailing unset bits (zeroes) in an integer type `T`.
1241 /// #![feature(core_intrinsics)]
1243 /// use std::intrinsics::cttz;
1245 /// let x = 0b0011_1000_u8;
1246 /// let num_trailing = unsafe { cttz(x) };
1247 /// assert_eq!(num_trailing, 3);
1250 /// An `x` with value `0` will return the bit width of `T`:
1253 /// #![feature(core_intrinsics)]
1255 /// use std::intrinsics::cttz;
1258 /// let num_trailing = unsafe { cttz(x) };
1259 /// assert_eq!(num_trailing, 16);
1261 pub fn cttz<T>(x: T) -> T;
1263 /// Like `cttz`, but extra-unsafe as it returns `undef` when
1264 /// given an `x` with value `0`.
1269 /// #![feature(core_intrinsics)]
1271 /// use std::intrinsics::cttz_nonzero;
1273 /// let x = 0b0011_1000_u8;
1274 /// let num_trailing = unsafe { cttz_nonzero(x) };
1275 /// assert_eq!(num_trailing, 3);
1277 pub fn cttz_nonzero<T>(x: T) -> T;
1279 /// Reverses the bytes in an integer type `T`.
1280 pub fn bswap<T>(x: T) -> T;
1282 /// Performs checked integer addition.
1283 /// The stabilized versions of this intrinsic are available on the integer
1284 /// primitives via the `overflowing_add` method. For example,
1285 /// [`std::u32::overflowing_add`](../../std/primitive.u32.html#method.overflowing_add)
1286 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
1288 /// Performs checked integer subtraction
1289 /// The stabilized versions of this intrinsic are available on the integer
1290 /// primitives via the `overflowing_sub` method. For example,
1291 /// [`std::u32::overflowing_sub`](../../std/primitive.u32.html#method.overflowing_sub)
1292 pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
1294 /// Performs checked integer multiplication
1295 /// The stabilized versions of this intrinsic are available on the integer
1296 /// primitives via the `overflowing_mul` method. For example,
1297 /// [`std::u32::overflowing_mul`](../../std/primitive.u32.html#method.overflowing_mul)
1298 pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
1300 /// Performs an unchecked division, resulting in undefined behavior
1301 /// where y = 0 or x = `T::min_value()` and y = -1
1302 pub fn unchecked_div<T>(x: T, y: T) -> T;
1303 /// Returns the remainder of an unchecked division, resulting in
1304 /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
1305 pub fn unchecked_rem<T>(x: T, y: T) -> T;
1307 /// Performs an unchecked left shift, resulting in undefined behavior when
1308 /// y < 0 or y >= N, where N is the width of T in bits.
1309 pub fn unchecked_shl<T>(x: T, y: T) -> T;
1310 /// Performs an unchecked right shift, resulting in undefined behavior when
1311 /// y < 0 or y >= N, where N is the width of T in bits.
1312 pub fn unchecked_shr<T>(x: T, y: T) -> T;
1314 /// Returns (a + b) mod 2<sup>N</sup>, where N is the width of T in bits.
1315 /// The stabilized versions of this intrinsic are available on the integer
1316 /// primitives via the `wrapping_add` method. For example,
1317 /// [`std::u32::wrapping_add`](../../std/primitive.u32.html#method.wrapping_add)
1318 pub fn overflowing_add<T>(a: T, b: T) -> T;
1319 /// Returns (a - b) mod 2<sup>N</sup>, where N is the width of T in bits.
1320 /// The stabilized versions of this intrinsic are available on the integer
1321 /// primitives via the `wrapping_sub` method. For example,
1322 /// [`std::u32::wrapping_sub`](../../std/primitive.u32.html#method.wrapping_sub)
1323 pub fn overflowing_sub<T>(a: T, b: T) -> T;
1324 /// Returns (a * b) mod 2<sup>N</sup>, where N is the width of T in bits.
1325 /// The stabilized versions of this intrinsic are available on the integer
1326 /// primitives via the `wrapping_mul` method. For example,
1327 /// [`std::u32::wrapping_mul`](../../std/primitive.u32.html#method.wrapping_mul)
1328 pub fn overflowing_mul<T>(a: T, b: T) -> T;
1330 /// Returns the value of the discriminant for the variant in 'v',
1331 /// cast to a `u64`; if `T` has no discriminant, returns 0.
1332 pub fn discriminant_value<T>(v: &T) -> u64;
1334 /// Rust's "try catch" construct which invokes the function pointer `f` with
1335 /// the data pointer `data`.
1337 /// The third pointer is a target-specific data pointer which is filled in
1338 /// with the specifics of the exception that occurred. For examples on Unix
1339 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
1340 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
1341 /// source as well as std's catch implementation.
1342 pub fn try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;