Source context

Program Rust

parse_long_mantissa

This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.

24 ///25 /// The algorithms described here are based on "Processing Long Numbers Quickly",26 /// available here: <https://arxiv.org/pdf/2101.11408.pdf#section.11>.27 pub(crate) fn parse_long_mantissa<F: Float>(s: &[u8]) -> BiasedFp {28     const MAX_SHIFT: usize = 60;29     const NUM_POWERS: usize = 19;30     const POWERS: [u8; 19] =31         [0, 3, 6, 9, 13, 16, 19, 23, 26, 29, 33, 36, 39, 43, 46, 49, 53, 56, 59];32 33     let get_shift = |n| {34         if n < NUM_POWERS { POWERS[n] as usize } else { MAX_SHIFT }35     };36 37     let fp_zero = BiasedFp::zero_pow2(0);38     let fp_inf = BiasedFp::zero_pow2(F::INFINITE_POWER);39 40     let mut d = parse_decimal_seq(s);41 42     // Short-circuit if the value can only be a literal 0 or infinity.43     if d.num_digits == 0 || d.decimal_point < -324 {44         return fp_zero;45     } else if d.decimal_point >= 310 {46         return fp_inf;47     }48     let mut exp2 = 0_i32;49     let mut loop_result = None;50     // Shift right toward (1/2 ... 1].51     while d.decimal_point > 0 && loop_result.is_none() {52         let n = d.decimal_point as usize;53         let shift = get_shift(n);54         d.right_shift(shift);55         if d.decimal_point < -DecimalSeq::DECIMAL_POINT_RANGE {56             // The source returns `fp_zero` at this exact point.  Storing that57             // result and terminating the loop performs no later loop update;58             // the return below is therefore behavior-identical and avoids an59             // Aeneas restriction on returns from nested loop translation.60             loop_result = Some(fp_zero);61         } else {62             exp2 += shift as i32;63         }64     }65     if let Some(result) = loop_result {66         return result;67     }68     // Shift left toward (1/2 ... 1].69     while d.decimal_point <= 0 && loop_result.is_none() {70         let shift = if d.decimal_point == 0 {71             match d.digits[0] {72                 digit if digit >= 5 => break,73                 0 | 1 => 2,74                 _ => 1,75             }76         } else {77             get_shift((-d.decimal_point) as _)78         };79         d.left_shift(shift);80         if d.decimal_point > DecimalSeq::DECIMAL_POINT_RANGE {81             // This records the source's immediate `fp_inf` return.  Since the82             // guarded loop exits before the exponent update, moving the return83             // just below the loop preserves every state transition.84             loop_result = Some(fp_inf);85         } else {86             exp2 -= shift as i32;87         }88     }89     if let Some(result) = loop_result {90         return result;91     }92     // We are now in the range [1/2 ... 1] but the binary format uses [1 ... 2].93     exp2 -= 1;94     while F::EXP_MIN > exp2 {95         let mut n = (F::EXP_MIN - exp2) as usize;96         if n > MAX_SHIFT {97             n = MAX_SHIFT;98         }99         d.right_shift(n);100         exp2 += n as i32;101     }102     if (exp2 - F::EXP_MIN + 1) >= F::INFINITE_POWER {103         return fp_inf;104     }105     // Shift the decimal to the hidden bit, and then round the value106     // to get the high mantissa+1 bits.107     d.left_shift(F::SIG_BITS as usize + 1);108     let mut mantissa = d.round();109     if mantissa >= (1_u64 << (F::SIG_BITS + 1)) {110         // Rounding up overflowed to the carry bit, need to111         // shift back to the hidden bit.112         d.right_shift(1);113         exp2 += 1;114         mantissa = d.round();115         if (exp2 - F::EXP_MIN + 1) >= F::INFINITE_POWER {116             return fp_inf;117         }118     }119     let mut power2 = exp2 - F::EXP_MIN + 1;120     if mantissa < (1_u64 << F::SIG_BITS) {121         power2 -= 1;122     }123     // Zero out all the bits above the explicit mantissa bits.124     mantissa &= (1_u64 << F::SIG_BITS) - 1;125     BiasedFp { m: mantissa, p_biased: power2 }126 }

Aeneas declaration

