]> git.lizzy.rs Git - rust.git/blob - src/libcore/intrinsics.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / libcore / intrinsics.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! rustc compiler intrinsics.
12 //!
13 //! The corresponding definitions are in librustc_trans/intrinsic.rs.
14 //!
15 //! # Volatiles
16 //!
17 //! The volatile intrinsics provide operations intended to act on I/O
18 //! memory, which are guaranteed to not be reordered by the compiler
19 //! across other volatile intrinsics. See the LLVM documentation on
20 //! [[volatile]].
21 //!
22 //! [volatile]: http://llvm.org/docs/LangRef.html#volatile-memory-accesses
23 //!
24 //! # Atomics
25 //!
26 //! The atomic intrinsics provide common atomic operations on machine
27 //! words, with multiple possible memory orderings. They obey the same
28 //! semantics as C++11. See the LLVM documentation on [[atomics]].
29 //!
30 //! [atomics]: http://llvm.org/docs/Atomics.html
31 //!
32 //! A quick refresher on memory ordering:
33 //!
34 //! * Acquire - a barrier for acquiring a lock. Subsequent reads and writes
35 //!   take place after the barrier.
36 //! * Release - a barrier for releasing a lock. Preceding reads and writes
37 //!   take place before the barrier.
38 //! * Sequentially consistent - sequentially consistent operations are
39 //!   guaranteed to happen in order. This is the standard mode for working
40 //!   with atomic types and is equivalent to Java's `volatile`.
41
42 #![unstable(feature = "core_intrinsics",
43             reason = "intrinsics are unlikely to ever be stabilized, instead \
44                       they should be used through stabilized interfaces \
45                       in the rest of the standard library",
46             issue = "0")]
47 #![allow(missing_docs)]
48
49 extern "rust-intrinsic" {
50
51     // NB: These intrinsics take raw pointers because they mutate aliased
52     // memory, which is not valid for either `&` or `&mut`.
53
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);
63
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);
73
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;
78
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);
83
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;
89
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;
95
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;
101
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;
107
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;
113
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;
119
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;
125
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;
131
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;
137
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;
143
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;
149 }
150
151 extern "rust-intrinsic" {
152
153     pub fn atomic_fence();
154     pub fn atomic_fence_acq();
155     pub fn atomic_fence_rel();
156     pub fn atomic_fence_acqrel();
157
158     /// A compiler-only memory barrier.
159     ///
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();
168
169     /// Magic intrinsic that derives its meaning from attributes
170     /// attached to the function.
171     ///
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;
177
178     /// Aborts the execution of the process.
179     pub fn abort() -> !;
180
181     /// Tells LLVM that this point in the code is not reachable,
182     /// enabling further optimizations.
183     ///
184     /// NB: This is very different from the `unreachable!()` macro!
185     pub fn unreachable() -> !;
186
187     /// Informs the optimizer that a condition is always true.
188     /// If the condition is false, the behavior is undefined.
189     ///
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);
196
197     /// Executes a breakpoint trap, for inspection by a debugger.
198     pub fn breakpoint();
199
200     /// The size of a type in bytes.
201     ///
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;
205
206     /// Moves a value to an uninitialized memory location.
207     ///
208     /// Drop glue is not run on the destination.
209     pub fn move_val_init<T>(dst: *mut T, src: T);
210
211     pub fn min_align_of<T>() -> usize;
212     pub fn pref_align_of<T>() -> usize;
213
214     pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
215     pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
216
217     /// Executes the destructor (if any) of the pointed-to value.
218     ///
219     /// This has two use cases:
220     ///
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.
224     ///
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
228     ///   copy.
229     ///
230     /// # Undefined Behavior
231     ///
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);
236
237     /// Gets a static string slice containing the name of a type.
238     pub fn type_name<T: ?Sized>() -> &'static str;
239
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;
244
245     /// Creates a value initialized to zero.
246     ///
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;
252
253     /// Creates an uninitialized value.
254     ///
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;
261
262     /// Moves a value out of scope without running drop glue.
263     pub fn forget<T>(_: T) -> ();
264
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).
268     ///
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`.
273     ///
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.
277     ///
278     /// The [nomicon](../../nomicon/transmutes.html) has additional
279     /// documentation.
280     ///
281     /// # Examples
282     ///
283     /// There are a few things that `transmute` is really useful for.
284     ///
285     /// Getting the bitpattern of a floating point type (or, more generally,
286     /// type punning, when `T` and `U` aren't pointers):
287     ///
288     /// ```
289     /// let bitpattern = unsafe {
290     ///     std::mem::transmute::<f32, u32>(1.0)
291     /// };
292     /// assert_eq!(bitpattern, 0x3F800000);
293     /// ```
294     ///
295     /// Turning a pointer into a function pointer:
296     ///
297     /// ```
298     /// fn foo() -> i32 {
299     ///     0
300     /// }
301     /// let pointer = foo as *const ();
302     /// let function = unsafe {
303     ///     std::mem::transmute::<*const (), fn() -> i32>(pointer)
304     /// };
305     /// assert_eq!(function(), 0);
306     /// ```
307     ///
308     /// Extending a lifetime, or shortening an invariant lifetime; this is
309     /// advanced, very unsafe rust:
310     ///
311     /// ```
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)
315     /// }
316     ///
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)
320     /// }
321     /// ```
322     ///
323     /// # Alternatives
324     ///
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`:
330     ///
331     /// Turning a pointer into a `usize`:
332     ///
333     /// ```
334     /// let ptr = &0;
335     /// let ptr_num_transmute = unsafe {
336     ///     std::mem::transmute::<&i32, usize>(ptr)
337     /// };
338     /// // Use an `as` cast instead
339     /// let ptr_num_cast = ptr as *const i32 as usize;
340     /// ```
341     ///
342     /// Turning a `*mut T` into an `&mut T`:
343     ///
344     /// ```
345     /// let ptr: *mut i32 = &mut 0;
346     /// let ref_transmuted = unsafe {
347     ///     std::mem::transmute::<*mut i32, &mut i32>(ptr)
348     /// };
349     /// // Use a reborrow instead
350     /// let ref_casted = unsafe { &mut *ptr };
351     /// ```
352     ///
353     /// Turning an `&mut T` into an `&mut U`:
354     ///
355     /// ```
356     /// let ptr = &mut 0;
357     /// let val_transmuted = unsafe {
358     ///     std::mem::transmute::<&mut i32, &mut u32>(ptr)
359     /// };
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) };
363     /// ```
364     ///
365     /// Turning an `&str` into an `&[u8]`:
366     ///
367     /// ```
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
375     /// // literal
376     /// assert_eq!(b"Rust", &[82, 117, 115, 116]);
377     /// ```
378     ///
379     /// Turning a `Vec<&T>` into a `Vec<Option<&T>>`:
380     ///
381     /// ```
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>>>(
388     ///         v_orig.clone())
389     /// };
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()
393     ///                         .into_iter()
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(),
405     ///                         v_orig.len(),
406     ///                         v_orig.capacity())
407     /// };
408     /// std::mem::forget(v_orig);
409     /// ```
410     ///
411     /// Implementing `split_at_mut`:
412     ///
413     /// ```
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);
421     ///     unsafe {
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])
427     ///     }
428     /// }
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);
435     ///     unsafe {
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])
440     ///     }
441     /// }
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);
448     ///     unsafe {
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))
457     ///     }
458     /// }
459     /// ```
460     #[stable(feature = "rust1", since = "1.0.0")]
461     pub fn transmute<T, U>(e: T) -> U;
462
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`.
466     ///
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;
470
471     /// Calculates the offset from a pointer.
472     ///
473     /// This is implemented as an intrinsic to avoid converting to and from an
474     /// integer, since the conversion would throw away aliasing information.
475     ///
476     /// # Safety
477     ///
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;
483
484     /// Calculates the offset from a pointer, potentially wrapping.
485     ///
486     /// This is implemented as an intrinsic to avoid converting to and from an
487     /// integer, since the conversion inhibits certain optimizations.
488     ///
489     /// # Safety
490     ///
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;
496
497     /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
498     /// and destination may *not* overlap.
499     ///
500     /// `copy_nonoverlapping` is semantically equivalent to C's `memcpy`.
501     ///
502     /// # Safety
503     ///
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.
510     ///
511     /// # Examples
512     ///
513     /// A safe swap function:
514     ///
515     /// ```
516     /// use std::mem;
517     /// use std::ptr;
518     ///
519     /// # #[allow(dead_code)]
520     /// fn swap<T>(x: &mut T, y: &mut T) {
521     ///     unsafe {
522     ///         // Give ourselves some scratch space to work with
523     ///         let mut t: T = mem::uninitialized();
524     ///
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);
529     ///
530     ///         // y and t now point to the same thing, but we need to completely forget `tmp`
531     ///         // because it's no longer relevant.
532     ///         mem::forget(t);
533     ///     }
534     /// }
535     /// ```
536     #[stable(feature = "rust1", since = "1.0.0")]
537     pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
538
539     /// Copies `count * size_of<T>` bytes from `src` to `dst`. The source
540     /// and destination may overlap.
541     ///
542     /// `copy` is semantically equivalent to C's `memmove`.
543     ///
544     /// # Safety
545     ///
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.
550     ///
551     /// # Examples
552     ///
553     /// Efficiently create a Rust vector from an unsafe buffer:
554     ///
555     /// ```
556     /// use std::ptr;
557     ///
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);
563     ///     dst
564     /// }
565     /// ```
566     ///
567     #[stable(feature = "rust1", since = "1.0.0")]
568     pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
569
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);
574
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>()`
578     ///
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,
581                                                   count: usize);
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>()`
585     ///
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>()`.
591     ///
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);
594
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);
599
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;
604
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;
609
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;
614
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;
619
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;
624
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;
629
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;
634
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;
639
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;
644
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;
649
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;
654
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;
659
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;
664
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;
669
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;
674
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;
679
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;
686
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;
691
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;
696
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;
700
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;
704
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;
708
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;
712
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;
716
717
718     /// Returns the number of bits set in an integer type `T`
719     pub fn ctpop<T>(x: T) -> T;
720
721     /// Returns the number of leading bits unset in an integer type `T`
722     pub fn ctlz<T>(x: T) -> T;
723
724     /// Returns the number of trailing bits unset in an integer type `T`
725     pub fn cttz<T>(x: T) -> T;
726
727     /// Reverses the bytes in an integer type `T`.
728     pub fn bswap<T>(x: T) -> T;
729
730     /// Performs checked integer addition.
731     pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
732
733     /// Performs checked integer subtraction
734     pub fn sub_with_overflow<T>(x: T, y: T) -> (T, bool);
735
736     /// Performs checked integer multiplication
737     pub fn mul_with_overflow<T>(x: T, y: T) -> (T, bool);
738
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;
745
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;
752
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;
756
757     /// Rust's "try catch" construct which invokes the function pointer `f` with
758     /// the data pointer `data`.
759     ///
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;
766 }