Skip to content

Commit 1d49419

Browse files
committed
auto merge of #11930 : bjz/rust/next_power_of_two, r=huonw
2 parents c80d28c + 1f15d24 commit 1d49419

File tree

11 files changed

+101
-156
lines changed

11 files changed

+101
-156
lines changed

src/libarena/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use std::ptr;
3636
use std::kinds::marker;
3737
use std::mem;
3838
use std::rt::global_heap;
39-
use std::uint;
4039
use std::unstable::intrinsics::{TyDesc, get_tydesc};
4140
use std::unstable::intrinsics;
4241
use std::util;
@@ -180,7 +179,7 @@ impl Arena {
180179
let new_min_chunk_size = num::max(n_bytes, chunk_size);
181180
self.chunks.set(@Cons(self.pod_head.clone(), self.chunks.get()));
182181
self.pod_head =
183-
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), true);
182+
chunk(num::next_power_of_two(new_min_chunk_size + 1u), true);
184183

185184
return self.alloc_pod_inner(n_bytes, align);
186185
}
@@ -222,7 +221,7 @@ impl Arena {
222221
let new_min_chunk_size = num::max(n_bytes, chunk_size);
223222
self.chunks.set(@Cons(self.head.clone(), self.chunks.get()));
224223
self.head =
225-
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), false);
224+
chunk(num::next_power_of_two(new_min_chunk_size + 1u), false);
226225

227226
return self.alloc_nonpod_inner(n_bytes, align);
228227
}

src/libstd/at_vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ pub mod raw {
177177
use cast::{transmute, transmute_copy};
178178
use container::Container;
179179
use option::None;
180-
use ptr;
181180
use mem;
182-
use uint;
181+
use num::next_power_of_two;
182+
use ptr;
183183
use unstable::intrinsics::{move_val_init, TyDesc};
184184
use unstable::intrinsics;
185185
use unstable::raw::{Box, Vec};
@@ -293,7 +293,7 @@ pub mod raw {
293293
*/
294294
#[inline]
295295
pub unsafe fn reserve_at_least<T>(v: &mut @[T], n: uint) {
296-
reserve(v, uint::next_power_of_two(n));
296+
reserve(v, next_power_of_two(n));
297297
}
298298
}
299299

src/libstd/hashmap.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ use num;
6464
use option::{None, Option, Some};
6565
use rand::Rng;
6666
use rand;
67-
use uint;
6867
use util::replace;
6968
use vec::{ImmutableVector, MutableVector, OwnedVector, Items, MutItems};
7069
use vec_ng;
@@ -388,7 +387,7 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
388387
pub fn reserve_at_least(&mut self, n: uint) {
389388
if n > self.buckets.len() {
390389
let buckets = n * 4 / 3 + 1;
391-
self.resize(uint::next_power_of_two(buckets));
390+
self.resize(num::next_power_of_two(buckets));
392391
}
393392
}
394393

src/libstd/num/int.rs

-7
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,3 @@ impl CheckedMul for int {
120120
}
121121
}
122122
}
123-
124-
#[test]
125-
fn test_overflows() {
126-
assert!((::int::MAX > 0));
127-
assert!((::int::MIN <= 0));
128-
assert!((::int::MIN + ::int::MAX + 1 == 0));
129-
}

src/libstd/num/int_macros.rs

