Skip to content

Commit a2b0176

Browse files
authored
Merge pull request #35 from DaniPopes/bitmath
2 parents 0c17c2e + b1deff2 commit a2b0176

File tree

3 files changed

+8
-113
lines changed

3 files changed

+8
-113
lines changed

src/bit_math.rs

+8-107
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,26 @@
1-
use std::ops::ShrAssign;
2-
1+
use crate::error::UniswapV3MathError;
32
use alloy::primitives::U256;
43

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> {
125
if x.is_zero() {
136
return Err(UniswapV3MathError::ZeroValue);
147
}
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)
559
}
5610

57-
pub fn least_significant_bit(mut x: U256) -> Result<u8, UniswapV3MathError> {
11+
pub fn least_significant_bit(x: U256) -> Result<u8, UniswapV3MathError> {
5812
if x.is_zero() {
5913
return Err(UniswapV3MathError::ZeroValue);
6014
}
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)
11116
}
11217

11318
#[cfg(test)]
11419
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-
12220
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;
12324

12425
#[test]
12526
fn test_most_significant_bit() {

src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,3 @@ const U256_262144: U256 = U256::from_limbs([262144, 0, 0, 0]);
4141
const U256_524288: U256 = U256::from_limbs([524288, 0, 0, 0]);
4242

4343
const U256_MAX_TICK: U256 = U256::from_limbs([887272, 0, 0, 0]);
44-
const U128_MAX: U256 = U256::from_limbs([u64::MAX, u64::MAX, 0, 0]);
45-
const U64_MAX: U256 = U256::from_limbs([u64::MAX, 0, 0, 0]);
46-
const U32_MAX: U256 = U256::from_limbs([u32::MAX as u64, 0, 0, 0]);
47-
const U16_MAX: U256 = U256::from_limbs([u16::MAX as u64, 0, 0, 0]);
48-
const U8_MAX: U256 = U256::from_limbs([u8::MAX as u64, 0, 0, 0]);

src/tick_math.rs

-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ pub fn get_tick_at_sqrt_ratio(sqrt_price_x_96: U256) -> Result<i32, UniswapV3Mat
229229
#[cfg(test)]
230230
mod test {
231231
use super::*;
232-
use alloy::primitives::U256;
233232
use std::{ops::Sub, str::FromStr};
234233

235234
#[test]

0 commit comments

Comments
 (0)