The Lean below was mechanically extracted by Aeneas from Rust.

def num.imp.dec2flt.slow.parse_long_mantissa
  {F : Type} {Clause0_Int : Type} (traitsFloatInst : num.imp.traits.Float F
  Clause0_Int) (s : Slice Std.U8) :
  Result num.imp.dec2flt.common.BiasedFp
  := do
  let fp_zero ← num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32
  let i ← traitsFloatInst.INFINITE_POWER
  let fp_inf ← num.imp.dec2flt.common.BiasedFp.zero_pow2 i
  let d ← num.imp.dec2flt.decimal_seq.parse_decimal_seq s
  if d.num_digits = 0#usize
  then ok fp_zero
  else
    if d.decimal_point < (-324)#i32
    then ok fp_zero
    else
      if d.decimal_point >= 310#i32
      then ok fp_inf
      else
        let (i1, i2, b, a, exp2, loop_result) ←
          num.imp.dec2flt.slow.parse_long_mantissa_loop0 traitsFloatInst
            fp_zero d.num_digits d.decimal_point d.truncated d.digits 0#i32
            none
        match loop_result with
        | none =>
          let (i3, i4, b1, a1, exp21, loop_result1) ←
            num.imp.dec2flt.slow.parse_long_mantissa_loop1 traitsFloatInst
              fp_inf i1 i2 b a exp2 none
          match loop_result1 with
          | none =>
            let exp22 ← exp21 - 1#i32
            let (d1, exp23) ←
              num.imp.dec2flt.slow.parse_long_mantissa_loop2 traitsFloatInst
                {
                  num_digits := i3,
                  decimal_point := i4,
                  truncated := b1,
                  digits := a1
                } exp22
            let i5 ← traitsFloatInst.EXP_MIN
            let i6 ← exp23 - i5
            let i7 ← i6 + 1#i32
            if i7 >= i
            then ok fp_inf
            else
              let i8 ← traitsFloatInst.SIG_BITS
              let i9 ← lift (UScalar.cast .Usize i8)
              let i10 ← i9 + 1#usize
              let d2 ←
                num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d1 i10
              let mantissa ← num.imp.dec2flt.decimal_seq.DecimalSeq.round d2
              let i11 ← i8 + 1#u32
              let i12 ← 1#u64 <<< i11
              if mantissa >= i12
              then
                let d3 ←
                  num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d2 1#usize
                let exp24 ← exp23 + 1#i32
                let mantissa1 ←
                  num.imp.dec2flt.decimal_seq.DecimalSeq.round d3
                let i13 ← exp24 - i5
                let i14 ← i13 + 1#i32
                if i14 >= i
                then ok fp_inf
                else
                  let i15 ← exp24 - i5
                  let power2 ← i15 + 1#i32
                  let i16 ← 1#u64 <<< i8
                  let power21 ←
                    if mantissa1 < i16
                    then power2 - 1#i32
                    else ok power2
                  let i17 ← 1#u64 <<< i8
                  let i18 ← i17 - 1#u64
                  let mantissa2 ← lift (mantissa1 &&& i18)
                  ok { m := mantissa2, p_biased := power21 }
              else
                let i13 ← exp23 - i5
                let power2 ← i13 + 1#i32
                let i14 ← 1#u64 <<< i8
                let power21 ←
                  if mantissa < i14
                  then power2 - 1#i32
                  else ok power2
                let i15 ← 1#u64 <<< i8
                let i16 ← i15 - 1#u64
                let mantissa1 ← lift (mantissa &&& i16)
                ok { m := mantissa1, p_biased := power21 }
          | some result => ok result
        | some result => ok result
Operations used here

BiasedFp · Program Rust

zero_pow2 · Program Rust

Float · Program Rust

round · Program Rust

left_shift · Program Rust

right_shift · Program Rust

parse_decimal_seq · Program Rust

parse_long_mantissa_loop0 · Program Rust

parse_long_mantissa_loop1 · Program Rust

parse_long_mantissa_loop2 · Program Rust

Source details
Declarationnum.imp.dec2flt.slow.parse_long_mantissa
Filenum/imp/../../dec2flt/slow.rs
Lines27:0–126:1
Program versionOriginal program