+7
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,13 @@ mod tests {
445445
use num::CheckedDiv;
446446
use num::Bitwise;
447447

448+
#[test]
449+
fn test_overflows() {
450+
assert!(MAX > 0);
451+
assert!(MIN <= 0);
452+
assert_eq!(MIN + MAX + 1, 0);
453+
}
454+
448455
#[test]
449456
fn test_num() {
450457
num::test_num(10 as $T, 2 as $T);

src/libstd/num/mod.rs

+75-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,39 @@ pub trait Primitive: Clone
440440
/// A collection of traits relevant to primitive signed and unsigned integers
441441
pub trait Int: Integer
442442
+ Primitive
443-
+ Bitwise {}
443+
+ Bitwise
444+
+ CheckedAdd
445+
+ CheckedSub
446+
// + CheckedMul // FIXME #8849: currently not impled on 32-bit
447+
+ CheckedDiv {}
448+
449+
/// Returns the smallest power of 2 greater than or equal to `n`.
450+
#[inline]
451+
pub fn next_power_of_two<T: Unsigned + Int>(n: T) -> T {
452+
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
453+
let mut tmp: T = n - one();
454+
let mut shift: T = one();
455+
while shift <= halfbits {
456+
tmp = tmp | (tmp >> shift);
457+
shift = shift << one();
458+
}
459+
tmp + one()
460+
}
461+
462+
/// Returns the smallest power of 2 greater than or equal to `n`. If the next
463+
/// power of two is greater than the type's maximum value, `None` is returned,
464+
/// otherwise the power of 2 is wrapped in `Some`.
465+
#[inline]
466+
pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
467+
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
468+
let mut tmp: T = n - one();
469+
let mut shift: T = one();
470+
while shift <= halfbits {
471+
tmp = tmp | (tmp >> shift);
472+
shift = shift << one();
473+
}
474+
tmp.checked_add(&one())
475+
}
444476

445477
/// Used for representing the classification of floating point numbers
446478
#[deriving(Eq)]
@@ -1589,6 +1621,48 @@ mod tests {
15891621
assert_eq!(third.checked_mul(&4), None);
15901622
}
15911623

1624+
macro_rules! test_next_power_of_two(
1625+
($test_name:ident, $T:ident) => (
1626+
fn $test_name() {
1627+
#[test];
1628+
assert_eq!(next_power_of_two::<$T>(0), 0);
1629+
let mut next_power = 1;
1630+
for i in range::<$T>(1, 40) {
1631+
assert_eq!(next_power_of_two(i), next_power);
1632+
if i == next_power { next_power *= 2 }
1633+
}
1634+
}
1635+
)
1636+
)
1637+
1638+
test_next_power_of_two!(test_next_power_of_two_u8, u8)
1639+
test_next_power_of_two!(test_next_power_of_two_u16, u16)
1640+
test_next_power_of_two!(test_next_power_of_two_u32, u32)
1641+
test_next_power_of_two!(test_next_power_of_two_u64, u64)
1642+
test_next_power_of_two!(test_next_power_of_two_uint, uint)
1643+
1644+
macro_rules! test_checked_next_power_of_two(
1645+
($test_name:ident, $T:ident) => (
1646+
fn $test_name() {
1647+
#[test];
1648+
assert_eq!(checked_next_power_of_two::<$T>(0), None);
1649+
let mut next_power = 1;
1650+
for i in range::<$T>(1, 40) {
1651+
assert_eq!(checked_next_power_of_two(i), Some(next_power));
1652+
if i == next_power { next_power *= 2 }
1653+
}
1654+
assert!(checked_next_power_of_two::<$T>($T::MAX / 2).is_some());
1655+
assert_eq!(checked_next_power_of_two::<$T>($T::MAX - 1), None);
1656+
assert_eq!(checked_next_power_of_two::<$T>($T::MAX), None);
1657+
}
1658+
)
1659+
)
1660+
1661+
test_checked_next_power_of_two!(test_checked_next_power_of_two_u8, u8)
1662+
test_checked_next_power_of_two!(test_checked_next_power_of_two_u16, u16)
1663+
test_checked_next_power_of_two!(test_checked_next_power_of_two_u32, u32)
1664+
test_checked_next_power_of_two!(test_checked_next_power_of_two_u64, u64)
1665+
test_checked_next_power_of_two!(test_checked_next_power_of_two_uint, uint)
15921666

15931667
#[deriving(Eq)]
15941668
struct Value { x: int }

src/libstd/num/uint.rs

-133
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use prelude::*;
1616

1717
use default::Default;
18-
use mem;
1918
use num::{Bitwise, Bounded};
2019
use num::{CheckedAdd, CheckedSub, CheckedMul};
2120
use num::{CheckedDiv, Zero, One, strconv};
@@ -26,79 +25,6 @@ use unstable::intrinsics;
2625

2726
uint_module!(uint, int, ::int::BITS)
2827

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 == 0u { div }
44-
else { div + 1u }
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 * 2u < y { div }
62-
else { div + 1u }
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>() * 4u;
86-
let mut tmp: uint = n - 1u;
87-
let mut shift: uint = 1u;
88-
while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
89-
tmp + 1u
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>() * 4u;
96-
let mut tmp: uint = n - 1u;
97-
let mut shift: uint = 1u;
98-
while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
99-
tmp.checked_add(&1)
100-
}
101-
10228
#[cfg(target_word_size = "32")]
10329
impl CheckedAdd for uint {
10430
#[inline]
@@ -164,62 +90,3 @@ impl CheckedMul for uint {
16490
}
16591
}
16692
}
167-
168-
#[test]
169-
fn test_next_power_of_two() {
170-
assert!((next_power_of_two(0u) == 0u));
171-
assert!((next_power_of_two(1u) == 1u));
172-
assert!((next_power_of_two(2u) == 2u));
173-
assert!((next_power_of_two(3u) == 4u));
174-
assert!((next_power_of_two(4u) == 4u));
175-
assert!((next_power_of_two(5u) == 8u));
176-
assert!((next_power_of_two(6u) == 8u));
177-
assert!((next_power_of_two(7u) == 8u));
178-
assert!((next_power_of_two(8u) == 8u));
179-
assert!((next_power_of_two(9u) == 16u));
180-
assert!((next_power_of_two(10u) == 16u));
181-
assert!((next_power_of_two(11u) == 16u));
182-
assert!((next_power_of_two(12u) == 16u));
183-
assert!((next_power_of_two(13u) == 16u));
184-
assert!((next_power_of_two(14u) == 16u));
185-
assert!((next_power_of_two(15u) == 16u));
186-
assert!((next_power_of_two(16u) == 16u));
187-
assert!((next_power_of_two(17u) == 32u));
188-
assert!((next_power_of_two(18u) == 32u));
189-
assert!((next_power_of_two(19u) == 32u));
190-
assert!((next_power_of_two(20u) == 32u));
191-
assert!((next_power_of_two(21u) == 32u));
192-
assert!((next_power_of_two(22u) == 32u));
193-
assert!((next_power_of_two(23u) == 32u));
194-
assert!((next_power_of_two(24u) == 32u));
195-
assert!((next_power_of_two(25u) == 32u));
196-
assert!((next_power_of_two(26u) == 32u));
197-
assert!((next_power_of_two(27u) == 32u));
198-
assert!((next_power_of_two(28u) == 32u));
199-
assert!((next_power_of_two(29u) == 32u));
200-
assert!((next_power_of_two(30u) == 32u));
201-
assert!((next_power_of_two(31u) == 32u));
202-
assert!((next_power_of_two(32u) == 32u));
203-
assert!((next_power_of_two(33u) == 64u));
204-
assert!((next_power_of_two(34u) == 64u));
205-
assert!((next_power_of_two(35u) == 64u));
206-
assert!((next_power_of_two(36u) == 64u));
207-
assert!((next_power_of_two(37u) == 64u));
208-
assert!((next_power_of_two(38u) == 64u));
209-
assert!((next_power_of_two(39u) == 64u));
210-
}
211-
212-
#[test]
213-
fn test_overflows() {
214-
use uint;
215-
assert!((uint::MAX > 0u));
216-
assert!((uint::MIN <= 0u));
217-
assert!((uint::MIN + uint::MAX + 1u == 0u));
218-
}
219-
220-
#[test]
221-
fn test_div() {
222-
assert!((div_floor(3u, 4u) == 0u));
223-
assert!((div_ceil(3u, 4u) == 1u));
224-
assert!((div_round(3u, 4u) == 1u));
225-
}

