refactor float-to-float conversions and fix AHP
v2 cover refactor float-to-float conversions and fix AHP
Hi,
This is a more polished version of the re-factoring of the softfloat fcvt code. I've split apart the fixes for ARM alternative half-precision format for easier review. Rather than rely on some questionable hacks it introduces a new FloatFmt to allow cleaner handling of the differences in the common code. If there are other alternative floating point formats they can follow a similar approach.
I've included the test case for reference although I expect it to be merged with my tcg-testing revival code which has the rest of the build and test machinery for the test case.
checkpatch dumps a bunch of false-positives as it doesn't like scientific notation for floats or inline assembler used in the test case.
Cheers,
Alex Bennée (3):
fpu/softfloat: re-factor float to float conversions fpu/softfloat: support ARM Alternative half-precision tests/tcg/aarch64: add fcvt test cases for AArch64 (!UPSTREAM)
fpu/softfloat-specialize.h | 40 - fpu/softfloat.c | 524 +++------ include/fpu/softfloat.h | 8 +- tests/tcg/aarch64/fcvt.c | 296 +++++ tests/tcg/aarch64/fcvt.ref | 2138 ++++++++++++++++++++++++++++++++++++ 5 files changed, 2588 insertions(+), 418 deletions(-) create mode 100644 tests/tcg/aarch64/fcvt.c create mode 100644 tests/tcg/aarch64/fcvt.ref
Signature · 1 line
2.17.0
PATCH v2 1/3 fpu/softfloat: re-factor float to float conversions
This allows us to delete a lot of additional boilerplate code which is no longer needed. Currently the ieee flag is ignored (everything is assumed to be ieee). Handling for ARM AHP will be in the next patch.
---
v2
- pass FloatFmt to float_to_float instead of sizes
- split AHP handling to another patch
- use rth's suggested re-packing (+ setting .exp)
--- fpu/softfloat-specialize.h | 40 ---- fpu/softfloat.c | 443 +++++++------------------------------ include/fpu/softfloat.h | 8 +- 3 files changed, 88 insertions(+), 403 deletions(-)
fpu/softfloat-specialize.h +0 -40
@@ -293,46 +293,6 @@ float16 float16_maybe_silence_nan(float16 a_, float_status *status)return a_; } -/*---------------------------------------------------------------------------- -| Returns the result of converting the half-precision floating-point NaN -| `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid -| exception is raised. -*----------------------------------------------------------------------------*/ - -static commonNaNT float16ToCommonNaN (float16 a,float_status *status) -{ -commonNaNT z; - -if (float16_is_signaling_nan (a, status)) { -float_raise (float_flag_invalid, status); - } - z.sign =float16_val (a) >>15 ; - z.low =0 ; - z.high = ((uint64_t )float16_val (a)) <<54 ; -return z; -} - -/*---------------------------------------------------------------------------- -| Returns the result of converting the canonical NaN `a' to the half- -| precision floating-point format. -*----------------------------------------------------------------------------*/ - -static float16 commonNaNToFloat16 (commonNaNT a,float_status *status) -{ -uint16_t mantissa = a.high >>54 ; - -if (status->default_nan_mode) { -return float16_default_nan (status); - } - -if (mantissa) { -return make_float16 (((((uint16_t ) a.sign) <<15 ) - | (0x1F <<10 ) | mantissa)); - }else { -return float16_default_nan (status); - } -} -#ifdef NO_SIGNALING_NANS int float32_is_quiet_nan (float32 a_,float_status *status) {
fpu/softfloat.c +84 -359
467 lines not on this page · raw message
include/fpu/softfloat.h +4 -4
@@ -211,10 +211,10 @@ float128 uint64_to_float128(uint64_t, float_status *status);/*---------------------------------------------------------------------------- | Software half-precision conversion routines. *----------------------------------------------------------------------------*/ -float16 float32_to_float16 (float32 ,flag ,float_status *status); -float32 float16_to_float32 (float16 ,flag ,float_status *status); -float16 float64_to_float16 (float64 a,flag ieee,float_status *status); -float64 float16_to_float64 (float16 a,flag ieee,float_status *status); +float16 float32_to_float16 (float32 ,bool ieee,float_status *status); +float32 float16_to_float32 (float16 ,bool ieee,float_status *status); +float16 float64_to_float16 (float64 a,bool ieee,float_status *status); +float64 float16_to_float64 (float16 a,bool ieee,float_status *status);int16_t float16_to_int16 (float16 ,float_status *status);uint16_t float16_to_uint16 (float16 a,float_status *status);int16_t float16_to_int16_round_to_zero (float16 ,float_status *status);
Signature · 1 line
2.17.0
Re: [PATCH v2 1/3] fpu/softfloat: re-factor float to float conversions
fpu/softfloat.c float64 float64_div(float64 a, float64 b, float_status *status) · line 1214
Quoted 3 lines · Alex Bennée
fpu/softfloat.c +3
-0+if (is_nan (a.cls)) { + +if (is_snan (a.cls)) {
Watch your whitespace.
r~
PATCH v2 2/3 fpu/softfloat: support ARM Alternative half-precision
In reply to Alex BennéeFor float16 ARM supports an alternative half-precision format which sacrifices the ability to represent NaN/Inf in return for a higher dynamic range. To support this I've added an additional FloatFmt (float16_params_ahp).
The new FloatFmt flag (arm_althp) is then used to modify the behaviour of canonicalize and round_canonical with respect to representation and exception raising.
Finally the float16_to_floatN and floatN_to_float16 conversion routines select the new alternative FloatFmt when !ieee.
--- fpu/softfloat.c | 97 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 74 insertions(+), 23 deletions(-)
fpu/softfloat.c +74 -23
@@ -234,6 +234,8 @@ typedef struct {* frac_lsb: least significant bit of fraction * fram_lsbm1: the bit bellow the least significant bit (for rounding) * round_mask/roundeven_mask: masks used for rounding +* The following optional modifiers are available: +* arm_althp: handle ARM Alternative Half Precision */ typedef struct { int exp_size;
@@ -245,6 +247,7 @@ typedef struct {uint64_t frac_lsbm1;uint64_t round_mask;uint64_t roundeven_mask; +bool arm_althp; } FloatFmt;/* Expand fields based on the size of exponent and fraction */
@@ -257,12 +260,17 @@ typedef struct { .frac_lsb = 1ull << (DECOMPOSED_BINARY_POINT - F), \ .frac_lsbm1 = 1ull << ((DECOMPOSED_BINARY_POINT - F) - 1), \ .round_mask = (1ull << (DECOMPOSED_BINARY_POINT - F)) - 1, \ - .roundeven_mask = (2ull << (DECOMPOSED_BINARY_POINT - F)) - 1 + .roundeven_mask = (2ull << (DECOMPOSED_BINARY_POINT -F )) -1 , static const FloatFmt float16_params = {FLOAT_PARAMS (5 ,10 ) }; +static const FloatFmt float16_params_ahp = { +FLOAT_PARAMS (5 ,10 ) + .arm_althp = true +}; +static const FloatFmt float32_params = {FLOAT_PARAMS (8 ,23 ) };
@@ -326,7 +334,7 @@ static inline float64 float64_pack_raw(FloatParts p)static FloatParts canonicalize (FloatParts part,const FloatFmt *parm,float_status *status) { -if (part.exp == parm->exp_max) { +if (part.exp == parm->exp_max && !parm->arm_althp) {if (part.frac ==0 ) { part.cls = float_class_inf; }else {
@@ -412,8 +420,9 @@ static FloatParts round_canonical(FloatParts p, float_status *s, exp += parm->exp_bias;if (likely (exp >0 )) { +bool maybe_inexact = false;if (frac & round_mask) { - flags |= float_flag_inexact; + maybe_inexact = true; frac += inc;if (frac &DECOMPOSED_OVERFLOW_BIT ) { frac >>=1 ;
@@ -422,14 +431,26 @@ static FloatParts round_canonical(FloatParts p, float_status *s, } frac >>= frac_shift; -if (unlikely (exp >= exp_max)) { - flags |= float_flag_overflow | float_flag_inexact; -if (overflow_norm) { - exp = exp_max -1 ; - frac =-1 ; - }else { - p.cls = float_class_inf; - goto do_inf; +if (parm->arm_althp) { +if (unlikely (exp >= exp_max +1 )) { + flags |= float_flag_invalid; + frac =-1 ; + exp = exp_max; + }else if (maybe_inexact) { + flags |= float_flag_inexact; + } + }else { +if (unlikely (exp >= exp_max)) { + flags |= float_flag_overflow | float_flag_inexact; +if (overflow_norm) { + exp = exp_max -1 ; + frac =-1 ; + }else { + p.cls = float_class_inf; + goto do_inf; + } + }else if (maybe_inexact) { + flags |= float_flag_inexact; } } }else if (s->flush_to_zero) {
@@ -474,7 +495,13 @@ static FloatParts round_canonical(FloatParts p, float_status *s,case float_class_inf: do_inf: exp = exp_max; - frac =0 ; +if (parm->arm_althp) { + flags |= float_flag_invalid; +/* Alt HP returns result = sign:Ones(M-1) */ + frac =-1 ; + }else { + frac =0 ; + }break ;case float_class_qnan:
@@ -492,12 +519,21 @@ static FloatParts round_canonical(FloatParts p, float_status *s,return p; } +/* Explicit FloatFmt version */ +static FloatParts float16a_unpack_canonical (const FloatFmt *params, +float16 f,float_status *s) +{ +return canonicalize (float16_unpack_raw (f), params, s); +} +static FloatParts float16_unpack_canonical (float16 f,float_status *s) { -return canonicalize (float16_unpack_raw (f), &float16_params, s); +return float16a_unpack_canonical (&float16_params, f, s); } -static float16 float16_round_pack_canonical (FloatParts p,float_status *s) + +static float16 float16a_round_pack_canonical (const FloatFmt *params, +FloatParts p,float_status *s) {switch (p.cls) {case float_class_dnan:
@@ -505,11 +541,16 @@ static float16 float16_round_pack_canonical(FloatParts p, float_status *s)case float_class_msnan:return float16_maybe_silence_nan (float16_pack_raw (p), s);default : - p =round_canonical (p, s, &float16_params); + p =round_canonical (p, s, params);return float16_pack_raw (p); } } +static float16 float16_round_pack_canonical (FloatParts p,float_status *s) +{ +return float16a_round_pack_canonical (&float16_params, p, s); +} +static FloatParts float32_unpack_canonical (float32 f,float_status *s) {return canonicalize (float32_unpack_raw (f), &float32_params, s);
@@ -1235,25 +1276,34 @@ static FloatParts float_to_float(FloatParts a,return a; } +/* +* Currently non-ieee implies ARM Alternative Half Precision handling +* for float16 values. If more are needed we'll need to expand the API +* into softfloat. +*/ +float32 float16_to_float32 (float16 a,bool ieee,float_status *s) { -FloatParts p =float16_unpack_canonical (a, s); -FloatParts pr =float_to_float (p, &float16_params, &float32_params, s); +const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp; +FloatParts p =float16a_unpack_canonical (fmt16, a, s); +FloatParts pr =float_to_float (p, fmt16, &float32_params, s);return float32_round_pack_canonical (pr, s); }float64 float16_to_float64 (float16 a,bool ieee,float_status *s) { -FloatParts p =float16_unpack_canonical (a, s); -FloatParts pr =float_to_float (p, &float16_params, &float64_params, s); +const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp; +FloatParts p =float16a_unpack_canonical (fmt16, a, s); +FloatParts pr =float_to_float (p, fmt16, &float64_params, s);return float64_round_pack_canonical (pr, s); }float16 float32_to_float16 (float32 a,bool ieee,float_status *s) { +const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;FloatParts p =float32_unpack_canonical (a, s); -FloatParts pr =float_to_float (p, &float32_params, &float16_params, s); -return float16_round_pack_canonical (pr, s); +FloatParts pr =float_to_float (p, &float32_params, fmt16, s); +return float16a_round_pack_canonical (fmt16, pr, s); }float64 float32_to_float64 (float32 a,float_status *s)
@@ -1265,9 +1315,10 @@ float64 float32_to_float64(float32 a, float_status *s)float16 float64_to_float16 (float64 a,bool ieee,float_status *s) { +const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;FloatParts p =float64_unpack_canonical (a, s); -FloatParts pr =float_to_float (p, &float64_params, &float16_params, s); -return float16_round_pack_canonical (pr, s); +FloatParts pr =float_to_float (p, &float64_params, fmt16, s); +return float16a_round_pack_canonical (fmt16, pr, s); }float32 float64_to_float32 (float64 a,float_status *s)
Signature · 1 line
2.17.0
Re: [PATCH v2 2/3] fpu/softfloat: support ARM Alternative half-precision
Quoted 9 lines · Alex Bennée
For float16 ARM supports an alternative half-precision format which sacrifices the ability to represent NaN/Inf in return for a higher dynamic range. To support this I've added an additional FloatFmt (
float16_params_ahp).The new FloatFmt flag (
arm_althp) is then used to modify the behaviour of canonicalize andround_canonicalwith respect to representation and exception raising.
Quoted 7 lines
Finally the
float16_to_floatNandfloatN_to_float16conversion routines select the new alternative FloatFmt when !ieee.Signed-off-by Alex Bennée <[email protected]>--- fpu/softfloat.c | 97 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 74 insertions(+), 23 deletions(-)
I found some corner cases where this patchset introduces regressions; details below... They're not all althp related but I started composing this email in reply to patch 2/3 and don't want to try to move it to replying to the cover letter now :-)
(1) Here's an example of a wrong 32->16 conversion in alt-hp mode:
for FCVT h5, s0 where s0 is an SNaN, then your code gives 0x7E00 when it should give 0x0, because float16a_round_pack_canonical() tries to return a NaN, which doesn't exist in alt-HP.
In the Arm ARM pseudocode this case is handled by the FPConvert pseudocode, which treats alt_hp specially when the input is a NaN. On that analogy I put this into float_to_float(), which seems to have the desired effect:
fpu/softfloat.c +11 -0
@@ -1256,6 +1256,17 @@ static FloatParts float_to_float(FloatParts a, s->float_exception_flags |= float_flag_invalid; } +if (dstf->arm_althp) { +/* There is no NaN in the destination format: raise Invalid +* and return a zero with the sign of the input NaN. +*/ + s->float_exception_flags |= float_flag_invalid; + a.cls = float_class_zero; + a.frac =0 ; + a.exp =0 ; +return a; + } +if (s->default_nan_mode) { a.cls = float_class_dnan;return a;
(2) You're also failing to set the Inexact flag for cases like
fcvt h1, s0 where s0 is 0x33000000
which should return result of 0, flags Inexact | Underflow, but is after this patchset returning just Underflow.
I think this is because you're not dealing with the odd handling of flush-to-zero for half-precision:
in the v8A Arm ARM pseudocode, float-to-float conversion uses the FPRoundCV() function, which squashes FZ16 to 0, and FPRoundBase() looks at fpcr.FZ for 32 and 64 bit floats but fpcr.FZ16 for 16 bit floats. (FZ16 exists only with the halfprec extension, and effectively applies only for the data processing and int<->fp conversions -- see FPRound().)
In our old code we handled this implicitly by having roundAndPackFloat16() not check status->flush_to_zero the way that roundAndPackFloat32/64 did. Now you've combined them all into one code path you need to do some special casing, I think. This change fixes things for the fcvt case, but (a) is a dubious hack and (b) you'll want to do something different to handle FZ16 for actual arithmetic operations on halfprec. If architectures other than Arm use halfprec (MIPS seems to) then we should check what their semantics are to see if they match Arm.
fpu/softfloat.c +0 -0
@@ -453,7 +453,7 @@ static FloatParts round_canonical(FloatParts p,
float_status *s,
flags |= float_flag_inexact;
}
}
- } else if (s->flush_to_zero) {
+ } else if (s->flush_to_zero && parm->exp_size != 5) {
flags |= float_flag_output_denormal;
p.cls = float_class_zero;
goto do_zero;(3) Here's a NaN case we get wrong now: 64 to IEEE-16 conversion, input is 0x7ff0000000000001 (an SNaN), we produce 0x7c00 (infinity) but should produce 0x7e00 (a QNaN).
This is because this code in float16a_round_pack_canonical():
case float_class_msnan:
return float16_maybe_silence_nan(float16_pack_raw(p), s);doesn't consider the possibility that float16_pack_raw() ends up with something that's not a NaN. In this case because the float-to-float conversion has thrown away the bottom bits of the double's mantissa, we have p.frac == 0, and float16_pack_raw() gives 0x7c00, which is an infinity, not a NaN. So when float16_maybe_silence_nan() calls float16_is_signaling_nan() on it it returns false and then we don't change the SNaN bit.
The code as of this patch seems to be a bit confused:
it does part of the conversion of NaNs from one format to the other in float_to_float() (which is where it's fiddling with the frac bits) and part of it in
the round_canonical() case (where it then messes about with quietening the NaN). In an ideal world this would all be punted out to the softfloat-specialize code to convert with access to the full details of the input number, because it's impdef how NaN conversion handles the fraction bits. Arm happens to choose to use the most significant bits of the fraction field, but there's no theoretical reason why you couldn't have an implementation that wanted to preserve the least significant bits, for instance.
Note also that we currently have workarounds at the target/arm level for the softfloat code not quietening input NaNs for fp-to-fp conversion: see the uses of float*_maybe_silence_nan() after float*_to_float* calls in target/arm/helper.c. If the softfloat code is now going to get these correct then we can drop those. HPPA, MIPS, RISCV and S390x have similar workarounds also. Overall, the maybe_silence_nan function was a dubious workaround for not having been able to do the NaN handling when we had a fully unpacked value, and perhaps we can minimise its use or even get rid of it... (target/i386 notably does not do this, we should check how SSE and x87 handle NaNs in fp conversions first.)
thanks
-- PMM
Re: [PATCH v2 2/3] fpu/softfloat: support ARM Alternative half-precision
Quoted 43 lines · Peter Maydell
Quoted 9 lines · Alex Bennée
For float16 ARM supports an alternative half-precision format which sacrifices the ability to represent NaN/Inf in return for a higher dynamic range. To support this I've added an additional FloatFmt (
float16_params_ahp).The new FloatFmt flag (
arm_althp) is then used to modify the behaviour of canonicalize andround_canonicalwith respect to representation and exception raising.Quoted 7 lines
Finally the
float16_to_floatNandfloatN_to_float16conversion routines select the new alternative FloatFmt when !ieee.Signed-off-by Alex Bennée <[email protected]>--- fpu/softfloat.c | 97 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 74 insertions(+), 23 deletions(-)I found some corner cases where this patchset introduces regressions; details below... They're not all althp related but I started composing this email in reply to patch 2/3 and don't want to try to move it to replying to the cover letter now :-)
(1) Here's an example of a wrong 32->16 conversion in alt-hp mode:
for FCVT h5, s0 where s0 is an SNaN, then your code gives0x7E00when it should give0x0, becausefloat16a_round_pack_canonical()tries to return a NaN, which doesn't exist in alt-HP.In the Arm ARM pseudocode this case is handled by the FPConvert pseudocode, which treats
alt_hpspecially when the input is a NaN. On that analogy I put this intofloat_to_float(), which seems to have the desired effect:fpu/softfloat.c +1
-0@@ -1256,6 +1256,17 @@ static FloatParts float_to_float(FloatParts a, s->float_exception_flags |= float_flag_invalid; } +if (dstf->arm_althp) {
fpu/softfloat.c static FloatParts float_to_float(FloatParts a, · line 1269
Quoted 10 lines
fpu/softfloat.c +10
-0+/* There is no NaN in the destination format: raise Invalid +* and return a zero with the sign of the input NaN. +*/ + s->float_exception_flags |= float_flag_invalid; + a.cls = float_class_zero; + a.frac =0 ; + a.exp =0 ; +return a; + } +
Doh. The previous version had handling for this in float_to_float but it was clear on my test case and I thought I'd handled it all in the unpack/canonicalize step.
Quoted 8 lines
fpu/softfloat.c +0
-0if (s->default_nan_mode) { a.cls = float_class_dnan;return a;(2) You're also failing to set the Inexact flag for cases like
fcvt h1, s0 where s0 is0x33000000
which should return result of 0, flags Inexact | Underflow, but is after this patchset returning just Underflow.
More cases for the test case ;-)
Quoted 10 lines
I think this is because you're not dealing with the odd handling of flush-to-zero for half-precision:
in the v8A Arm ARM pseudocode, float-to-float conversion uses theFPRoundCV()function, which squashes FZ16 to 0, andFPRoundBase()looks at fpcr.FZ for 32 and 64 bit floats but fpcr.FZ16 for 16 bit floats. (FZ16 exists only with the halfprec extension, and effectively applies only for the data processing and int<->fp conversions -- seeFPRound().)
Quoted 7 lines
In our old code we handled this implicitly by having
roundAndPackFloat16()not checkstatus->flush_to_zerothe way that roundAndPackFloat32/64 did. Now you've combined them all into one code path you need to do some special casing, I think. This change fixes things for the fcvt case, but (a) is a dubious hack and (b) you'll want to do something different to handle FZ16 for actual arithmetic operations on halfprec.
We handle FZ16 by passing a different float_status for halfprec. But I guess the fcvt helpers can do the save/squash/restore dance.
Quoted 3 lines
If architectures other than Arm use halfprec (MIPS seems to) then we should check what their semantics are to see if they match Arm.
There are helpers but I couldn't see them actually being called. I was half tempted to delete the helpers and rationalise the softfloat API convention but decided against it to avoid churn.
Quoted 14 lines
fpu/softfloat.c +1
-1@@ -453,7 +453,7 @@ static FloatParts round_canonical(FloatParts p, float_status *s, flags |= float_flag_inexact; } } - }else if (s->flush_to_zero) { + }else if (s->flush_to_zero && parm->exp_size !=5 ) { flags |= float_flag_output_denormal; p.cls = float_class_zero;goto do_zero;
Quoted 3 lines
(3) Here's a NaN case we get wrong now: 64 to IEEE-16 conversion, input is
0x7ff0000000000001(an SNaN), we produce0x7c00(infinity) but should produce0x7e00(a QNaN).
OK.
Quoted 30 lines
This is because this code in
float16a_round_pack_canonical():case float_class_msnan: return float16_maybe_silence_nan(float16_pack_raw(p), s);doesn't consider the possibility that
float16_pack_raw()ends up with something that's not a NaN. In this case because the float-to-float conversion has thrown away the bottom bits of the double's mantissa, we have p.frac == 0, andfloat16_pack_raw()gives0x7c00, which is an infinity, not a NaN. So whenfloat16_maybe_silence_nan()callsfloat16_is_signaling_nan()on it it returns false and then we don't change the SNaN bit.The code as of this patch seems to be a bit confused:
it does part of the conversion of NaNs from one format to the other infloat_to_float()(which is where it's fiddling with the frac bits) and part of it in
theround_canonical()case (where it then messes about with quietening the NaN). In an ideal world this would all be punted out to the softfloat-specialize code to convert with access to the full details of the input number, because it's impdef how NaN conversion handles the fraction bits. Arm happens to choose to use the most significant bits of the fraction field, but there's no theoretical reason why you couldn't have an implementation that wanted to preserve the least significant bits, for instance.
Quoted 12 lines
Note also that we currently have workarounds at the target/arm level for the softfloat code not quietening input NaNs for fp-to-fp conversion: see the uses of float*_maybe_silence_nan() after float*_to_float* calls in
target/arm/helper.c. If the softfloat code is now going to get these correct then we can drop those. HPPA, MIPS, RISCV and S390x have similar workarounds also. Overall, themaybe_silence_nanfunction was a dubious workaround for not having been able to do the NaN handling when we had a fully unpacked value, and perhaps we can minimise its use or even get rid of it... (target/i386 notably does not do this, we should check how SSE and x87 handle NaNs in fp conversions first.)
I guess it is time to expose some of the details for the unpacked float handling to specialize so its not an after the fact hack.
Quoted 3 lines
thanks
-- PMM
--
Alex Bennée
Re: [PATCH v2 2/3] fpu/softfloat: support ARM Alternative half-precision
In reply to Peter MaydellQuoted 2 lines · Peter Maydell
(target/i386 notably does not do this, we should check how SSE and x87 handle NaNs in fp conversions first.)
Hardware does silence NaNs. I tested that earlier:
https://lists.gnu.org/archive/html/qemu-devel/2018-04/msg03114.html
r~
Re: [PATCH v2 2/3] fpu/softfloat: support ARM Alternative half-precision
Quoted 7 lines · Richard Henderson
Quoted 2 lines · Peter Maydell
(target/i386 notably does not do this, we should check how SSE and x87 handle NaNs in fp conversions first.)
Hardware does silence NaNs. I tested that earlier:
https://lists.gnu.org/archive/html/qemu-devel/2018-04/msg03114.html
Does that include SSE? I know the hardware will silence NaN's if the value is ever pushed into an x87 register (as GCC will do when spilling/filling float).
Quoted 3 lines
r~
--
Alex Bennée
Re: [PATCH v2 2/3] fpu/softfloat: support ARM Alternative half-precision
Quoted 4 lines · Alex Bennée
Quoted 1 line · Richard Henderson
On 05/03/2018 11:17 AM, Peter Maydell wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
(target/i386 notably does not do this, we should check how SSE and x87 handle NaNs in fp conversions first.)
Hardware does silence NaNs. I tested that earlier:
https://lists.gnu.org/archive/html/qemu-devel/2018-04/msg03114.html
Does that include SSE?
Yes.
Quoted 3 lines
I know the hardware will silence NaN's if the
value is ever pushed into an x87 register (as GCC will do when spilling/filling float).
Actually, loads and stores are purely bit operations that do not modify data.
r~
PATCH v2 3/3 tests/tcg/aarch64: add fcvt test cases for AArch64 (!UPSTREAM)
In reply to Alex BennéeThis runs through the usual float to float conversions and crucially also runs with ARM Alternative Half Precision Format.
[!UPSTREAM: will be in next revision of tcg-tests-revival]
---
v4
- add fcvt.ref and check results against it
- fix single_to_half, single_to_double conversions
- properly toggle AHP mode (fpsr->fpcr)
- more values around the AHP margins
--- tests/tcg/aarch64/fcvt.c | 296 +++++ tests/tcg/aarch64/fcvt.ref | 2138 ++++++++++++++++++++++++++++++++++++ 2 files changed, 2434 insertions(+) create mode 100644 tests/tcg/aarch64/fcvt.c create mode 100644 tests/tcg/aarch64/fcvt.ref
tests/tcg/aarch64/fcvt.c new file +296 -0
@@ -0,0 +1,296 @@ +/* +* Test Floating Point Conversion +*/ + +#include <stdio.h> +#include <inttypes.h> +#include <math.h> +#include <float.h> +#include <fenv.h> + +#define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) + +static char flag_str[256 ]; + +static char *get_flag_state (int flags) +{ +if (flags) { +snprintf (flag_str,sizeof (flag_str),"%s %s %s %s %s" , + flags &FE_OVERFLOW ?"OVERFLOW" :"" , + flags &FE_UNDERFLOW ?"UNDERFLOW" :"" , + flags &FE_DIVBYZERO ?"DIV0" :"" , + flags &FE_INEXACT ?"INEXACT" :"" , + flags &FE_INVALID ?"INVALID" :"" ); + }else { +snprintf (flag_str,sizeof (flag_str),"OK" ); + } + +return flag_str; +} + +static void print_double_number (int i,double num) +{ +uint64_t double_as_hex = *(uint64_t *) # +int flags =fetestexcept (FE_ALL_EXCEPT ); +char *fstr =get_flag_state (flags); + +printf ("%02d DOUBLE: %02.20e / %#020lx (%#x => %s)\n" , + i, num, double_as_hex, flags, fstr); +} + +static void print_single_number (int i,float num) +{ +uint32_t single_as_hex = *(uint32_t *) # +int flags =fetestexcept (FE_ALL_EXCEPT ); +char *fstr =get_flag_state (flags); + +printf ("%02d SINGLE: %02.20e / %#010x (%#x => %s)\n" , + i, num, single_as_hex, flags, fstr); +} + +static void print_half_number (int i,uint16_t num) +{ +int flags =fetestexcept (FE_ALL_EXCEPT ); +char *fstr =get_flag_state (flags); + +printf ("%02d HALF: %#04x (%#x => %s)\n" , + i, num, flags, fstr); +} + +float single_numbers[] = { -FLT_MAX , +-1.111E+31 , +-1.111E+30 , +-1.08700982e-12 , +-1.78051176e-20 , + -FLT_MIN , +0.0 , +FLT_MIN , +5.96046E-8 ,/* min positive FP16 subnormal */ +6.09756E-5 ,/* max subnormal FP16 */ +6.10352E-5 ,/* min positive normal FP16 */ +1.0 , +1.0009765625 ,/* smallest float after 1.0 FP16 */ +2.0 , +M_E ,M_PI , +65503.0 , +65504.0 ,/* max FP16 */ +65505.0 , +131007.0 , +131008.0 ,/* max AFP */ +131009.0 , +1.111E+30 , +FLT_MAX }; + +static void convert_single_to_half (void ) +{ +int i; + +printf ("Converting single-precision to half-precision\n" ); + +for (i =0 ; i <ARRAY_SIZE (single_numbers); ++i) { +float input = single_numbers[i]; +uint16_t output; + +feclearexcept (FE_ALL_EXCEPT ); + +print_single_number (i, input); + asm("fcvt %h0, %s1" :"=w" (output) :"x" (input)); +print_half_number (i, output); + } +} + +static void convert_single_to_double (void ) +{ +int i; + +printf ("Converting single-precision to double-precision\n" ); + +for (i =0 ; i <ARRAY_SIZE (single_numbers); ++i) { +float input = single_numbers[i]; +uint64_t output; + +feclearexcept (FE_ALL_EXCEPT ); + +print_single_number (i, input); + asm("fcvt %d0, %s1" :"=w" (output) :"x" (input)); +print_double_number (i, output); + } +} + +double double_numbers[] = { -DBL_MAX , + -FLT_MAX -1.0 , + -FLT_MAX , +-1.111E+31 , +-1.111E+30 ,/* half prec */ +-2.0 ,-1.0 , + -DBL_MIN , + -FLT_MIN , +0.0 , +FLT_MIN , +5.96046E-8 ,/* min positive FP16 subnormal */ +6.09756E-5 ,/* max subnormal FP16 */ +6.10352E-5 ,/* min positive normal FP16 */ +1.0 , +1.0009765625 ,/* smallest float after 1.0 FP16 */ +DBL_MIN , +1.3789972848607228e-308 , +1.4914738736681624e-308 , +1.0 ,2.0 , +M_E ,M_PI , +65503.0 , +65504.0 ,/* max FP16 */ +65505.0 , +131007.0 , +131008.0 ,/* max AFP */ +131009.0 , +FLT_MAX , +FLT_MAX +1.0 , +DBL_MAX }; + +static void convert_double_to_half (void ) +{ +int i; + +printf ("Converting double-precision to half-precision\n" ); + +for (i =0 ; i <ARRAY_SIZE (single_numbers); ++i) { +double input = double_numbers[i]; +uint16_t output; + +feclearexcept (FE_ALL_EXCEPT ); + +print_double_number (i, input); + +/* as we don't have _Float16 support */ + asm("fcvt %h0, %d1" :"=w" (output) :"x" (input)); +print_half_number (i, output); + } +} + +static void convert_double_to_single (void ) +{ +int i; + +printf ("Converting double-precision to single-precision\n" ); + +for (i =0 ; i <ARRAY_SIZE (single_numbers); ++i) { +double input = double_numbers[i]; +uint32_t output; + +feclearexcept (FE_ALL_EXCEPT ); + +print_double_number (i, input); + + asm("fcvt %s0, %d1" :"=w" (output) :"x" (input)); + +print_single_number (i, output); + } +} + +/* no handy defines for these numbers */ +uint16_t half_numbers[] = { +0xffff ,/* -NaN / AHP -Max */ +0xfcff ,/* -NaN / AHP */ +0xfc01 ,/* -NaN / AHP */ +0xfc00 ,/* -Inf */ +0xfbff ,/* -Max */ +0xc000 ,/* -2 */ +0xbc00 ,/* -1 */ +0x8001 ,/* -MIN subnormal */ +0x8000 ,/* -0 */ +0x0000 ,/* +0 */ +0x0001 ,/* MIN subnormal */ +0x3c00 ,/* 1 */ +0x7bff ,/* Max */ +0x7c00 ,/* Inf */ +0x7c01 ,/* NaN / AHP */ +0x7cff ,/* NaN / AHP */ +0x7fff ,/* NaN / AHP +Max*/ +}; + +static void convert_half_to_double (void ) +{ +int i; + +printf ("Converting half-precision to double-precision\n" ); + +for (i =0 ; i <ARRAY_SIZE (half_numbers); ++i) { +uint16_t input = half_numbers[i]; +double output; + +feclearexcept (FE_ALL_EXCEPT ); + +print_half_number (i, input); + asm("fcvt %d0, %h1" :"=w" (output) :"x" (input)); +print_double_number (i, output); + } +} + +static void convert_half_to_single (void ) +{ +int i; + +printf ("Converting half-precision to single-precision\n" ); + +for (i =0 ; i <ARRAY_SIZE (half_numbers); ++i) { +uint16_t input = half_numbers[i]; +float output; + +feclearexcept (FE_ALL_EXCEPT ); + +print_half_number (i, input); + asm("fcvt %s0, %h1" :"=w" (output) :"x" (input)); +print_single_number (i, output); + } +} + +typedef struct { +int flag; +char *desc; +}float_mapping ; + +float_mapping round_flags[] = { + {FE_TONEAREST ,"to nearest" }, + {FE_UPWARD ,"upwards" }, + {FE_DOWNWARD ,"downwards" }, + {FE_TOWARDZERO ,"to zero" } +}; + +int main (int argc,char *argv[argc]) +{ +int i; + +printf ("#### Enabling IEEE Half Precision\n" ); + +for (i =0 ; i <ARRAY_SIZE (round_flags); ++i) { +fesetround (round_flags[i].flag); +printf ("### Rounding %s\n" , round_flags[i].desc); +convert_single_to_half (); +convert_single_to_double (); +convert_double_to_half (); +convert_double_to_single (); +convert_half_to_single (); +convert_half_to_double (); + } + +/* And now with ARM alternative FP16 */ + asm("mrs x1, fpcr\n\t" +"orr x1, x1, %[flags]\n\t" +"msr fpcr, x1\n\t" + :/* no output */ : [flags]"n" (1 <<26 ) :"x1" ); + +printf ("#### Enabling ARM Alternative Half Precision\n" ); + +for (i =0 ; i <ARRAY_SIZE (round_flags); ++i) { +fesetround (round_flags[i].flag); +printf ("### Rounding %s\n" , round_flags[i].desc); +convert_single_to_half (); +convert_single_to_double (); +convert_double_to_half (); +convert_double_to_single (); +convert_half_to_single (); +convert_half_to_double (); + } + +return 0 ; +}
tests/tcg/aarch64/fcvt.ref new file +2138 -0
2,138 lines not on this page · raw message
Signature · 1 line
2.17.0
This series seems to have some coding style problems. See output below for more information:
Hi,
This series seems to have some coding style problems. See output below for more information:
Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH v2 0/3] refactor float-to-float conversions and fix AHP
=== TEST SCRIPT BEGIN ===
#!/bin/bash
BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do
echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
failed=1
echo
fi
n=$((n+1))done
exit $failed
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
t [tag update] patchew/[email protected] -> patchew/[email protected]
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'df8db34ba1 tests/tcg/aarch64: add fcvt test cases for AArch64 (!UPSTREAM) c2fe988262 fpu/softfloat: support ARM Alternative half-precision ec8c70c013 fpu/softfloat: re-factor float to float conversions
=== OUTPUT BEGIN ===
Checking PATCH 1/3: fpu/softfloat: re-factor float to float conversions... Checking PATCH 2/3: fpu/softfloat: support ARM Alternative half-precision... Checking PATCH 3/3: tests/tcg/aarch64: add fcvt test cases for AArch64 (!UPSTREAM)...
ERROR: spaces required around that '+' (ctx:VxV)
#85: FILE: tests/tcg/aarch64/fcvt.c:61:
+ -1.111E+31,
^
ERROR: spaces required around that '+' (ctx:VxV)
#86: FILE: tests/tcg/aarch64/fcvt.c:62:
+ -1.111E+30,
^
ERROR: spaces required around that '-' (ctx:VxV)
#87: FILE: tests/tcg/aarch64/fcvt.c:63:
+ -1.08700982e-12,
^
ERROR: spaces required around that '-' (ctx:VxV)
#88: FILE: tests/tcg/aarch64/fcvt.c:64:
+ -1.78051176e-20,
^
ERROR: spaces required around that '-' (ctx:VxV)
#92: FILE: tests/tcg/aarch64/fcvt.c:68:
+ 5.96046E-8, /* min positive FP16 subnormal */
^
ERROR: spaces required around that '-' (ctx:VxV)
#93: FILE: tests/tcg/aarch64/fcvt.c:69:
+ 6.09756E-5, /* max subnormal FP16 */
^
ERROR: spaces required around that '-' (ctx:VxV)
#94: FILE: tests/tcg/aarch64/fcvt.c:70:
+ 6.10352E-5, /* min positive normal FP16 */
^
ERROR: spaces required around that '+' (ctx:VxV)
#105: FILE: tests/tcg/aarch64/fcvt.c:81:
+ 1.111E+30,
^
ERROR: spaces required around that '-' (ctx:VxV)
#145: FILE: tests/tcg/aarch64/fcvt.c:121:
+ -FLT_MAX-1.0,
^
ERROR: spaces required around that '+' (ctx:VxV)
#147: FILE: tests/tcg/aarch64/fcvt.c:123:
+ -1.111E+31,
^
ERROR: spaces required around that '+' (ctx:VxV)
#148: FILE: tests/tcg/aarch64/fcvt.c:124:
+ -1.111E+30, /* half prec */
^
ERROR: spaces required around that '-' (ctx:VxV)
#154: FILE: tests/tcg/aarch64/fcvt.c:130:
+ 5.96046E-8, /* min positive FP16 subnormal */
^
ERROR: spaces required around that '-' (ctx:VxV)
#155: FILE: tests/tcg/aarch64/fcvt.c:131:
+ 6.09756E-5, /* max subnormal FP16 */
^
ERROR: spaces required around that '-' (ctx:VxV)
#156: FILE: tests/tcg/aarch64/fcvt.c:132:
+ 6.10352E-5, /* min positive normal FP16 */
^
ERROR: spaces required around that '-' (ctx:VxV)
#160: FILE: tests/tcg/aarch64/fcvt.c:136:
+ 1.3789972848607228e-308,
^
ERROR: spaces required around that '-' (ctx:VxV)
#161: FILE: tests/tcg/aarch64/fcvt.c:137:
+ 1.4914738736681624e-308,
^
ERROR: space prohibited before open square bracket '[' #304: FILE: tests/tcg/aarch64/fcvt.c:280:
+ : /* no output */ : [flags] "n" (1 << 26) : "x1" );
ERROR: space prohibited before that close parenthesis ')' #304: FILE: tests/tcg/aarch64/fcvt.c:280:
+ : /* no output */ : [flags] "n" (1 << 26) : "x1" );
total: 18 errors, 0 warnings, 2434 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]
Quoted 15 lines · Alex Bennée
This is a more polished version of the re-factoring of the softfloat fcvt code. I've split apart the fixes for ARM alternative half-precision format for easier review. Rather than rely on some questionable hacks it introduces a new FloatFmt to allow cleaner handling of the differences in the common code. If there are other alternative floating point formats they can follow a similar approach.
I've included the test case for reference although I expect it to be merged with my tcg-testing revival code which has the rest of the build and test machinery for the test case.
checkpatch dumps a bunch of false-positives as it doesn't like scientific notation for floats or inline assembler used in the test case.
Quoted 6 lines
Cheers,
Alex Bennée (3):
fpu/softfloat: re-factor float to float conversions fpu/softfloat: support ARM Alternative half-precision tests/tcg/aarch64: add fcvt test cases for AArch64 (!UPSTREAM)
r~