1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! rustc compiler intrinsics.
13 //! The corresponding definitions are in librustc_trans/trans/intrinsic.rs.
17 //! The volatile intrinsics provide operations intended to act on I/O
18 //! memory, which are guaranteed to not be reordered by the compiler
19 //! across other volatile intrinsics. See the LLVM documentation on
22 //! [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
26 //! The atomic intrinsics provide common atomic operations on machine
27 //! words, with multiple possible memory orderings. They obey the same
28 //! semantics as C++11. See the LLVM documentation on [[atomics]].
30 //! [atomics]: http://llvm.org/docs/Atomics.html
32 //! A quick refresher on memory ordering:
34 //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
35 //! take place after the barrier.
36 //! * Release - a barrier for releasing a lock. Preceding reads and writes
37 //! take place before the barrier.
38 //! * Sequentially consistent - sequentially consistent operations are
39 //! guaranteed to happen in order. This is the standard mode for working
40 //! with atomic types and is equivalent to Java's `volatile`.
42 #![unstable(feature = "core")]
43 #![allow(missing_docs)]
47 pub type GlueFn = extern "Rust" fn(*const i8);
58 // Called when a value of type `T` is no longer needed
59 pub drop_glue: GlueFn,
61 // Name corresponding to the type
62 pub name: &'static str,
65 extern "rust-intrinsic" {
67 // NB: These intrinsics take unsafe pointers because they mutate aliased
68 // memory, which is not valid for either `&` or `&mut`.
70 pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> T;
71 pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> T;
72 pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> T;
73 pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> T;
74 pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> T;
76 pub fn atomic_load<T>(src: *const T) -> T;
77 pub fn atomic_load_acq<T>(src: *const T) -> T;
78 pub fn atomic_load_relaxed<T>(src: *const T) -> T;
79 pub fn atomic_load_unordered<T>(src: *const T) -> T;
81 pub fn atomic_store<T>(dst: *mut T, val: T);
82 pub fn atomic_store_rel<T>(dst: *mut T, val: T);
83 pub fn atomic_store_relaxed<T>(dst: *mut T, val: T);
84 pub fn atomic_store_unordered<T>(dst: *mut T, val: T);
86 pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
87 pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
88 pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
89 pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T;
90 pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T;
92 pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
93 pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
94 pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
95 pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T;
96 pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T;
98 pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
99 pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
100 pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
101 pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T;
102 pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T;
104 pub fn atomic_and<T>(dst: *mut T, src: T) -> T;
105 pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T;
106 pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T;
107 pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T;
108 pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T;
110 pub fn atomic_nand<T>(dst: *mut T, src: T) -> T;
111 pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T;
112 pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T;
113 pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T;
114 pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T;
116 pub fn atomic_or<T>(dst: *mut T, src: T) -> T;
117 pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T;
118 pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T;
119 pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T;
120 pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T;
122 pub fn atomic_xor<T>(dst: *mut T, src: T) -> T;
123 pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T;
124 pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T;
125 pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T;
126 pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T;
128 pub fn atomic_max<T>(dst: *mut T, src: T) -> T;
129 pub fn atomic_max_acq<T>(dst: *mut T, src: T) -> T;
130 pub fn atomic_max_rel<T>(dst: *mut T, src: T) -> T;
131 pub fn atomic_max_acqrel<T>(dst: *mut T, src: T) -> T;
132 pub fn atomic_max_relaxed<T>(dst: *mut T, src: T) -> T;
134 pub fn atomic_min<T>(dst: *mut T, src: T) -> T;
135 pub fn atomic_min_acq<T>(dst: *mut T, src: T) -> T;
136 pub fn atomic_min_rel<T>(dst: *mut T, src: T) -> T;
137 pub fn atomic_min_acqrel<T>(dst: *mut T, src: T) -> T;
138 pub fn atomic_min_relaxed<T>(dst: *mut T, src: T) -> T;
140 pub fn atomic_umin<T>(dst: *mut T, src: T) -> T;
141 pub fn atomic_umin_acq<T>(dst: *mut T, src: T) -> T;
142 pub fn atomic_umin_rel<T>(dst: *mut T, src: T) -> T;
143 pub fn atomic_umin_acqrel<T>(dst: *mut T, src: T) -> T;
144 pub fn atomic_umin_relaxed<T>(dst: *mut T, src: T) -> T;
146 pub fn atomic_umax<T>(dst: *mut T, src: T) -> T;
147 pub fn atomic_umax_acq<T>(dst: *mut T, src: T) -> T;
148 pub fn atomic_umax_rel<T>(dst: *mut T, src: T) -> T;
149 pub fn atomic_umax_acqrel<T>(dst: *mut T, src: T) -> T;
150 pub fn atomic_umax_relaxed<T>(dst: *mut T, src: T) -> T;
153 extern "rust-intrinsic" {
155 pub fn atomic_fence();
156 pub fn atomic_fence_acq();
157 pub fn atomic_fence_rel();
158 pub fn atomic_fence_acqrel();
160 /// Abort the execution of the process.
163 /// Tell LLVM that this point in the code is not reachable,
164 /// enabling further optimizations.
166 /// NB: This is very different from the `unreachable!()` macro!
167 pub fn unreachable() -> !;
169 /// Inform the optimizer that a condition is always true.
170 /// If the condition is false, the behavior is undefined.
172 /// No code is generated for this intrinsic, but the optimizer will try
173 /// to preserve it (and its condition) between passes, which may interfere
174 /// with optimization of surrounding code and reduce performance. It should
175 /// not be used if the invariant can be discovered by the optimizer on its
176 /// own, or if it does not enable any significant optimizations.
177 pub fn assume(b: bool);
179 /// Execute a breakpoint trap, for inspection by a debugger.
182 /// The size of a type in bytes.
184 /// This is the exact number of bytes in memory taken up by a
185 /// value of the given type. In other words, a memset of this size
186 /// would *exactly* overwrite a value. When laid out in vectors
187 /// and structures there may be additional padding between
189 pub fn size_of<T>() -> uint;
191 /// Move a value to an uninitialized memory location.
193 /// Drop glue is not run on the destination.
194 pub fn move_val_init<T>(dst: &mut T, src: T);
196 pub fn min_align_of<T>() -> uint;
197 pub fn pref_align_of<T>() -> uint;
199 /// Get a static pointer to a type descriptor.
200 pub fn get_tydesc<T: ?Sized>() -> *const TyDesc;
202 /// Gets an identifier which is globally unique to the specified type. This
203 /// function will return the same value for a type regardless of whichever
204 /// crate it is invoked in.
205 pub fn type_id<T: ?Sized + 'static>() -> u64;
207 /// Create a value initialized to zero.
209 /// `init` is unsafe because it returns a zeroed-out datum,
210 /// which is unsafe unless T is Copy.
211 pub fn init<T>() -> T;
213 /// Create an uninitialized value.
214 pub fn uninit<T>() -> T;
216 /// Move a value out of scope without running drop glue.
218 /// `forget` is unsafe because the caller is responsible for
219 /// ensuring the argument is deallocated already.
220 #[stable(feature = "rust1", since = "1.0.0")]
221 pub fn forget<T>(_: T) -> ();
223 /// Unsafely transforms a value of one type into a value of another type.
225 /// Both types must have the same size.
232 /// let v: &[u8] = unsafe { mem::transmute("L") };
233 /// assert!(v == [76u8]);
235 #[stable(feature = "rust1", since = "1.0.0")]
236 pub fn transmute<T,U>(e: T) -> U;
238 /// Gives the address for the return value of the enclosing function.
240 /// Using this intrinsic in a function that does not use an out pointer
241 /// will trigger a compiler error.
242 pub fn return_address() -> *const u8;
244 /// Returns `true` if a type requires drop glue.
245 pub fn needs_drop<T>() -> bool;
247 /// Returns `true` if a type is managed (will be allocated on the local heap)
248 pub fn owns_managed<T>() -> bool;
250 /// Calculates the offset from a pointer. The offset *must* be in-bounds of
251 /// the object, or one-byte-past-the-end. An arithmetic overflow is also
252 /// undefined behaviour.
254 /// This is implemented as an intrinsic to avoid converting to and from an
255 /// integer, since the conversion would throw away aliasing information.
256 pub fn offset<T>(dst: *const T, offset: int) -> *const T;
258 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
259 /// and destination may *not* overlap.
261 /// `copy_nonoverlapping_memory` is semantically equivalent to C's `memcpy`.
265 /// Beyond requiring that the program must be allowed to access both regions
266 /// of memory, it is Undefined Behaviour for source and destination to
267 /// overlap. Care must also be taken with the ownership of `src` and
268 /// `dst`. This method semantically moves the values of `src` into `dst`.
269 /// However it does not drop the contents of `dst`, or prevent the contents
270 /// of `src` from being dropped or used.
274 /// A safe swap function:
280 /// fn swap<T>(x: &mut T, y: &mut T) {
282 /// // Give ourselves some scratch space to work with
283 /// let mut t: T = mem::uninitialized();
285 /// // Perform the swap, `&mut` pointers never alias
286 /// ptr::copy_nonoverlapping_memory(&mut t, &*x, 1);
287 /// ptr::copy_nonoverlapping_memory(x, &*y, 1);
288 /// ptr::copy_nonoverlapping_memory(y, &t, 1);
290 /// // y and t now point to the same thing, but we need to completely forget `tmp`
291 /// // because it's no longer relevant.
296 #[unstable(feature = "core")]
297 pub fn copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: uint);
299 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
300 /// and destination may overlap.
302 /// `copy_memory` is semantically equivalent to C's `memmove`.
306 /// Care must be taken with the ownership of `src` and `dst`.
307 /// This method semantically moves the values of `src` into `dst`.
308 /// However it does not drop the contents of `dst`, or prevent the contents of `src`
309 /// from being dropped or used.
313 /// Efficiently create a Rust vector from an unsafe buffer:
318 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: uint) -> Vec<T> {
319 /// let mut dst = Vec::with_capacity(elts);
320 /// dst.set_len(elts);
321 /// ptr::copy_memory(dst.as_mut_ptr(), ptr, elts);
326 #[unstable(feature = "core")]
327 pub fn copy_memory<T>(dst: *mut T, src: *const T, count: uint);
329 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
330 /// bytes of memory starting at `dst` to `c`.
331 #[unstable(feature = "core",
332 reason = "uncertain about naming and semantics")]
333 pub fn set_memory<T>(dst: *mut T, val: u8, count: uint);
335 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
336 /// a size of `count` * `size_of::<T>()` and an alignment of
337 /// `min_align_of::<T>()`
339 /// The volatile parameter parameter is set to `true`, so it will not be optimized out.
340 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
342 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
343 /// a size of `count` * `size_of::<T>()` and an alignment of
344 /// `min_align_of::<T>()`
346 /// The volatile parameter parameter is set to `true`, so it will not be optimized out.
347 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: uint);
348 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
349 /// size of `count` * `size_of::<T>()` and an alignment of
350 /// `min_align_of::<T>()`.
352 /// The volatile parameter parameter is set to `true`, so it will not be optimized out.
353 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: uint);
355 /// Perform a volatile load from the `src` pointer.
356 pub fn volatile_load<T>(src: *const T) -> T;
357 /// Perform a volatile store to the `dst` pointer.
358 pub fn volatile_store<T>(dst: *mut T, val: T);
360 /// Returns the square root of an `f32`
361 pub fn sqrtf32(x: f32) -> f32;
362 /// Returns the square root of an `f64`
363 pub fn sqrtf64(x: f64) -> f64;
365 /// Raises an `f32` to an integer power.
366 pub fn powif32(a: f32, x: i32) -> f32;
367 /// Raises an `f64` to an integer power.
368 pub fn powif64(a: f64, x: i32) -> f64;
370 /// Returns the sine of an `f32`.
371 pub fn sinf32(x: f32) -> f32;
372 /// Returns the sine of an `f64`.
373 pub fn sinf64(x: f64) -> f64;
375 /// Returns the cosine of an `f32`.
376 pub fn cosf32(x: f32) -> f32;
377 /// Returns the cosine of an `f64`.
378 pub fn cosf64(x: f64) -> f64;
380 /// Raises an `f32` to an `f32` power.
381 pub fn powf32(a: f32, x: f32) -> f32;
382 /// Raises an `f64` to an `f64` power.
383 pub fn powf64(a: f64, x: f64) -> f64;
385 /// Returns the exponential of an `f32`.
386 pub fn expf32(x: f32) -> f32;
387 /// Returns the exponential of an `f64`.
388 pub fn expf64(x: f64) -> f64;
390 /// Returns 2 raised to the power of an `f32`.
391 pub fn exp2f32(x: f32) -> f32;
392 /// Returns 2 raised to the power of an `f64`.
393 pub fn exp2f64(x: f64) -> f64;
395 /// Returns the natural logarithm of an `f32`.
396 pub fn logf32(x: f32) -> f32;
397 /// Returns the natural logarithm of an `f64`.
398 pub fn logf64(x: f64) -> f64;
400 /// Returns the base 10 logarithm of an `f32`.
401 pub fn log10f32(x: f32) -> f32;
402 /// Returns the base 10 logarithm of an `f64`.
403 pub fn log10f64(x: f64) -> f64;
405 /// Returns the base 2 logarithm of an `f32`.
406 pub fn log2f32(x: f32) -> f32;
407 /// Returns the base 2 logarithm of an `f64`.
408 pub fn log2f64(x: f64) -> f64;
410 /// Returns `a * b + c` for `f32` values.
411 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
412 /// Returns `a * b + c` for `f64` values.
413 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
415 /// Returns the absolute value of an `f32`.
416 pub fn fabsf32(x: f32) -> f32;
417 /// Returns the absolute value of an `f64`.
418 pub fn fabsf64(x: f64) -> f64;
420 /// Copies the sign from `y` to `x` for `f32` values.
421 pub fn copysignf32(x: f32, y: f32) -> f32;
422 /// Copies the sign from `y` to `x` for `f64` values.
423 pub fn copysignf64(x: f64, y: f64) -> f64;
425 /// Returns the largest integer less than or equal to an `f32`.
426 pub fn floorf32(x: f32) -> f32;
427 /// Returns the largest integer less than or equal to an `f64`.
428 pub fn floorf64(x: f64) -> f64;
430 /// Returns the smallest integer greater than or equal to an `f32`.
431 pub fn ceilf32(x: f32) -> f32;
432 /// Returns the smallest integer greater than or equal to an `f64`.
433 pub fn ceilf64(x: f64) -> f64;
435 /// Returns the integer part of an `f32`.
436 pub fn truncf32(x: f32) -> f32;
437 /// Returns the integer part of an `f64`.
438 pub fn truncf64(x: f64) -> f64;
440 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
441 /// if the argument is not an integer.
442 pub fn rintf32(x: f32) -> f32;
443 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
444 /// if the argument is not an integer.
445 pub fn rintf64(x: f64) -> f64;
447 /// Returns the nearest integer to an `f32`.
448 pub fn nearbyintf32(x: f32) -> f32;
449 /// Returns the nearest integer to an `f64`.
450 pub fn nearbyintf64(x: f64) -> f64;
452 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
453 pub fn roundf32(x: f32) -> f32;
454 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
455 pub fn roundf64(x: f64) -> f64;
457 /// Returns the number of bits set in a `u8`.
458 pub fn ctpop8(x: u8) -> u8;
459 /// Returns the number of bits set in a `u16`.
460 pub fn ctpop16(x: u16) -> u16;
461 /// Returns the number of bits set in a `u32`.
462 pub fn ctpop32(x: u32) -> u32;
463 /// Returns the number of bits set in a `u64`.
464 pub fn ctpop64(x: u64) -> u64;
466 /// Returns the number of leading bits unset in a `u8`.
467 pub fn ctlz8(x: u8) -> u8;
468 /// Returns the number of leading bits unset in a `u16`.
469 pub fn ctlz16(x: u16) -> u16;
470 /// Returns the number of leading bits unset in a `u32`.
471 pub fn ctlz32(x: u32) -> u32;
472 /// Returns the number of leading bits unset in a `u64`.
473 pub fn ctlz64(x: u64) -> u64;
475 /// Returns the number of trailing bits unset in a `u8`.
476 pub fn cttz8(x: u8) -> u8;
477 /// Returns the number of trailing bits unset in a `u16`.
478 pub fn cttz16(x: u16) -> u16;
479 /// Returns the number of trailing bits unset in a `u32`.
480 pub fn cttz32(x: u32) -> u32;
481 /// Returns the number of trailing bits unset in a `u64`.
482 pub fn cttz64(x: u64) -> u64;
484 /// Reverses the bytes in a `u16`.
485 pub fn bswap16(x: u16) -> u16;
486 /// Reverses the bytes in a `u32`.
487 pub fn bswap32(x: u32) -> u32;
488 /// Reverses the bytes in a `u64`.
489 pub fn bswap64(x: u64) -> u64;
491 /// Performs checked `i8` addition.
492 pub fn i8_add_with_overflow(x: i8, y: i8) -> (i8, bool);
493 /// Performs checked `i16` addition.
494 pub fn i16_add_with_overflow(x: i16, y: i16) -> (i16, bool);
495 /// Performs checked `i32` addition.
496 pub fn i32_add_with_overflow(x: i32, y: i32) -> (i32, bool);
497 /// Performs checked `i64` addition.
498 pub fn i64_add_with_overflow(x: i64, y: i64) -> (i64, bool);
500 /// Performs checked `u8` addition.
501 pub fn u8_add_with_overflow(x: u8, y: u8) -> (u8, bool);
502 /// Performs checked `u16` addition.
503 pub fn u16_add_with_overflow(x: u16, y: u16) -> (u16, bool);
504 /// Performs checked `u32` addition.
505 pub fn u32_add_with_overflow(x: u32, y: u32) -> (u32, bool);
506 /// Performs checked `u64` addition.
507 pub fn u64_add_with_overflow(x: u64, y: u64) -> (u64, bool);
509 /// Performs checked `i8` subtraction.
510 pub fn i8_sub_with_overflow(x: i8, y: i8) -> (i8, bool);
511 /// Performs checked `i16` subtraction.
512 pub fn i16_sub_with_overflow(x: i16, y: i16) -> (i16, bool);
513 /// Performs checked `i32` subtraction.
514 pub fn i32_sub_with_overflow(x: i32, y: i32) -> (i32, bool);
515 /// Performs checked `i64` subtraction.
516 pub fn i64_sub_with_overflow(x: i64, y: i64) -> (i64, bool);
518 /// Performs checked `u8` subtraction.
519 pub fn u8_sub_with_overflow(x: u8, y: u8) -> (u8, bool);
520 /// Performs checked `u16` subtraction.
521 pub fn u16_sub_with_overflow(x: u16, y: u16) -> (u16, bool);
522 /// Performs checked `u32` subtraction.
523 pub fn u32_sub_with_overflow(x: u32, y: u32) -> (u32, bool);
524 /// Performs checked `u64` subtraction.
525 pub fn u64_sub_with_overflow(x: u64, y: u64) -> (u64, bool);
527 /// Performs checked `i8` multiplication.
528 pub fn i8_mul_with_overflow(x: i8, y: i8) -> (i8, bool);
529 /// Performs checked `i16` multiplication.
530 pub fn i16_mul_with_overflow(x: i16, y: i16) -> (i16, bool);
531 /// Performs checked `i32` multiplication.
532 pub fn i32_mul_with_overflow(x: i32, y: i32) -> (i32, bool);
533 /// Performs checked `i64` multiplication.
534 pub fn i64_mul_with_overflow(x: i64, y: i64) -> (i64, bool);
536 /// Performs checked `u8` multiplication.
537 pub fn u8_mul_with_overflow(x: u8, y: u8) -> (u8, bool);
538 /// Performs checked `u16` multiplication.
539 pub fn u16_mul_with_overflow(x: u16, y: u16) -> (u16, bool);
540 /// Performs checked `u32` multiplication.
541 pub fn u32_mul_with_overflow(x: u32, y: u32) -> (u32, bool);
542 /// Performs checked `u64` multiplication.
543 pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool);