Skip to content

Commit db5f678

Browse files
committed
Change float to integer tests to have an apfloat fallback
1 parent 392caf9 commit db5f678

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

testcrate/tests/conv.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,41 @@ fn int_to_float() {
9696
}
9797

9898
macro_rules! f_to_i {
99-
($x:ident, $($f:ty, $fn:ident);*;) => {
99+
($x:ident, $f_ty:ty, $apfloat_ty:ident, $sys_available:meta, $($i_ty:ty, $fn:ident);*;) => {
100100
$(
101101
// it is undefined behavior in the first place to do conversions with NaNs
102-
if !$x.is_nan() {
103-
let conv0 = $x as $f;
104-
let conv1: $f = $fn($x);
102+
if !apfloat_fallback!(
103+
$f_ty, $apfloat_ty, $sys_available, |x: FloatTy| x.is_nan() => no_convert, $x
104+
) {
105+
let conv0 = apfloat_fallback!(
106+
$f_ty, $apfloat_ty, $sys_available,
107+
// Use an `as` cast when the builtin is available on the system.
108+
|x| x as $i_ty;
109+
// When the builtin is not available, we need to use a different conversion
110+
// method (since apfloat doesn't support `as` casting).
111+
|x: $f_ty| {
112+
use compiler_builtins::int::MinInt;
113+
114+
let apf = FloatTy::from_bits(x.to_bits().into());
115+
let bits: usize = <$i_ty>::BITS.try_into().unwrap();
116+
117+
let err_fn = || panic!(
118+
"Unable to convert value {x:?} to type {}:", stringify!($i_ty)
119+
);
120+
121+
if <$i_ty>::SIGNED {
122+
<$i_ty>::try_from(apf.to_i128(bits).value).ok().unwrap_or_else(err_fn)
123+
} else {
124+
<$i_ty>::try_from(apf.to_u128(bits).value).ok().unwrap_or_else(err_fn)
125+
}
126+
},
127+
$x
128+
);
129+
130+
// $x as $i_ty;
131+
let conv1: $i_ty = $fn($x);
105132
if conv0 != conv1 {
106-
panic!("{}({}): std: {}, builtins: {}", stringify!($fn), $x, conv0, conv1);
133+
panic!("{}({:?}): std: {:?}, builtins: {:?}", stringify!($fn), $x, conv0, conv1);
107134
}
108135
}
109136
)*
@@ -120,7 +147,7 @@ fn float_to_int() {
120147
};
121148

122149
fuzz_float(N, |x: f32| {
123-
f_to_i!(x,
150+
f_to_i!(x, f32, Single, all(),
124151
u32, __fixunssfsi;
125152
u64, __fixunssfdi;
126153
u128, __fixunssfti;
@@ -130,7 +157,7 @@ fn float_to_int() {
130157
);
131158
});
132159
fuzz_float(N, |x: f64| {
133-
f_to_i!(x,
160+
f_to_i!(x, f64, Double, all(),
134161
u32, __fixunsdfsi;
135162
u64, __fixunsdfdi;
136163
u128, __fixunsdfti;

0 commit comments

Comments
 (0)