Program Rust
compute_product_approx
This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.
131 // This will compute or rather approximate w * 5**q and return a pair of 64-bit words132 // approximating the result, with the "high" part corresponding to the most significant133 // bits and the low part corresponding to the least significant bits.134 fn compute_product_approx(q: i64, w: u64, precision: usize) -> (u64, u64) {135 debug_assert!(q >= SMALLEST_POWER_OF_FIVE as i64);136 debug_assert!(q <= LARGEST_POWER_OF_FIVE as i64);137 debug_assert!(precision <= 64);138 139 let mask = if precision < 64 {140 0xFFFF_FFFF_FFFF_FFFF_u64 >> precision141 } else {142 0xFFFF_FFFF_FFFF_FFFF_u64143 };144 145 // 5^q < 2^64, then the multiplication always provides an exact value.146 // That means whenever we need to round ties to even, we always have147 // an exact value.148 let index = (q - SMALLEST_POWER_OF_FIVE as i64) as usize;149 let (lo5, hi5) = POWER_OF_FIVE_128[index];150 // Only need one multiplication as long as there is 1 zero but151 // in the explicit mantissa bits, +1 for the hidden bit, +1 to152 // determine the rounding direction, +1 for if the computed153 // product has a leading zero.154 let (mut first_lo, mut first_hi) = full_multiplication(w, lo5);155 if first_hi & mask == mask {156 // Need to do a second multiplication to get better precision157 // for the lower product. This will always be exact158 // where q is < 55, since 5^55 < 2^128. If this wraps,159 // then we need to round up the hi product.160 let (_, second_hi) = full_multiplication(w, hi5);161 first_lo = first_lo.wrapping_add(second_hi);162 if second_hi > first_lo {163 first_hi += 1;164 }165 }166 (first_lo, first_hi)167 }Aeneas declaration
The Lean below was mechanically extracted by Aeneas from Rust.
def num.imp.dec2flt.lemire.compute_product_approx
(q : Std.I64) (w : Std.U64) (precision : Std.Usize) :
Result (Std.U64 × Std.U64)
:= do
let i ←
lift (IScalar.cast .I64 num.imp.dec2flt.table.SMALLEST_POWER_OF_FIVE)
massert (q >= i)
let i1 ←
lift (IScalar.cast .I64 num.imp.dec2flt.table.LARGEST_POWER_OF_FIVE)
massert (q <= i1)
massert (precision <= 64#usize)
let mask ←
if precision < 64#usize
then 18446744073709551615#u64 >>> precision
else ok 18446744073709551615#u64
let i2 ←
lift (IScalar.cast .I64 num.imp.dec2flt.table.SMALLEST_POWER_OF_FIVE)
let i3 ← q - i2
let index ← lift (IScalar.hcast .Usize i3)
let p ← Array.index_usize num.imp.dec2flt.table.POWER_OF_FIVE_128 index
let (lo5, _) := p
let (_, hi5) := p
let (first_lo, first_hi) ← num.imp.dec2flt.lemire.full_multiplication w lo5
let i4 ← lift (first_hi &&& mask)
if i4 = mask
then
let (_, second_hi) ← num.imp.dec2flt.lemire.full_multiplication w hi5
let first_lo1 ← lift (core.num.U64.wrapping_add first_lo second_hi)
if second_hi > first_lo1
then let first_hi1 ← first_hi + 1#u64
ok (first_lo1, first_hi1)
else ok (first_lo1, first_hi)
else ok (first_lo, first_hi)Operations used here
POWER_OF_FIVE_128 · Program Rust
LARGEST_POWER_OF_FIVE · Program Rust
Source details
| Declaration | num.imp.dec2flt.lemire.compute_product_approx |
|---|---|
| File | num/imp/../../dec2flt/lemire.rs |
| Lines | 134:0–167:1 |
| Program version | Preparation 1 |