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`.
43 #![allow(missing_docs)]
47 #[cfg(stage0)] use any::TypeId;
49 pub type GlueFn = extern "Rust" fn(*const i8);
60 // Called when a value of type `T` is no longer needed
61 pub drop_glue: GlueFn,
63 // Name corresponding to the type
64 pub name: &'static str,
67 extern "rust-intrinsic" {
69 // NB: These intrinsics take unsafe pointers because they mutate aliased
70 // memory, which is not valid for either `&` or `&mut`.
72 pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> T;
73 pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> T;
74 pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> T;
75 pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> T;
76 pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> T;
78 pub fn atomic_load<T>(src: *const T) -> T;
79 pub fn atomic_load_acq<T>(src: *const T) -> T;
80 pub fn atomic_load_relaxed<T>(src: *const T) -> T;
81 pub fn atomic_load_unordered<T>(src: *const T) -> T;
83 pub fn atomic_store<T>(dst: *mut T, val: T);
84 pub fn atomic_store_rel<T>(dst: *mut T, val: T);
85 pub fn atomic_store_relaxed<T>(dst: *mut T, val: T);
86 pub fn atomic_store_unordered<T>(dst: *mut T, val: T);
88 pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
89 pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
90 pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
91 pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T;
92 pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T;
94 pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
95 pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
96 pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
97 pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T;
98 pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T;
100 pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
101 pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
102 pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
103 pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T;
104 pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T;
106 pub fn atomic_and<T>(dst: *mut T, src: T) -> T;
107 pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T;
108 pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T;
109 pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T;
110 pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T;
112 pub fn atomic_nand<T>(dst: *mut T, src: T) -> T;
113 pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T;
114 pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T;
115 pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T;
116 pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T;
118 pub fn atomic_or<T>(dst: *mut T, src: T) -> T;
119 pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T;
120 pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T;
121 pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T;
122 pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T;
124 pub fn atomic_xor<T>(dst: *mut T, src: T) -> T;
125 pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T;
126 pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T;
127 pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T;
128 pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T;
130 pub fn atomic_max<T>(dst: *mut T, src: T) -> T;
131 pub fn atomic_max_acq<T>(dst: *mut T, src: T) -> T;
132 pub fn atomic_max_rel<T>(dst: *mut T, src: T) -> T;
133 pub fn atomic_max_acqrel<T>(dst: *mut T, src: T) -> T;
134 pub fn atomic_max_relaxed<T>(dst: *mut T, src: T) -> T;
136 pub fn atomic_min<T>(dst: *mut T, src: T) -> T;
137 pub fn atomic_min_acq<T>(dst: *mut T, src: T) -> T;
138 pub fn atomic_min_rel<T>(dst: *mut T, src: T) -> T;
139 pub fn atomic_min_acqrel<T>(dst: *mut T, src: T) -> T;
140 pub fn atomic_min_relaxed<T>(dst: *mut T, src: T) -> T;
142 pub fn atomic_umin<T>(dst: *mut T, src: T) -> T;
143 pub fn atomic_umin_acq<T>(dst: *mut T, src: T) -> T;
144 pub fn atomic_umin_rel<T>(dst: *mut T, src: T) -> T;
145 pub fn atomic_umin_acqrel<T>(dst: *mut T, src: T) -> T;
146 pub fn atomic_umin_relaxed<T>(dst: *mut T, src: T) -> T;
148 pub fn atomic_umax<T>(dst: *mut T, src: T) -> T;
149 pub fn atomic_umax_acq<T>(dst: *mut T, src: T) -> T;
150 pub fn atomic_umax_rel<T>(dst: *mut T, src: T) -> T;
151 pub fn atomic_umax_acqrel<T>(dst: *mut T, src: T) -> T;
152 pub fn atomic_umax_relaxed<T>(dst: *mut T, src: T) -> T;
155 extern "rust-intrinsic" {
157 pub fn atomic_fence();
158 pub fn atomic_fence_acq();
159 pub fn atomic_fence_rel();
160 pub fn atomic_fence_acqrel();
162 /// Abort the execution of the process.
165 /// Tell LLVM that this point in the code is not reachable,
166 /// enabling further optimizations.
168 /// NB: This is very different from the `unreachable!()` macro!
169 pub fn unreachable() -> !;
171 /// Inform the optimizer that a condition is always true.
172 /// If the condition is false, the behavior is undefined.
174 /// No code is generated for this intrinsic, but the optimizer will try
175 /// to preserve it (and its condition) between passes, which may interfere
176 /// with optimization of surrounding code and reduce performance. It should
177 /// not be used if the invariant can be discovered by the optimizer on its
178 /// own, or if it does not enable any significant optimizations.
179 pub fn assume(b: bool);
181 /// Execute a breakpoint trap, for inspection by a debugger.
184 /// The size of a type in bytes.
186 /// This is the exact number of bytes in memory taken up by a
187 /// value of the given type. In other words, a memset of this size
188 /// would *exactly* overwrite a value. When laid out in vectors
189 /// and structures there may be additional padding between
191 pub fn size_of<T>() -> uint;
193 /// Move a value to an uninitialized memory location.
195 /// Drop glue is not run on the destination.
196 pub fn move_val_init<T>(dst: &mut T, src: T);
198 pub fn min_align_of<T>() -> uint;
199 pub fn pref_align_of<T>() -> uint;
201 /// Get a static pointer to a type descriptor.
203 pub fn get_tydesc<T: ?Sized>() -> *const TyDesc;
206 pub fn get_tydesc<T>() -> *const TyDesc;
208 /// Gets an identifier which is globally unique to the specified type. This
209 /// function will return the same value for a type regardless of whichever
210 /// crate it is invoked in.
212 pub fn type_id<T: ?Sized + 'static>() -> u64;
215 pub fn type_id<T: ?Sized + 'static>() -> TypeId;
217 /// Create a value initialized to zero.
219 /// `init` is unsafe because it returns a zeroed-out datum,
220 /// which is unsafe unless T is Copy.
221 pub fn init<T>() -> T;
223 /// Create an uninitialized value.
224 pub fn uninit<T>() -> T;
226 /// Move a value out of scope without running drop glue.
228 /// `forget` is unsafe because the caller is responsible for
229 /// ensuring the argument is deallocated already.
231 pub fn forget<T>(_: T) -> ();
233 /// Unsafely transforms a value of one type into a value of another type.
235 /// Both types must have the same size and alignment, and this guarantee
236 /// is enforced at compile-time.
243 /// let v: &[u8] = unsafe { mem::transmute("L") };
244 /// assert!(v == [76u8]);
247 pub fn transmute<T,U>(e: T) -> U;
249 /// Gives the address for the return value of the enclosing function.
251 /// Using this intrinsic in a function that does not use an out pointer
252 /// will trigger a compiler error.
253 pub fn return_address() -> *const u8;
255 /// Returns `true` if a type requires drop glue.
256 pub fn needs_drop<T>() -> bool;
258 /// Returns `true` if a type is managed (will be allocated on the local heap)
259 pub fn owns_managed<T>() -> bool;
261 /// Calculates the offset from a pointer. The offset *must* be in-bounds of
262 /// the object, or one-byte-past-the-end. An arithmetic overflow is also
263 /// undefined behaviour.
265 /// This is implemented as an intrinsic to avoid converting to and from an
266 /// integer, since the conversion would throw away aliasing information.
267 pub fn offset<T>(dst: *const T, offset: int) -> *const T;
269 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
270 /// and destination may *not* overlap.
272 /// `copy_nonoverlapping_memory` is semantically equivalent to C's `memcpy`.
276 /// Beyond requiring that both regions of memory be allocated, it is Undefined Behaviour
277 /// for source and destination to overlap. Care must also be taken with the ownership of
278 /// `src` and `dst`. This method semantically moves the values of `src` into `dst`.
279 /// However it does not drop the contents of `dst`, or prevent the contents of `src`
280 /// from being dropped or used.
284 /// A safe swap function:
290 /// fn swap<T>(x: &mut T, y: &mut T) {
292 /// // Give ourselves some scratch space to work with
293 /// let mut t: T = mem::uninitialized();
295 /// // Perform the swap, `&mut` pointers never alias
296 /// ptr::copy_nonoverlapping_memory(&mut t, &*x, 1);
297 /// ptr::copy_nonoverlapping_memory(x, &*y, 1);
298 /// ptr::copy_nonoverlapping_memory(y, &t, 1);
300 /// // y and t now point to the same thing, but we need to completely forget `tmp`
301 /// // because it's no longer relevant.
307 pub fn copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: uint);
309 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
310 /// and destination may overlap.
312 /// `copy_memory` is semantically equivalent to C's `memmove`.
316 /// Care must be taken with the ownership of `src` and `dst`.
317 /// This method semantically moves the values of `src` into `dst`.
318 /// However it does not drop the contents of `dst`, or prevent the contents of `src`
319 /// from being dropped or used.
323 /// Efficiently create a Rust vector from an unsafe buffer:
328 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: uint) -> Vec<T> {
329 /// let mut dst = Vec::with_capacity(elts);
330 /// dst.set_len(elts);
331 /// ptr::copy_memory(dst.as_mut_ptr(), ptr, elts);
337 pub fn copy_memory<T>(dst: *mut T, src: *const T, count: uint);
339 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
340 /// bytes of memory starting at `dst` to `c`.
341 #[unstable = "uncertain about naming and semantics"]
342 pub fn set_memory<T>(dst: *mut T, val: u8, count: uint);
344 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
345 /// a size of `count` * `size_of::<T>()` and an alignment of
346 /// `min_align_of::<T>()`
348 /// The volatile parameter parameter is set to `true`, so it will not be optimized out.
349 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
351 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
352 /// a size of `count` * `size_of::<T>()` and an alignment of
353 /// `min_align_of::<T>()`
355 /// The volatile parameter parameter is set to `true`, so it will not be optimized out.
356 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: uint);
357 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
358 /// size of `count` * `size_of::<T>()` and an alignment of
359 /// `min_align_of::<T>()`.
361 /// The volatile parameter parameter is set to `true`, so it will not be optimized out.
362 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: uint);
364 /// Perform a volatile load from the `src` pointer.
365 pub fn volatile_load<T>(src: *const T) -> T;
366 /// Perform a volatile store to the `dst` pointer.
367 pub fn volatile_store<T>(dst: *mut T, val: T);
369 /// Returns the square root of an `f32`
370 pub fn sqrtf32(x: f32) -> f32;
371 /// Returns the square root of an `f64`
372 pub fn sqrtf64(x: f64) -> f64;
374 /// Raises an `f32` to an integer power.
375 pub fn powif32(a: f32, x: i32) -> f32;
376 /// Raises an `f64` to an integer power.
377 pub fn powif64(a: f64, x: i32) -> f64;
379 /// Returns the sine of an `f32`.
380 pub fn sinf32(x: f32) -> f32;
381 /// Returns the sine of an `f64`.
382 pub fn sinf64(x: f64) -> f64;
384 /// Returns the cosine of an `f32`.
385 pub fn cosf32(x: f32) -> f32;
386 /// Returns the cosine of an `f64`.
387 pub fn cosf64(x: f64) -> f64;
389 /// Raises an `f32` to an `f32` power.
390 pub fn powf32(a: f32, x: f32) -> f32;
391 /// Raises an `f64` to an `f64` power.
392 pub fn powf64(a: f64, x: f64) -> f64;
394 /// Returns the exponential of an `f32`.
395 pub fn expf32(x: f32) -> f32;
396 /// Returns the exponential of an `f64`.
397 pub fn expf64(x: f64) -> f64;
399 /// Returns 2 raised to the power of an `f32`.
400 pub fn exp2f32(x: f32) -> f32;
401 /// Returns 2 raised to the power of an `f64`.
402 pub fn exp2f64(x: f64) -> f64;
404 /// Returns the natural logarithm of an `f32`.
405 pub fn logf32(x: f32) -> f32;
406 /// Returns the natural logarithm of an `f64`.
407 pub fn logf64(x: f64) -> f64;
409 /// Returns the base 10 logarithm of an `f32`.
410 pub fn log10f32(x: f32) -> f32;
411 /// Returns the base 10 logarithm of an `f64`.
412 pub fn log10f64(x: f64) -> f64;
414 /// Returns the base 2 logarithm of an `f32`.
415 pub fn log2f32(x: f32) -> f32;
416 /// Returns the base 2 logarithm of an `f64`.
417 pub fn log2f64(x: f64) -> f64;
419 /// Returns `a * b + c` for `f32` values.
420 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
421 /// Returns `a * b + c` for `f64` values.
422 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
424 /// Returns the absolute value of an `f32`.
425 pub fn fabsf32(x: f32) -> f32;
426 /// Returns the absolute value of an `f64`.
427 pub fn fabsf64(x: f64) -> f64;
429 /// Copies the sign from `y` to `x` for `f32` values.
430 pub fn copysignf32(x: f32, y: f32) -> f32;
431 /// Copies the sign from `y` to `x` for `f64` values.
432 pub fn copysignf64(x: f64, y: f64) -> f64;
434 /// Returns the largest integer less than or equal to an `f32`.
435 pub fn floorf32(x: f32) -> f32;
436 /// Returns the largest integer less than or equal to an `f64`.
437 pub fn floorf64(x: f64) -> f64;
439 /// Returns the smallest integer greater than or equal to an `f32`.
440 pub fn ceilf32(x: f32) -> f32;
441 /// Returns the smallest integer greater than or equal to an `f64`.
442 pub fn ceilf64(x: f64) -> f64;
444 /// Returns the integer part of an `f32`.
445 pub fn truncf32(x: f32) -> f32;
446 /// Returns the integer part of an `f64`.
447 pub fn truncf64(x: f64) -> f64;
449 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
450 /// if the argument is not an integer.
451 pub fn rintf32(x: f32) -> f32;
452 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
453 /// if the argument is not an integer.
454 pub fn rintf64(x: f64) -> f64;
456 /// Returns the nearest integer to an `f32`.
457 pub fn nearbyintf32(x: f32) -> f32;
458 /// Returns the nearest integer to an `f64`.
459 pub fn nearbyintf64(x: f64) -> f64;
461 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
462 pub fn roundf32(x: f32) -> f32;
463 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
464 pub fn roundf64(x: f64) -> f64;
466 /// Returns the number of bits set in a `u8`.
467 pub fn ctpop8(x: u8) -> u8;
468 /// Returns the number of bits set in a `u16`.
469 pub fn ctpop16(x: u16) -> u16;
470 /// Returns the number of bits set in a `u32`.
471 pub fn ctpop32(x: u32) -> u32;
472 /// Returns the number of bits set in a `u64`.
473 pub fn ctpop64(x: u64) -> u64;
475 /// Returns the number of leading bits unset in a `u8`.
476 pub fn ctlz8(x: u8) -> u8;
477 /// Returns the number of leading bits unset in a `u16`.
478 pub fn ctlz16(x: u16) -> u16;
479 /// Returns the number of leading bits unset in a `u32`.
480 pub fn ctlz32(x: u32) -> u32;
481 /// Returns the number of leading bits unset in a `u64`.
482 pub fn ctlz64(x: u64) -> u64;
484 /// Returns the number of trailing bits unset in a `u8`.
485 pub fn cttz8(x: u8) -> u8;
486 /// Returns the number of trailing bits unset in a `u16`.
487 pub fn cttz16(x: u16) -> u16;
488 /// Returns the number of trailing bits unset in a `u32`.
489 pub fn cttz32(x: u32) -> u32;
490 /// Returns the number of trailing bits unset in a `u64`.
491 pub fn cttz64(x: u64) -> u64;
493 /// Reverses the bytes in a `u16`.
494 pub fn bswap16(x: u16) -> u16;
495 /// Reverses the bytes in a `u32`.
496 pub fn bswap32(x: u32) -> u32;
497 /// Reverses the bytes in a `u64`.
498 pub fn bswap64(x: u64) -> u64;
500 /// Performs checked `i8` addition.
501 pub fn i8_add_with_overflow(x: i8, y: i8) -> (i8, bool);
502 /// Performs checked `i16` addition.
503 pub fn i16_add_with_overflow(x: i16, y: i16) -> (i16, bool);
504 /// Performs checked `i32` addition.
505 pub fn i32_add_with_overflow(x: i32, y: i32) -> (i32, bool);
506 /// Performs checked `i64` addition.
507 pub fn i64_add_with_overflow(x: i64, y: i64) -> (i64, bool);
509 /// Performs checked `u8` addition.
510 pub fn u8_add_with_overflow(x: u8, y: u8) -> (u8, bool);
511 /// Performs checked `u16` addition.
512 pub fn u16_add_with_overflow(x: u16, y: u16) -> (u16, bool);
513 /// Performs checked `u32` addition.
514 pub fn u32_add_with_overflow(x: u32, y: u32) -> (u32, bool);
515 /// Performs checked `u64` addition.
516 pub fn u64_add_with_overflow(x: u64, y: u64) -> (u64, bool);
518 /// Performs checked `i8` subtraction.
519 pub fn i8_sub_with_overflow(x: i8, y: i8) -> (i8, bool);
520 /// Performs checked `i16` subtraction.
521 pub fn i16_sub_with_overflow(x: i16, y: i16) -> (i16, bool);
522 /// Performs checked `i32` subtraction.
523 pub fn i32_sub_with_overflow(x: i32, y: i32) -> (i32, bool);
524 /// Performs checked `i64` subtraction.
525 pub fn i64_sub_with_overflow(x: i64, y: i64) -> (i64, bool);
527 /// Performs checked `u8` subtraction.
528 pub fn u8_sub_with_overflow(x: u8, y: u8) -> (u8, bool);
529 /// Performs checked `u16` subtraction.
530 pub fn u16_sub_with_overflow(x: u16, y: u16) -> (u16, bool);
531 /// Performs checked `u32` subtraction.
532 pub fn u32_sub_with_overflow(x: u32, y: u32) -> (u32, bool);
533 /// Performs checked `u64` subtraction.
534 pub fn u64_sub_with_overflow(x: u64, y: u64) -> (u64, bool);
536 /// Performs checked `i8` multiplication.
537 pub fn i8_mul_with_overflow(x: i8, y: i8) -> (i8, bool);
538 /// Performs checked `i16` multiplication.
539 pub fn i16_mul_with_overflow(x: i16, y: i16) -> (i16, bool);
540 /// Performs checked `i32` multiplication.
541 pub fn i32_mul_with_overflow(x: i32, y: i32) -> (i32, bool);
542 /// Performs checked `i64` multiplication.
543 pub fn i64_mul_with_overflow(x: i64, y: i64) -> (i64, bool);
545 /// Performs checked `u8` multiplication.
546 pub fn u8_mul_with_overflow(x: u8, y: u8) -> (u8, bool);
547 /// Performs checked `u16` multiplication.
548 pub fn u16_mul_with_overflow(x: u16, y: u16) -> (u16, bool);
549 /// Performs checked `u32` multiplication.
550 pub fn u32_mul_with_overflow(x: u32, y: u32) -> (u32, bool);
551 /// Performs checked `u64` multiplication.
552 pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool);