Skip to content

Commit a7e2deb

Browse files
committed
Advance the port to llvm/llvm-project@768d6dd
(last APFloat-related LLVM commit from 2017).
1 parent 7265c11 commit a7e2deb

File tree

6 files changed

+54
-7
lines changed

6 files changed

+54
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
members = ["fuzz"]
33

44
[workspace.package]
5-
version = "0.0.0+llvm-f3598e8fca83"
5+
version = "0.0.1+llvm-768d6dd08783"
66
edition = "2021"
77
license = "Apache-2.0 WITH LLVM-exception"
88

fuzz/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ curl -sS "$llvm_project_tgz_url" | tar -C "$OUT_DIR" -xz
6868
llvm="$OUT_DIR"/llvm-project-"$llvm_project_git_hash"/llvm
6969
7070
mkdir -p "$OUT_DIR"/fake-config/llvm/Config
71-
touch "$OUT_DIR"/fake-config/llvm/Config/{abi-breaking,llvm-config}.h
71+
touch "$OUT_DIR"/fake-config/llvm/Config/{abi-breaking,config,llvm-config}.h
7272
7373
# HACK(eddyb) we want standard `assert`s to work, but `NDEBUG` also controls
7474
# unrelated LLVM facilities that are spread all over the place and it's harder
@@ -91,8 +91,8 @@ echo | clang++ -x c++ - -std=c++17 \
9191
$clang_codegen_flags \
9292
-I "$llvm"/include \
9393
-I "$OUT_DIR"/fake-config \
94-
-DNDEBUG \
95-
--include="$llvm"/lib/Support/{APInt,APFloat,SmallVector}.cpp \
94+
-DNDEBUG -DHAVE_UNISTD_H \
95+
--include="$llvm"/lib/Support/{APInt,APFloat,SmallVector,ErrorHandling}.cpp \
9696
--include="$OUT_DIR"/cxx_apf_fuzz.cpp \
9797
-c -emit-llvm -o "$OUT_DIR"/cxx_apf_fuzz.bc
9898

src/ieee.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,8 @@ impl<S: Semantics> Float for IeeeFloat<S> {
10031003
(Category::Infinity, _) | (_, Category::Zero) => Status::INVALID_OP.and(Self::NAN),
10041004

10051005
(Category::Normal, Category::Normal) => {
1006+
let orig_sign = self.sign;
1007+
10061008
while self.is_finite_non_zero()
10071009
&& rhs.is_finite_non_zero()
10081010
&& self.cmp_abs_normal(rhs) != Ordering::Less
@@ -1017,6 +1019,9 @@ impl<S: Semantics> Float for IeeeFloat<S> {
10171019
self = unpack!(status=, self - v);
10181020
assert_eq!(status, Status::OK);
10191021
}
1022+
if self.is_zero() {
1023+
self.sign = orig_sign;
1024+
}
10201025
Status::OK.and(self)
10211026
}
10221027
}
@@ -1182,8 +1187,8 @@ impl<S: Semantics> Float for IeeeFloat<S> {
11821187

11831188
// Handle special cases.
11841189
match s {
1185-
"inf" | "INFINITY" => return Ok(Status::OK.and(Self::INFINITY)),
1186-
"-inf" | "-INFINITY" => return Ok(Status::OK.and(-Self::INFINITY)),
1190+
"inf" | "INFINITY" | "+Inf" => return Ok(Status::OK.and(Self::INFINITY)),
1191+
"-inf" | "-INFINITY" | "-Inf" => return Ok(Status::OK.and(-Self::INFINITY)),
11871192
"nan" | "NaN" => return Ok(Status::OK.and(Self::NAN)),
11881193
"-nan" | "-NaN" => return Ok(Status::OK.and(-Self::NAN)),
11891194
_ => {}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Port of LLVM's APFloat software floating-point implementation from the
22
//! following C++ sources (please update commit hash when backporting):
3-
//! https://github.com/llvm/llvm-project/commit/f3598e8fca83ccfb11f58ec7957c229e349765e3
3+
//! https://github.com/llvm/llvm-project/commit/768d6dd08783440606da83dac490889329619898
44
//! * `llvm/include/llvm/ADT/APFloat.h` -> `Float` and `FloatConvert` traits
55
//! * `llvm/lib/Support/APFloat.cpp` -> `ieee` and `ppc` modules
66
//! * `llvm/unittests/ADT/APFloatTest.cpp` -> `tests` directory

src/ppc.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ where
407407
self.0.category()
408408
}
409409

410+
fn is_integer(self) -> bool {
411+
self.0.is_integer() && self.1.is_integer()
412+
}
413+
410414
fn get_exact_inverse(self) -> Option<Self> {
411415
Fallback::from(self).get_exact_inverse().map(Self::from)
412416
}
@@ -428,3 +432,13 @@ where
428432
DoubleFloat(a, b)
429433
}
430434
}
435+
436+
// HACK(eddyb) this is here instead of in `tests/ppc.rs` because `DoubleFloat`
437+
// has private fields, and it's not worth it to make them public just for testing.
438+
#[test]
439+
fn is_integer() {
440+
let double_from_f64 = |f: f64| ieee::Double::from_bits(f.to_bits().into());
441+
assert!(DoubleFloat(double_from_f64(-0.0), double_from_f64(-0.0)).is_integer());
442+
assert!(!DoubleFloat(double_from_f64(3.14159), double_from_f64(-0.0)).is_integer());
443+
assert!(!DoubleFloat(double_from_f64(-0.0), double_from_f64(3.14159)).is_integer());
444+
}

