Skip to content

Commit

Permalink
Add f128 float to integer conversion functions
Browse files Browse the repository at this point in the history
Add the following:

- `__fixtfsi`
- `__fixtfdi`
- `__fixtfti`
- `__fixunstfsi`
- `__fixunstfdi`
- `__fixunstfti`
  • Loading branch information
tgross35 committed May 21, 2024
1 parent 8aa675d commit 4edcd5a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 22 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ These builtins are needed to support `f16` and `f128`, which are in the process
- [x] extendhfsf2.c
- [x] extendhftf2.c
- [x] extendsftf2.c
- [ ] fixtfdi.c
- [ ] fixtfsi.c
- [ ] fixtfti.c
- [ ] fixunstfdi.c
- [ ] fixunstfsi.c
- [ ] fixunstfti.c
- [x] fixtfdi.c
- [x] fixtfsi.c
- [x] fixtfti.c
- [x] fixunstfdi.c
- [x] fixunstfsi.c
- [x] fixunstfti.c
- [ ] floatditf.c
- [ ] floatsitf.c
- [ ] floatunditf.c
Expand Down
10 changes: 0 additions & 10 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,6 @@ mod c {
if (target_arch == "aarch64" || target_arch == "arm64ec") && consider_float_intrinsics {
sources.extend(&[
("__comparetf2", "comparetf2.c"),
("__fixtfdi", "fixtfdi.c"),
("__fixtfsi", "fixtfsi.c"),
("__fixtfti", "fixtfti.c"),
("__fixunstfdi", "fixunstfdi.c"),
("__fixunstfsi", "fixunstfsi.c"),
("__fixunstfti", "fixunstfti.c"),
("__floatditf", "floatditf.c"),
("__floatsitf", "floatsitf.c"),
("__floatunditf", "floatunditf.c"),
Expand All @@ -561,9 +555,7 @@ mod c {
if target_arch == "mips64" {
sources.extend(&[
("__netf2", "comparetf2.c"),
("__fixtfsi", "fixtfsi.c"),
("__floatsitf", "floatsitf.c"),
("__fixunstfsi", "fixunstfsi.c"),
("__floatunsitf", "floatunsitf.c"),
("__fe_getround", "fp_mode.c"),
]);
Expand All @@ -572,9 +564,7 @@ mod c {
if target_arch == "loongarch64" {
sources.extend(&[
("__netf2", "comparetf2.c"),
("__fixtfsi", "fixtfsi.c"),
("__floatsitf", "floatsitf.c"),
("__fixunstfsi", "fixunstfsi.c"),
("__floatunsitf", "floatunsitf.c"),
("__fe_getround", "fp_mode.c"),
]);
Expand Down
30 changes: 30 additions & 0 deletions src/float/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ intrinsics! {
pub extern "C" fn __fixunsdfti(f: f64) -> u128 {
float_to_unsigned_int(f)
}

#[cfg(not(feature = "no-f16-f128"))]
pub extern "C" fn __fixunstfsi(f: f128) -> u32 {
float_to_unsigned_int(f)
}

#[cfg(not(feature = "no-f16-f128"))]
pub extern "C" fn __fixunstfdi(f: f128) -> u64 {
float_to_unsigned_int(f)
}

#[cfg(not(feature = "no-f16-f128"))]
pub extern "C" fn __fixunstfti(f: f128) -> u128 {
float_to_unsigned_int(f)
}
}

// Conversions from floats to signed integers.
Expand Down Expand Up @@ -294,4 +309,19 @@ intrinsics! {
pub extern "C" fn __fixdfti(f: f64) -> i128 {
float_to_signed_int(f)
}

#[cfg(not(feature = "no-f16-f128"))]
pub extern "C" fn __fixtfsi(f: f128) -> i32 {
float_to_signed_int(f)
}

#[cfg(not(feature = "no-f16-f128"))]
pub extern "C" fn __fixtfdi(f: f128) -> i64 {
float_to_signed_int(f)
}

#[cfg(not(feature = "no-f16-f128"))]
pub extern "C" fn __fixtfti(f: f128) -> i128 {
float_to_signed_int(f)
}
}
4 changes: 3 additions & 1 deletion testcrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ no-f16-f128 = ["compiler_builtins/no-f16-f128"]
mem = ["compiler_builtins/mem"]
mangled-names = ["compiler_builtins/mangled-names"]
# Skip tests that rely on f128 symbols being available on the system
no-sys-f128 = []
no-sys-f128 = ["no-sys-f128-int-convert"]
# Some platforms have some f128 functions but everything except integer conversions
no-sys-f128-int-convert = []
31 changes: 28 additions & 3 deletions testcrate/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use std::env;
use std::{collections::HashSet, env};

/// Features to enable
#[derive(Debug, PartialEq, Eq, Hash)]
enum Feature {
NoSysF128,
NoSysF128IntConvert,
}

fn main() {
let target = env::var("TARGET").unwrap();
let mut features = HashSet::new();

// These platforms do not have f128 symbols available in their system libraries, so
// skip related tests.
Expand All @@ -21,7 +29,24 @@ fn main() {
// <https://github.com/rust-lang/compiler-builtins/pull/606#issuecomment-2105657287>.
|| target.starts_with("powerpc64-")
{
println!("cargo:warning=using apfloat fallback for f128");
println!("cargo:rustc-cfg=feature=\"no-sys-f128\"");
features.insert(Feature::NoSysF128);
features.insert(Feature::NoSysF128IntConvert);
}

if target.starts_with("i586") || target.starts_with("i686") {
// 32-bit x86 seems to not have `__fixunstfti`, but does have everything else
features.insert(Feature::NoSysF128IntConvert);
}

for feature in features {
let (name, warning) = match feature {
Feature::NoSysF128 => ("no-sys-f128", "using apfloat fallback for f128"),
Feature::NoSysF128IntConvert => (
"no-sys-f128-int-convert",
"using apfloat fallback for f128 to int conversions",
),
};
println!("cargo:warning={warning}");
println!("cargo:rustc-cfg=feature=\"{name}\"");
}
}
27 changes: 25 additions & 2 deletions testcrate/tests/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod f_to_i {
};

fuzz_float(N, |x: f32| {
f_to_i!(x,
f_to_i!(x, f32, Single, all(),
u32, __fixunssfsi;
u64, __fixunssfdi;
u128, __fixunssfti;
Expand All @@ -164,7 +164,7 @@ mod f_to_i {
};

fuzz_float(N, |x: f64| {
f_to_i!(x,
f_to_i!(x, f64, Double, all(),
u32, __fixunsdfsi;
u64, __fixunsdfdi;
u128, __fixunsdfti;
Expand All @@ -174,6 +174,29 @@ mod f_to_i {
);
});
}

#[test]
#[cfg(not(feature = "no-f16-f128"))]
fn f128_to_int() {
use compiler_builtins::float::conv::{
__fixtfdi, __fixtfsi, __fixtfti, __fixunstfdi, __fixunstfsi, __fixunstfti,
};

fuzz_float(N, |x: f128| {
f_to_i!(
x,
f128,
Quad,
not(feature = "no-sys-f128-int-convert"),
u32, __fixunstfsi;
u64, __fixunstfdi;
u128, __fixunstfti;
i32, __fixtfsi;
i64, __fixtfdi;
i128, __fixtfti;
);
});
}
}

macro_rules! conv {
Expand Down

0 comments on commit 4edcd5a

Please sign in to comment.