diff --git a/Cargo.toml b/Cargo.toml index 40560cd..2d54628 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ members = ["fuzz"] [workspace.package] -version = "0.2.3+llvm-462a31f5a5ab" +version = "0.2.3+llvm-038f7debfda0" edition = "2021" license = "Apache-2.0 WITH LLVM-exception" diff --git a/src/ieee.rs b/src/ieee.rs index 9b8c5a0..6b38bbc 100644 --- a/src/ieee.rs +++ b/src/ieee.rs @@ -1861,7 +1861,7 @@ impl Float for IeeeFloat { let max_change = S::MAX_EXP as i32 - (S::MIN_EXP as i32 - sig_bits) + 1; // Clamp to one past the range ends to let normalize handle overflow. - let exp_change = cmp::min(cmp::max(exp as i32, -max_change - 1), max_change); + let exp_change = (exp as i32).clamp(-max_change - 1, max_change); self.exp = self.exp.saturating_add(exp_change as ExpInt); self = self.normalize(round, Loss::ExactlyZero).value; if self.is_nan() { diff --git a/src/lib.rs b/src/lib.rs index 5efb169..0b0cd65 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ //! Port of LLVM's APFloat software floating-point implementation from the //! following C++ sources (please update commit hash when backporting): -//! +//! //! * `llvm/include/llvm/ADT/APFloat.h` -> `Float` and `FloatConvert` traits //! * `llvm/lib/Support/APFloat.cpp` -> `ieee` and `ppc` modules //! * `llvm/unittests/ADT/APFloatTest.cpp` -> `tests` directory @@ -275,6 +275,16 @@ pub trait Float: // FIXME(eddyb) provide a default when qnan becomes const fn. const NAN: Self; + /// Number of bits needed to represent the largest integer that + /// the floating point type can hold. + // FIXME should be const fn. + fn max_int_bits(signed: bool) -> usize { + // The max FP value is pow(2, MaxExponent) * (1 + MaxFraction), so we need + // at least one more bit than the MaxExponent to hold the max FP value. + // Another extra sign bit is needed for signed integers. + Self::MAX_EXP as usize + 1 + (signed as usize) + } + /// Factory for QNaN values. // FIXME(eddyb) should be const fn. fn qnan(payload: Option) -> Self;