From ba30eaf203b9fd3d9ed954ff14268e069218c8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20S=C3=A1nchez=20Mu=C3=B1oz?= Date: Sat, 14 Oct 2023 13:05:31 +0200 Subject: [PATCH 1/3] Bump LLVM commit to https://github.com/llvm/llvm-project/commit/e60b91df1357e6a5f66840581f4d5f57e258c0b4 Skips through the followings commits that do not affect the Rust port: * 38818b60c58c76ba89b990978cdfd2d7b6799260: only affects C++/LLVM specific details * 85395af27241ab9c8d5763b8afcaa07f1bab26d5: reverted by next commit * e60b91df1357e6a5f66840581f4d5f57e258c0b4: reverts previous commit --- Cargo.toml | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 40560cd..4535b63 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-e60b91df1357" edition = "2021" license = "Apache-2.0 WITH LLVM-exception" diff --git a/src/lib.rs b/src/lib.rs index 5efb169..638b18e 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 From c9d707140c23905ebceb4cc5b074ac6075f9f91a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20S=C3=A1nchez=20Mu=C3=B1oz?= Date: Sat, 14 Oct 2023 13:10:06 +0200 Subject: [PATCH 2/3] Replace mix/max combo with `Ord::clamp` Bumps LLVM commit to https://github.com/llvm/llvm-project/commit/4dc08de9d2f680309cdd639169d3b8802c76ae9a, where the equivalent was done in the C++ version. --- Cargo.toml | 2 +- src/ieee.rs | 2 +- src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4535b63..a6107ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ members = ["fuzz"] [workspace.package] -version = "0.2.3+llvm-e60b91df1357" +version = "0.2.3+llvm-4dc08de9d2f6" 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 638b18e..5ea0bf6 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 From eb79bbcd9fa1178c6ed56d077d4fadd1fd55fbc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20S=C3=A1nchez=20Mu=C3=B1oz?= Date: Sat, 14 Oct 2023 13:22:24 +0200 Subject: [PATCH 3/3] Add `Float::max_int_bits` Returns the number of bits needed to represent the largest integer that a floating point type can hold. Bumps LLVM commit to https://github.com/llvm/llvm-project/commit/038f7debfda01471ce0d4eb1fed20da61e5c8b32, where this was added upstream. --- Cargo.toml | 2 +- src/lib.rs | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a6107ed..2d54628 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ members = ["fuzz"] [workspace.package] -version = "0.2.3+llvm-4dc08de9d2f6" +version = "0.2.3+llvm-038f7debfda0" edition = "2021" license = "Apache-2.0 WITH LLVM-exception" diff --git a/src/lib.rs b/src/lib.rs index 5ea0bf6..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;