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