Support parsing NaN and infinities from the hf* functions

This isn't very useful for constants since the trait constants are
available, but does enable roundtripping via hex float syntax.
This commit is contained in:
Trevor Gross 2025-01-28 09:50:21 +00:00
parent 396725e146
commit 99c1080556
4 changed files with 61 additions and 12 deletions

View file

@ -3,6 +3,8 @@
use std::cmp::{self, Ordering};
use std::{fmt, ops};
use libm::support::hex_float::parse_any;
use crate::Float;
/// Sometimes verifying float logic is easiest when all values can quickly be checked exhaustively
@ -490,3 +492,7 @@ impl fmt::LowerHex for f8 {
self.0.fmt(f)
}
}
pub const fn hf8(s: &str) -> f8 {
f8(parse_any(s, 8, 3) as u8)
}

View file

@ -20,7 +20,7 @@ use std::path::PathBuf;
use std::sync::LazyLock;
use std::time::SystemTime;
pub use f8_impl::f8;
pub use f8_impl::{f8, hf8};
pub use libm::support::{Float, Int, IntTy, MinInt};
pub use num::{FloatExt, linear_ints, logspace};
pub use op::{

View file

@ -26,17 +26,25 @@ pub const fn hf128(s: &str) -> f128 {
f128::from_bits(parse_any(s, 128, 112))
}
const fn parse_any(s: &str, bits: u32, sig_bits: u32) -> u128 {
/// Parse any float from hex to its bitwise representation.
///
/// `nan_repr` is passed rather than constructed so the platform-specific NaN is returned.
pub const fn parse_any(s: &str, bits: u32, sig_bits: u32) -> u128 {
let exp_bits: u32 = bits - sig_bits - 1;
let max_msb: i32 = (1 << (exp_bits - 1)) - 1;
// The exponent of one ULP in the subnormals
let min_lsb: i32 = 1 - max_msb - sig_bits as i32;
let (neg, mut sig, exp) = parse_hex(s.as_bytes());
let exp_mask = ((1 << exp_bits) - 1) << sig_bits;
if sig == 0 {
return (neg as u128) << (bits - 1);
}
let (neg, mut sig, exp) = match parse_hex(s.as_bytes()) {
Parsed::Finite { neg, sig: 0, .. } => return (neg as u128) << (bits - 1),
Parsed::Finite { neg, sig, exp } => (neg, sig, exp),
Parsed::Infinite { neg } => return ((neg as u128) << (bits - 1)) | exp_mask,
Parsed::Nan { neg } => {
return ((neg as u128) << (bits - 1)) | exp_mask | 1 << (sig_bits - 1);
}
};
// exponents of the least and most significant bits in the value
let lsb = sig.trailing_zeros() as i32;
@ -76,11 +84,24 @@ const fn parse_any(s: &str, bits: u32, sig_bits: u32) -> u128 {
sig | ((neg as u128) << (bits - 1))
}
/// A parsed floating point number.
enum Parsed {
/// Absolute value sig * 2^e
Finite {
neg: bool,
sig: u128,
exp: i32,
},
Infinite {
neg: bool,
},
Nan {
neg: bool,
},
}
/// Parse a hexadecimal float x
/// returns (s,n,e):
/// s == x.is_sign_negative()
/// n * 2^e == x.abs()
const fn parse_hex(mut b: &[u8]) -> (bool, u128, i32) {
const fn parse_hex(mut b: &[u8]) -> Parsed {
let mut neg = false;
let mut sig: u128 = 0;
let mut exp: i32 = 0;
@ -90,6 +111,12 @@ const fn parse_hex(mut b: &[u8]) -> (bool, u128, i32) {
neg = c == b'-';
}
match *b {
[b'i' | b'I', b'n' | b'N', b'f' | b'F'] => return Parsed::Infinite { neg },
[b'n' | b'N', b'a' | b'A', b'n' | b'N'] => return Parsed::Nan { neg },
_ => (),
}
if let &[b'0', b'x' | b'X', ref rest @ ..] = b {
b = rest;
} else {
@ -152,7 +179,7 @@ const fn parse_hex(mut b: &[u8]) -> (bool, u128, i32) {
exp += pexp;
}
(neg, sig, exp)
Parsed::Finite { neg, sig, exp }
}
const fn dec_digit(c: u8) -> u8 {
@ -272,6 +299,10 @@ mod tests {
("-0x1.998p-4", (-0.1f16).to_bits()),
("0x0.123p-12", 0x0123),
("0x1p-24", 0x0001),
("nan", f16::NAN.to_bits()),
("-nan", (-f16::NAN).to_bits()),
("inf", f16::INFINITY.to_bits()),
("-inf", f16::NEG_INFINITY.to_bits()),
];
for (s, exp) in checks {
println!("parsing {s}");
@ -322,6 +353,10 @@ mod tests {
("0x1.111114p-127", 0x00444445),
("0x1.23456p-130", 0x00091a2b),
("0x1p-149", 0x00000001),
("nan", f32::NAN.to_bits()),
("-nan", (-f32::NAN).to_bits()),
("inf", f32::INFINITY.to_bits()),
("-inf", f32::NEG_INFINITY.to_bits()),
];
for (s, exp) in checks {
println!("parsing {s}");
@ -360,6 +395,10 @@ mod tests {
("0x0.8000000000001p-1022", 0x0008000000000001),
("0x0.123456789abcdp-1022", 0x000123456789abcd),
("0x0.0000000000002p-1022", 0x0000000000000002),
("nan", f64::NAN.to_bits()),
("-nan", (-f64::NAN).to_bits()),
("inf", f64::INFINITY.to_bits()),
("-inf", f64::NEG_INFINITY.to_bits()),
];
for (s, exp) in checks {
println!("parsing {s}");
@ -401,6 +440,10 @@ mod tests {
("-0x1.999999999999999999999999999ap-4", (-0.1f128).to_bits()),
("0x0.abcdef0123456789abcdef012345p-16382", 0x0000abcdef0123456789abcdef012345),
("0x1p-16494", 0x00000000000000000000000000000001),
("nan", f128::NAN.to_bits()),
("-nan", (-f128::NAN).to_bits()),
("inf", f128::INFINITY.to_bits()),
("-inf", f128::NEG_INFINITY.to_bits()),
];
for (s, exp) in checks {
println!("parsing {s}");

View file

@ -2,7 +2,7 @@
pub mod macros;
mod big;
mod float_traits;
mod hex_float;
pub mod hex_float;
mod int_traits;
#[allow(unused_imports)]