]> git.lizzy.rs Git - rust.git/blob - src/libcore/intrinsics.rs
33f2a49d6d560cba10c9dd49d185c1663fa312ce
[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/middle/trans/foreign.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 */
43
44 #![experimental]
45 #![allow(missing_doc)]
46
47 // This is needed to prevent duplicate lang item definitions.
48 #[cfg(test)]
49 pub use realcore::intrinsics::{TyDesc, Opaque, TyVisitor, TypeId};
50
51 pub type GlueFn = extern "Rust" fn(*i8);
52
53 #[lang="ty_desc"]
54 #[cfg(not(test))]
55 pub struct TyDesc {
56     // sizeof(T)
57     pub size: uint,
58
59     // alignof(T)
60     pub align: uint,
61
62     // Called when a value of type `T` is no longer needed
63     pub drop_glue: GlueFn,
64
65     // Called by reflection visitor to visit a value of type `T`
66     pub visit_glue: GlueFn,
67
68     // Name corresponding to the type
69     pub name: &'static str,
70 }
71
72 #[lang="opaque"]
73 #[cfg(not(test))]
74 pub enum Opaque { }
75
76 pub type Disr = u64;
77
78 #[lang="ty_visitor"]
79 #[cfg(not(test))]
80 pub trait TyVisitor {
81     fn visit_bot(&mut self) -> bool;
82     fn visit_nil(&mut self) -> bool;
83     fn visit_bool(&mut self) -> bool;
84
85     fn visit_int(&mut self) -> bool;
86     fn visit_i8(&mut self) -> bool;
87     fn visit_i16(&mut self) -> bool;
88     fn visit_i32(&mut self) -> bool;
89     fn visit_i64(&mut self) -> bool;
90
91     fn visit_uint(&mut self) -> bool;
92     fn visit_u8(&mut self) -> bool;
93     fn visit_u16(&mut self) -> bool;
94     fn visit_u32(&mut self) -> bool;
95     fn visit_u64(&mut self) -> bool;
96
97     fn visit_f32(&mut self) -> bool;
98     fn visit_f64(&mut self) -> bool;
99     fn visit_f128(&mut self) -> bool;
100
101     fn visit_char(&mut self) -> bool;
102
103     #[cfg(stage0)]
104     fn visit_estr_box(&mut self) -> bool;
105     #[cfg(stage0)]
106     fn visit_estr_uniq(&mut self) -> bool;
107     fn visit_estr_slice(&mut self) -> bool;
108     fn visit_estr_fixed(&mut self, n: uint, sz: uint, align: uint) -> bool;
109
110     fn visit_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
111     fn visit_uniq(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
112     fn visit_ptr(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
113     fn visit_rptr(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
114
115     #[cfg(stage0)]
116     fn visit_evec_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
117     #[cfg(stage0)]
118     fn visit_evec_uniq(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
119     fn visit_evec_slice(&mut self, mtbl: uint, inner: *TyDesc) -> bool;
120     fn visit_evec_fixed(&mut self, n: uint, sz: uint, align: uint,
121                         mtbl: uint, inner: *TyDesc) -> bool;
122
123     fn visit_enter_rec(&mut self, n_fields: uint,
124                        sz: uint, align: uint) -> bool;
125     fn visit_rec_field(&mut self, i: uint, name: &str,
126                        mtbl: uint, inner: *TyDesc) -> bool;
127     fn visit_leave_rec(&mut self, n_fields: uint,
128                        sz: uint, align: uint) -> bool;
129
130     fn visit_enter_class(&mut self, name: &str, named_fields: bool, n_fields: uint,
131                          sz: uint, align: uint) -> bool;
132     fn visit_class_field(&mut self, i: uint, name: &str, named: bool,
133                          mtbl: uint, inner: *TyDesc) -> bool;
134     fn visit_leave_class(&mut self, name: &str, named_fields: bool, n_fields: uint,
135                          sz: uint, align: uint) -> bool;
136
137     fn visit_enter_tup(&mut self, n_fields: uint,
138                        sz: uint, align: uint) -> bool;
139     fn visit_tup_field(&mut self, i: uint, inner: *TyDesc) -> bool;
140     fn visit_leave_tup(&mut self, n_fields: uint,
141                        sz: uint, align: uint) -> bool;
142
143     fn visit_enter_enum(&mut self, n_variants: uint,
144                         get_disr: unsafe extern fn(ptr: *Opaque) -> Disr,
145                         sz: uint, align: uint) -> bool;
146     fn visit_enter_enum_variant(&mut self, variant: uint,
147                                 disr_val: Disr,
148                                 n_fields: uint,
149                                 name: &str) -> bool;
150     fn visit_enum_variant_field(&mut self, i: uint, offset: uint, inner: *TyDesc) -> bool;
151     fn visit_leave_enum_variant(&mut self, variant: uint,
152                                 disr_val: Disr,
153                                 n_fields: uint,
154                                 name: &str) -> bool;
155     fn visit_leave_enum(&mut self, n_variants: uint,
156                         get_disr: unsafe extern fn(ptr: *Opaque) -> Disr,
157                         sz: uint, align: uint) -> bool;
158
159     fn visit_enter_fn(&mut self, purity: uint, proto: uint,
160                       n_inputs: uint, retstyle: uint) -> bool;
161     fn visit_fn_input(&mut self, i: uint, mode: uint, inner: *TyDesc) -> bool;
162     fn visit_fn_output(&mut self, retstyle: uint, variadic: bool, inner: *TyDesc) -> bool;
163     fn visit_leave_fn(&mut self, purity: uint, proto: uint,
164                       n_inputs: uint, retstyle: uint) -> bool;
165
166     fn visit_trait(&mut self, name: &str) -> bool;
167     fn visit_param(&mut self, i: uint) -> bool;
168     fn visit_self(&mut self) -> bool;
169 }
170
171 extern "rust-intrinsic" {
172
173     // NB: These intrinsics take unsafe pointers because they mutate aliased
174     // memory, which is not valid for either `&` or `&mut`.
175
176     pub fn atomic_cxchg<T>(dst: *mut T, old: T, src: T) -> T;
177     pub fn atomic_cxchg_acq<T>(dst: *mut T, old: T, src: T) -> T;
178     pub fn atomic_cxchg_rel<T>(dst: *mut T, old: T, src: T) -> T;
179     pub fn atomic_cxchg_acqrel<T>(dst: *mut T, old: T, src: T) -> T;
180     pub fn atomic_cxchg_relaxed<T>(dst: *mut T, old: T, src: T) -> T;
181
182     pub fn atomic_load<T>(src: *T) -> T;
183     pub fn atomic_load_acq<T>(src: *T) -> T;
184     pub fn atomic_load_relaxed<T>(src: *T) -> T;
185
186     pub fn atomic_store<T>(dst: *mut T, val: T);
187     pub fn atomic_store_rel<T>(dst: *mut T, val: T);
188     pub fn atomic_store_relaxed<T>(dst: *mut T, val: T);
189
190     pub fn atomic_xchg<T>(dst: *mut T, src: T) -> T;
191     pub fn atomic_xchg_acq<T>(dst: *mut T, src: T) -> T;
192     pub fn atomic_xchg_rel<T>(dst: *mut T, src: T) -> T;
193     pub fn atomic_xchg_acqrel<T>(dst: *mut T, src: T) -> T;
194     pub fn atomic_xchg_relaxed<T>(dst: *mut T, src: T) -> T;
195
196     pub fn atomic_xadd<T>(dst: *mut T, src: T) -> T;
197     pub fn atomic_xadd_acq<T>(dst: *mut T, src: T) -> T;
198     pub fn atomic_xadd_rel<T>(dst: *mut T, src: T) -> T;
199     pub fn atomic_xadd_acqrel<T>(dst: *mut T, src: T) -> T;
200     pub fn atomic_xadd_relaxed<T>(dst: *mut T, src: T) -> T;
201
202     pub fn atomic_xsub<T>(dst: *mut T, src: T) -> T;
203     pub fn atomic_xsub_acq<T>(dst: *mut T, src: T) -> T;
204     pub fn atomic_xsub_rel<T>(dst: *mut T, src: T) -> T;
205     pub fn atomic_xsub_acqrel<T>(dst: *mut T, src: T) -> T;
206     pub fn atomic_xsub_relaxed<T>(dst: *mut T, src: T) -> T;
207
208     pub fn atomic_and<T>(dst: *mut T, src: T) -> T;
209     pub fn atomic_and_acq<T>(dst: *mut T, src: T) -> T;
210     pub fn atomic_and_rel<T>(dst: *mut T, src: T) -> T;
211     pub fn atomic_and_acqrel<T>(dst: *mut T, src: T) -> T;
212     pub fn atomic_and_relaxed<T>(dst: *mut T, src: T) -> T;
213
214     pub fn atomic_nand<T>(dst: *mut T, src: T) -> T;
215     pub fn atomic_nand_acq<T>(dst: *mut T, src: T) -> T;
216     pub fn atomic_nand_rel<T>(dst: *mut T, src: T) -> T;
217     pub fn atomic_nand_acqrel<T>(dst: *mut T, src: T) -> T;
218     pub fn atomic_nand_relaxed<T>(dst: *mut T, src: T) -> T;
219
220     pub fn atomic_or<T>(dst: *mut T, src: T) -> T;
221     pub fn atomic_or_acq<T>(dst: *mut T, src: T) -> T;
222     pub fn atomic_or_rel<T>(dst: *mut T, src: T) -> T;
223     pub fn atomic_or_acqrel<T>(dst: *mut T, src: T) -> T;
224     pub fn atomic_or_relaxed<T>(dst: *mut T, src: T) -> T;
225
226     pub fn atomic_xor<T>(dst: *mut T, src: T) -> T;
227     pub fn atomic_xor_acq<T>(dst: *mut T, src: T) -> T;
228     pub fn atomic_xor_rel<T>(dst: *mut T, src: T) -> T;
229     pub fn atomic_xor_acqrel<T>(dst: *mut T, src: T) -> T;
230     pub fn atomic_xor_relaxed<T>(dst: *mut T, src: T) -> T;
231
232     pub fn atomic_max<T>(dst: *mut T, src: T) -> T;
233     pub fn atomic_max_acq<T>(dst: *mut T, src: T) -> T;
234     pub fn atomic_max_rel<T>(dst: *mut T, src: T) -> T;
235     pub fn atomic_max_acqrel<T>(dst: *mut T, src: T) -> T;
236     pub fn atomic_max_relaxed<T>(dst: *mut T, src: T) -> T;
237
238     pub fn atomic_min<T>(dst: *mut T, src: T) -> T;
239     pub fn atomic_min_acq<T>(dst: *mut T, src: T) -> T;
240     pub fn atomic_min_rel<T>(dst: *mut T, src: T) -> T;
241     pub fn atomic_min_acqrel<T>(dst: *mut T, src: T) -> T;
242     pub fn atomic_min_relaxed<T>(dst: *mut T, src: T) -> T;
243
244     pub fn atomic_umin<T>(dst: *mut T, src: T) -> T;
245     pub fn atomic_umin_acq<T>(dst: *mut T, src: T) -> T;
246     pub fn atomic_umin_rel<T>(dst: *mut T, src: T) -> T;
247     pub fn atomic_umin_acqrel<T>(dst: *mut T, src: T) -> T;
248     pub fn atomic_umin_relaxed<T>(dst: *mut T, src: T) -> T;
249
250     pub fn atomic_umax<T>(dst: *mut T, src: T) -> T;
251     pub fn atomic_umax_acq<T>(dst: *mut T, src: T) -> T;
252     pub fn atomic_umax_rel<T>(dst: *mut T, src: T) -> T;
253     pub fn atomic_umax_acqrel<T>(dst: *mut T, src: T) -> T;
254     pub fn atomic_umax_relaxed<T>(dst: *mut T, src: T) -> T;
255 }
256
257 extern "rust-intrinsic" {
258
259     pub fn atomic_fence();
260     pub fn atomic_fence_acq();
261     pub fn atomic_fence_rel();
262     pub fn atomic_fence_acqrel();
263
264     /// Abort the execution of the process.
265     pub fn abort() -> !;
266
267     /// Execute a breakpoint trap, for inspection by a debugger.
268     pub fn breakpoint();
269
270     /// The size of a type in bytes.
271     ///
272     /// This is the exact number of bytes in memory taken up by a
273     /// value of the given type. In other words, a memset of this size
274     /// would *exactly* overwrite a value. When laid out in vectors
275     /// and structures there may be additional padding between
276     /// elements.
277     pub fn size_of<T>() -> uint;
278
279     /// Move a value to an uninitialized memory location.
280     ///
281     /// Drop glue is not run on the destination.
282     pub fn move_val_init<T>(dst: &mut T, src: T);
283
284     pub fn min_align_of<T>() -> uint;
285     pub fn pref_align_of<T>() -> uint;
286
287     /// Get a static pointer to a type descriptor.
288     pub fn get_tydesc<T>() -> *TyDesc;
289
290     /// Gets an identifier which is globally unique to the specified type. This
291     /// function will return the same value for a type regardless of whichever
292     /// crate it is invoked in.
293     pub fn type_id<T: 'static>() -> TypeId;
294
295
296     /// Create a value initialized to zero.
297     ///
298     /// `init` is unsafe because it returns a zeroed-out datum,
299     /// which is unsafe unless T is Copy.
300     pub fn init<T>() -> T;
301
302     /// Create an uninitialized value.
303     pub fn uninit<T>() -> T;
304
305     /// Move a value out of scope without running drop glue.
306     ///
307     /// `forget` is unsafe because the caller is responsible for
308     /// ensuring the argument is deallocated already.
309     pub fn forget<T>(_: T) -> ();
310
311     /// Unsafely transforms a value of one type into a value of another type.
312     ///
313     /// Both types must have the same size and alignment, and this guarantee
314     /// is enforced at compile-time.
315     ///
316     /// # Example
317     ///
318     /// ```rust
319     /// use std::mem;
320     ///
321     /// let v: &[u8] = unsafe { mem::transmute("L") };
322     /// assert!(v == [76u8]);
323     /// ```
324     pub fn transmute<T,U>(e: T) -> U;
325
326     /// Returns `true` if a type requires drop glue.
327     pub fn needs_drop<T>() -> bool;
328
329     /// Returns `true` if a type is managed (will be allocated on the local heap)
330     pub fn owns_managed<T>() -> bool;
331
332     pub fn visit_tydesc(td: *TyDesc, tv: &mut TyVisitor);
333
334     /// Calculates the offset from a pointer. The offset *must* be in-bounds of
335     /// the object, or one-byte-past-the-end. An arithmetic overflow is also
336     /// undefined behaviour.
337     ///
338     /// This is implemented as an intrinsic to avoid converting to and from an
339     /// integer, since the conversion would throw away aliasing information.
340     pub fn offset<T>(dst: *T, offset: int) -> *T;
341
342     /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
343     /// a size of `count` * `size_of::<T>()` and an alignment of
344     /// `min_align_of::<T>()`
345     pub fn copy_nonoverlapping_memory<T>(dst: *mut T, src: *T, count: uint);
346
347     /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
348     /// a size of `count` * `size_of::<T>()` and an alignment of
349     /// `min_align_of::<T>()`
350     pub fn copy_memory<T>(dst: *mut T, src: *T, count: uint);
351
352     /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
353     /// size of `count` * `size_of::<T>()` and an alignment of
354     /// `min_align_of::<T>()`
355     pub fn set_memory<T>(dst: *mut T, val: u8, count: uint);
356
357     /// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
358     /// a size of `count` * `size_of::<T>()` and an alignment of
359     /// `min_align_of::<T>()`
360     ///
361     /// The volatile parameter parameter is set to `true`, so it will not be optimized out.
362     pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *T, count: uint);
363     /// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
364     /// a size of `count` * `size_of::<T>()` and an alignment of
365     /// `min_align_of::<T>()`
366     ///
367     /// The volatile parameter parameter is set to `true`, so it will not be optimized out.
368     pub fn volatile_copy_memory<T>(dst: *mut T, src: *T, count: uint);
369     /// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
370     /// size of `count` * `size_of::<T>()` and an alignment of
371     /// `min_align_of::<T>()`.
372     ///
373     /// The volatile parameter parameter is set to `true`, so it will not be optimized out.
374     pub fn volatile_set_memory<T>(dst: *mut T, val: u8, count: uint);
375
376     /// Perform a volatile load from the `src` pointer.
377     pub fn volatile_load<T>(src: *T) -> T;
378     /// Perform a volatile store to the `dst` pointer.
379     pub fn volatile_store<T>(dst: *mut T, val: T);
380
381     /// Returns the square root of an `f32`
382     pub fn sqrtf32(x: f32) -> f32;
383     /// Returns the square root of an `f64`
384     pub fn sqrtf64(x: f64) -> f64;
385
386     /// Raises an `f32` to an integer power.
387     pub fn powif32(a: f32, x: i32) -> f32;
388     /// Raises an `f64` to an integer power.
389     pub fn powif64(a: f64, x: i32) -> f64;
390
391     /// Returns the sine of an `f32`.
392     pub fn sinf32(x: f32) -> f32;
393     /// Returns the sine of an `f64`.
394     pub fn sinf64(x: f64) -> f64;
395
396     /// Returns the cosine of an `f32`.
397     pub fn cosf32(x: f32) -> f32;
398     /// Returns the cosine of an `f64`.
399     pub fn cosf64(x: f64) -> f64;
400
401     /// Raises an `f32` to an `f32` power.
402     pub fn powf32(a: f32, x: f32) -> f32;
403     /// Raises an `f64` to an `f64` power.
404     pub fn powf64(a: f64, x: f64) -> f64;
405
406     /// Returns the exponential of an `f32`.
407     pub fn expf32(x: f32) -> f32;
408     /// Returns the exponential of an `f64`.
409     pub fn expf64(x: f64) -> f64;
410
411     /// Returns 2 raised to the power of an `f32`.
412     pub fn exp2f32(x: f32) -> f32;
413     /// Returns 2 raised to the power of an `f64`.
414     pub fn exp2f64(x: f64) -> f64;
415
416     /// Returns the natural logarithm of an `f32`.
417     pub fn logf32(x: f32) -> f32;
418     /// Returns the natural logarithm of an `f64`.
419     pub fn logf64(x: f64) -> f64;
420
421     /// Returns the base 10 logarithm of an `f32`.
422     pub fn log10f32(x: f32) -> f32;
423     /// Returns the base 10 logarithm of an `f64`.
424     pub fn log10f64(x: f64) -> f64;
425
426     /// Returns the base 2 logarithm of an `f32`.
427     pub fn log2f32(x: f32) -> f32;
428     /// Returns the base 2 logarithm of an `f64`.
429     pub fn log2f64(x: f64) -> f64;
430
431     /// Returns `a * b + c` for `f32` values.
432     pub fn fmaf32(a: f32, b: f32, c: f32) -> f32;
433     /// Returns `a * b + c` for `f64` values.
434     pub fn fmaf64(a: f64, b: f64, c: f64) -> f64;
435
436     /// Returns the absolute value of an `f32`.
437     pub fn fabsf32(x: f32) -> f32;
438     /// Returns the absolute value of an `f64`.
439     pub fn fabsf64(x: f64) -> f64;
440
441     /// Copies the sign from `y` to `x` for `f32` values.
442     pub fn copysignf32(x: f32, y: f32) -> f32;
443     /// Copies the sign from `y` to `x` for `f64` values.
444     pub fn copysignf64(x: f64, y: f64) -> f64;
445
446     /// Returns the largest integer less than or equal to an `f32`.
447     pub fn floorf32(x: f32) -> f32;
448     /// Returns the largest integer less than or equal to an `f64`.
449     pub fn floorf64(x: f64) -> f64;
450
451     /// Returns the smallest integer greater than or equal to an `f32`.
452     pub fn ceilf32(x: f32) -> f32;
453     /// Returns the smallest integer greater than or equal to an `f64`.
454     pub fn ceilf64(x: f64) -> f64;
455
456     /// Returns the integer part of an `f32`.
457     pub fn truncf32(x: f32) -> f32;
458     /// Returns the integer part of an `f64`.
459     pub fn truncf64(x: f64) -> f64;
460
461     /// Returns the nearest integer to an `f32`. May raise an inexact floating-point exception
462     /// if the argument is not an integer.
463     pub fn rintf32(x: f32) -> f32;
464     /// Returns the nearest integer to an `f64`. May raise an inexact floating-point exception
465     /// if the argument is not an integer.
466     pub fn rintf64(x: f64) -> f64;
467
468     /// Returns the nearest integer to an `f32`.
469     pub fn nearbyintf32(x: f32) -> f32;
470     /// Returns the nearest integer to an `f64`.
471     pub fn nearbyintf64(x: f64) -> f64;
472
473     /// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
474     pub fn roundf32(x: f32) -> f32;
475     /// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
476     pub fn roundf64(x: f64) -> f64;
477
478     /// Returns the number of bits set in a `u8`.
479     pub fn ctpop8(x: u8) -> u8;
480     /// Returns the number of bits set in a `u16`.
481     pub fn ctpop16(x: u16) -> u16;
482     /// Returns the number of bits set in a `u32`.
483     pub fn ctpop32(x: u32) -> u32;
484     /// Returns the number of bits set in a `u64`.
485     pub fn ctpop64(x: u64) -> u64;
486
487     /// Returns the number of leading bits unset in a `u8`.
488     pub fn ctlz8(x: u8) -> u8;
489     /// Returns the number of leading bits unset in a `u16`.
490     pub fn ctlz16(x: u16) -> u16;
491     /// Returns the number of leading bits unset in a `u32`.
492     pub fn ctlz32(x: u32) -> u32;
493     /// Returns the number of leading bits unset in a `u64`.
494     pub fn ctlz64(x: u64) -> u64;
495
496     /// Returns the number of trailing bits unset in a `u8`.
497     pub fn cttz8(x: u8) -> u8;
498     /// Returns the number of trailing bits unset in a `u16`.
499     pub fn cttz16(x: u16) -> u16;
500     /// Returns the number of trailing bits unset in a `u32`.
501     pub fn cttz32(x: u32) -> u32;
502     /// Returns the number of trailing bits unset in a `u64`.
503     pub fn cttz64(x: u64) -> u64;
504
505     /// Reverses the bytes in a `u16`.
506     pub fn bswap16(x: u16) -> u16;
507     /// Reverses the bytes in a `u32`.
508     pub fn bswap32(x: u32) -> u32;
509     /// Reverses the bytes in a `u64`.
510     pub fn bswap64(x: u64) -> u64;
511
512     /// Performs checked `i8` addition.
513     pub fn i8_add_with_overflow(x: i8, y: i8) -> (i8, bool);
514     /// Performs checked `i16` addition.
515     pub fn i16_add_with_overflow(x: i16, y: i16) -> (i16, bool);
516     /// Performs checked `i32` addition.
517     pub fn i32_add_with_overflow(x: i32, y: i32) -> (i32, bool);
518     /// Performs checked `i64` addition.
519     pub fn i64_add_with_overflow(x: i64, y: i64) -> (i64, bool);
520
521     /// Performs checked `u8` addition.
522     pub fn u8_add_with_overflow(x: u8, y: u8) -> (u8, bool);
523     /// Performs checked `u16` addition.
524     pub fn u16_add_with_overflow(x: u16, y: u16) -> (u16, bool);
525     /// Performs checked `u32` addition.
526     pub fn u32_add_with_overflow(x: u32, y: u32) -> (u32, bool);
527     /// Performs checked `u64` addition.
528     pub fn u64_add_with_overflow(x: u64, y: u64) -> (u64, bool);
529
530     /// Performs checked `i8` subtraction.
531     pub fn i8_sub_with_overflow(x: i8, y: i8) -> (i8, bool);
532     /// Performs checked `i16` subtraction.
533     pub fn i16_sub_with_overflow(x: i16, y: i16) -> (i16, bool);
534     /// Performs checked `i32` subtraction.
535     pub fn i32_sub_with_overflow(x: i32, y: i32) -> (i32, bool);
536     /// Performs checked `i64` subtraction.
537     pub fn i64_sub_with_overflow(x: i64, y: i64) -> (i64, bool);
538
539     /// Performs checked `u8` subtraction.
540     pub fn u8_sub_with_overflow(x: u8, y: u8) -> (u8, bool);
541     /// Performs checked `u16` subtraction.
542     pub fn u16_sub_with_overflow(x: u16, y: u16) -> (u16, bool);
543     /// Performs checked `u32` subtraction.
544     pub fn u32_sub_with_overflow(x: u32, y: u32) -> (u32, bool);
545     /// Performs checked `u64` subtraction.
546     pub fn u64_sub_with_overflow(x: u64, y: u64) -> (u64, bool);
547
548     /// Performs checked `i8` multiplication.
549     pub fn i8_mul_with_overflow(x: i8, y: i8) -> (i8, bool);
550     /// Performs checked `i16` multiplication.
551     pub fn i16_mul_with_overflow(x: i16, y: i16) -> (i16, bool);
552     /// Performs checked `i32` multiplication.
553     pub fn i32_mul_with_overflow(x: i32, y: i32) -> (i32, bool);
554     /// Performs checked `i64` multiplication.
555     pub fn i64_mul_with_overflow(x: i64, y: i64) -> (i64, bool);
556
557     /// Performs checked `u8` multiplication.
558     pub fn u8_mul_with_overflow(x: u8, y: u8) -> (u8, bool);
559     /// Performs checked `u16` multiplication.
560     pub fn u16_mul_with_overflow(x: u16, y: u16) -> (u16, bool);
561     /// Performs checked `u32` multiplication.
562     pub fn u32_mul_with_overflow(x: u32, y: u32) -> (u32, bool);
563     /// Performs checked `u64` multiplication.
564     pub fn u64_mul_with_overflow(x: u64, y: u64) -> (u64, bool);
565 }
566
567
568 /// `TypeId` represents a globally unique identifier for a type
569 #[lang="type_id"] // This needs to be kept in lockstep with the code in trans/intrinsic.rs and
570                   // middle/lang_items.rs
571 #[deriving(PartialEq, Eq, Show)]
572 #[cfg(not(test))]
573 pub struct TypeId {
574     t: u64,
575 }
576
577 #[cfg(not(test))]
578 impl TypeId {
579     /// Returns the `TypeId` of the type this generic function has been instantiated with
580     pub fn of<T: 'static>() -> TypeId {
581         unsafe { type_id::<T>() }
582     }
583     pub fn hash(&self) -> u64 { self.t }
584 }