]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | //! rustc compiler intrinsics. | |
12 | //! | |
13 | //! The corresponding definitions are in librustc_trans/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 | ||
62682a34 SL |
42 | #![unstable(feature = "core_intrinsics", |
43 | reason = "intrinsics are unlikely to ever be stabilized, instead \ | |
44 | they should be used through stabilized interfaces \ | |
e9174d1e SL |
45 | in the rest of the standard library", |
46 | issue = "0")] | |
1a4d82fc JJ |
47 | #![allow(missing_docs)] |
48 | ||
1a4d82fc JJ |
49 | use marker::Sized; |
50 | ||
1a4d82fc JJ |
51 | extern "rust-intrinsic" { |
52 | ||
62682a34 | 53 | // NB: These intrinsics take raw pointers because they mutate aliased |
1a4d82fc JJ |
54 | // memory, which is not valid for either `&` or `&mut`. |
55 | ||
54a0048b | 56 | #[cfg(all(stage0, not(cargobuild)))] |
1a4d82fc | 57 | pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> T; |
54a0048b | 58 | #[cfg(all(stage0, not(cargobuild)))] |
1a4d82fc | 59 | pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> T; |
54a0048b | 60 | #[cfg(all(stage0, not(cargobuild)))] |
1a4d82fc | 61 | pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> T; |
54a0048b | 62 | #[cfg(all(stage0, not(cargobuild)))] |
1a4d82fc | 63 | pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> T; |
54a0048b | 64 | #[cfg(all(stage0, not(cargobuild)))] |
1a4d82fc | 65 | pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> T; |
7453a54e | 66 | |
54a0048b SL |
67 | #[cfg(any(not(stage0), cargobuild))] |
68 | pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> (T, bool); | |
69 | #[cfg(any(not(stage0), cargobuild))] | |
70 | pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool); | |
71 | #[cfg(any(not(stage0), cargobuild))] | |
72 | pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool); | |
73 | #[cfg(any(not(stage0), cargobuild))] | |
74 | pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool); | |
75 | #[cfg(any(not(stage0), cargobuild))] | |
76 | pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool); | |
77 | #[cfg(any(not(stage0), cargobuild))] | |
78 | pub fn atomic_cxchg_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool); | |
79 | #[cfg(any(not(stage0), cargobuild))] | |
80 | pub fn atomic_cxchg_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool); | |
81 | #[cfg(any(not(stage0), cargobuild))] | |
82 | pub fn atomic_cxchg_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool); | |
83 | #[cfg(any(not(stage0), cargobuild))] | |
84 | pub fn atomic_cxchg_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool); | |
85 | ||
7453a54e | 86 | pub fn atomic_cxchgweak<T>(dst: *mut T, old: T, src: T) -> (T, bool); |
7453a54e | 87 | pub fn atomic_cxchgweak_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool); |
7453a54e | 88 | pub fn atomic_cxchgweak_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool); |
7453a54e | 89 | pub fn atomic_cxchgweak_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool); |
7453a54e | 90 | pub fn atomic_cxchgweak_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool); |
7453a54e | 91 | pub fn atomic_cxchgweak_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool); |
7453a54e | 92 | pub fn atomic_cxchgweak_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool); |
7453a54e | 93 | pub fn atomic_cxchgweak_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool); |
7453a54e | 94 | pub fn atomic_cxchgweak_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool); |
1a4d82fc JJ |
95 | |
96 | pub fn atomic_load<T>(src: *const T) -> T; | |
97 | pub fn atomic_load_acq<T>(src: *const T) -> T; | |
98 | pub fn atomic_load_relaxed<T>(src: *const T) -> T; | |
99 | pub fn atomic_load_unordered<T>(src: *const T) -> T; | |
100 | ||
101 | pub fn atomic_store<T>(dst: *mut T, val: T); | |
102 | pub fn atomic_store_rel<T>(dst: *mut T, val: T); | |
103 | pub fn atomic_store_relaxed<T>(dst: *mut T, val: T); | |
104 | pub fn atomic_store_unordered<T>(dst: *mut T, val: T); | |
105 | ||
106 | pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T; | |
107 | pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T; | |
108 | pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T; | |
109 | pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T; | |
110 | pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T; | |
111 | ||
112 | pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T; | |
113 | pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T; | |
114 | pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T; | |
115 | pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T; | |
116 | pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T; | |
117 | ||
118 | pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T; | |
119 | pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T; | |
120 | pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T; | |
121 | pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T; | |
122 | pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T; | |
123 | ||
124 | pub fn atomic_and<T>(dst: *mut T, src: T) -> T; | |
125 | pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T; | |
126 | pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T; | |
127 | pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T; | |
128 | pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T; | |
129 | ||
130 | pub fn atomic_nand<T>(dst: *mut T, src: T) -> T; | |
131 | pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T; | |
132 | pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T; | |
133 | pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T; | |
134 | pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T; | |
135 | ||
136 | pub fn atomic_or<T>(dst: *mut T, src: T) -> T; | |
137 | pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T; | |
138 | pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T; | |
139 | pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T; | |
140 | pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T; | |
141 | ||
142 | pub fn atomic_xor<T>(dst: *mut T, src: T) -> T; | |
143 | pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T; | |
144 | pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T; | |
145 | pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T; | |
146 | pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T; | |
147 | ||
148 | pub fn atomic_max<T>(dst: *mut T, src: T) -> T; | |
149 | pub fn atomic_max_acq<T>(dst: *mut T, src: T) -> T; | |
150 | pub fn atomic_max_rel<T>(dst: *mut T, src: T) -> T; | |
151 | pub fn atomic_max_acqrel<T>(dst: *mut T, src: T) -> T; | |
152 | pub fn atomic_max_relaxed<T>(dst: *mut T, src: T) -> T; | |
153 | ||
154 | pub fn atomic_min<T>(dst: *mut T, src: T) -> T; | |
155 | pub fn atomic_min_acq<T>(dst: *mut T, src: T) -> T; | |
156 | pub fn atomic_min_rel<T>(dst: *mut T, src: T) -> T; | |
157 | pub fn atomic_min_acqrel<T>(dst: *mut T, src: T) -> T; | |
158 | pub fn atomic_min_relaxed<T>(dst: *mut T, src: T) -> T; | |
159 | ||
160 | pub fn atomic_umin<T>(dst: *mut T, src: T) -> T; | |
161 | pub fn atomic_umin_acq<T>(dst: *mut T, src: T) -> T; | |
162 | pub fn atomic_umin_rel<T>(dst: *mut T, src: T) -> T; | |
163 | pub fn atomic_umin_acqrel<T>(dst: *mut T, src: T) -> T; | |
164 | pub fn atomic_umin_relaxed<T>(dst: *mut T, src: T) -> T; | |
165 | ||
166 | pub fn atomic_umax<T>(dst: *mut T, src: T) -> T; | |
167 | pub fn atomic_umax_acq<T>(dst: *mut T, src: T) -> T; | |
168 | pub fn atomic_umax_rel<T>(dst: *mut T, src: T) -> T; | |
169 | pub fn atomic_umax_acqrel<T>(dst: *mut T, src: T) -> T; | |
170 | pub fn atomic_umax_relaxed<T>(dst: *mut T, src: T) -> T; | |
171 | } | |
172 | ||
173 | extern "rust-intrinsic" { | |
174 | ||
175 | pub fn atomic_fence(); | |
176 | pub fn atomic_fence_acq(); | |
177 | pub fn atomic_fence_rel(); | |
178 | pub fn atomic_fence_acqrel(); | |
179 | ||
d9579d0f AL |
180 | /// A compiler-only memory barrier. |
181 | /// | |
62682a34 SL |
182 | /// Memory accesses will never be reordered across this barrier by the |
183 | /// compiler, but no instructions will be emitted for it. This is | |
184 | /// appropriate for operations on the same thread that may be preempted, | |
185 | /// such as when interacting with signal handlers. | |
d9579d0f | 186 | pub fn atomic_singlethreadfence(); |
d9579d0f | 187 | pub fn atomic_singlethreadfence_acq(); |
d9579d0f | 188 | pub fn atomic_singlethreadfence_rel(); |
d9579d0f AL |
189 | pub fn atomic_singlethreadfence_acqrel(); |
190 | ||
9346a6ac | 191 | /// Aborts the execution of the process. |
1a4d82fc JJ |
192 | pub fn abort() -> !; |
193 | ||
9346a6ac | 194 | /// Tells LLVM that this point in the code is not reachable, |
1a4d82fc JJ |
195 | /// enabling further optimizations. |
196 | /// | |
197 | /// NB: This is very different from the `unreachable!()` macro! | |
198 | pub fn unreachable() -> !; | |
199 | ||
9346a6ac | 200 | /// Informs the optimizer that a condition is always true. |
1a4d82fc JJ |
201 | /// If the condition is false, the behavior is undefined. |
202 | /// | |
203 | /// No code is generated for this intrinsic, but the optimizer will try | |
204 | /// to preserve it (and its condition) between passes, which may interfere | |
205 | /// with optimization of surrounding code and reduce performance. It should | |
206 | /// not be used if the invariant can be discovered by the optimizer on its | |
207 | /// own, or if it does not enable any significant optimizations. | |
208 | pub fn assume(b: bool); | |
209 | ||
9346a6ac | 210 | /// Executes a breakpoint trap, for inspection by a debugger. |
1a4d82fc JJ |
211 | pub fn breakpoint(); |
212 | ||
213 | /// The size of a type in bytes. | |
214 | /// | |
215 | /// This is the exact number of bytes in memory taken up by a | |
216 | /// value of the given type. In other words, a memset of this size | |
217 | /// would *exactly* overwrite a value. When laid out in vectors | |
218 | /// and structures there may be additional padding between | |
219 | /// elements. | |
85aaf69f | 220 | pub fn size_of<T>() -> usize; |
1a4d82fc | 221 | |
9346a6ac | 222 | /// Moves a value to an uninitialized memory location. |
1a4d82fc JJ |
223 | /// |
224 | /// Drop glue is not run on the destination. | |
c1a9b12d | 225 | pub fn move_val_init<T>(dst: *mut T, src: T); |
1a4d82fc | 226 | |
85aaf69f SL |
227 | pub fn min_align_of<T>() -> usize; |
228 | pub fn pref_align_of<T>() -> usize; | |
1a4d82fc | 229 | |
d9579d0f | 230 | pub fn size_of_val<T: ?Sized>(_: &T) -> usize; |
d9579d0f | 231 | pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize; |
92a42be0 SL |
232 | |
233 | /// Executes the destructor (if any) of the pointed-to value. | |
234 | /// | |
235 | /// This has two use cases: | |
236 | /// | |
237 | /// * It is *required* to use `drop_in_place` to drop unsized types like | |
238 | /// trait objects, because they can't be read out onto the stack and | |
239 | /// dropped normally. | |
240 | /// | |
241 | /// * It is friendlier to the optimizer to do this over `ptr::read` when | |
242 | /// dropping manually allocated memory (e.g. when writing Box/Rc/Vec), | |
243 | /// as the compiler doesn't need to prove that it's sound to elide the | |
244 | /// copy. | |
245 | /// | |
246 | /// # Undefined Behavior | |
247 | /// | |
248 | /// This has all the same safety problems as `ptr::read` with respect to | |
249 | /// invalid pointers, types, and double drops. | |
7453a54e | 250 | #[stable(feature = "drop_in_place", since = "1.8.0")] |
92a42be0 | 251 | pub fn drop_in_place<T: ?Sized>(to_drop: *mut T); |
d9579d0f | 252 | |
c34b1796 AL |
253 | /// Gets a static string slice containing the name of a type. |
254 | pub fn type_name<T: ?Sized>() -> &'static str; | |
1a4d82fc JJ |
255 | |
256 | /// Gets an identifier which is globally unique to the specified type. This | |
257 | /// function will return the same value for a type regardless of whichever | |
258 | /// crate it is invoked in. | |
85aaf69f | 259 | pub fn type_id<T: ?Sized + 'static>() -> u64; |
1a4d82fc | 260 | |
9346a6ac | 261 | /// Creates a value initialized to so that its drop flag, |
c34b1796 AL |
262 | /// if any, says that it has been dropped. |
263 | /// | |
264 | /// `init_dropped` is unsafe because it returns a datum with all | |
265 | /// of its bytes set to the drop flag, which generally does not | |
266 | /// correspond to a valid value. | |
267 | /// | |
268 | /// This intrinsic is likely to be deprecated in the future when | |
269 | /// Rust moves to non-zeroing dynamic drop (and thus removes the | |
270 | /// embedded drop flags that are being established by this | |
271 | /// intrinsic). | |
272 | pub fn init_dropped<T>() -> T; | |
273 | ||
9346a6ac | 274 | /// Creates a value initialized to zero. |
1a4d82fc JJ |
275 | /// |
276 | /// `init` is unsafe because it returns a zeroed-out datum, | |
c34b1796 AL |
277 | /// which is unsafe unless T is `Copy`. Also, even if T is |
278 | /// `Copy`, an all-zero value may not correspond to any legitimate | |
279 | /// state for the type in question. | |
1a4d82fc JJ |
280 | pub fn init<T>() -> T; |
281 | ||
9346a6ac | 282 | /// Creates an uninitialized value. |
c34b1796 AL |
283 | /// |
284 | /// `uninit` is unsafe because there is no guarantee of what its | |
285 | /// contents are. In particular its drop-flag may be set to any | |
286 | /// state, which means it may claim either dropped or | |
287 | /// undropped. In the general case one must use `ptr::write` to | |
288 | /// initialize memory previous set to the result of `uninit`. | |
1a4d82fc JJ |
289 | pub fn uninit<T>() -> T; |
290 | ||
9346a6ac | 291 | /// Moves a value out of scope without running drop glue. |
1a4d82fc JJ |
292 | pub fn forget<T>(_: T) -> (); |
293 | ||
294 | /// Unsafely transforms a value of one type into a value of another type. | |
295 | /// | |
85aaf69f | 296 | /// Both types must have the same size. |
1a4d82fc JJ |
297 | /// |
298 | /// # Examples | |
299 | /// | |
85aaf69f | 300 | /// ``` |
1a4d82fc JJ |
301 | /// use std::mem; |
302 | /// | |
e9174d1e SL |
303 | /// let array: &[u8] = unsafe { mem::transmute("Rust") }; |
304 | /// assert_eq!(array, [82, 117, 115, 116]); | |
1a4d82fc | 305 | /// ``` |
85aaf69f | 306 | #[stable(feature = "rust1", since = "1.0.0")] |
e9174d1e | 307 | pub fn transmute<T, U>(e: T) -> U; |
1a4d82fc JJ |
308 | |
309 | /// Gives the address for the return value of the enclosing function. | |
310 | /// | |
311 | /// Using this intrinsic in a function that does not use an out pointer | |
312 | /// will trigger a compiler error. | |
313 | pub fn return_address() -> *const u8; | |
314 | ||
c34b1796 AL |
315 | /// Returns `true` if the actual type given as `T` requires drop |
316 | /// glue; returns `false` if the actual type provided for `T` | |
317 | /// implements `Copy`. | |
318 | /// | |
319 | /// If the actual type neither requires drop glue nor implements | |
320 | /// `Copy`, then may return `true` or `false`. | |
1a4d82fc JJ |
321 | pub fn needs_drop<T>() -> bool; |
322 | ||
d9579d0f | 323 | /// Calculates the offset from a pointer. |
1a4d82fc JJ |
324 | /// |
325 | /// This is implemented as an intrinsic to avoid converting to and from an | |
326 | /// integer, since the conversion would throw away aliasing information. | |
d9579d0f AL |
327 | /// |
328 | /// # Safety | |
329 | /// | |
330 | /// Both the starting and resulting pointer must be either in bounds or one | |
331 | /// byte past the end of an allocated object. If either pointer is out of | |
332 | /// bounds or arithmetic overflow occurs then any further use of the | |
333 | /// returned value will result in undefined behavior. | |
85aaf69f | 334 | pub fn offset<T>(dst: *const T, offset: isize) -> *const T; |
1a4d82fc | 335 | |
62682a34 SL |
336 | /// Calculates the offset from a pointer, potentially wrapping. |
337 | /// | |
338 | /// This is implemented as an intrinsic to avoid converting to and from an | |
339 | /// integer, since the conversion inhibits certain optimizations. | |
340 | /// | |
341 | /// # Safety | |
342 | /// | |
343 | /// Unlike the `offset` intrinsic, this intrinsic does not restrict the | |
344 | /// resulting pointer to point into or one byte past the end of an allocated | |
345 | /// object, and it wraps with two's complement arithmetic. The resulting | |
346 | /// value is not necessarily valid to be used to actually access memory. | |
347 | pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T; | |
348 | ||
1a4d82fc JJ |
349 | /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source |
350 | /// and destination may *not* overlap. | |
351 | /// | |
c34b1796 | 352 | /// `copy_nonoverlapping` is semantically equivalent to C's `memcpy`. |
1a4d82fc JJ |
353 | /// |
354 | /// # Safety | |
355 | /// | |
85aaf69f | 356 | /// Beyond requiring that the program must be allowed to access both regions |
b039eaaf | 357 | /// of memory, it is Undefined Behavior for source and destination to |
85aaf69f SL |
358 | /// overlap. Care must also be taken with the ownership of `src` and |
359 | /// `dst`. This method semantically moves the values of `src` into `dst`. | |
360 | /// However it does not drop the contents of `dst`, or prevent the contents | |
361 | /// of `src` from being dropped or used. | |
1a4d82fc JJ |
362 | /// |
363 | /// # Examples | |
364 | /// | |
365 | /// A safe swap function: | |
366 | /// | |
367 | /// ``` | |
368 | /// use std::mem; | |
369 | /// use std::ptr; | |
370 | /// | |
92a42be0 | 371 | /// # #[allow(dead_code)] |
1a4d82fc JJ |
372 | /// fn swap<T>(x: &mut T, y: &mut T) { |
373 | /// unsafe { | |
374 | /// // Give ourselves some scratch space to work with | |
375 | /// let mut t: T = mem::uninitialized(); | |
376 | /// | |
377 | /// // Perform the swap, `&mut` pointers never alias | |
c34b1796 AL |
378 | /// ptr::copy_nonoverlapping(x, &mut t, 1); |
379 | /// ptr::copy_nonoverlapping(y, x, 1); | |
380 | /// ptr::copy_nonoverlapping(&t, y, 1); | |
1a4d82fc JJ |
381 | /// |
382 | /// // y and t now point to the same thing, but we need to completely forget `tmp` | |
383 | /// // because it's no longer relevant. | |
384 | /// mem::forget(t); | |
385 | /// } | |
386 | /// } | |
387 | /// ``` | |
c34b1796 | 388 | #[stable(feature = "rust1", since = "1.0.0")] |
c34b1796 AL |
389 | pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize); |
390 | ||
1a4d82fc JJ |
391 | /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source |
392 | /// and destination may overlap. | |
393 | /// | |
c34b1796 | 394 | /// `copy` is semantically equivalent to C's `memmove`. |
1a4d82fc JJ |
395 | /// |
396 | /// # Safety | |
397 | /// | |
398 | /// Care must be taken with the ownership of `src` and `dst`. | |
399 | /// This method semantically moves the values of `src` into `dst`. | |
400 | /// However it does not drop the contents of `dst`, or prevent the contents of `src` | |
401 | /// from being dropped or used. | |
402 | /// | |
403 | /// # Examples | |
404 | /// | |
405 | /// Efficiently create a Rust vector from an unsafe buffer: | |
406 | /// | |
407 | /// ``` | |
408 | /// use std::ptr; | |
409 | /// | |
92a42be0 | 410 | /// # #[allow(dead_code)] |
c34b1796 | 411 | /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> { |
1a4d82fc JJ |
412 | /// let mut dst = Vec::with_capacity(elts); |
413 | /// dst.set_len(elts); | |
c34b1796 | 414 | /// ptr::copy(ptr, dst.as_mut_ptr(), elts); |
1a4d82fc JJ |
415 | /// dst |
416 | /// } | |
417 | /// ``` | |
418 | /// | |
c34b1796 | 419 | #[stable(feature = "rust1", since = "1.0.0")] |
c34b1796 AL |
420 | pub fn copy<T>(src: *const T, dst: *mut T, count: usize); |
421 | ||
1a4d82fc | 422 | /// Invokes memset on the specified pointer, setting `count * size_of::<T>()` |
92a42be0 | 423 | /// bytes of memory starting at `dst` to `val`. |
c34b1796 AL |
424 | #[stable(feature = "rust1", since = "1.0.0")] |
425 | pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize); | |
1a4d82fc JJ |
426 | |
427 | /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with | |
428 | /// a size of `count` * `size_of::<T>()` and an alignment of | |
429 | /// `min_align_of::<T>()` | |
430 | /// | |
b039eaaf | 431 | /// The volatile parameter is set to `true`, so it will not be optimized out. |
1a4d82fc | 432 | pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, |
85aaf69f | 433 | count: usize); |
1a4d82fc JJ |
434 | /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with |
435 | /// a size of `count` * `size_of::<T>()` and an alignment of | |
436 | /// `min_align_of::<T>()` | |
437 | /// | |
b039eaaf | 438 | /// The volatile parameter is set to `true`, so it will not be optimized out. |
85aaf69f | 439 | pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize); |
1a4d82fc JJ |
440 | /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a |
441 | /// size of `count` * `size_of::<T>()` and an alignment of | |
442 | /// `min_align_of::<T>()`. | |
443 | /// | |
b039eaaf | 444 | /// The volatile parameter is set to `true`, so it will not be optimized out. |
85aaf69f | 445 | pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize); |
1a4d82fc JJ |
446 | |
447 | /// Perform a volatile load from the `src` pointer. | |
448 | pub fn volatile_load<T>(src: *const T) -> T; | |
449 | /// Perform a volatile store to the `dst` pointer. | |
450 | pub fn volatile_store<T>(dst: *mut T, val: T); | |
451 | ||
452 | /// Returns the square root of an `f32` | |
453 | pub fn sqrtf32(x: f32) -> f32; | |
454 | /// Returns the square root of an `f64` | |
455 | pub fn sqrtf64(x: f64) -> f64; | |
456 | ||
457 | /// Raises an `f32` to an integer power. | |
458 | pub fn powif32(a: f32, x: i32) -> f32; | |
459 | /// Raises an `f64` to an integer power. | |
460 | pub fn powif64(a: f64, x: i32) -> f64; | |
461 | ||
462 | /// Returns the sine of an `f32`. | |
463 | pub fn sinf32(x: f32) -> f32; | |
464 | /// Returns the sine of an `f64`. | |
465 | pub fn sinf64(x: f64) -> f64; | |
466 | ||
467 | /// Returns the cosine of an `f32`. | |
468 | pub fn cosf32(x: f32) -> f32; | |
469 | /// Returns the cosine of an `f64`. | |
470 | pub fn cosf64(x: f64) -> f64; | |
471 | ||
472 | /// Raises an `f32` to an `f32` power. | |
473 | pub fn powf32(a: f32, x: f32) -> f32; | |
474 | /// Raises an `f64` to an `f64` power. | |
475 | pub fn powf64(a: f64, x: f64) -> f64; | |
476 | ||
477 | /// Returns the exponential of an `f32`. | |
478 | pub fn expf32(x: f32) -> f32; | |
479 | /// Returns the exponential of an `f64`. | |
480 | pub fn expf64(x: f64) -> f64; | |
481 | ||
482 | /// Returns 2 raised to the power of an `f32`. | |
483 | pub fn exp2f32(x: f32) -> f32; | |
484 | /// Returns 2 raised to the power of an `f64`. | |
485 | pub fn exp2f64(x: f64) -> f64; | |
486 | ||
487 | /// Returns the natural logarithm of an `f32`. | |
488 | pub fn logf32(x: f32) -> f32; | |
489 | /// Returns the natural logarithm of an `f64`. | |
490 | pub fn logf64(x: f64) -> f64; | |
491 | ||
492 | /// Returns the base 10 logarithm of an `f32`. | |
493 | pub fn log10f32(x: f32) -> f32; | |
494 | /// Returns the base 10 logarithm of an `f64`. | |
495 | pub fn log10f64(x: f64) -> f64; | |
496 | ||
497 | /// Returns the base 2 logarithm of an `f32`. | |
498 | pub fn log2f32(x: f32) -> f32; | |
499 | /// Returns the base 2 logarithm of an `f64`. | |
500 | pub fn log2f64(x: f64) -> f64; | |
501 | ||
502 | /// Returns `a * b + c` for `f32` values. | |
503 | pub fn fmaf32(a: f32, b: f32, c: f32) -> f32; | |
504 | /// Returns `a * b + c` for `f64` values. | |
505 | pub fn fmaf64(a: f64, b: f64, c: f64) -> f64; | |
506 | ||
507 | /// Returns the absolute value of an `f32`. | |
508 | pub fn fabsf32(x: f32) -> f32; | |
509 | /// Returns the absolute value of an `f64`. | |
510 | pub fn fabsf64(x: f64) -> f64; | |
511 | ||
512 | /// Copies the sign from `y` to `x` for `f32` values. | |
513 | pub fn copysignf32(x: f32, y: f32) -> f32; | |
514 | /// Copies the sign from `y` to `x` for `f64` values. | |
515 | pub fn copysignf64(x: f64, y: f64) -> f64; | |
516 | ||
517 | /// Returns the largest integer less than or equal to an `f32`. | |
518 | pub fn floorf32(x: f32) -> f32; | |
519 | /// Returns the largest integer less than or equal to an `f64`. | |
520 | pub fn floorf64(x: f64) -> f64; | |
521 | ||
522 | /// Returns the smallest integer greater than or equal to an `f32`. | |
523 | pub fn ceilf32(x: f32) -> f32; | |
524 | /// Returns the smallest integer greater than or equal to an `f64`. | |
525 | pub fn ceilf64(x: f64) -> f64; | |
526 | ||
527 | /// Returns the integer part of an `f32`. | |
528 | pub fn truncf32(x: f32) -> f32; | |
529 | /// Returns the integer part of an `f64`. | |
530 | pub fn truncf64(x: f64) -> f64; | |
531 | ||
532 | /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception | |
533 | /// if the argument is not an integer. | |
534 | pub fn rintf32(x: f32) -> f32; | |
535 | /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception | |
536 | /// if the argument is not an integer. | |
537 | pub fn rintf64(x: f64) -> f64; | |
538 | ||
539 | /// Returns the nearest integer to an `f32`. | |
540 | pub fn nearbyintf32(x: f32) -> f32; | |
541 | /// Returns the nearest integer to an `f64`. | |
542 | pub fn nearbyintf64(x: f64) -> f64; | |
543 | ||
544 | /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero. | |
545 | pub fn roundf32(x: f32) -> f32; | |
546 | /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero. | |
547 | pub fn roundf64(x: f64) -> f64; | |
548 | ||
54a0048b SL |
549 | /// Float addition that allows optimizations based on algebraic rules. |
550 | /// May assume inputs are finite. | |
551 | #[cfg(not(stage0))] | |
552 | pub fn fadd_fast<T>(a: T, b: T) -> T; | |
553 | ||
554 | /// Float subtraction that allows optimizations based on algebraic rules. | |
555 | /// May assume inputs are finite. | |
556 | #[cfg(not(stage0))] | |
557 | pub fn fsub_fast<T>(a: T, b: T) -> T; | |
558 | ||
559 | /// Float multiplication that allows optimizations based on algebraic rules. | |
560 | /// May assume inputs are finite. | |
561 | #[cfg(not(stage0))] | |
562 | pub fn fmul_fast<T>(a: T, b: T) -> T; | |
563 | ||
564 | /// Float division that allows optimizations based on algebraic rules. | |
565 | /// May assume inputs are finite. | |
566 | #[cfg(not(stage0))] | |
567 | pub fn fdiv_fast<T>(a: T, b: T) -> T; | |
568 | ||
569 | /// Float remainder that allows optimizations based on algebraic rules. | |
570 | /// May assume inputs are finite. | |
571 | #[cfg(not(stage0))] | |
572 | pub fn frem_fast<T>(a: T, b: T) -> T; | |
573 | ||
574 | ||
92a42be0 | 575 | /// Returns the number of bits set in an integer type `T` |
92a42be0 | 576 | pub fn ctpop<T>(x: T) -> T; |
1a4d82fc | 577 | |
92a42be0 | 578 | /// Returns the number of leading bits unset in an integer type `T` |
92a42be0 | 579 | pub fn ctlz<T>(x: T) -> T; |
1a4d82fc | 580 | |
92a42be0 | 581 | /// Returns the number of trailing bits unset in an integer type `T` |
92a42be0 | 582 | pub fn cttz<T>(x: T) -> T; |
1a4d82fc | 583 | |
92a42be0 | 584 | /// Reverses the bytes in an integer type `T`. |
92a42be0 | 585 | pub fn bswap<T>(x: T) -> T; |
1a4d82fc | 586 | |
92a42be0 | 587 | /// Performs checked integer addition. |
92a42be0 SL |
588 | pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool); |
589 | ||
92a42be0 | 590 | /// Performs checked integer subtraction |
92a42be0 SL |
591 | pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool); |
592 | ||
92a42be0 | 593 | /// Performs checked integer multiplication |
92a42be0 SL |
594 | pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool); |
595 | ||
596 | /// Performs an unchecked division, resulting in undefined behavior | |
597 | /// where y = 0 or x = `T::min_value()` and y = -1 | |
92a42be0 SL |
598 | pub fn unchecked_div<T>(x: T, y: T) -> T; |
599 | /// Returns the remainder of an unchecked division, resulting in | |
600 | /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1 | |
92a42be0 SL |
601 | pub fn unchecked_rem<T>(x: T, y: T) -> T; |
602 | ||
603 | /// Returns (a + b) mod 2^N, where N is the width of T in bits. | |
c34b1796 | 604 | pub fn overflowing_add<T>(a: T, b: T) -> T; |
92a42be0 | 605 | /// Returns (a - b) mod 2^N, where N is the width of T in bits. |
c34b1796 | 606 | pub fn overflowing_sub<T>(a: T, b: T) -> T; |
92a42be0 | 607 | /// Returns (a * b) mod 2^N, where N is the width of T in bits. |
c34b1796 | 608 | pub fn overflowing_mul<T>(a: T, b: T) -> T; |
9346a6ac | 609 | |
62682a34 SL |
610 | /// Returns the value of the discriminant for the variant in 'v', |
611 | /// cast to a `u64`; if `T` has no discriminant, returns 0. | |
612 | pub fn discriminant_value<T>(v: &T) -> u64; | |
c1a9b12d SL |
613 | |
614 | /// Rust's "try catch" construct which invokes the function pointer `f` with | |
7453a54e SL |
615 | /// the data pointer `data`. |
616 | /// | |
617 | /// The third pointer is a target-specific data pointer which is filled in | |
618 | /// with the specifics of the exception that occurred. For examples on Unix | |
619 | /// platforms this is a `*mut *mut T` which is filled in by the compiler and | |
620 | /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's | |
621 | /// source as well as std's catch implementation. | |
622 | pub fn try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32; | |
d9579d0f | 623 | } |