-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74f7bcc
commit 5f3c4f7
Showing
2 changed files
with
22 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
extern crate compiler_builtins; | ||
use rand::random; | ||
|
||
use compiler_builtins::int::__clzsi2; | ||
|
||
#[test] | ||
fn __clzsi2_test() { | ||
let mut i: usize = core::usize::MAX; | ||
// Check all values above 0 | ||
while i > 0 { | ||
assert_eq!(__clzsi2(i) as u32, i.leading_zeros()); | ||
i >>= 1; | ||
} | ||
// check 0 also | ||
i = 0; | ||
assert_eq!(__clzsi2(i) as u32, i.leading_zeros()); | ||
// double check for bit patterns that aren't just solid 1s | ||
i = 1; | ||
for _ in 0..63 { | ||
assert_eq!(__clzsi2(i) as u32, i.leading_zeros()); | ||
i <<= 2; | ||
i += 1; | ||
// binary fuzzer | ||
let mut x = 0usize; | ||
let mut ones: usize; | ||
// creates a mask for indexing the bits of the type | ||
let bit_indexing_mask = usize::MAX.count_ones() - 1; | ||
for _ in 0..1000 { | ||
for _ in 0..4 { | ||
let r0: u32 = bit_indexing_mask & random::<u32>(); | ||
ones = !0 >> r0; | ||
let r1: u32 = bit_indexing_mask & random::<u32>(); | ||
let mask = ones.rotate_left(r1); | ||
match (random(), random()) { | ||
(false, false) => x |= mask, | ||
(false, true) => x &= mask, | ||
(true, _) => x ^= mask, | ||
} | ||
} | ||
assert_eq!(__clzsi2(x), x.leading_zeros() as usize); | ||
} | ||
} |