(index<- ) ./libcore/num/uint.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 2014
1 // Copyright 2012-2014 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 //! Operations and constants for architecture-sized unsigned integers (`uint` type)
12
13 use default::Default;
14 use intrinsics;
15 use num::{Bitwise, Bounded, Zero, One, Unsigned, Num, Int, Primitive};
16 use num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
17 use option::{Some, None, Option};
18
19 #[cfg(not(test))]
20 use cmp::{Eq, Ord, TotalEq, TotalOrd, Less, Greater, Equal, Ordering};
21 #[cfg(not(test))]
22 use ops::{Add, Sub, Mul, Div, Rem, Neg, BitAnd, BitOr, BitXor};
23 #[cfg(not(test))]
24 use ops::{Shl, Shr, Not};
25
26 uint_module!(uint, int, ::int::BITS)
27
28 #[cfg(target_word_size = "32")]
29 impl CheckedAdd for uint {
30 #[inline]
31 fn checked_add(&self, v: &uint) -> Option<uint> {
32 unsafe {
33 let (x, y) = intrinsics::u32_add_with_overflow(*self as u32, *v as u32);
34 if y { None } else { Some(x as uint) }
35 }
36 }
37 }
38
39 #[cfg(target_word_size = "64")]
40 impl CheckedAdd for uint {
41 #[inline]
42 fn checked_add(&self, v: &uint) -> Option<uint> {
43 unsafe {
44 let (x, y) = intrinsics::u64_add_with_overflow(*self as u64, *v as u64);
45 if y { None } else { Some(x as uint) }
46 }
47 }
48 }
49
50 #[cfg(target_word_size = "32")]
51 impl CheckedSub for uint {
52 #[inline]
53 fn checked_sub(&self, v: &uint) -> Option<uint> {
54 unsafe {
55 let (x, y) = intrinsics::u32_sub_with_overflow(*self as u32, *v as u32);
56 if y { None } else { Some(x as uint) }
57 }
58 }
59 }
60
61 #[cfg(target_word_size = "64")]
62 impl CheckedSub for uint {
63 #[inline]
64 fn checked_sub(&self, v: &uint) -> Option<uint> {
65 unsafe {
66 let (x, y) = intrinsics::u64_sub_with_overflow(*self as u64, *v as u64);
67 if y { None } else { Some(x as uint) }
68 }
69 }
70 }
71
72 #[cfg(target_word_size = "32")]
73 impl CheckedMul for uint {
74 #[inline]
75 fn checked_mul(&self, v: &uint) -> Option<uint> {
76 unsafe {
77 let (x, y) = intrinsics::u32_mul_with_overflow(*self as u32, *v as u32);
78 if y { None } else { Some(x as uint) }
79 }
80 }
81 }
82
83 #[cfg(target_word_size = "64")]
84 impl CheckedMul for uint {
85 #[inline]
86 fn checked_mul(&self, v: &uint) -> Option<uint> {
87 unsafe {
88 let (x, y) = intrinsics::u64_mul_with_overflow(*self as u64, *v as u64);
89 if y { None } else { Some(x as uint) }
90 }
91 }
92 }