tests/ieee.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,20 @@ fn from_decimal_string() {
860860
assert_eq!(2.71828, "2.71828".parse::<Double>().unwrap().to_f64());
861861
}
862862

863+
#[test]
864+
fn from_to_string_specials() {
865+
assert_eq!("+Inf", "+Inf".parse::<Double>().unwrap().to_string());
866+
assert_eq!("+Inf", "INFINITY".parse::<Double>().unwrap().to_string());
867+
assert_eq!("+Inf", "inf".parse::<Double>().unwrap().to_string());
868+
assert_eq!("-Inf", "-Inf".parse::<Double>().unwrap().to_string());
869+
assert_eq!("-Inf", "-INFINITY".parse::<Double>().unwrap().to_string());
870+
assert_eq!("-Inf", "-inf".parse::<Double>().unwrap().to_string());
871+
assert_eq!("NaN", "NaN".parse::<Double>().unwrap().to_string());
872+
assert_eq!("NaN", "nan".parse::<Double>().unwrap().to_string());
873+
assert_eq!("NaN", "-NaN".parse::<Double>().unwrap().to_string());
874+
assert_eq!("NaN", "-nan".parse::<Double>().unwrap().to_string());
875+
}
876+
863877
#[test]
864878
fn from_hexadecimal_string() {
865879
assert_eq!(1.0, "0x1p0".parse::<Double>().unwrap().to_f64());
@@ -3190,4 +3204,18 @@ fn modulo() {
31903204
assert!(unpack!(status=, f1 % f2).is_nan());
31913205
assert_eq!(status, Status::INVALID_OP);
31923206
}
3207+
{
3208+
let f1 = "-4.0".parse::<Double>().unwrap();
3209+
let f2 = "-2.0".parse::<Double>().unwrap();
3210+
let expected = "-0.0".parse::<Double>().unwrap();
3211+
assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
3212+
assert_eq!(status, Status::OK);
3213+
}
3214+
{
3215+
let f1 = "-4.0".parse::<Double>().unwrap();
3216+
let f2 = "2.0".parse::<Double>().unwrap();
3217+
let expected = "-0.0".parse::<Double>().unwrap();
3218+
assert!(unpack!(status=, f1 % f2).bitwise_eq(expected));
3219+
assert_eq!(status, Status::OK);
3220+
}
31933221
}

0 commit comments

Comments
 (0)