Program Rust
parse_inf_nan
This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.
238 }239 240 /// Try to parse a special, non-finite float.241 pub(crate) fn parse_inf_nan<F: Float>(s: &[u8], negative: bool) -> Option<F> {242 // Since a valid string has at most the length 8, we can load243 // all relevant characters into a u64 and work from there.244 // This also generates much better code.245 246 let mut register;247 let len: usize;248 249 // All valid strings are either of length 8 or 3.250 if s.len() == 8 {251 register = s.read_u64();252 len = 8;253 } else if s.len() == 3 {254 let a = s[0] as u64;255 let b = s[1] as u64;256 let c = s[2] as u64;257 register = (c << 16) | (b << 8) | a;258 len = 3;259 } else {260 return None;261 }262 263 // Clear out the bits which turn ASCII uppercase characters into264 // lowercase characters. The resulting string is all uppercase.265 // What happens to other characters is irrelevant.266 register &= 0xDFDFDFDFDFDFDFDF;267 268 // u64 values corresponding to relevant cases269 const INF_3: u64 = 0x464E49; // "INF"270 const INF_8: u64 = 0x5954494E49464E49; // "INFINITY"271 const NAN: u64 = 0x4E414E; // "NAN"272 273 // Match register value to constant to parse string.274 // Also match on the string length to catch edge cases275 // like "inf\0\0\0\0\0".276 let float = match (register, len) {277 (INF_3, 3) => F::INFINITY,278 (INF_8, 8) => F::INFINITY,279 (NAN, 3) => F::NAN,280 _ => return None,281 };282 283 if negative { Some(-float) } else { Some(float) }284 }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 noneSource details
| Declaration | num.imp.dec2flt.parse.parse_inf_nan |
|---|---|
| File | num/imp/../../dec2flt/parse.rs |
| Lines | 241:0–284:1 |
| Program version | Original program |