Skip to content

Commit

Permalink
Update __clzsi2 test
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronKutch committed Jul 13, 2020
1 parent 74f7bcc commit 5f3c4f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
3 changes: 3 additions & 0 deletions testcrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ doctest = false
[build-dependencies]
rand = "0.7"

[dev-dependencies]
rand = "0.7"

[dependencies.compiler_builtins]
path = ".."
default-features = false
Expand Down
35 changes: 19 additions & 16 deletions testcrate/tests/count_leading_zeros.rs
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);
}
}

0 comments on commit 5f3c4f7

Please sign in to comment.