softfloat: re-factor float to float conversions
RFC PATCH 1/1 softfloat: re-factor float to float conversions
This allows us to delete a lot of additional boilerplate code which is no longer needed. The ieee flag is only exposed externally for functions that touch FP16 values and is currently synonymous with ARM Alternate Half Precision. The MIPS front-end has some un-used helpers which also use this flag.
The conversion of NaNs follows the rules set out by the ARM ARM. I couldn't find a codified conversion in the IEEE754 (2008) spec so we may need to introduce a specialisation for other architectures but I'll need some test cases first. However I think the bit re-packing approach is more controllable than the chained FooToCommonNaN functions. They are still used for the 80 and 128 bit floating point conversions.
--- fpu/softfloat-specialize.h | 40 --- fpu/softfloat.c | 481 ++++++++++--------------------------- include/fpu/softfloat.h | 8 +- 3 files changed, 126 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 +122 -359
505 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
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] [RFC PATCH] softfloat: re-factor float to float conversions
=== 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
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'9e28813cb0 softfloat: re-factor float to float conversions
=== OUTPUT BEGIN ===
Checking PATCH 1/1: softfloat: re-factor float to float conversions...
ERROR: space prohibited before that close parenthesis ')' #103: FILE: fpu/softfloat.c:1216:
+ if (is_snan(a.cls) || arm_hp ) {
total: 1 errors, 0 warnings, 565 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 1 line · Alex Bennée
+ bool
arm_hp= !ieee && (src_sz== 16);
Surely you should be testing the destination format. The float16_unpack_canonical step should be handling the (non-)special cases to get into FloatParts.
It would probably be better to invert the parameter to be arm_hp rather than ieee; that way you don't need to test the format; it'll be implied.
Quoted 9 lines
fpu/softfloat.c +9
-0+/* Our only option now is to "re-pack" the NaN. As the +* canonilization process doesn't mess with fraction bits for +* NaNs we do it all here +*/ +switch (src_sz) { +case 64 : +break ; +case 32 : + a.frac =deposit64 (0 ,29 ,22 ,extract64 (a.frac,0 ,22 ));
fpu/softfloat.c float64 float64_div(float64 a, float64 b, float_status *status) · line 1248
Quoted 10 lines
fpu/softfloat.c +10
-0+break ; +case 16 : + a.frac =deposit64 (0 ,42 ,9 ,extract64 (a.frac,0 ,9 )); +break ; +default : +g_assert_not_reached (); +break ; + } + +/* Get all the frac bits we need with MSB set */
This is assuming QNaN has MSB set. We also support SNaN with MSB set.
Quoted 4 lines
fpu/softfloat.c +4
-0+switch (dst_sz) { +case 64 : + a.frac = (1ULL <<51 ) |extract64 (a.frac,0 ,51 ); +break ;
fpu/softfloat.c float64 float64_div(float64 a, float64 b, float_status *status) · line 1262
Quoted 10 lines
fpu/softfloat.c +10
-0+case 32 : + a.frac = (1 <<22 ) |extract64 (a.frac,29 ,22 ); +break ; +case 16 : + a.frac = (1 <<9 ) |extract64 (a.frac,42 ,9 ); +break ; +default : +g_assert_not_reached (); +break ; + }
This would be better with
a.frac = a.frac << (64 - src->frac_size) >> (64 - dst->frac_size); a.cls = float_class_msnan;
which has no such magic numbers.
fpu/softfloat.c float64 float64_div(float64 a, float64 b, float_status *status) · line 1270
Quoted 8 lines
fpu/softfloat.c +8
-0+ }else if (a.cls == float_class_inf && arm_hp) { +/* FPProcessException(FPExc_InvalidOp, fpcr); */ + s->float_exception_flags |= float_flag_invalid; +/* Alt HP returns result = sign:Ones(M-1) - faking qnan +* forces round_canonical to use frac for the final bits +*/ + a.cls = float_class_qnan; + a.frac =-1 ;
Wow, that's... ugly. I would really prefer that you put the correct values into the structure.
You'll need to pass ieee into float16_round_pack_canonical anyway, since otherwise you'll get the wrong results for e.g. 1.111p31 (overflows to inf with ieee hp; representable with armhp).
r~