Source context

Program Rust

compute_float

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

23 /// at a Gigabyte per Second" in section 5, "Fast Algorithm", and24 /// section 6, "Exact Numbers And Ties", available online:25 /// <https://arxiv.org/abs/2101.11408.pdf>.26 pub fn compute_float<F: Float>(q: i64, mut w: u64) -> BiasedFp {27     let fp_zero = BiasedFp::zero_pow2(0);28     let fp_inf = BiasedFp::zero_pow2(F::INFINITE_POWER);29     let fp_error = BiasedFp::zero_pow2(-1);30 31     // Short-circuit if the value can only be a literal 0 or infinity.32     if w == 0 || q < F::SMALLEST_POWER_OF_TEN as i64 {33         return fp_zero;34     } else if q > F::LARGEST_POWER_OF_TEN as i64 {35         return fp_inf;36     }37     // Normalize our significant digits, so the most-significant bit is set.38     let lz = w.leading_zeros();39     w <<= lz;40     let (lo, hi) = compute_product_approx(q, w, F::SIG_BITS as usize + 3);41     if lo == 0xFFFF_FFFF_FFFF_FFFF {42         // If we have failed to approximate w x 5^-q with our 128-bit value.43         // Since the addition of 1 could lead to an overflow which could then44         // round up over the half-way point, this can lead to improper rounding45         // of a float.46         //47         // However, this can only occur if q ∈ [-27, 55]. The upper bound of q48         // is 55 because 5^55 < 2^128, however, this can only happen if 5^q > 2^64,49         // since otherwise the product can be represented in 64-bits, producing50         // an exact result. For negative exponents, rounding-to-even can51         // only occur if 5^-q < 2^64.52         //53         // For detailed explanations of rounding for negative exponents, see54         // <https://arxiv.org/pdf/2101.11408.pdf#section.9.1>. For detailed55         // explanations of rounding for positive exponents, see56         // <https://arxiv.org/pdf/2101.11408.pdf#section.8>.57         let inside_safe_exponent = (q >= -27) && (q <= 55);58         if !inside_safe_exponent {59             return fp_error;60         }61     }62     let upperbit = (hi >> 63) as i32;63     let mut mantissa = hi >> (upperbit + 64 - F::SIG_BITS as i32 - 3);64     let mut power2 = power(q as i32) + upperbit - lz as i32 - F::EXP_MIN + 1;65     if power2 <= 0 {66         if -power2 + 1 >= 64 {67             // Have more than 64 bits below the minimum exponent, must be 0.68             return fp_zero;69         }70         // Have a subnormal value.71         mantissa >>= -power2 + 1;72         mantissa += mantissa & 1;73         mantissa >>= 1;74         power2 = (mantissa >= (1_u64 << F::SIG_BITS)) as i32;75         return BiasedFp { m: mantissa, p_biased: power2 };76     }77     // Need to handle rounding ties. Normally, we need to round up,78     // but if we fall right in between and we have an even basis, we79     // need to round down.80     //81     // This will only occur if:82     //  1. The lower 64 bits of the 128-bit representation is 0.83     //      IE, 5^q fits in single 64-bit word.84     //  2. The least-significant bit prior to truncated mantissa is odd.85     //  3. All the bits truncated when shifting to mantissa bits + 1 are 0.86     //87     // Or, we may fall between two floats: we are exactly halfway.88     if lo <= 189         && q >= F::MIN_EXPONENT_ROUND_TO_EVEN as i6490         && q <= F::MAX_EXPONENT_ROUND_TO_EVEN as i6491         && mantissa & 0b11 == 0b0192         && (mantissa << (upperbit + 64 - F::SIG_BITS as i32 - 3)) == hi93     {94         // Zero the lowest bit, so we don't round up.95         mantissa &= !1_u64;96     }97     // Round-to-even, then shift the significant digits into place.98     mantissa += mantissa & 1;99     mantissa >>= 1;100     if mantissa >= (2_u64 << F::SIG_BITS) {101         // Rounding up overflowed, so the carry bit is set. Set the102         // mantissa to 1 (only the implicit, hidden bit is set) and103         // increase the exponent.104         mantissa = 1_u64 << F::SIG_BITS;105         power2 += 1;106     }107     // Zero out the hidden bit.108     mantissa &= !(1_u64 << F::SIG_BITS);109     if power2 >= F::INFINITE_POWER {110         // Exponent is above largest normal value, must be infinite.111         return fp_inf;112     }113     BiasedFp { m: mantissa, p_biased: power2 }114 }115 116 /// Calculate a base 2 exponent from a decimal exponent.117 /// This uses a pre-computed integer approximation for

