Source context

Program Rust

dec2flt_with_trace

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

341 }342 343 /// Run the actual dispatcher while retaining its approved internal observations.344 pub fn dec2flt_with_trace<345     'a,346     F: Lemire347         + core::ops::Div<Output = F>348         + core::ops::Mul<Output = F>349         + core::ops::Neg<Output = F>,350 >(351     s: &'a str,352 ) -> (Result<F, ParseFloatError>, Dec2FltTrace<'a, F>) {353     let mut body = s.as_bytes();354     let Some(&c) = body.first() else {355         return (Err(pfe_empty()), Dec2FltTrace::EmptyInput);356     };357     let negative = c == b'-';358     if c == b'-' || c == b'+' {359         body = &body[1..];360     }361     if body.is_empty() {362         return (Err(pfe_invalid()), Dec2FltTrace::SignOnly { body, negative });363     }364 365     let parse_number_result = parse_number(body);366     let parsed = match parse_number_result {367         Some(r) => r,368         None => {369             let parse_inf_nan_result = parse_inf_nan(body, negative);370             return match parse_inf_nan_result {371                 Some(value) => (372                     Ok(value),373                     Dec2FltTrace::Special {374                         body,375                         negative,376                         parse_number_result,377                         parse_inf_nan_result,378                         result: value,379                     },380                 ),381                 None => (382                     Err(pfe_invalid()),383                     Dec2FltTrace::Invalid {384                         body,385                         negative,386                         parse_number_result,387                         parse_inf_nan_result,388                     },389                 ),390             };391         }392     };393 394     let mut num = parsed;395     num.negative = negative;396     if !cfg!(feature = "optimize_for_size") {397         let fast_path_result = num.try_fast_path::<F>();398         if let Some(value) = fast_path_result {399             return (400                 Ok(value),401                 Dec2FltTrace::FastPath {402                     body,403                     negative,404                     parse_number_result,405                     fast_path_input: num,406                     fast_path_result,407                     result: value,408                 },409             );410         }411     }412 413     let first_compute_exponent = num.exponent;414     let first_compute_mantissa = num.mantissa;415     let first_compute_result = compute_float::<F>(first_compute_exponent, first_compute_mantissa);416     let mut fp = first_compute_result;417 418     // This expands the source short-circuit conjunction without moving the419     // checked `mantissa + 1`: that addition and the second helper call still420     // occur exactly when `many_digits && fp.p_biased >= 0`.421     let second_compute_guard = num.many_digits && fp.p_biased >= 0;422     let mut second_compute_exponent = None;423     let mut second_compute_mantissa = None;424     let mut second_compute_result = None;425     let mut second_equals_first = None;426     let mut forced_deferral = false;427     if second_compute_guard {428         let exponent = num.exponent;429         let mantissa = num.mantissa + 1;430         let second = compute_float::<F>(exponent, mantissa);431         let equal = fp == second;432         second_compute_exponent = Some(exponent);433         second_compute_mantissa = Some(mantissa);434         second_compute_result = Some(second);435         second_equals_first = Some(equal);436         if !equal {437             fp.p_biased = -1;438             forced_deferral = true;439         }440     }441     let post_comparison_pair = fp;442 443     let slow_path_guard = fp.p_biased < 0;444     let mut slow_path_result = None;445     if slow_path_guard {446         // The exact slow-path input is the trace's common `body`; the guard447         // records whether this call occurred, avoiding a duplicate live slice448         // reference that Aeneas cannot join across this branch.449         let replacement = parse_long_mantissa::<F>(body);450         slow_path_result = Some(replacement);451         fp = replacement;452     }453 454     let selected_pair = fp;455     // This is the same pure cast/shift/OR used by `biased_fp_to_float`; recording456     // it neither changes the selected pair nor introduces a new branch or call.457     let assembled_word = fp.m | ((fp.p_biased as u64) << F::SIG_BITS);458     let assembly_result = biased_fp_to_float::<F>(fp);459     let lexical_sign_applied = num.negative;460     let result = if lexical_sign_applied {461         core::ops::Neg::neg(assembly_result)462     } else {463         assembly_result464     };465 466     (467         Ok(result),468         Dec2FltTrace::Assembled(Dec2FltAssemblyTrace {469             body,470             negative,471             parse_number_result,472             fast_path_input: num,473             fast_path_result: None,474             first_compute_exponent,475             first_compute_mantissa,476             first_compute_result,477             second_compute_guard,478             second_compute_exponent,479             second_compute_mantissa,480             second_compute_result,481             second_equals_first,482             forced_deferral,483             post_comparison_pair,484             slow_path_guard,485             slow_path_result,486             selected_pair,487             assembled_word,488             assembly_result,489             lexical_sign_applied,490             result,491         }),492     )493 }494 495 /// Converts a decimal string into a floating point number.496 #[inline(always)] // Will be inlined into a function with `#[inline(never)]`, see above

