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 #![macro_escape]
12 #![doc(hidden)]
13
14 macro_rules! uint_module (($T:ty, $T_SIGNED:ty, $bits:expr) => (
15
16 pub static BITS : uint = $bits;
17 pub static BYTES : uint = ($bits / 8);
18
19 pub static MIN: $T = 0 as $T;
20 pub static MAX: $T = 0 as $T - 1 as $T;
21
22 #[cfg(not(test))]
23 impl Ord for $T {
24 #[inline]
25 fn lt(&self, other: &$T) -> bool { *self < *other }
26 }
27 #[cfg(not(test))]
28 impl TotalEq for $T {}
29 #[cfg(not(test))]
30 impl Eq for $T {
31 #[inline]
32 fn eq(&self, other: &$T) -> bool { *self == *other }
33 }
34 #[cfg(not(test))]
35 impl TotalOrd for $T {
36 #[inline]
37 fn cmp(&self, other: &$T) -> Ordering {
38 if *self < *other { Less }
39 else if *self > *other { Greater }
40 else { Equal }
41 }
42 }
43
44 impl Num for $T {}
45
46 impl Zero for $T {
47 #[inline]
48 fn zero() -> $T { 0 }
49
50 #[inline]
51 fn is_zero(&self) -> bool { *self == 0 }
52 }
53
54 impl One for $T {
55 #[inline]
56 fn one() -> $T { 1 }
57 }
58
59 #[cfg(not(test))]
60 impl Add<$T,$T> for $T {
61 #[inline]
62 fn add(&self, other: &$T) -> $T { *self + *other }
63 }
64
65 #[cfg(not(test))]
66 impl Sub<$T,$T> for $T {
67 #[inline]
68 fn sub(&self, other: &$T) -> $T { *self - *other }
69 }
70
71 #[cfg(not(test))]
72 impl Mul<$T,$T> for $T {
73 #[inline]
74 fn mul(&self, other: &$T) -> $T { *self * *other }
75 }
76
77 #[cfg(not(test))]
78 impl Div<$T,$T> for $T {
79 #[inline]
80 fn div(&self, other: &$T) -> $T { *self / *other }
81 }
82
83 #[cfg(not(test))]
84 impl Rem<$T,$T> for $T {
85 #[inline]
86 fn rem(&self, other: &$T) -> $T { *self % *other }
87 }
88
89 #[cfg(not(test))]
90 impl Neg<$T> for $T {
91 #[inline]
92 fn neg(&self) -> $T { -(*self as $T_SIGNED) as $T }
93 }
94
95 impl Unsigned for $T {}
96
97 #[cfg(not(test))]
98 impl BitOr<$T,$T> for $T {
99 #[inline]
100 fn bitor(&self, other: &$T) -> $T { *self | *other }
101 }
102
103 #[cfg(not(test))]
104 impl BitAnd<$T,$T> for $T {
105 #[inline]
106 fn bitand(&self, other: &$T) -> $T { *self & *other }
107 }
108
109 #[cfg(not(test))]
110 impl BitXor<$T,$T> for $T {
111 #[inline]
112 fn bitxor(&self, other: &$T) -> $T { *self ^ *other }
113 }
114
115 #[cfg(not(test))]
116 impl Shl<$T,$T> for $T {
117 #[inline]
118 fn shl(&self, other: &$T) -> $T { *self << *other }
119 }
120
121 #[cfg(not(test))]
122 impl Shr<$T,$T> for $T {
123 #[inline]
124 fn shr(&self, other: &$T) -> $T { *self >> *other }
125 }
126
127 #[cfg(not(test))]
128 impl Not<$T> for $T {
129 #[inline]
130 fn not(&self) -> $T { !*self }
131 }
132
133 impl Bounded for $T {
134 #[inline]
135 fn min_value() -> $T { MIN }
136
137 #[inline]
138 fn max_value() -> $T { MAX }
139 }
140
141 impl Bitwise for $T {
142 /// Returns the number of ones in the binary representation of the number.
143 #[inline]
144 fn count_ones(&self) -> $T {
145 (*self as $T_SIGNED).count_ones() as $T
146 }
147
148 /// Returns the number of leading zeros in the in the binary representation
149 /// of the number.
150 #[inline]
151 fn leading_zeros(&self) -> $T {
152 (*self as $T_SIGNED).leading_zeros() as $T
153 }
154
155 /// Returns the number of trailing zeros in the in the binary representation
156 /// of the number.
157 #[inline]
158 fn trailing_zeros(&self) -> $T {
159 (*self as $T_SIGNED).trailing_zeros() as $T
160 }
161 }
162
163 impl CheckedDiv for $T {
164 #[inline]
165 fn checked_div(&self, v: &$T) -> Option<$T> {
166 if *v == 0 {
167 None
168 } else {
169 Some(self / *v)
170 }
171 }
172 }
173
174 impl Int for $T {}
175
176 impl Primitive for $T {}
177
178 impl Default for $T {
179 #[inline]
180 fn default() -> $T { 0 }
181 }
182
183 #[cfg(test)]
184 mod tests {
185 use prelude::*;
186 use super::*;
187
188 use num;
189 use num::CheckedDiv;
190 use num::Bitwise;
191
192 #[test]
193 fn test_overflows() {
194 assert!(MAX > 0);
195 assert!(MIN <= 0);
196 assert!(MIN + MAX + 1 == 0);
197 }
198
199 #[test]
200 fn test_num() {
201 num::test_num(10 as $T, 2 as $T);
202 }
203
204 #[test]
205 fn test_bitwise() {
206 assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
207 assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
208 assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
209 assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
210 assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
211 assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
212 }
213
214 #[test]
215 fn test_count_ones() {
216 assert!((0b0101100 as $T).count_ones() == 3);
217 assert!((0b0100001 as $T).count_ones() == 2);
218 assert!((0b1111001 as $T).count_ones() == 5);
219 }
220
221 #[test]
222 fn test_count_zeros() {
223 assert!((0b0101100 as $T).count_zeros() == BITS as $T - 3);
224 assert!((0b0100001 as $T).count_zeros() == BITS as $T - 2);
225 assert!((0b1111001 as $T).count_zeros() == BITS as $T - 5);
226 }
227
228 #[test]
229 fn test_unsigned_checked_div() {
230 assert!(10u.checked_div(&2) == Some(5));
231 assert!(5u.checked_div(&0) == None);
232 }
233 }
234
235 ))