src/libstd/num/uint_macros.rs

+7
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,13 @@ mod tests {
317317
use num::Bitwise;
318318
use u16;
319319

320+
#[test]
321+
fn test_overflows() {
322+
assert!(MAX > 0);
323+
assert!(MIN <= 0);
324+
assert_eq!(MIN + MAX + 1, 0);
325+
}
326+
320327
#[test]
321328
fn test_num() {
322329
num::test_num(10 as $T, 2 as $T);

src/libstd/str.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,12 @@ use iter::{Iterator, FromIterator, Extendable, range};
104104
use iter::{Filter, AdditiveIterator, Map};
105105
use iter::{Rev, DoubleEndedIterator, ExactSize};
106106
use libc;
107-
use num::{Saturating};
107+
use num::{Saturating, checked_next_power_of_two};
108108
use option::{None, Option, Some};
109109
use ptr;
110110
use ptr::RawPtr;
111111
use to_str::ToStr;
112112
use from_str::FromStr;
113-
use uint;
114113
use vec;
115114
use vec::{OwnedVector, OwnedCloneableVector, ImmutableVector, MutableVector};
116115
use default::Default;
@@ -2640,7 +2639,7 @@ impl OwnedStr for ~str {
26402639

26412640
#[inline]
26422641
fn reserve_at_least(&mut self, n: uint) {
2643-
self.reserve(uint::next_power_of_two_opt(n).unwrap_or(n))
2642+
self.reserve(checked_next_power_of_two(n).unwrap_or(n))
26442643
}
26452644

26462645
#[inline]

src/libstd/sync/mpmc_bounded_queue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131

3232
use clone::Clone;
3333
use kinds::Send;
34+
use num::next_power_of_two;
3435
use option::{Option, Some, None};
3536
use sync::arc::UnsafeArc;
3637
use sync::atomics::{AtomicUint,Relaxed,Release,Acquire};
37-
use uint;
3838
use vec;
3939

4040
struct Node<T> {
@@ -64,7 +64,7 @@ impl<T: Send> State<T> {
6464
2u
6565
} else {
6666
// use next power of 2 as capacity
67-
uint::next_power_of_two(capacity)
67+
next_power_of_two(capacity)
6868
}
6969
} else {
7070
capacity

0 commit comments

Comments
 (0)