Aeneas declaration

The Lean below was mechanically extracted by Aeneas from Rust.

def num.imp.dec2flt.lemire.compute_float
  {F : Type} {Clause0_Int : Type} (traitsFloatInst : num.imp.traits.Float F
  Clause0_Int) (q : Std.I64) (w : Std.U64) :
  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 fp_error ← num.imp.dec2flt.common.BiasedFp.zero_pow2 (-1)#i32
  if w = 0#u64
  then ok fp_zero
  else
    let i1 ← traitsFloatInst.SMALLEST_POWER_OF_TEN
    let i2 ← lift (IScalar.cast .I64 i1)
    if q < i2
    then ok fp_zero
    else
      let i3 ← traitsFloatInst.LARGEST_POWER_OF_TEN
      let i4 ← lift (IScalar.cast .I64 i3)
      if q > i4
      then ok fp_inf
      else
        let lz ← lift (core.num.U64.leading_zeros w)
        let w1 ← w <<< lz
        let i5 ← traitsFloatInst.SIG_BITS
        let i6 ← lift (UScalar.cast .Usize i5)
        let i7 ← i6 + 3#usize
        let (lo, hi) ← num.imp.dec2flt.lemire.compute_product_approx q w1 i7
        if lo = 18446744073709551615#u64
        then
          let inside_safe_exponent ←
            if q >= (-27)#i64
            then ok (q <= 55#i64)
            else ok false
          if inside_safe_exponent
          then
            let i8 ← hi >>> 63#i32
            let upperbit ← lift (UScalar.hcast .I32 i8)
            let i9 ← upperbit + 64#i32
            let i10 ← lift (UScalar.hcast .I32 i5)
            let i11 ← i9 - i10
            let i12 ← i11 - 3#i32
            let mantissa ← hi >>> i12
            let i13 ← lift (IScalar.cast .I32 q)
            let i14 ← num.imp.dec2flt.lemire.power i13
            let i15 ← i14 + upperbit
            let i16 ← lift (UScalar.hcast .I32 lz)
            let i17 ← i15 - i16
            let i18 ← traitsFloatInst.EXP_MIN
            let i19 ← i17 - i18
            let power2 ← i19 + 1#i32
            if power2 <= 0#i32
            then
              let i20 ← -. power2
              let i21 ← i20 + 1#i32
              if i21 >= 64#i32
              then ok fp_zero
              else
                let i22 ← i20 + 1#i32
                let mantissa1 ← mantissa >>> i22
                let i23 ← lift (mantissa1 &&& 1#u64)
                let mantissa2 ← mantissa1 + i23
                let mantissa3 ← mantissa2 >>> 1#i32
                let i24 ← 1#u64 <<< i5
                let power21 ←
                  lift (IScalar.cast_fromBool .I32 (mantissa3 >= i24))
                ok { m := mantissa3, p_biased := power21 }
            else
              let mantissa1 ←
                if lo <= 1#u64
                then
                  do
                  let i20 ← traitsFloatInst.MIN_EXPONENT_ROUND_TO_EVEN
                  let i21 ← lift (IScalar.cast .I64 i20)
                  if q >= i21
                  then
                    let i22 ← traitsFloatInst.MAX_EXPONENT_ROUND_TO_EVEN
                    let i23 ← lift (IScalar.cast .I64 i22)
                    if q <= i23
                    then
                      let i24 ← lift (mantissa &&& 3#u64)
                      if i24 = 1#u64
                      then
                        let i25 ← lift (UScalar.hcast .I32 i5)
                        let i26 ← i9 - i25
                        let i27 ← i26 - 3#i32
                        let i28 ← mantissa <<< i27
                        if i28 = hi
                        then let i29 ← lift (~~~ 1#u64)
                             ok (mantissa &&& i29)
                        else ok mantissa
                      else ok mantissa
                    else ok mantissa
                  else ok mantissa
                else ok mantissa
              let i20 ← lift (mantissa1 &&& 1#u64)
              let mantissa2 ← mantissa1 + i20
              let mantissa3 ← mantissa2 >>> 1#i32
              let i21 ← 2#u64 <<< i5
              let (mantissa4, power21) ←
                if mantissa3 >= i21
                then
                  do
                  let mantissa5 ← 1#u64 <<< i5
                  let power22 ← power2 + 1#i32
                  ok (mantissa5, power22)
                else ok (mantissa3, power2)
              let i22 ← 1#u64 <<< i5
              let i23 ← lift (~~~ i22)
              let mantissa5 ← lift (mantissa4 &&& i23)
              if power21 >= i
              then ok fp_inf
              else ok { m := mantissa5, p_biased := power21 }
          else ok fp_error
        else
          let i8 ← hi >>> 63#i32
          let upperbit ← lift (UScalar.hcast .I32 i8)
          let i9 ← upperbit + 64#i32
          let i10 ← lift (UScalar.hcast .I32 i5)
          let i11 ← i9 - i10
          let i12 ← i11 - 3#i32
          let mantissa ← hi >>> i12
          let i13 ← lift (IScalar.cast .I32 q)
          let i14 ← num.imp.dec2flt.lemire.power i13
          let i15 ← i14 + upperbit
          let i16 ← lift (UScalar.hcast .I32 lz)
          let i17 ← i15 - i16
          let i18 ← traitsFloatInst.EXP_MIN
          let i19 ← i17 - i18
          let power2 ← i19 + 1#i32
          if power2 <= 0#i32
          then
            let i20 ← -. power2
            let i21 ← i20 + 1#i32
            if i21 >= 64#i32
            then ok fp_zero
            else
              let i22 ← i20 + 1#i32
              let mantissa1 ← mantissa >>> i22
              let i23 ← lift (mantissa1 &&& 1#u64)
              let mantissa2 ← mantissa1 + i23
              let mantissa3 ← mantissa2 >>> 1#i32
              let i24 ← 1#u64 <<< i5
              let power21 ←
                lift (IScalar.cast_fromBool .I32 (mantissa3 >= i24))
              ok { m := mantissa3, p_biased := power21 }
          else
            let mantissa1 ←
              if lo <= 1#u64
              then
                do
                let i20 ← traitsFloatInst.MIN_EXPONENT_ROUND_TO_EVEN
                let i21 ← lift (IScalar.cast .I64 i20)
                if q >= i21
                then
                  let i22 ← traitsFloatInst.MAX_EXPONENT_ROUND_TO_EVEN
                  let i23 ← lift (IScalar.cast .I64 i22)
                  if q <= i23
                  then
                    let i24 ← lift (mantissa &&& 3#u64)
                    if i24 = 1#u64
                    then
                      let i25 ← lift (UScalar.hcast .I32 i5)
                      let i26 ← i9 - i25
                      let i27 ← i26 - 3#i32
                      let i28 ← mantissa <<< i27
                      if i28 = hi
                      then let i29 ← lift (~~~ 1#u64)
                           ok (mantissa &&& i29)
                      else ok mantissa
                    else ok mantissa
                  else ok mantissa
                else ok mantissa
              else ok mantissa
            let i20 ← lift (mantissa1 &&& 1#u64)
            let mantissa2 ← mantissa1 + i20
            let mantissa3 ← mantissa2 >>> 1#i32
            let i21 ← 2#u64 <<< i5
            let (mantissa4, power21) ←
              if mantissa3 >= i21
              then
                do
                let mantissa5 ← 1#u64 <<< i5
                let power22 ← power2 + 1#i32
                ok (mantissa5, power22)
              else ok (mantissa3, power2)
            let i22 ← 1#u64 <<< i5
            let i23 ← lift (~~~ i22)
            let mantissa5 ← lift (mantissa4 &&& i23)
            if power21 >= i
            then ok fp_inf
            else ok { m := mantissa5, p_biased := power21 }
Operations used here

BiasedFp · Program Rust

zero_pow2 · Program Rust

Float · Program Rust

compute_product_approx · Program Rust

power · Program Rust

Source details
Declarationnum.imp.dec2flt.lemire.compute_float
Filenum/imp/../../dec2flt/lemire.rs
Lines26:0–114:1
Program versionOriginal program