Skip to content

Commit 607c889

Browse files
committed
perf: remove bound checks for casts
1 parent d076286 commit 607c889

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

src/lib.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ pub trait UnsignedBase:
5858
+ BitOrAssign
5959
{
6060
fn leading_zeros(self) -> u32;
61+
// Save since will only be used for usize <= 8 bit for LUT lookup
62+
fn as_usize(self) -> usize;
63+
// Save since number will never exceed 8 bits
64+
fn as_u8(self) -> u8;
6165
const ZERO: Self;
6266
}
6367

@@ -70,6 +74,16 @@ macro_rules! base_impl {
7074
fn leading_zeros(self) -> u32 {
7175
<$T>::leading_zeros(self)
7276
}
77+
78+
#[inline]
79+
fn as_usize(self) -> usize {
80+
self as usize
81+
}
82+
83+
#[inline]
84+
fn as_u8(self) -> u8 {
85+
self as u8
86+
}
7387
}
7488
};
7589
}
@@ -125,8 +139,6 @@ impl Unsigned for u8 {
125139
/// assert_eq!(hilbert, 0b11u128);
126140
///```
127141
pub fn xy2h<T: Unsigned>(x: T, y: T, order: u8) -> <T as Unsigned>::Key
128-
where
129-
<T as TryInto<usize>>::Error: core::fmt::Debug,
130142
{
131143
// Mapping from State and coordinates to hilbert states
132144
// SXXXYYY => SHHH
@@ -151,22 +163,21 @@ where
151163
let useless_bits = (x | y).leading_zeros() & !1;
152164
let lowest_order = (coor_bits - useless_bits) as u8 + (order & 1);
153165

154-
let mut result: <T as Unsigned>::Key = <T as Unsigned>::Key::ZERO;
166+
let mut result: T::Key = T::Key::ZERO;
155167
let mut state = 0u8;
156168
let mut shift_factor = lowest_order as i8 - 3;
157169

158170
while shift_factor > 0 {
159171
let x_in = ((x >> shift_factor) & T::SEVEN) << 3i8;
160172
let y_in = (y >> shift_factor) & T::SEVEN;
161173

162-
let index: T = x_in | y_in | state.into();
163-
let index: usize = index.try_into().unwrap();
174+
let index = (x_in | y_in | state.into()).as_usize();
164175

165176
let r = LUT_3[index];
166177
state = r & 0b11000000;
167-
let r: <T as Unsigned>::Key = r.into();
178+
let r: T::Key = r.into();
168179

169-
let mut hhh: <T as Unsigned>::Key = r & T::SIXTY_THREE;
180+
let mut hhh: T::Key = r & T::SIXTY_THREE;
170181
hhh <<= ((shift_factor as u8) << 1).into();
171182
result |= hhh;
172183
shift_factor -= 3;
@@ -176,12 +187,11 @@ where
176187
let x_in = ((x << shift_factor) & T::SEVEN) << 3i8;
177188
let y_in = (y << shift_factor) & T::SEVEN;
178189

179-
let index: T = x_in | y_in | state.into();
180-
let index = index.try_into().unwrap();
190+
let index = (x_in | y_in | state.into()).as_usize();
181191
let r: u8 = LUT_3[index];
182-
let r: <T as Unsigned>::Key = r.into();
192+
let r: T::Key = r.into();
183193

184-
let mut hhh: <T as Unsigned>::Key = r & T::SIXTY_THREE;
194+
let mut hhh: T::Key = r & T::SIXTY_THREE;
185195
hhh >>= ((shift_factor as u8) << 1).into();
186196

187197
result | hhh
@@ -202,10 +212,6 @@ where
202212
/// assert_eq!(y, 0u64);
203213
///```
204214
pub fn h2xy<T: Unsigned>(h: <T as Unsigned>::Key, order: u8) -> (T, T)
205-
where
206-
<T as TryInto<usize>>::Error: core::fmt::Debug,
207-
<<T as Unsigned>::Key as TryInto<u8>>::Error: core::fmt::Debug,
208-
<T as Unsigned>::Key: TryInto<u8>,
209215
{
210216
// Mapping from hilbert states to 2D coordinates
211217
// SHHH => SXXXYYY
@@ -236,9 +242,9 @@ where
236242
let mut shift_factor = lowest_order as i8 - 3;
237243

238244
while shift_factor > 0 {
239-
let h_in: <T as Unsigned>::Key = h >> ((shift_factor as usize) << 1);
240-
let h_in: <T as Unsigned>::Key = h_in & T::SIXTY_THREE;
241-
let h_in: u8 = h_in.try_into().unwrap();
245+
let h_in: T::Key = h >> ((shift_factor as usize) << 1);
246+
let h_in: T::Key = h_in & T::SIXTY_THREE;
247+
let h_in: u8 = h_in.as_u8();
242248

243249
let r: u8 = LUT_3_REV[state as usize | h_in as usize];
244250
state = r & 0b11000000;
@@ -256,9 +262,9 @@ where
256262
}
257263

258264
shift_factor *= -1;
259-
let h_in: <T as Unsigned>::Key = h << ((shift_factor as usize) << 1);
260-
let h_in: <T as Unsigned>::Key = h_in & T::SIXTY_THREE;
261-
let h_in: u8 = h_in.try_into().unwrap();
265+
let h_in: T::Key = h << ((shift_factor as usize) << 1);
266+
let h_in: T::Key = h_in & T::SIXTY_THREE;
267+
let h_in: u8 = h_in.as_u8();
262268

263269
let r: u8 = LUT_3_REV[state as usize | h_in as usize];
264270

0 commit comments

Comments
 (0)