Program Rust
try_fast_path
This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.
56 ///57 /// There is an exception: disguised fast-path cases, where we can shift58 /// powers-of-10 from the exponent to the significant digits.59 pub fn try_fast_path<60 F: Lemire61 + core::ops::Div<Output = F>62 + core::ops::Mul<Output = F>63 + core::ops::Neg<Output = F>,64 >(65 &self,66 ) -> Option<F> {67 // `Lemire` already entails these three operator bounds through68 // `Float`; spelling them directly changes no admitted type. Charon69 // needs the direct clauses to resolve each explicit call's `Output`.70 // Here we need to work around <https://github.com/rust-lang/rust/issues/114479>.71 // The fast path crucially depends on arithmetic being rounded to the correct number of bits72 // without any intermediate rounding. On x86 (without SSE or SSE2) this requires the precision73 // of the x87 FPU stack to be changed so that it directly rounds to 64/32 bit.74 // The `set_precision` function takes care of setting the precision on architectures which75 // require setting it by changing the global state (like the control word of the x87 FPU).76 let _cw = set_precision::<F>();77 78 if !self.can_use_fast_path::<F>() {79 return None;80 }81 82 let value = if self.exponent <= F::MAX_EXPONENT_FAST_PATH {83 // normal fast path84 let value = F::from_u64(self.mantissa);85 if self.exponent < 0 {86 // Explicit trait calls preserve Rust arithmetic and resolve87 // the generic output type that Charon leaves ambiguous.88 core::ops::Div::div(value, F::pow10_fast_path((-self.exponent) as _))89 } else {90 core::ops::Mul::mul(value, F::pow10_fast_path(self.exponent as _))91 }92 } else {93 // disguised fast path94 let shift = self.exponent - F::MAX_EXPONENT_FAST_PATH;95 let mantissa = self.mantissa.checked_mul(INT_POW10[shift as usize])?;96 if mantissa > F::MAX_MANTISSA_FAST_PATH {97 return None;98 }99 core::ops::Mul::mul(100 F::from_u64(mantissa),101 F::pow10_fast_path(F::MAX_EXPONENT_FAST_PATH as _),102 )103 };104 105 if self.negative {106 Some(core::ops::Neg::neg(value))107 } else {108 Some(value)109 }110 }111 }Aeneas declaration
The Lean below was mechanically extracted by Aeneas from Rust.
def num.imp.dec2flt.decimal.Decimal.try_fast_path
{F : Type} {Clause0_Clause0_Clause0_Int : Type} (LemireInst :
num.imp.dec2flt.Lemire F Clause0_Clause0_Clause0_Int) (coreopsarithDivInst :
core.ops.arith.Div F F F) (coreopsarithMulInst : core.ops.arith.Mul F F F)
(coreopsarithNegInst : core.ops.arith.Neg F F)
(self : num.imp.dec2flt.decimal.Decimal) :
Result (Option F)
:= do
num.imp.dec2flt.fpu.fpu_precision.set_precision F
let b ← num.imp.dec2flt.decimal.Decimal.can_use_fast_path LemireInst self
if b
then
let i ← LemireInst.MAX_EXPONENT_FAST_PATH
if self.exponent <= i
then
let value ← LemireInst.from_u64 self.mantissa
let value1 ←
if self.exponent < 0#i64
then
do
let i1 ← -. self.exponent
let i2 ← lift (IScalar.hcast .Usize i1)
let t ← LemireInst.pow10_fast_path i2
coreopsarithDivInst.div value t
else
do
let i1 ← lift (IScalar.hcast .Usize self.exponent)
let t ← LemireInst.pow10_fast_path i1
coreopsarithMulInst.mul value t
if self.negative
then let t ← coreopsarithNegInst.neg value1
ok (some t)
else ok (some value1)
else
let shift ← self.exponent - i
let i1 ← lift (IScalar.hcast .Usize shift)
let i2 ← Array.index_usize num.imp.dec2flt.decimal.INT_POW10 i1
let o ← lift (U64.checked_mul self.mantissa i2)
let cf ← core.option.Option.Insts.CoreOpsTry_traitTry.branch o
match cf with
| core.ops.control_flow.ControlFlow.Continue val =>
let i3 ← LemireInst.MAX_MANTISSA_FAST_PATH
if val > i3
then ok none
else
let t ← LemireInst.from_u64 val
let i4 ← lift (IScalar.hcast .Usize i)
let t1 ← LemireInst.pow10_fast_path i4
let value ← coreopsarithMulInst.mul t t1
if self.negative
then let t2 ← coreopsarithNegInst.neg value
ok (some t2)
else ok (some value)
| core.ops.control_flow.ControlFlow.Break residual =>
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual
F residual
else ok noneOperations used here
Source details
| Declaration | num.imp.dec2flt.decimal.Decimal.try_fast_path |
|---|---|
| File | num/imp/../../dec2flt/decimal.rs |
| Lines | 59:4–110:5 |
| Program version | Revised program |