15
15
use prelude:: * ;
16
16
17
17
use default:: Default ;
18
- use mem;
19
18
use num:: { Bitwise , Bounded } ;
20
19
use num:: { CheckedAdd , CheckedSub , CheckedMul } ;
21
20
use num:: { CheckedDiv , Zero , One , strconv} ;
@@ -26,79 +25,6 @@ use unstable::intrinsics;
26
25
27
26
uint_module ! ( uint, int, :: int:: BITS )
28
27
29
- ///
30
- /// Divide two numbers, return the result, rounded up.
31
- ///
32
- /// # Arguments
33
- ///
34
- /// * x - an integer
35
- /// * y - an integer distinct from 0u
36
- ///
37
- /// # Return value
38
- ///
39
- /// The smallest integer `q` such that `x/y <= q`.
40
- ///
41
- pub fn div_ceil ( x : uint , y : uint ) -> uint {
42
- let div = x / y;
43
- if x % y == 0 u { div }
44
- else { div + 1 u }
45
- }
46
-
47
- ///
48
- /// Divide two numbers, return the result, rounded to the closest integer.
49
- ///
50
- /// # Arguments
51
- ///
52
- /// * x - an integer
53
- /// * y - an integer distinct from 0u
54
- ///
55
- /// # Return value
56
- ///
57
- /// The integer `q` closest to `x/y`.
58
- ///
59
- pub fn div_round ( x : uint , y : uint ) -> uint {
60
- let div = x / y;
61
- if x % y * 2 u < y { div }
62
- else { div + 1 u }
63
- }
64
-
65
- ///
66
- /// Divide two numbers, return the result, rounded down.
67
- ///
68
- /// Note: This is the same function as `div`.
69
- ///
70
- /// # Arguments
71
- ///
72
- /// * x - an integer
73
- /// * y - an integer distinct from 0u
74
- ///
75
- /// # Return value
76
- ///
77
- /// The smallest integer `q` such that `x/y <= q`. This
78
- /// is either `x/y` or `x/y + 1`.
79
- ///
80
- pub fn div_floor ( x : uint , y : uint ) -> uint { return x / y; }
81
-
82
- /// Returns the smallest power of 2 greater than or equal to `n`
83
- #[ inline]
84
- pub fn next_power_of_two ( n : uint ) -> uint {
85
- let halfbits: uint = mem:: size_of :: < uint > ( ) * 4 u;
86
- let mut tmp: uint = n - 1 u;
87
- let mut shift: uint = 1 u;
88
- while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1 u; }
89
- tmp + 1 u
90
- }
91
-
92
- /// Returns the smallest power of 2 greater than or equal to `n`
93
- #[ inline]
94
- pub fn next_power_of_two_opt ( n : uint ) -> Option < uint > {
95
- let halfbits: uint = mem:: size_of :: < uint > ( ) * 4 u;
96
- let mut tmp: uint = n - 1 u;
97
- let mut shift: uint = 1 u;
98
- while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1 u; }
99
- tmp. checked_add ( & 1 )
100
- }
101
-
102
28
#[ cfg( target_word_size = "32" ) ]
103
29
impl CheckedAdd for uint {
104
30
#[ inline]
@@ -164,62 +90,3 @@ impl CheckedMul for uint {
164
90
}
165
91
}
166
92
}
167
-
168
- #[ test]
169
- fn test_next_power_of_two ( ) {
170
- assert ! ( ( next_power_of_two( 0 u) == 0 u) ) ;
171
- assert ! ( ( next_power_of_two( 1 u) == 1 u) ) ;
172
- assert ! ( ( next_power_of_two( 2 u) == 2 u) ) ;
173
- assert ! ( ( next_power_of_two( 3 u) == 4 u) ) ;
174
- assert ! ( ( next_power_of_two( 4 u) == 4 u) ) ;
175
- assert ! ( ( next_power_of_two( 5 u) == 8 u) ) ;
176
- assert ! ( ( next_power_of_two( 6 u) == 8 u) ) ;
177
- assert ! ( ( next_power_of_two( 7 u) == 8 u) ) ;
178
- assert ! ( ( next_power_of_two( 8 u) == 8 u) ) ;
179
- assert ! ( ( next_power_of_two( 9 u) == 16 u) ) ;
180
- assert ! ( ( next_power_of_two( 10 u) == 16 u) ) ;
181
- assert ! ( ( next_power_of_two( 11 u) == 16 u) ) ;
182
- assert ! ( ( next_power_of_two( 12 u) == 16 u) ) ;
183
- assert ! ( ( next_power_of_two( 13 u) == 16 u) ) ;
184
- assert ! ( ( next_power_of_two( 14 u) == 16 u) ) ;
185
- assert ! ( ( next_power_of_two( 15 u) == 16 u) ) ;
186
- assert ! ( ( next_power_of_two( 16 u) == 16 u) ) ;
187
- assert ! ( ( next_power_of_two( 17 u) == 32 u) ) ;
188
- assert ! ( ( next_power_of_two( 18 u) == 32 u) ) ;
189
- assert ! ( ( next_power_of_two( 19 u) == 32 u) ) ;
190
- assert ! ( ( next_power_of_two( 20 u) == 32 u) ) ;
191
- assert ! ( ( next_power_of_two( 21 u) == 32 u) ) ;
192
- assert ! ( ( next_power_of_two( 22 u) == 32 u) ) ;
193
- assert ! ( ( next_power_of_two( 23 u) == 32 u) ) ;
194
- assert ! ( ( next_power_of_two( 24 u) == 32 u) ) ;
195
- assert ! ( ( next_power_of_two( 25 u) == 32 u) ) ;
196
- assert ! ( ( next_power_of_two( 26 u) == 32 u) ) ;
197
- assert ! ( ( next_power_of_two( 27 u) == 32 u) ) ;
198
- assert ! ( ( next_power_of_two( 28 u) == 32 u) ) ;
199
- assert ! ( ( next_power_of_two( 29 u) == 32 u) ) ;
200
- assert ! ( ( next_power_of_two( 30 u) == 32 u) ) ;
201
- assert ! ( ( next_power_of_two( 31 u) == 32 u) ) ;
202
- assert ! ( ( next_power_of_two( 32 u) == 32 u) ) ;
203
- assert ! ( ( next_power_of_two( 33 u) == 64 u) ) ;
204
- assert ! ( ( next_power_of_two( 34 u) == 64 u) ) ;
205
- assert ! ( ( next_power_of_two( 35 u) == 64 u) ) ;
206
- assert ! ( ( next_power_of_two( 36 u) == 64 u) ) ;
207
- assert ! ( ( next_power_of_two( 37 u) == 64 u) ) ;
208
- assert ! ( ( next_power_of_two( 38 u) == 64 u) ) ;
209
- assert ! ( ( next_power_of_two( 39 u) == 64 u) ) ;
210
- }
211
-
212
- #[ test]
213
- fn test_overflows ( ) {
214
- use uint;
215
- assert ! ( ( uint:: MAX > 0 u) ) ;
216
- assert ! ( ( uint:: MIN <= 0 u) ) ;
217
- assert ! ( ( uint:: MIN + uint:: MAX + 1 u == 0 u) ) ;
218
- }
219
-
220
- #[ test]
221
- fn test_div ( ) {
222
- assert ! ( ( div_floor( 3 u, 4 u) == 0 u) ) ;
223
- assert ! ( ( div_ceil( 3 u, 4 u) == 1 u) ) ;
224
- assert ! ( ( div_round( 3 u, 4 u) == 1 u) ) ;
225
- }
0 commit comments