berkeley-testfloat-3 icon indicating copy to clipboard operation
berkeley-testfloat-3 copied to clipboard

[FLOAT16] Extending minimal support for testfloat f16 testing to multiple f16 operations

Open nibrunieAtSi5 opened this issue 7 months ago • 1 comments

It looks like FLOAT16 was only partially available in testfloat and that generic mapping for subj_f16_add, subj_f16_sub, subj_f16_mul, and subj_f16_div were not yet available.

This patch adds such support, for now conditioned on FLOAT16 macro. This may need to be revisited as I am not sure the FLOAT16 macro was meant to indicate arithmetic support for half precision.

Support for the following operation is introduced: f16_add, f16_sub, f16_mul, f16_div, f16_to_f32, f32_to_f16, f16_to_f64, f64_to_f16, f16_eq, f16_lt, f16_le.

As half-precision arithmetic support is becoming more and more pervasive (e.g. RISC-V Zfh scalar extension or vector Zvfh extension, ARM scalar and vector support in v8 as well), it seems natural to extend testfloat to support this format fully.

⚠️ f16_to_f32 and f32_to_f16 require https://github.com/ucb-bar/berkeley-testfloat-3/pull/19 (their entries appear after BFloat16 conversions in the array subjfloat_functions and standardFunctionInfos and so they are susceptible to the bug addressed in the PR).

How this was tested

After building softfloat and testfloat on a Mac with Apple Silicon (AArch64):

for op in f16_add f16_sub f16_mul f16_div f16_to_f32 f32_to_f16 f16_to_f64 f64_to_f16 f16_eq f16_le f16_lt; do ./testfloat $op -level2; done
Testing f16_add, rounding near_even.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_add, rounding near_even.
Testing f16_add, rounding minMag.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_add, rounding minMag.
Testing f16_add, rounding min.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_add, rounding min.
Testing f16_add, rounding max.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_add, rounding max.
Testing f16_sub, rounding near_even.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_sub, rounding near_even.
Testing f16_sub, rounding minMag.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_sub, rounding minMag.
Testing f16_sub, rounding min.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_sub, rounding min.
Testing f16_sub, rounding max.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_sub, rounding max.
Testing f16_mul, rounding near_even.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_mul, rounding near_even.
Testing f16_mul, rounding minMag.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_mul, rounding minMag.
Testing f16_mul, rounding min.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_mul, rounding min.
Testing f16_mul, rounding max.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_mul, rounding max.
Testing f16_div, rounding near_even.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_div, rounding near_even.
Testing f16_div, rounding minMag.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_div, rounding minMag.
Testing f16_div, rounding min.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_div, rounding min.
Testing f16_div, rounding max.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_div, rounding max.
Testing f16_to_f32.
2448 tests total.
2448 tests performed.
In 2448 tests, no errors found in f16_to_f32.
Testing f32_to_f16, rounding near_even.
8800 tests total.
8800 tests performed.
In 8800 tests, no errors found in f32_to_f16, rounding near_even.
Testing f32_to_f16, rounding minMag.
8800 tests total.
8800 tests performed.
In 8800 tests, no errors found in f32_to_f16, rounding minMag.
Testing f32_to_f16, rounding min.
8800 tests total.
8800 tests performed.
In 8800 tests, no errors found in f32_to_f16, rounding min.
Testing f32_to_f16, rounding max.
8800 tests total.
8800 tests performed.
In 8800 tests, no errors found in f32_to_f16, rounding max.
Testing f16_to_f64.
2448 tests total.
2448 tests performed.
In 2448 tests, no errors found in f16_to_f64.
Testing f64_to_f16, rounding near_even.
26112 tests total.
26112 tests performed.
In 26112 tests, no errors found in f64_to_f16, rounding near_even.
Testing f64_to_f16, rounding minMag.
26112 tests total.
26112 tests performed.
In 26112 tests, no errors found in f64_to_f16, rounding minMag.
Testing f64_to_f16, rounding min.
26112 tests total.
26112 tests performed.
In 26112 tests, no errors found in f64_to_f16, rounding min.
Testing f64_to_f16, rounding max.
26112 tests total.
26112 tests performed.
In 26112 tests, no errors found in f64_to_f16, rounding max.
Testing f16_eq.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_eq.
Testing f16_le.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_le.
Testing f16_lt.
1254528 tests total.
1254528 tests performed.
In 1254528 tests, no errors found in f16_lt.

nibrunieAtSi5 avatar Jun 21 '25 15:06 nibrunieAtSi5

There's a general expectation that this code be valid standard C, of which __fp16 is not a part, so you need to do two things:

  • use _Float16 rather than __fp16
  • detect that the type _Float16 actually exists, the standards-compliant means of which appears to be:
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include <float.h>

#ifdef FLT16_MIN
// in here, you know that _Float16 is a valid type
#endif

Note that the __STDC_WANT_IEC_60559_TYPES_EXT__ macro needs to be defined before the first time float.h is included. Fortunately, testfloat never appears to include float.h, so just sticking the #define / #include at the top of subjfloat_config.h should suffice.

Thank you @aswaterman , 123a5f108bb657b124191004d44a5c9a41b9f7b6 should clean things up

nibrunieAtSi5 avatar Jun 22 '25 01:06 nibrunieAtSi5