Source context

Program Rust

parse_inf_nan

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

241 }242 243 /// Try to parse a special, non-finite float.244 pub(crate) fn parse_inf_nan<F: Float>(s: &[u8], negative: bool) -> Option<F> {245     // Since a valid string has at most the length 8, we can load246     // all relevant characters into a u64 and work from there.247     // This also generates much better code.248 249     let mut register;250     let len: usize;251 252     // All valid strings are either of length 8 or 3.253     if s.len() == 8 {254         register = s.read_u64();255         len = 8;256     } else if s.len() == 3 {257         let a = s[0] as u64;258         let b = s[1] as u64;259         let c = s[2] as u64;260         register = (c << 16) | (b << 8) | a;261         len = 3;262     } else {263         return None;264     }265 266     // Clear out the bits which turn ASCII uppercase characters into267     // lowercase characters. The resulting string is all uppercase.268     // What happens to other characters is irrelevant.269     register &= 0xDFDFDFDFDFDFDFDF;270 271     // u64 values corresponding to relevant cases272     const INF_3: u64 = 0x464E49; // "INF"273     const INF_8: u64 = 0x5954494E49464E49; // "INFINITY"274     const NAN: u64 = 0x4E414E; // "NAN"275 276     // Match register value to constant to parse string.277     // Also match on the string length to catch edge cases278     // like "inf\0\0\0\0\0".279     let float = match (register, len) {280         (INF_3, 3) => F::INFINITY,281         (INF_8, 8) => F::INFINITY,282         (NAN, 3) => F::NAN,283         _ => return None,284     };285 286     if negative { Some(-float) } else { Some(float) }287 }

Aeneas declaration

The Lean below was mechanically extracted by Aeneas from Rust.

def num.imp.dec2flt.parse.parse_inf_nan
  {F : Type} {Clause0_Int : Type} (traitsFloatInst : num.imp.traits.Float F
  Clause0_Int) (s : Slice Std.U8) (negative : Bool) :
  Result (Option F)
  := do
  let i := Slice.len s
  if i = 8#usize
  then
    let register ←
      SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.read_u64 s
    let register1 ← lift (register &&& 16131858542891098079#u64)
    match register1 with
    | 4607561#uscalar => ok none
    | 6436850368004902473#uscalar =>
      let float ← traitsFloatInst.INFINITY
      if negative
      then let t ← traitsFloatInst.coreopsarithNegInst.neg float
           ok (some t)
      else ok (some float)
    | 5128526#uscalar => ok none
    | _ => ok none
  else
    let i1 := Slice.len s
    if i1 = 3#usize
    then
      let i2 ← Slice.index_usize s 0#usize
      let a ← lift (UScalar.cast .U64 i2)
      let i3 ← Slice.index_usize s 1#usize
      let b ← lift (UScalar.cast .U64 i3)
      let i4 ← Slice.index_usize s 2#usize
      let c ← lift (UScalar.cast .U64 i4)
      let i5 ← c <<< 16#i32
      let i6 ← b <<< 8#i32
      let i7 ← lift (i5 ||| i6)
      let register ← lift (i7 ||| a)
      let register1 ← lift (register &&& 16131858542891098079#u64)
      match register1 with
      | 4607561#uscalar =>
        let float ← traitsFloatInst.INFINITY
        if negative
        then
          let t ← traitsFloatInst.coreopsarithNegInst.neg float
          ok (some t)
        else ok (some float)
      | 6436850368004902473#uscalar => ok none
      | 5128526#uscalar =>
        let float ← traitsFloatInst.NAN
        if negative
        then
          let t ← traitsFloatInst.coreopsarithNegInst.neg float
          ok (some t)
        else ok (some float)
      | _ => ok none
    else ok none
Operations used here

read_u64 · Program Rust

Float · Program Rust

Source details
Declarationnum.imp.dec2flt.parse.parse_inf_nan
Filenum/imp/../../dec2flt/parse.rs
Lines244:0–287:1
Program versionRevised program