Aeneas declaration

The Lean below was mechanically extracted by Aeneas from Rust.

def num.imp.dec2flt.dec2flt_with_trace
  {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) (s : Str) :
  Result ((core.result.Result F num.float_parse.ParseFloatError) ×
    (num.imp.dec2flt.Dec2FltTrace F))
  := do
  let body ← core.str.Str.as_bytes s
  let o ← core.slice.Slice.first body
  match o with
  | none =>
    let pfe ← num.imp.dec2flt.pfe_empty
    ok (core.result.Result.Err pfe, num.imp.dec2flt.Dec2FltTrace.EmptyInput)
  | some c =>
    let body1 ←
      if c = 45#u8
      then
        core.slice.index.Slice.index
          (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) body
          { start := 1#usize }
      else
        if c = 43#u8
        then
          core.slice.index.Slice.index
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) body
            { start := 1#usize }
        else ok body
    let b ← core.slice.Slice.is_empty body1
    if b
    then
      let pfe ← num.imp.dec2flt.pfe_invalid
      ok (core.result.Result.Err pfe, num.imp.dec2flt.Dec2FltTrace.SignOnly
        body1 (c = 45#u8))
    else
      let parse_number_result ← num.imp.dec2flt.parse.parse_number body1
      match parse_number_result with
      | none =>
        let parse_inf_nan_result ←
          num.imp.dec2flt.parse.parse_inf_nan
            LemireInst.traitsFloatExtInst.FloatInst body1 (c = 45#u8)
        match parse_inf_nan_result with
        | none =>
          let pfe ← num.imp.dec2flt.pfe_invalid
          ok (core.result.Result.Err pfe, num.imp.dec2flt.Dec2FltTrace.Invalid
            body1 (c = 45#u8) none none)
        | some value =>
          ok (core.result.Result.Ok value, num.imp.dec2flt.Dec2FltTrace.Special
            body1 (c = 45#u8) none parse_inf_nan_result value)
      | some r =>
        let fast_path_result ←
          num.imp.dec2flt.decimal.Decimal.try_fast_path LemireInst
            coreopsarithDivInst coreopsarithMulInst coreopsarithNegInst
            { r with negative := (c = 45#u8) }
        match fast_path_result with
        | none =>
          let first_compute_result ←
            num.imp.dec2flt.lemire.compute_float
              LemireInst.traitsFloatExtInst.FloatInst r.exponent r.mantissa
          let (b1, b2, first_compute_result1, fp, second_compute_guard) ←
            if r.many_digits
            then
              ok (true, true, first_compute_result, first_compute_result,
                first_compute_result.p_biased >= 0#i32)
            else
              ok (false, false, first_compute_result, first_compute_result,
                false)
          let (post_comparison_pair, second_compute_exponent,
            second_compute_mantissa, second_compute_result,
            second_equals_first, forced_deferral) ←
            if second_compute_guard
            then
              do
              let mantissa ← r.mantissa + 1#u64
              let second ←
                num.imp.dec2flt.lemire.compute_float
                  LemireInst.traitsFloatExtInst.FloatInst r.exponent mantissa
              let equal ←
                num.imp.dec2flt.common.BiasedFp.Insts.CoreCmpPartialEqBiasedFp.eq
                  fp second
              let (bf, b3) ←
                if equal
                then ok (fp, false)
                else ok ({ fp with p_biased := (-1)#i32 }, true)
              ok (bf, some r.exponent, some mantissa, some second, some equal,
                b3)
            else ok (fp, none, none, none, none, false)
          let (selected_pair, slow_path_guard, slow_path_result) ←
            if post_comparison_pair.p_biased < 0#i32
            then
              do
              let replacement ←
                num.imp.dec2flt.slow.parse_long_mantissa
                  LemireInst.traitsFloatExtInst.FloatInst body1
              ok (replacement, true, some replacement)
            else ok (post_comparison_pair, false, none)
          let i ← lift (IScalar.hcast .U64 selected_pair.p_biased)
          let i1 ← LemireInst.traitsFloatExtInst.FloatInst.SIG_BITS
          let i2 ← i <<< i1
          let assembled_word ← lift (selected_pair.m ||| i2)
          let assembly_result ←
            num.imp.dec2flt.biased_fp_to_float LemireInst.traitsFloatExtInst
              selected_pair
          if c = 45#u8
          then
            let result ← coreopsarithNegInst.neg assembly_result
            ok (core.result.Result.Ok result,
              num.imp.dec2flt.Dec2FltTrace.Assembled
              {
                body := body1,
                negative := true,
                parse_number_result := (some { r with many_digits := b1 }),
                fast_path_input :=
                  { r with negative := true, many_digits := b2 },
                fast_path_result := none,
                first_compute_exponent := r.exponent,
                first_compute_mantissa := r.mantissa,
                first_compute_result := first_compute_result1,
                second_compute_guard,
                second_compute_exponent,
                second_compute_mantissa,
                second_compute_result,
                second_equals_first,
                forced_deferral,
                post_comparison_pair,
                slow_path_guard,
                slow_path_result,
                selected_pair,
                assembled_word,
                assembly_result,
                lexical_sign_applied := true,
                result
              })
          else
            ok (core.result.Result.Ok assembly_result,
              num.imp.dec2flt.Dec2FltTrace.Assembled
              {
                body := body1,
                negative := false,
                parse_number_result := (some { r with many_digits := b1 }),
                fast_path_input :=
                  { r with negative := false, many_digits := b2 },
                fast_path_result := none,
                first_compute_exponent := r.exponent,
                first_compute_mantissa := r.mantissa,
                first_compute_result := first_compute_result1,
                second_compute_guard,
                second_compute_exponent,
                second_compute_mantissa,
                second_compute_result,
                second_equals_first,
                forced_deferral,
                post_comparison_pair,
                slow_path_guard,
                slow_path_result,
                selected_pair,
                assembled_word,
                assembly_result,
                lexical_sign_applied := false,
                result := assembly_result
              })
        | some value =>
          ok (core.result.Result.Ok value,
            num.imp.dec2flt.Dec2FltTrace.FastPath body1 (c = 45#u8)
            parse_number_result { r with negative := (c = 45#u8) }
            fast_path_result value)
Operations used here

Mul · Rust library

Div · Rust library

Neg · Rust library

first · Rust library

as_bytes · Rust library

eq · Program Rust

Lemire · Program Rust

try_fast_path · Program Rust

compute_float · Program Rust

ParseFloatError · Program Rust

pfe_empty · Program Rust

pfe_invalid · Program Rust

biased_fp_to_float · Program Rust

Dec2FltTrace · Program Rust

parse_long_mantissa · Program Rust

parse_inf_nan · Program Rust

parse_number · Program Rust

Source details
Declarationnum.imp.dec2flt.dec2flt_with_trace
Filenum/imp/../../dec2flt/mod.rs
Lines344:0–493:1
Program versionPreparation 1