Skip to content

Commit c8daa4d

Browse files
authored
Merge pull request #8 from Alexhuszagh/issue_02
Ensure checked indexing is used for the power-of-5 table lookup.
2 parents 57bf987 + 6f430e6 commit c8daa4d

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/binary.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ fn compute_product_approx(q: i64, w: u64, precision: usize) -> (u64, u64) {
8282
0xFFFF_FFFF_FFFF_FFFF_u64
8383
};
8484
let index = (q - SMALLEST_POWER_OF_FIVE as i64) as usize;
85-
let (lo5, hi5) = unsafe { *POWER_OF_FIVE_128.get_unchecked(index) };
85+
// NOTE: this cannot be ellided by the compiler, but the proof the index
86+
// must be within the bounds is non-trivial, especially because this
87+
// comes from a parsed result. Since this is unlikely to have any major
88+
// performance implications, as is determined empirically, we keep the
89+
// bounds check despite the performance hit.
90+
let (lo5, hi5) = POWER_OF_FIVE_128[index];
8691
let (mut first_lo, mut first_hi) = full_multiplication(w, lo5);
8792
if first_hi & mask == mask {
8893
let (_, second_hi) = full_multiplication(w, hi5);

0 commit comments

Comments
 (0)