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