|
3 | 3 | // adding a zero check at the beginning, but `__clzsi2` has a precondition that `x != 0`.
|
4 | 4 | // Compilers will insert the check for zero in cases where it is needed.
|
5 | 5 |
|
6 |
| -use crate::int::{CastInto, Int}; |
| 6 | +#[cfg(feature = "public-test-deps")] |
| 7 | +pub use implementation::{leading_zeros_default, leading_zeros_riscv}; |
| 8 | +#[cfg(not(feature = "public-test-deps"))] |
| 9 | +pub(crate) use implementation::{leading_zeros_default, leading_zeros_riscv}; |
7 | 10 |
|
8 |
| -public_test_dep! { |
9 |
| -/// Returns the number of leading binary zeros in `x`. |
10 |
| -#[allow(dead_code)] |
11 |
| -pub(crate) fn leading_zeros_default<T: Int + CastInto<usize>>(x: T) -> usize { |
12 |
| - // The basic idea is to test if the higher bits of `x` are zero and bisect the number |
13 |
| - // of leading zeros. It is possible for all branches of the bisection to use the same |
14 |
| - // code path by conditionally shifting the higher parts down to let the next bisection |
15 |
| - // step work on the higher or lower parts of `x`. Instead of starting with `z == 0` |
16 |
| - // and adding to the number of zeros, it is slightly faster to start with |
17 |
| - // `z == usize::MAX.count_ones()` and subtract from the potential number of zeros, |
18 |
| - // because it simplifies the final bisection step. |
19 |
| - let mut x = x; |
20 |
| - // the number of potential leading zeros |
21 |
| - let mut z = T::BITS as usize; |
22 |
| - // a temporary |
23 |
| - let mut t: T; |
| 11 | +mod implementation { |
| 12 | + use crate::int::{CastInto, Int}; |
24 | 13 |
|
25 |
| - const { assert!(T::BITS <= 64) }; |
26 |
| - if T::BITS >= 64 { |
27 |
| - t = x >> 32; |
| 14 | + /// Returns the number of leading binary zeros in `x`. |
| 15 | + #[allow(dead_code)] |
| 16 | + pub fn leading_zeros_default<T: Int + CastInto<usize>>(x: T) -> usize { |
| 17 | + // The basic idea is to test if the higher bits of `x` are zero and bisect the number |
| 18 | + // of leading zeros. It is possible for all branches of the bisection to use the same |
| 19 | + // code path by conditionally shifting the higher parts down to let the next bisection |
| 20 | + // step work on the higher or lower parts of `x`. Instead of starting with `z == 0` |
| 21 | + // and adding to the number of zeros, it is slightly faster to start with |
| 22 | + // `z == usize::MAX.count_ones()` and subtract from the potential number of zeros, |
| 23 | + // because it simplifies the final bisection step. |
| 24 | + let mut x = x; |
| 25 | + // the number of potential leading zeros |
| 26 | + let mut z = T::BITS as usize; |
| 27 | + // a temporary |
| 28 | + let mut t: T; |
| 29 | + |
| 30 | + const { assert!(T::BITS <= 64) }; |
| 31 | + if T::BITS >= 64 { |
| 32 | + t = x >> 32; |
| 33 | + if t != T::ZERO { |
| 34 | + z -= 32; |
| 35 | + x = t; |
| 36 | + } |
| 37 | + } |
| 38 | + if T::BITS >= 32 { |
| 39 | + t = x >> 16; |
| 40 | + if t != T::ZERO { |
| 41 | + z -= 16; |
| 42 | + x = t; |
| 43 | + } |
| 44 | + } |
| 45 | + const { assert!(T::BITS >= 16) }; |
| 46 | + t = x >> 8; |
28 | 47 | if t != T::ZERO {
|
29 |
| - z -= 32; |
| 48 | + z -= 8; |
30 | 49 | x = t;
|
31 | 50 | }
|
32 |
| - } |
33 |
| - if T::BITS >= 32 { |
34 |
| - t = x >> 16; |
| 51 | + t = x >> 4; |
35 | 52 | if t != T::ZERO {
|
36 |
| - z -= 16; |
| 53 | + z -= 4; |
37 | 54 | x = t;
|
38 | 55 | }
|
39 |
| - } |
40 |
| - const { assert!(T::BITS >= 16) }; |
41 |
| - t = x >> 8; |
42 |
| - if t != T::ZERO { |
43 |
| - z -= 8; |
44 |
| - x = t; |
45 |
| - } |
46 |
| - t = x >> 4; |
47 |
| - if t != T::ZERO { |
48 |
| - z -= 4; |
49 |
| - x = t; |
50 |
| - } |
51 |
| - t = x >> 2; |
52 |
| - if t != T::ZERO { |
53 |
| - z -= 2; |
54 |
| - x = t; |
55 |
| - } |
56 |
| - // the last two bisections are combined into one conditional |
57 |
| - t = x >> 1; |
58 |
| - if t != T::ZERO { |
59 |
| - z - 2 |
60 |
| - } else { |
61 |
| - z - x.cast() |
62 |
| - } |
| 56 | + t = x >> 2; |
| 57 | + if t != T::ZERO { |
| 58 | + z -= 2; |
| 59 | + x = t; |
| 60 | + } |
| 61 | + // the last two bisections are combined into one conditional |
| 62 | + t = x >> 1; |
| 63 | + if t != T::ZERO { |
| 64 | + z - 2 |
| 65 | + } else { |
| 66 | + z - x.cast() |
| 67 | + } |
63 | 68 |
|
64 |
| - // We could potentially save a few cycles by using the LUT trick from |
65 |
| - // "https://embeddedgurus.com/state-space/2014/09/ |
66 |
| - // fast-deterministic-and-portable-counting-leading-zeros/". |
67 |
| - // However, 256 bytes for a LUT is too large for embedded use cases. We could remove |
68 |
| - // the last 3 bisections and use this 16 byte LUT for the rest of the work: |
69 |
| - //const LUT: [u8; 16] = [0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4]; |
70 |
| - //z -= LUT[x] as usize; |
71 |
| - //z |
72 |
| - // However, it ends up generating about the same number of instructions. When benchmarked |
73 |
| - // on x86_64, it is slightly faster to use the LUT, but this is probably because of OOO |
74 |
| - // execution effects. Changing to using a LUT and branching is risky for smaller cores. |
75 |
| -} |
76 |
| -} |
| 69 | + // We could potentially save a few cycles by using the LUT trick from |
| 70 | + // "https://embeddedgurus.com/state-space/2014/09/ |
| 71 | + // fast-deterministic-and-portable-counting-leading-zeros/". |
| 72 | + // However, 256 bytes for a LUT is too large for embedded use cases. We could remove |
| 73 | + // the last 3 bisections and use this 16 byte LUT for the rest of the work: |
| 74 | + //const LUT: [u8; 16] = [0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4]; |
| 75 | + //z -= LUT[x] as usize; |
| 76 | + //z |
| 77 | + // However, it ends up generating about the same number of instructions. When benchmarked |
| 78 | + // on x86_64, it is slightly faster to use the LUT, but this is probably because of OOO |
| 79 | + // execution effects. Changing to using a LUT and branching is risky for smaller cores. |
| 80 | + } |
77 | 81 |
|
78 |
| -// The above method does not compile well on RISC-V (because of the lack of predicated |
79 |
| -// instructions), producing code with many branches or using an excessively long |
80 |
| -// branchless solution. This method takes advantage of the set-if-less-than instruction on |
81 |
| -// RISC-V that allows `(x >= power-of-two) as usize` to be branchless. |
| 82 | + // The above method does not compile well on RISC-V (because of the lack of predicated |
| 83 | + // instructions), producing code with many branches or using an excessively long |
| 84 | + // branchless solution. This method takes advantage of the set-if-less-than instruction on |
| 85 | + // RISC-V that allows `(x >= power-of-two) as usize` to be branchless. |
82 | 86 |
|
83 |
| -public_test_dep! { |
84 |
| -/// Returns the number of leading binary zeros in `x`. |
85 |
| -#[allow(dead_code)] |
86 |
| -pub(crate) fn leading_zeros_riscv<T: Int + CastInto<usize>>(x: T) -> usize { |
87 |
| - let mut x = x; |
88 |
| - // the number of potential leading zeros |
89 |
| - let mut z = T::BITS; |
90 |
| - // a temporary |
91 |
| - let mut t: u32; |
| 87 | + /// Returns the number of leading binary zeros in `x`. |
| 88 | + #[allow(dead_code)] |
| 89 | + pub fn leading_zeros_riscv<T: Int + CastInto<usize>>(x: T) -> usize { |
| 90 | + let mut x = x; |
| 91 | + // the number of potential leading zeros |
| 92 | + let mut z = T::BITS; |
| 93 | + // a temporary |
| 94 | + let mut t: u32; |
92 | 95 |
|
93 |
| - // RISC-V does not have a set-if-greater-than-or-equal instruction and |
94 |
| - // `(x >= power-of-two) as usize` will get compiled into two instructions, but this is |
95 |
| - // still the most optimal method. A conditional set can only be turned into a single |
96 |
| - // immediate instruction if `x` is compared with an immediate `imm` (that can fit into |
97 |
| - // 12 bits) like `x < imm` but not `imm < x` (because the immediate is always on the |
98 |
| - // right). If we try to save an instruction by using `x < imm` for each bisection, we |
99 |
| - // have to shift `x` left and compare with powers of two approaching `usize::MAX + 1`, |
100 |
| - // but the immediate will never fit into 12 bits and never save an instruction. |
101 |
| - const { assert!(T::BITS <= 64) }; |
102 |
| - if T::BITS >= 64 { |
103 |
| - // If the upper 32 bits of `x` are not all 0, `t` is set to `1 << 5`, otherwise |
104 |
| - // `t` is set to 0. |
105 |
| - t = ((x >= (T::ONE << 32)) as u32) << 5; |
106 |
| - // If `t` was set to `1 << 5`, then the upper 32 bits are shifted down for the |
107 |
| - // next step to process. |
| 96 | + // RISC-V does not have a set-if-greater-than-or-equal instruction and |
| 97 | + // `(x >= power-of-two) as usize` will get compiled into two instructions, but this is |
| 98 | + // still the most optimal method. A conditional set can only be turned into a single |
| 99 | + // immediate instruction if `x` is compared with an immediate `imm` (that can fit into |
| 100 | + // 12 bits) like `x < imm` but not `imm < x` (because the immediate is always on the |
| 101 | + // right). If we try to save an instruction by using `x < imm` for each bisection, we |
| 102 | + // have to shift `x` left and compare with powers of two approaching `usize::MAX + 1`, |
| 103 | + // but the immediate will never fit into 12 bits and never save an instruction. |
| 104 | + const { assert!(T::BITS <= 64) }; |
| 105 | + if T::BITS >= 64 { |
| 106 | + // If the upper 32 bits of `x` are not all 0, `t` is set to `1 << 5`, otherwise |
| 107 | + // `t` is set to 0. |
| 108 | + t = ((x >= (T::ONE << 32)) as u32) << 5; |
| 109 | + // If `t` was set to `1 << 5`, then the upper 32 bits are shifted down for the |
| 110 | + // next step to process. |
| 111 | + x >>= t; |
| 112 | + // If `t` was set to `1 << 5`, then we subtract 32 from the number of potential |
| 113 | + // leading zeros |
| 114 | + z -= t; |
| 115 | + } |
| 116 | + if T::BITS >= 32 { |
| 117 | + t = ((x >= (T::ONE << 16)) as u32) << 4; |
| 118 | + x >>= t; |
| 119 | + z -= t; |
| 120 | + } |
| 121 | + const { assert!(T::BITS >= 16) }; |
| 122 | + t = ((x >= (T::ONE << 8)) as u32) << 3; |
108 | 123 | x >>= t;
|
109 |
| - // If `t` was set to `1 << 5`, then we subtract 32 from the number of potential |
110 |
| - // leading zeros |
111 | 124 | z -= t;
|
112 |
| - } |
113 |
| - if T::BITS >= 32 { |
114 |
| - t = ((x >= (T::ONE << 16)) as u32) << 4; |
| 125 | + t = ((x >= (T::ONE << 4)) as u32) << 2; |
| 126 | + x >>= t; |
| 127 | + z -= t; |
| 128 | + t = ((x >= (T::ONE << 2)) as u32) << 1; |
115 | 129 | x >>= t;
|
116 | 130 | z -= t;
|
| 131 | + t = (x >= (T::ONE << 1)) as u32; |
| 132 | + x >>= t; |
| 133 | + z -= t; |
| 134 | + // All bits except the LSB are guaranteed to be zero for this final bisection step. |
| 135 | + // If `x != 0` then `x == 1` and subtracts one potential zero from `z`. |
| 136 | + z as usize - x.cast() |
117 | 137 | }
|
118 |
| - const { assert!(T::BITS >= 16) }; |
119 |
| - t = ((x >= (T::ONE << 8)) as u32) << 3; |
120 |
| - x >>= t; |
121 |
| - z -= t; |
122 |
| - t = ((x >= (T::ONE << 4)) as u32) << 2; |
123 |
| - x >>= t; |
124 |
| - z -= t; |
125 |
| - t = ((x >= (T::ONE << 2)) as u32) << 1; |
126 |
| - x >>= t; |
127 |
| - z -= t; |
128 |
| - t = (x >= (T::ONE << 1)) as u32; |
129 |
| - x >>= t; |
130 |
| - z -= t; |
131 |
| - // All bits except the LSB are guaranteed to be zero for this final bisection step. |
132 |
| - // If `x != 0` then `x == 1` and subtracts one potential zero from `z`. |
133 |
| - z as usize - x.cast() |
134 |
| -} |
135 | 138 | }
|
136 | 139 |
|
137 | 140 | intrinsics! {
|
|
0 commit comments