Source context

Program Rust

dec2flt_with_trace

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

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