Skip to content

Commit c6f95d7

Browse files
committed
Update float conversion tests
Since there are more platforms that do not have symbols present, we need to use `rustc_apfloat` for more conversion tests. Make use of the fallback like other tests, and refactor so each test gets its own function. Previously we were testing both apfloat and system conversion methods when possible. This changes to only test one or the other, depending on whether or not the system version is available. This seems reasonable because it is consistent with all other tests, but we should consider updating all tests to check both at some point. This also includes an adjustment of PowerPC configuration to account for the linking errors at [1]. [1]: #655
1 parent 614f96b commit c6f95d7

File tree

5 files changed

+100
-176
lines changed

5 files changed

+100
-176
lines changed

testcrate/benches/float_extend.rs

+11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ float_bench! {
8282
asm: [],
8383
}
8484

85+
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
8586
criterion_group!(
8687
float_extend,
8788
extend_f16_f32,
@@ -90,4 +91,14 @@ criterion_group!(
9091
extend_f32_f128,
9192
extend_f64_f128,
9293
);
94+
95+
// FIXME(#655): `f16` tests disabled until we can bootstrap symbols
96+
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
97+
criterion_group!(
98+
float_extend,
99+
extend_f32_f64,
100+
extend_f32_f128,
101+
extend_f64_f128,
102+
);
103+
93104
criterion_main!(float_extend);

testcrate/benches/float_trunc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ float_bench! {
115115
asm: [],
116116
}
117117

118+
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
118119
criterion_group!(
119120
float_trunc,
120121
trunc_f32_f16,
@@ -124,4 +125,9 @@ criterion_group!(
124125
trunc_f128_f32,
125126
trunc_f128_f64,
126127
);
128+
129+
// FIXME(#655): `f16` tests disabled until we can bootstrap symbols
130+
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
131+
criterion_group!(float_trunc, trunc_f64_f32, trunc_f128_f32, trunc_f128_f64,);
132+
127133
criterion_main!(float_trunc);

testcrate/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ fn main() {
5151
|| target.starts_with("powerpc-")
5252
|| target.starts_with("powerpc64-")
5353
|| target.starts_with("powerpc64le-")
54+
|| target.starts_with("i586-")
5455
|| target.contains("windows-")
5556
// Linking says "error: function signature mismatch: __extendhfsf2" and seems to
5657
// think the signature is either `(i32) -> f32` or `(f32) -> f32`

testcrate/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ macro_rules! apfloat_fallback {
282282
// The expression to run. This expression may use `FloatTy` for its signature.
283283
// Optionally, the final conversion back to a float can be suppressed using
284284
// `=> no_convert` (for e.g. operations that return a bool).
285+
//
286+
// If the apfloat needs a different operation, it can be provided here.
285287
$op:expr $(=> $convert:ident)? $(; $apfloat_op:expr)?,
286288
// Arguments that get passed to `$op` after converting to a float
287289
$($arg:expr),+
@@ -318,15 +320,15 @@ macro_rules! apfloat_fallback {
318320

319321
// Some apfloat operations return a `StatusAnd` that we need to extract the value from. This
320322
// is the default.
321-
(@inner fty: $float_ty:ty, op_res: $val:expr, args: $($_arg:expr),+) => {{
323+
(@inner fty: $float_ty:ty, op_res: $val:expr, args: $($_arg:expr),+) => {{
322324
// ignore the status, just get the value
323325
let unwrapped = $val.value;
324326

325327
<$float_ty>::from_bits(FloatTy::to_bits(unwrapped).try_into().unwrap())
326328
}};
327329

328330
// This is the case where we can't use the same expression for the default builtin and
329-
// nonstandard apfloat fallbac (e.g. `as` casts in std are normal functions in apfloat, so
331+
// nonstandard apfloat fallback (e.g. `as` casts in std are normal functions in apfloat, so
330332
// two separate expressions must be specified.
331333
(@inner
332334
fty: $float_ty:ty, op_res: $_val:expr,

testcrate/tests/conv.rs

+78-174
Original file line numberDiff line numberDiff line change
@@ -206,212 +206,116 @@ mod f_to_i {
206206
}
207207
}
208208

209-
macro_rules! conv {
210-
($fX:ident, $fD:ident, $fn:ident, $apfloatX:ident, $apfloatD:ident) => {
211-
fuzz_float(N, |x: $fX| {
212-
let tmp0: $apfloatD = $apfloatX::from_bits(x.to_bits().into())
213-
.convert(&mut false)
214-
.value;
215-
let tmp0 = $fD::from_bits(tmp0.to_bits().try_into().unwrap());
216-
let tmp1: $fD = $fn(x);
217-
if !Float::eq_repr(tmp0, tmp1) {
218-
panic!(
219-
"{}({x:?}): apfloat: {tmp0:?}, builtins: {tmp1:?}",
220-
stringify!($fn)
221-
);
222-
}
223-
})
224-
};
225-
}
226-
227-
macro_rules! extend {
228-
($fX:ident, $fD:ident, $fn:ident) => {
209+
macro_rules! f_to_f {
210+
(
211+
$mod:ident,
212+
$(
213+
$from_ty:ty => $to_ty:ty,
214+
$from_ap_ty:ident => $to_ap_ty:ident,
215+
$fn:ident, $sys_available:meta
216+
);+;
217+
) => {$(
229218
#[test]
230219
fn $fn() {
231-
use compiler_builtins::float::extend::$fn;
220+
use compiler_builtins::float::{$mod::$fn, Float};
221+
use rustc_apfloat::ieee::{$from_ap_ty, $to_ap_ty};
222+
223+
fuzz_float(N, |x: $from_ty| {
224+
let tmp0: $to_ty = apfloat_fallback!(
225+
$from_ty,
226+
$from_ap_ty,
227+
$sys_available,
228+
|x: $from_ty| x as $to_ty;
229+
|x: $from_ty| {
230+
let from_apf = FloatTy::from_bits(x.to_bits().into());
231+
// Get `value` directly to ignore INVALID_OP
232+
let to_apf: $to_ap_ty = from_apf.convert(&mut false).value;
233+
<$to_ty>::from_bits(to_apf.to_bits().try_into().unwrap())
234+
},
235+
x
236+
);
237+
let tmp1: $to_ty = $fn(x);
232238

233-
fuzz_float(N, |x: $fX| {
234-
let tmp0 = x as $fD;
235-
let tmp1: $fD = $fn(x);
236239
if !Float::eq_repr(tmp0, tmp1) {
237240
panic!(
238-
"{}({}): std: {}, builtins: {}",
241+
"{}({:?}): std: {:?}, builtins: {:?}",
239242
stringify!($fn),
240243
x,
241244
tmp0,
242245
tmp1
243246
);
244247
}
245-
});
248+
})
246249
}
247-
};
250+
)+};
248251
}
249252

250-
// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
251-
#[cfg(not(target_arch = "powerpc64"))]
252-
mod float_extend {
253+
mod extend {
253254
use super::*;
254255

255-
extend!(f32, f64, __extendsfdf2);
256-
257-
#[test]
258-
fn conv() {
259-
use compiler_builtins::float::extend::__extendsfdf2;
260-
use rustc_apfloat::ieee::{Double, Single};
261-
262-
conv!(f32, f64, __extendsfdf2, Single, Double);
256+
f_to_f! {
257+
extend,
258+
f32 => f64, Single => Double, __extendsfdf2, all();
263259
}
264-
}
265-
266-
#[cfg(not(feature = "no-f16-f128"))]
267-
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
268-
mod float_extend_f128 {
269-
use super::*;
270-
271-
#[test]
272-
fn conv() {
273-
use compiler_builtins::float::extend::{
274-
__extenddftf2, __extendhfsf2, __extendhftf2, __extendsftf2, __gnu_h2f_ieee,
275-
};
276-
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
277260

278-
// FIXME(f16_f128): Also do extend!() for `f16` and `f128` when builtins are in nightly
279-
conv!(f16, f32, __extendhfsf2, Half, Single);
280-
conv!(f16, f32, __gnu_h2f_ieee, Half, Single);
281-
conv!(f16, f128, __extendhftf2, Half, Quad);
282-
conv!(f32, f128, __extendsftf2, Single, Quad);
283-
conv!(f64, f128, __extenddftf2, Double, Quad);
261+
#[cfg(target_arch = "arm")]
262+
f_to_f! {
263+
extend,
264+
f32 => f64, Single => Double, __extendsfdf2vfp, all();
284265
}
285-
}
286266

287-
#[cfg(not(feature = "no-f16-f128"))]
288-
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
289-
mod float_extend_f128_ppc {
290-
use super::*;
291-
292-
#[test]
293-
fn conv() {
294-
use compiler_builtins::float::extend::{
295-
__extenddfkf2, __extendhfkf2, __extendhfsf2, __extendsfkf2, __gnu_h2f_ieee,
296-
};
297-
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
298-
299-
// FIXME(f16_f128): Also do extend!() for `f16` and `f128` when builtins are in nightly
300-
conv!(f16, f32, __extendhfsf2, Half, Single);
301-
conv!(f16, f32, __gnu_h2f_ieee, Half, Single);
302-
conv!(f16, f128, __extendhfkf2, Half, Quad);
303-
conv!(f32, f128, __extendsfkf2, Single, Quad);
304-
conv!(f64, f128, __extenddfkf2, Double, Quad);
267+
#[cfg(not(feature = "no-f16-f128"))]
268+
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
269+
f_to_f! {
270+
extend,
271+
f16 => f32, Half => Single, __extendhfsf2, not(feature = "no-sys-f16");
272+
f16 => f32, Half => Single, __gnu_h2f_ieee, not(feature = "no-sys-f16");
273+
f16 => f128, Half => Quad, __extendhftf2, not(feature = "no-sys-f16-f128-convert");
274+
f32 => f128, Single => Quad, __extendsftf2, not(feature = "no-sys-f128");
275+
f64 => f128, Double => Quad, __extenddftf2, not(feature = "no-sys-f128");
305276
}
306-
}
307-
308-
#[cfg(target_arch = "arm")]
309-
mod float_extend_arm {
310-
use super::*;
311277

312-
extend!(f32, f64, __extendsfdf2vfp);
313-
314-
#[test]
315-
fn conv() {
316-
use compiler_builtins::float::extend::__extendsfdf2vfp;
317-
use rustc_apfloat::ieee::{Double, Single};
318-
319-
conv!(f32, f64, __extendsfdf2vfp, Single, Double);
278+
#[cfg(not(feature = "no-f16-f128"))]
279+
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
280+
f_to_f! {
281+
extend,
282+
// FIXME(#655): `f16` tests disabled until we can bootstrap symbols
283+
f32 => f128, Single => Quad, __extendsfkf2, not(feature = "no-sys-f128");
284+
f64 => f128, Double => Quad, __extenddfkf2, not(feature = "no-sys-f128");
320285
}
321286
}
322287

323-
macro_rules! trunc {
324-
($fX:ident, $fD:ident, $fn:ident) => {
325-
#[test]
326-
fn $fn() {
327-
use compiler_builtins::float::trunc::$fn;
328-
329-
fuzz_float(N, |x: $fX| {
330-
let tmp0 = x as $fD;
331-
let tmp1: $fD = $fn(x);
332-
if !Float::eq_repr(tmp0, tmp1) {
333-
panic!(
334-
"{}({}): std: {}, builtins: {}",
335-
stringify!($fn),
336-
x,
337-
tmp0,
338-
tmp1
339-
);
340-
}
341-
});
342-
}
343-
};
344-
}
345-
346-
// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
347-
#[cfg(not(target_arch = "powerpc64"))]
348-
mod float_trunc {
288+
mod trunc {
349289
use super::*;
350290

351-
trunc!(f64, f32, __truncdfsf2);
352-
353-
#[test]
354-
fn conv() {
355-
use compiler_builtins::float::trunc::__truncdfsf2;
356-
use rustc_apfloat::ieee::{Double, Single};
357-
358-
conv!(f64, f32, __truncdfsf2, Double, Single);
291+
f_to_f! {
292+
trunc,
293+
f64 => f32, Double => Single, __truncdfsf2, all();
359294
}
360-
}
361295

362-
#[cfg(not(feature = "no-f16-f128"))]
363-
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
364-
mod float_trunc_f128 {
365-
use super::*;
366-
367-
#[test]
368-
fn conv() {
369-
use compiler_builtins::float::trunc::{__gnu_f2h_ieee, __truncdfhf2, __truncsfhf2};
370-
use compiler_builtins::float::trunc::{__trunctfdf2, __trunctfhf2, __trunctfsf2};
371-
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
372-
373-
// FIXME(f16_f128): Also do trunc!() for `f16` and `f128` when builtins are in nightly
374-
conv!(f32, f16, __truncsfhf2, Single, Half);
375-
conv!(f32, f16, __gnu_f2h_ieee, Single, Half);
376-
conv!(f64, f16, __truncdfhf2, Double, Half);
377-
conv!(f128, f16, __trunctfhf2, Quad, Half);
378-
conv!(f128, f32, __trunctfsf2, Quad, Single);
379-
conv!(f128, f64, __trunctfdf2, Quad, Double);
296+
#[cfg(target_arch = "arm")]
297+
f_to_f! {
298+
trunc,
299+
f64 => f32, Double => Single, __truncdfsf2vfp, all();
380300
}
381-
}
382-
383-
#[cfg(not(feature = "no-f16-f128"))]
384-
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
385-
mod float_trunc_f128_ppc {
386-
use super::*;
387-
388-
#[test]
389-
fn conv() {
390-
use compiler_builtins::float::trunc::{__gnu_f2h_ieee, __truncdfhf2, __truncsfhf2};
391-
use compiler_builtins::float::trunc::{__trunckfdf2, __trunckfhf2, __trunckfsf2};
392-
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
393301

394-
// FIXME(f16_f128): Also do trunc!() for `f16` and `f128` when builtins are in nightly
395-
conv!(f32, f16, __truncsfhf2, Single, Half);
396-
conv!(f32, f16, __gnu_f2h_ieee, Single, Half);
397-
conv!(f64, f16, __truncdfhf2, Double, Half);
398-
conv!(f128, f16, __trunckfhf2, Quad, Half);
399-
conv!(f128, f32, __trunckfsf2, Quad, Single);
400-
conv!(f128, f64, __trunckfdf2, Quad, Double);
302+
#[cfg(not(feature = "no-f16-f128"))]
303+
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
304+
f_to_f! {
305+
trunc,
306+
f32 => f16, Single => Half, __truncsfhf2, not(feature = "no-sys-f16");
307+
f32 => f16, Single => Half, __gnu_f2h_ieee, not(feature = "no-sys-f16");
308+
f128 => f16, Quad => Half, __trunctfhf2, not(feature = "no-sys-f16-f128-convert");
309+
f128 => f32, Quad => Single, __trunctfsf2, not(feature = "no-sys-f128");
310+
f128 => f64, Quad => Double, __trunctfdf2, not(feature = "no-sys-f128");
401311
}
402-
}
403-
404-
#[cfg(target_arch = "arm")]
405-
mod float_trunc_arm {
406-
use super::*;
407312

408-
trunc!(f64, f32, __truncdfsf2vfp);
409-
410-
#[test]
411-
fn conv() {
412-
use compiler_builtins::float::trunc::__truncdfsf2vfp;
413-
use rustc_apfloat::ieee::{Double, Single};
414-
415-
conv!(f64, f32, __truncdfsf2vfp, Double, Single)
313+
#[cfg(not(feature = "no-f16-f128"))]
314+
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
315+
f_to_f! {
316+
trunc,
317+
// FIXME(#655): `f16` tests disabled until we can bootstrap symbols
318+
f128 => f32, Quad => Single, __trunckfsf2, not(feature = "no-sys-f128");
319+
f128 => f64, Quad => Double, __trunckfdf2, not(feature = "no-sys-f128");
416320
}
417321
}

0 commit comments

Comments
 (0)