1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! rustc compiler intrinsics.
13 //! The corresponding definitions are in librustc_trans/intrinsic.rs.
17 //! The volatile intrinsics provide operations intended to act on I/O
18 //! memory, which are guaranteed to not be reordered by the compiler
19 //! across other volatile intrinsics. See the LLVM documentation on
22 //! [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
26 //! The atomic intrinsics provide common atomic operations on machine
27 //! words, with multiple possible memory orderings. They obey the same
28 //! semantics as C++11. See the LLVM documentation on [[atomics]].
30 //! [atomics]: http://llvm.org/docs/Atomics.html
32 //! A quick refresher on memory ordering:
34 //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
35 //! take place after the barrier.
36 //! * Release - a barrier for releasing a lock. Preceding reads and writes
37 //! take place before the barrier.
38 //! * Sequentially consistent - sequentially consistent operations are
39 //! guaranteed to happen in order. This is the standard mode for working
40 //! with atomic types and is equivalent to Java's `volatile`.
42 #![unstable(feature = "core_intrinsics",
43 reason = "intrinsics are unlikely to ever be stabilized, instead \
44 they should be used through stabilized interfaces \
45 in the rest of the standard library",
47 #![allow(missing_docs)]
49 extern "rust-intrinsic" {
51 // NB: These intrinsics take raw pointers because they mutate aliased
52 // memory, which is not valid for either `&` or `&mut`.
54 pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> (T, bool);
55 pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
56 pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
57 pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
58 pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
59 pub fn atomic_cxchg_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
60 pub fn atomic_cxchg_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
61 pub fn atomic_cxchg_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
62 pub fn atomic_cxchg_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
64 pub fn atomic_cxchgweak<T>(dst: *mut T, old: T, src: T) -> (T, bool);
65 pub fn atomic_cxchgweak_acq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
66 pub fn atomic_cxchgweak_rel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
67 pub fn atomic_cxchgweak_acqrel<T>(dst: *mut T, old: T, src: T) -> (T, bool);
68 pub fn atomic_cxchgweak_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
69 pub fn atomic_cxchgweak_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
70 pub fn atomic_cxchgweak_failacq<T>(dst: *mut T, old: T, src: T) -> (T, bool);
71 pub fn atomic_cxchgweak_acq_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
72 pub fn atomic_cxchgweak_acqrel_failrelaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);
74 pub fn atomic_load<T>(src: *const T) -> T;
75 pub fn atomic_load_acq<T>(src: *const T) -> T;
76 pub fn atomic_load_relaxed<T>(src: *const T) -> T;
77 pub fn atomic_load_unordered<T>(src: *const T) -> T;
79 pub fn atomic_store<T>(dst: *mut T, val: T);
80 pub fn atomic_store_rel<T>(dst: *mut T, val: T);
81 pub fn atomic_store_relaxed<T>(dst: *mut T, val: T);
82 pub fn atomic_store_unordered<T>(dst: *mut T, val: T);
84 pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
85 pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
86 pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
87 pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T;
88 pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T;
90 pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
91 pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
92 pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
93 pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T;
94 pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T;
96 pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
97 pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
98 pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
99 pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T;
100 pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T;
102 pub fn atomic_and<T>(dst: *mut T, src: T) -> T;
103 pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T;
104 pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T;
105 pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T;
106 pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T;
108 pub fn atomic_nand<T>(dst: *mut T, src: T) -> T;
109 pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T;
110 pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T;
111 pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T;
112 pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T;
114 pub fn atomic_or<T>(dst: *mut T, src: T) -> T;
115 pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T;
116 pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T;
117 pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T;
118 pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T;
120 pub fn atomic_xor<T>(dst: *mut T, src: T) -> T;
121 pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T;
122 pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T;
123 pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T;
124 pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T;
126 pub fn atomic_max<T>(dst: *mut T, src: T) -> T;
127 pub fn atomic_max_acq<T>(dst: *mut T, src: T) -> T;
128 pub fn atomic_max_rel<T>(dst: *mut T, src: T) -> T;
129 pub fn atomic_max_acqrel<T>(dst: *mut T, src: T) -> T;
130 pub fn atomic_max_relaxed<T>(dst: *mut T, src: T) -> T;
132 pub fn atomic_min<T>(dst: *mut T, src: T) -> T;
133 pub fn atomic_min_acq<T>(dst: *mut T, src: T) -> T;
134 pub fn atomic_min_rel<T>(dst: *mut T, src: T) -> T;
135 pub fn atomic_min_acqrel<T>(dst: *mut T, src: T) -> T;
136 pub fn atomic_min_relaxed<T>(dst: *mut T, src: T) -> T;
138 pub fn atomic_umin<T>(dst: *mut T, src: T) -> T;
139 pub fn atomic_umin_acq<T>(dst: *mut T, src: T) -> T;
140 pub fn atomic_umin_rel<T>(dst: *mut T, src: T) -> T;
141 pub fn atomic_umin_acqrel<T>(dst: *mut T, src: T) -> T;
142 pub fn atomic_umin_relaxed<T>(dst: *mut T, src: T) -> T;
144 pub fn atomic_umax<T>(dst: *mut T, src: T) -> T;
145 pub fn atomic_umax_acq<T>(dst: *mut T, src: T) -> T;
146 pub fn atomic_umax_rel<T>(dst: *mut T, src: T) -> T;
147 pub fn atomic_umax_acqrel<T>(dst: *mut T, src: T) -> T;
148 pub fn atomic_umax_relaxed<T>(dst: *mut T, src: T) -> T;
151 extern "rust-intrinsic" {
153 pub fn atomic_fence();
154 pub fn atomic_fence_acq();
155 pub fn atomic_fence_rel();
156 pub fn atomic_fence_acqrel();
158 /// A compiler-only memory barrier.
160 /// Memory accesses will never be reordered across this barrier by the
161 /// compiler, but no instructions will be emitted for it. This is
162 /// appropriate for operations on the same thread that may be preempted,
163 /// such as when interacting with signal handlers.
164 pub fn atomic_singlethreadfence();
165 pub fn atomic_singlethreadfence_acq();
166 pub fn atomic_singlethreadfence_rel();
167 pub fn atomic_singlethreadfence_acqrel();
169 /// Magic intrinsic that derives its meaning from attributes
170 /// attached to the function.
172 /// For example, dataflow uses this to inject static assertions so
173 /// that `rustc_peek(potentially_uninitialized)` would actually
174 /// double-check that dataflow did indeed compute that it is
175 /// uninitialized at that point in the control flow.
176 pub fn rustc_peek<T>(_: T) -> T;
178 /// Aborts the execution of the process.
181 /// Tells LLVM that this point in the code is not reachable,
182 /// enabling further optimizations.
184 /// NB: This is very different from the `unreachable!()` macro!
185 pub fn unreachable() -> !;
187 /// Informs the optimizer that a condition is always true.
188 /// If the condition is false, the behavior is undefined.
190 /// No code is generated for this intrinsic, but the optimizer will try
191 /// to preserve it (and its condition) between passes, which may interfere
192 /// with optimization of surrounding code and reduce performance. It should
193 /// not be used if the invariant can be discovered by the optimizer on its
194 /// own, or if it does not enable any significant optimizations.
195 pub fn assume(b: bool);
197 /// Executes a breakpoint trap, for inspection by a debugger.
200 /// The size of a type in bytes.
202 /// More specifically, this is the offset in bytes between successive
203 /// items of the same type, including alignment padding.
204 pub fn size_of<T>() -> usize;
206 /// Moves a value to an uninitialized memory location.
208 /// Drop glue is not run on the destination.
209 pub fn move_val_init<T>(dst: *mut T, src: T);
211 pub fn min_align_of<T>() -> usize;
212 pub fn pref_align_of<T>() -> usize;
214 pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
215 pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
217 /// Executes the destructor (if any) of the pointed-to value.
219 /// This has two use cases:
221 /// * It is *required* to use `drop_in_place` to drop unsized types like
222 /// trait objects, because they can't be read out onto the stack and
223 /// dropped normally.
225 /// * It is friendlier to the optimizer to do this over `ptr::read` when
226 /// dropping manually allocated memory (e.g. when writing Box/Rc/Vec),
227 /// as the compiler doesn't need to prove that it's sound to elide the
230 /// # Undefined Behavior
232 /// This has all the same safety problems as `ptr::read` with respect to
233 /// invalid pointers, types, and double drops.
234 #[stable(feature = "drop_in_place", since = "1.8.0")]
235 pub fn drop_in_place<T: ?Sized>(to_drop: *mut T);
237 /// Gets a static string slice containing the name of a type.
238 pub fn type_name<T: ?Sized>() -> &'static str;
240 /// Gets an identifier which is globally unique to the specified type. This
241 /// function will return the same value for a type regardless of whichever
242 /// crate it is invoked in.
243 pub fn type_id<T: ?Sized + 'static>() -> u64;
245 /// Creates a value initialized to zero.
247 /// `init` is unsafe because it returns a zeroed-out datum,
248 /// which is unsafe unless T is `Copy`. Also, even if T is
249 /// `Copy`, an all-zero value may not correspond to any legitimate
250 /// state for the type in question.
251 pub fn init<T>() -> T;
253 /// Creates an uninitialized value.
255 /// `uninit` is unsafe because there is no guarantee of what its
256 /// contents are. In particular its drop-flag may be set to any
257 /// state, which means it may claim either dropped or
258 /// undropped. In the general case one must use `ptr::write` to
259 /// initialize memory previous set to the result of `uninit`.
260 pub fn uninit<T>() -> T;
262 /// Moves a value out of scope without running drop glue.
263 pub fn forget<T>(_: T) -> ();
265 /// Reinterprets the bits of a value of one type as another type; both types
266 /// must have the same size. Neither the original, nor the result, may be an
267 /// [invalid value] (../../nomicon/meet-safe-and-unsafe.html).
269 /// `transmute` is semantically equivalent to a bitwise move of one type
270 /// into another. It copies the bits from the destination type into the
271 /// source type, then forgets the original. It's equivalent to C's `memcpy`
272 /// under the hood, just like `transmute_copy`.
274 /// `transmute` is incredibly unsafe. There are a vast number of ways to
275 /// cause undefined behavior with this function. `transmute` should be
276 /// the absolute last resort.
278 /// The [nomicon](../../nomicon/transmutes.html) has additional
283 /// There are a few things that `transmute` is really useful for.
285 /// Getting the bitpattern of a floating point type (or, more generally,
286 /// type punning, when `T` and `U` aren't pointers):
289 /// let bitpattern = unsafe {
290 /// std::mem::transmute::<f32, u32>(1.0)
292 /// assert_eq!(bitpattern, 0x3F800000);
295 /// Turning a pointer into a function pointer:
298 /// fn foo() -> i32 {
301 /// let pointer = foo as *const ();
302 /// let function = unsafe {
303 /// std::mem::transmute::<*const (), fn() -> i32>(pointer)
305 /// assert_eq!(function(), 0);
308 /// Extending a lifetime, or shortening an invariant lifetime; this is
309 /// advanced, very unsafe rust:
312 /// struct R<'a>(&'a i32);
313 /// unsafe fn extend_lifetime<'b>(r: R<'b>) -> R<'static> {
314 /// std::mem::transmute::<R<'b>, R<'static>>(r)
317 /// unsafe fn shorten_invariant_lifetime<'b, 'c>(r: &'b mut R<'static>)
318 /// -> &'b mut R<'c> {
319 /// std::mem::transmute::<&'b mut R<'static>, &'b mut R<'c>>(r)
325 /// However, many uses of `transmute` can be achieved through other means.
326 /// `transmute` can transform any type into any other, with just the caveat
327 /// that they're the same size, and often interesting results occur. Below
328 /// are common applications of `transmute` which can be replaced with safe
329 /// applications of `as`:
331 /// Turning a pointer into a `usize`:
335 /// let ptr_num_transmute = unsafe {
336 /// std::mem::transmute::<&i32, usize>(ptr)
338 /// // Use an `as` cast instead
339 /// let ptr_num_cast = ptr as *const i32 as usize;
342 /// Turning a `*mut T` into an `&mut T`:
345 /// let ptr: *mut i32 = &mut 0;
346 /// let ref_transmuted = unsafe {
347 /// std::mem::transmute::<*mut i32, &mut i32>(ptr)
349 /// // Use a reborrow instead
350 /// let ref_casted = unsafe { &mut *ptr };
353 /// Turning an `&mut T` into an `&mut U`:
356 /// let ptr = &mut 0;
357 /// let val_transmuted = unsafe {
358 /// std::mem::transmute::<&mut i32, &mut u32>(ptr)
360 /// // Now, put together `as` and reborrowing - note the chaining of `as`
361 /// // `as` is not transitive
362 /// let val_casts = unsafe { &mut *(ptr as *mut i32 as *mut u32) };
365 /// Turning an `&str` into an `&[u8]`:
368 /// // this is not a good way to do this.
369 /// let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
370 /// assert_eq!(slice, &[82, 117, 115, 116]);
371 /// // You could use `str::as_bytes`
372 /// let slice = "Rust".as_bytes();
373 /// assert_eq!(slice, &[82, 117, 115, 116]);
374 /// // Or, just use a byte string, if you have control over the string
376 /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
379 /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
382 /// let store = [0, 1, 2, 3];
383 /// let mut v_orig = store.iter().collect::<Vec<&i32>>();
384 /// // Using transmute: this is Undefined Behavior, and a bad idea.
385 /// // However, it is no-copy.
386 /// let v_transmuted = unsafe {
387 /// std::mem::transmute::<Vec<&i32>, Vec<Option<&i32>>>(
390 /// // This is the suggested, safe way.
391 /// // It does copy the entire Vector, though, into a new array.
392 /// let v_collected = v_orig.clone()
394 /// .map(|r| Some(r))
395 /// .collect::<Vec<Option<&i32>>>();
396 /// // The no-copy, unsafe way, still using transmute, but not UB.
397 /// // This is equivalent to the original, but safer, and reuses the
398 /// // same Vec internals. Therefore the new inner type must have the
399 /// // exact same size, and the same or lesser alignment, as the old
400 /// // type. The same caveats exist for this method as transmute, for
401 /// // the original inner type (`&i32`) to the converted inner type
402 /// // (`Option<&i32>`), so read the nomicon pages linked above.
403 /// let v_from_raw = unsafe {
404 /// Vec::from_raw_parts(v_orig.as_mut_ptr(),
406 /// v_orig.capacity())
408 /// std::mem::forget(v_orig);
411 /// Implementing `split_at_mut`:
414 /// use std::{slice, mem};
415 /// // There are multiple ways to do this; and there are multiple problems
416 /// // with the following, transmute, way.
417 /// fn split_at_mut_transmute<T>(slice: &mut [T], mid: usize)
418 /// -> (&mut [T], &mut [T]) {
419 /// let len = slice.len();
420 /// assert!(mid <= len);
422 /// let slice2 = mem::transmute::<&mut [T], &mut [T]>(slice);
423 /// // first: transmute is not typesafe; all it checks is that T and
424 /// // U are of the same size. Second, right here, you have two
425 /// // mutable references pointing to the same memory.
426 /// (&mut slice[0..mid], &mut slice2[mid..len])
429 /// // This gets rid of the typesafety problems; `&mut *` will *only* give
430 /// // you an `&mut T` from an `&mut T` or `*mut T`.
431 /// fn split_at_mut_casts<T>(slice: &mut [T], mid: usize)
432 /// -> (&mut [T], &mut [T]) {
433 /// let len = slice.len();
434 /// assert!(mid <= len);
436 /// let slice2 = &mut *(slice as *mut [T]);
437 /// // however, you still have two mutable references pointing to
438 /// // the same memory.
439 /// (&mut slice[0..mid], &mut slice2[mid..len])
442 /// // This is how the standard library does it. This is the best method, if
443 /// // you need to do something like this
444 /// fn split_at_stdlib<T>(slice: &mut [T], mid: usize)
445 /// -> (&mut [T], &mut [T]) {
446 /// let len = slice.len();
447 /// assert!(mid <= len);
449 /// let ptr = slice.as_mut_ptr();
450 /// // This now has three mutable references pointing at the same
451 /// // memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
452 /// // `slice` is never used after `let ptr = ...`, and so one can
453 /// // treat it as "dead", and therefore, you only have two real
454 /// // mutable slices.
455 /// (slice::from_raw_parts_mut(ptr, mid),
456 /// slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid))
460 #[stable(feature = "rust1", since = "1.0.0")]
461 pub fn transmute<T, U>(e: T) -> U;
463 /// Returns `true` if the actual type given as `T` requires drop
464 /// glue; returns `false` if the actual type provided for `T`
465 /// implements `Copy`.
467 /// If the actual type neither requires drop glue nor implements
468 /// `Copy`, then may return `true` or `false`.
469 pub fn needs_drop<T>() -> bool;
471 /// Calculates the offset from a pointer.
473 /// This is implemented as an intrinsic to avoid converting to and from an
474 /// integer, since the conversion would throw away aliasing information.
478 /// Both the starting and resulting pointer must be either in bounds or one
479 /// byte past the end of an allocated object. If either pointer is out of
480 /// bounds or arithmetic overflow occurs then any further use of the
481 /// returned value will result in undefined behavior.
482 pub fn offset<T>(dst: *const T, offset: isize) -> *const T;
484 /// Calculates the offset from a pointer, potentially wrapping.
486 /// This is implemented as an intrinsic to avoid converting to and from an
487 /// integer, since the conversion inhibits certain optimizations.
491 /// Unlike the `offset` intrinsic, this intrinsic does not restrict the
492 /// resulting pointer to point into or one byte past the end of an allocated
493 /// object, and it wraps with two's complement arithmetic. The resulting
494 /// value is not necessarily valid to be used to actually access memory.
495 pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
497 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
498 /// and destination may *not* overlap.
500 /// `copy_nonoverlapping` is semantically equivalent to C's `memcpy`.
504 /// Beyond requiring that the program must be allowed to access both regions
505 /// of memory, it is Undefined Behavior for source and destination to
506 /// overlap. Care must also be taken with the ownership of `src` and
507 /// `dst`. This method semantically moves the values of `src` into `dst`.
508 /// However it does not drop the contents of `dst`, or prevent the contents
509 /// of `src` from being dropped or used.
513 /// A safe swap function:
519 /// # #[allow(dead_code)]
520 /// fn swap<T>(x: &mut T, y: &mut T) {
522 /// // Give ourselves some scratch space to work with
523 /// let mut t: T = mem::uninitialized();
525 /// // Perform the swap, `&mut` pointers never alias
526 /// ptr::copy_nonoverlapping(x, &mut t, 1);
527 /// ptr::copy_nonoverlapping(y, x, 1);
528 /// ptr::copy_nonoverlapping(&t, y, 1);
530 /// // y and t now point to the same thing, but we need to completely forget `tmp`
531 /// // because it's no longer relevant.
536 #[stable(feature = "rust1", since = "1.0.0")]
537 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
539 /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
540 /// and destination may overlap.
542 /// `copy` is semantically equivalent to C's `memmove`.
546 /// Care must be taken with the ownership of `src` and `dst`.
547 /// This method semantically moves the values of `src` into `dst`.
548 /// However it does not drop the contents of `dst`, or prevent the contents of `src`
549 /// from being dropped or used.
553 /// Efficiently create a Rust vector from an unsafe buffer:
558 /// # #[allow(dead_code)]
559 /// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
560 /// let mut dst = Vec::with_capacity(elts);
561 /// dst.set_len(elts);
562 /// ptr::copy(ptr, dst.as_mut_ptr(), elts);
567 #[stable(feature = "rust1", since = "1.0.0")]
568 pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
570 /// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
571 /// bytes of memory starting at `dst` to `val`.
572 #[stable(feature = "rust1", since = "1.0.0")]
573 pub fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
575 /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
576 /// a size of `count` * `size_of::<T>()` and an alignment of
577 /// `min_align_of::<T>()`
579 /// The volatile parameter is set to `true`, so it will not be optimized out.
580 pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
582 /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
583 /// a size of `count` * `size_of::<T>()` and an alignment of
584 /// `min_align_of::<T>()`
586 /// The volatile parameter is set to `true`, so it will not be optimized out.
587 pub fn volatile_copy_memory<T>(dst: *mut T, src: *const T, count: usize);
588 /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
589 /// size of `count` * `size_of::<T>()` and an alignment of
590 /// `min_align_of::<T>()`.
592 /// The volatile parameter is set to `true`, so it will not be optimized out.
593 pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: usize);
595 /// Perform a volatile load from the `src` pointer.
596 pub fn volatile_load<T>(src: *const T) -> T;
597 /// Perform a volatile store to the `dst` pointer.
598 pub fn volatile_store<T>(dst: *mut T, val: T);
600 /// Returns the square root of an `f32`
601 pub fn sqrtf32(x: f32) -> f32;
602 /// Returns the square root of an `f64`
603 pub fn sqrtf64(x: f64) -> f64;
605 /// Raises an `f32` to an integer power.
606 pub fn powif32(a: f32, x: i32) -> f32;
607 /// Raises an `f64` to an integer power.
608 pub fn powif64(a: f64, x: i32) -> f64;
610 /// Returns the sine of an `f32`.
611 pub fn sinf32(x: f32) -> f32;
612 /// Returns the sine of an `f64`.
613 pub fn sinf64(x: f64) -> f64;
615 /// Returns the cosine of an `f32`.
616 pub fn cosf32(x: f32) -> f32;
617 /// Returns the cosine of an `f64`.
618 pub fn cosf64(x: f64) -> f64;
620 /// Raises an `f32` to an `f32` power.
621 pub fn powf32(a: f32, x: f32) -> f32;
622 /// Raises an `f64` to an `f64` power.
623 pub fn powf64(a: f64, x: f64) -> f64;
625 /// Returns the exponential of an `f32`.
626 pub fn expf32(x: f32) -> f32;
627 /// Returns the exponential of an `f64`.
628 pub fn expf64(x: f64) -> f64;
630 /// Returns 2 raised to the power of an `f32`.
631 pub fn exp2f32(x: f32) -> f32;
632 /// Returns 2 raised to the power of an `f64`.
633 pub fn exp2f64(x: f64) -> f64;
635 /// Returns the natural logarithm of an `f32`.
636 pub fn logf32(x: f32) -> f32;
637 /// Returns the natural logarithm of an `f64`.
638 pub fn logf64(x: f64) -> f64;
640 /// Returns the base 10 logarithm of an `f32`.
641 pub fn log10f32(x: f32) -> f32;
642 /// Returns the base 10 logarithm of an `f64`.
643 pub fn log10f64(x: f64) -> f64;
645 /// Returns the base 2 logarithm of an `f32`.
646 pub fn log2f32(x: f32) -> f32;
647 /// Returns the base 2 logarithm of an `f64`.
648 pub fn log2f64(x: f64) -> f64;
650 /// Returns `a * b + c` for `f32` values.
651 pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
652 /// Returns `a * b + c` for `f64` values.
653 pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
655 /// Returns the absolute value of an `f32`.
656 pub fn fabsf32(x: f32) -> f32;
657 /// Returns the absolute value of an `f64`.
658 pub fn fabsf64(x: f64) -> f64;
660 /// Copies the sign from `y` to `x` for `f32` values.
661 pub fn copysignf32(x: f32, y: f32) -> f32;
662 /// Copies the sign from `y` to `x` for `f64` values.
663 pub fn copysignf64(x: f64, y: f64) -> f64;
665 /// Returns the largest integer less than or equal to an `f32`.
666 pub fn floorf32(x: f32) -> f32;
667 /// Returns the largest integer less than or equal to an `f64`.
668 pub fn floorf64(x: f64) -> f64;
670 /// Returns the smallest integer greater than or equal to an `f32`.
671 pub fn ceilf32(x: f32) -> f32;
672 /// Returns the smallest integer greater than or equal to an `f64`.
673 pub fn ceilf64(x: f64) -> f64;
675 /// Returns the integer part of an `f32`.
676 pub fn truncf32(x: f32) -> f32;
677 /// Returns the integer part of an `f64`.
678 pub fn truncf64(x: f64) -> f64;
680 /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
681 /// if the argument is not an integer.
682 pub fn rintf32(x: f32) -> f32;
683 /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
684 /// if the argument is not an integer.
685 pub fn rintf64(x: f64) -> f64;
687 /// Returns the nearest integer to an `f32`.
688 pub fn nearbyintf32(x: f32) -> f32;
689 /// Returns the nearest integer to an `f64`.
690 pub fn nearbyintf64(x: f64) -> f64;
692 /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
693 pub fn roundf32(x: f32) -> f32;
694 /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
695 pub fn roundf64(x: f64) -> f64;
697 /// Float addition that allows optimizations based on algebraic rules.
698 /// May assume inputs are finite.
699 pub fn fadd_fast<T>(a: T, b: T) -> T;
701 /// Float subtraction that allows optimizations based on algebraic rules.
702 /// May assume inputs are finite.
703 pub fn fsub_fast<T>(a: T, b: T) -> T;
705 /// Float multiplication that allows optimizations based on algebraic rules.
706 /// May assume inputs are finite.
707 pub fn fmul_fast<T>(a: T, b: T) -> T;
709 /// Float division that allows optimizations based on algebraic rules.
710 /// May assume inputs are finite.
711 pub fn fdiv_fast<T>(a: T, b: T) -> T;
713 /// Float remainder that allows optimizations based on algebraic rules.
714 /// May assume inputs are finite.
715 pub fn frem_fast<T>(a: T, b: T) -> T;
718 /// Returns the number of bits set in an integer type `T`
719 pub fn ctpop<T>(x: T) -> T;
721 /// Returns the number of leading bits unset in an integer type `T`
722 pub fn ctlz<T>(x: T) -> T;
724 /// Returns the number of trailing bits unset in an integer type `T`
725 pub fn cttz<T>(x: T) -> T;
727 /// Reverses the bytes in an integer type `T`.
728 pub fn bswap<T>(x: T) -> T;
730 /// Performs checked integer addition.
731 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
733 /// Performs checked integer subtraction
734 pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
736 /// Performs checked integer multiplication
737 pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
739 /// Performs an unchecked division, resulting in undefined behavior
740 /// where y = 0 or x = `T::min_value()` and y = -1
741 pub fn unchecked_div<T>(x: T, y: T) -> T;
742 /// Returns the remainder of an unchecked division, resulting in
743 /// undefined behavior where y = 0 or x = `T::min_value()` and y = -1
744 pub fn unchecked_rem<T>(x: T, y: T) -> T;
746 /// Returns (a + b) mod 2^N, where N is the width of T in bits.
747 pub fn overflowing_add<T>(a: T, b: T) -> T;
748 /// Returns (a - b) mod 2^N, where N is the width of T in bits.
749 pub fn overflowing_sub<T>(a: T, b: T) -> T;
750 /// Returns (a * b) mod 2^N, where N is the width of T in bits.
751 pub fn overflowing_mul<T>(a: T, b: T) -> T;
753 /// Returns the value of the discriminant for the variant in 'v',
754 /// cast to a `u64`; if `T` has no discriminant, returns 0.
755 pub fn discriminant_value<T>(v: &T) -> u64;
757 /// Rust's "try catch" construct which invokes the function pointer `f` with
758 /// the data pointer `data`.
760 /// The third pointer is a target-specific data pointer which is filled in
761 /// with the specifics of the exception that occurred. For examples on Unix
762 /// platforms this is a `*mut *mut T` which is filled in by the compiler and
763 /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
764 /// source as well as std's catch implementation.
765 pub fn try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;