|
1 |
| -use std::ops::ShrAssign; |
2 |
| - |
| 1 | +use crate::error::UniswapV3MathError; |
3 | 2 | use alloy::primitives::U256;
|
4 | 3 |
|
5 |
| -use crate::{ |
6 |
| - error::UniswapV3MathError, U128_MAX, U16_MAX, U256_1, U256_15, U256_3, U32_MAX, U64_MAX, U8_MAX, |
7 |
| -}; |
8 |
| - |
9 |
| -pub fn most_significant_bit(mut x: U256) -> Result<u8, UniswapV3MathError> { |
10 |
| - let mut r = 0; |
11 |
| - |
| 4 | +pub fn most_significant_bit(x: U256) -> Result<u8, UniswapV3MathError> { |
12 | 5 | if x.is_zero() {
|
13 | 6 | return Err(UniswapV3MathError::ZeroValue);
|
14 | 7 | }
|
15 |
| - |
16 |
| - if x >= U256::from_limbs([0, 0, 1, 0]) { |
17 |
| - x.shr_assign(128); |
18 |
| - r += 128; |
19 |
| - } |
20 |
| - |
21 |
| - if x >= U256::from_limbs([0, 1, 0, 0]) { |
22 |
| - x.shr_assign(64); |
23 |
| - r += 64; |
24 |
| - } |
25 |
| - |
26 |
| - if x >= U256::from_limbs([4294967296, 0, 0, 0]) { |
27 |
| - x.shr_assign(32); |
28 |
| - r += 32; |
29 |
| - } |
30 |
| - |
31 |
| - if x >= U256::from_limbs([65536, 0, 0, 0]) { |
32 |
| - x.shr_assign(16); |
33 |
| - r += 16; |
34 |
| - } |
35 |
| - |
36 |
| - if x >= U256::from_limbs([256, 0, 0, 0]) { |
37 |
| - x.shr_assign(8); |
38 |
| - r += 8; |
39 |
| - } |
40 |
| - |
41 |
| - if x >= U256::from_limbs([16, 0, 0, 0]) { |
42 |
| - x.shr_assign(4); |
43 |
| - r += 4; |
44 |
| - } |
45 |
| - if x >= U256::from_limbs([4, 0, 0, 0]) { |
46 |
| - x.shr_assign(2); |
47 |
| - r += 2; |
48 |
| - } |
49 |
| - |
50 |
| - if x >= U256::from_limbs([2, 0, 0, 0]) { |
51 |
| - r += 1; |
52 |
| - } |
53 |
| - |
54 |
| - Ok(r) |
| 8 | + Ok(255 - x.leading_zeros() as u8) |
55 | 9 | }
|
56 | 10 |
|
57 |
| -pub fn least_significant_bit(mut x: U256) -> Result<u8, UniswapV3MathError> { |
| 11 | +pub fn least_significant_bit(x: U256) -> Result<u8, UniswapV3MathError> { |
58 | 12 | if x.is_zero() {
|
59 | 13 | return Err(UniswapV3MathError::ZeroValue);
|
60 | 14 | }
|
61 |
| - |
62 |
| - let mut r = 255; |
63 |
| - |
64 |
| - if x & U128_MAX > U256::ZERO { |
65 |
| - r -= 128; |
66 |
| - } else { |
67 |
| - x >>= 128; |
68 |
| - } |
69 |
| - |
70 |
| - if x & U64_MAX > U256::ZERO { |
71 |
| - r -= 64; |
72 |
| - } else { |
73 |
| - x >>= 64; |
74 |
| - } |
75 |
| - |
76 |
| - if x & U32_MAX > U256::ZERO { |
77 |
| - r -= 32; |
78 |
| - } else { |
79 |
| - x >>= 32; |
80 |
| - } |
81 |
| - |
82 |
| - if x & U16_MAX > U256::ZERO { |
83 |
| - r -= 16; |
84 |
| - } else { |
85 |
| - x >>= 16; |
86 |
| - } |
87 |
| - |
88 |
| - if x & U8_MAX > U256::ZERO { |
89 |
| - r -= 8; |
90 |
| - } else { |
91 |
| - x >>= 8; |
92 |
| - } |
93 |
| - |
94 |
| - if x & U256_15 > U256::ZERO { |
95 |
| - r -= 4; |
96 |
| - } else { |
97 |
| - x >>= 4; |
98 |
| - } |
99 |
| - |
100 |
| - if x & U256_3 > U256::ZERO { |
101 |
| - r -= 2; |
102 |
| - } else { |
103 |
| - x >>= 2; |
104 |
| - } |
105 |
| - |
106 |
| - if x & U256_1 > U256::ZERO { |
107 |
| - r -= 1; |
108 |
| - } |
109 |
| - |
110 |
| - Ok(r) |
| 15 | + Ok(x.trailing_zeros() as u8) |
111 | 16 | }
|
112 | 17 |
|
113 | 18 | #[cfg(test)]
|
114 | 19 | mod test {
|
115 |
| - |
116 |
| - use std::str::FromStr; |
117 |
| - |
118 |
| - use alloy::primitives::U256; |
119 |
| - |
120 |
| - use crate::{bit_math::least_significant_bit, U256_1}; |
121 |
| - |
122 | 20 | use super::most_significant_bit;
|
| 21 | + use crate::{bit_math::least_significant_bit, U256_1}; |
| 22 | + use alloy::primitives::U256; |
| 23 | + use std::str::FromStr; |
123 | 24 |
|
124 | 25 | #[test]
|
125 | 26 | fn test_most_significant_bit() {
|
|
0 commit comments