File tree Expand file tree Collapse file tree 2 files changed +22
-16
lines changed Expand file tree Collapse file tree 2 files changed +22
-16
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ doctest = false
1111[build-dependencies ]
1212rand = " 0.7"
1313
14+ [dev-dependencies ]
15+ rand = " 0.7"
16+
1417[dependencies .compiler_builtins ]
1518path = " .."
1619default-features = false
Original file line number Diff line number Diff line change 1- extern crate compiler_builtins ;
1+ use rand :: random ;
22
33use compiler_builtins:: int:: __clzsi2;
44
55#[ test]
66fn __clzsi2_test ( ) {
7- let mut i: usize = core:: usize:: MAX ;
8- // Check all values above 0
9- while i > 0 {
10- assert_eq ! ( __clzsi2( i) as u32 , i. leading_zeros( ) ) ;
11- i >>= 1 ;
12- }
13- // check 0 also
14- i = 0 ;
15- assert_eq ! ( __clzsi2( i) as u32 , i. leading_zeros( ) ) ;
16- // double check for bit patterns that aren't just solid 1s
17- i = 1 ;
18- for _ in 0 ..63 {
19- assert_eq ! ( __clzsi2( i) as u32 , i. leading_zeros( ) ) ;
20- i <<= 2 ;
21- i += 1 ;
7+ // binary fuzzer
8+ let mut x = 0usize ;
9+ let mut ones: usize ;
10+ // creates a mask for indexing the bits of the type
11+ let bit_indexing_mask = usize:: MAX . count_ones ( ) - 1 ;
12+ for _ in 0 ..1000 {
13+ for _ in 0 ..4 {
14+ let r0: u32 = bit_indexing_mask & random :: < u32 > ( ) ;
15+ ones = !0 >> r0;
16+ let r1: u32 = bit_indexing_mask & random :: < u32 > ( ) ;
17+ let mask = ones. rotate_left ( r1) ;
18+ match ( random ( ) , random ( ) ) {
19+ ( false , false ) => x |= mask,
20+ ( false , true ) => x &= mask,
21+ ( true , _) => x ^= mask,
22+ }
23+ }
24+ assert_eq ! ( __clzsi2( x) , x. leading_zeros( ) as usize ) ;
2225 }
2326}
You can’t perform that action at this time.
0 commit comments