softfloat: Name rounding mode enum

Give the previously unnamed enum a typedef name.  Use the packed
attribute so that we do not affect the layout of the float_status
struct.  Use it in the prototypes of relevant functions.

Adjust switch statements as necessary to avoid compiler warnings.

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2020-05-05 09:01:49 -07:00
parent a828b373bd
commit 3dede407cc
6 changed files with 66 additions and 51 deletions

View File

@ -759,6 +759,8 @@ static FloatParts round_canonical(FloatParts p, float_status *s,
case float_round_to_odd: case float_round_to_odd:
inc = frac & frac_lsb ? 0 : round_mask; inc = frac & frac_lsb ? 0 : round_mask;
break; break;
default:
break;
} }
flags |= float_flag_inexact; flags |= float_flag_inexact;
frac += inc; frac += inc;
@ -1928,7 +1930,7 @@ float32 float64_to_float32(float64 a, float_status *s)
* Arithmetic. * Arithmetic.
*/ */
static FloatParts round_to_int(FloatParts a, int rmode, static FloatParts round_to_int(FloatParts a, FloatRoundMode rmode,
int scale, float_status *s) int scale, float_status *s)
{ {
switch (a.cls) { switch (a.cls) {
@ -2061,8 +2063,8 @@ float64 float64_round_to_int(float64 a, float_status *s)
* is returned. * is returned.
*/ */
static int64_t round_to_int_and_pack(FloatParts in, int rmode, int scale, static int64_t round_to_int_and_pack(FloatParts in, FloatRoundMode rmode,
int64_t min, int64_t max, int scale, int64_t min, int64_t max,
float_status *s) float_status *s)
{ {
uint64_t r; uint64_t r;
@ -2107,63 +2109,63 @@ static int64_t round_to_int_and_pack(FloatParts in, int rmode, int scale,
} }
} }
int16_t float16_to_int16_scalbn(float16 a, int rmode, int scale, int16_t float16_to_int16_scalbn(float16 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_int_and_pack(float16_unpack_canonical(a, s), return round_to_int_and_pack(float16_unpack_canonical(a, s),
rmode, scale, INT16_MIN, INT16_MAX, s); rmode, scale, INT16_MIN, INT16_MAX, s);
} }
int32_t float16_to_int32_scalbn(float16 a, int rmode, int scale, int32_t float16_to_int32_scalbn(float16 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_int_and_pack(float16_unpack_canonical(a, s), return round_to_int_and_pack(float16_unpack_canonical(a, s),
rmode, scale, INT32_MIN, INT32_MAX, s); rmode, scale, INT32_MIN, INT32_MAX, s);
} }
int64_t float16_to_int64_scalbn(float16 a, int rmode, int scale, int64_t float16_to_int64_scalbn(float16 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_int_and_pack(float16_unpack_canonical(a, s), return round_to_int_and_pack(float16_unpack_canonical(a, s),
rmode, scale, INT64_MIN, INT64_MAX, s); rmode, scale, INT64_MIN, INT64_MAX, s);
} }
int16_t float32_to_int16_scalbn(float32 a, int rmode, int scale, int16_t float32_to_int16_scalbn(float32 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_int_and_pack(float32_unpack_canonical(a, s), return round_to_int_and_pack(float32_unpack_canonical(a, s),
rmode, scale, INT16_MIN, INT16_MAX, s); rmode, scale, INT16_MIN, INT16_MAX, s);
} }
int32_t float32_to_int32_scalbn(float32 a, int rmode, int scale, int32_t float32_to_int32_scalbn(float32 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_int_and_pack(float32_unpack_canonical(a, s), return round_to_int_and_pack(float32_unpack_canonical(a, s),
rmode, scale, INT32_MIN, INT32_MAX, s); rmode, scale, INT32_MIN, INT32_MAX, s);
} }
int64_t float32_to_int64_scalbn(float32 a, int rmode, int scale, int64_t float32_to_int64_scalbn(float32 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_int_and_pack(float32_unpack_canonical(a, s), return round_to_int_and_pack(float32_unpack_canonical(a, s),
rmode, scale, INT64_MIN, INT64_MAX, s); rmode, scale, INT64_MIN, INT64_MAX, s);
} }
int16_t float64_to_int16_scalbn(float64 a, int rmode, int scale, int16_t float64_to_int16_scalbn(float64 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_int_and_pack(float64_unpack_canonical(a, s), return round_to_int_and_pack(float64_unpack_canonical(a, s),
rmode, scale, INT16_MIN, INT16_MAX, s); rmode, scale, INT16_MIN, INT16_MAX, s);
} }
int32_t float64_to_int32_scalbn(float64 a, int rmode, int scale, int32_t float64_to_int32_scalbn(float64 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_int_and_pack(float64_unpack_canonical(a, s), return round_to_int_and_pack(float64_unpack_canonical(a, s),
rmode, scale, INT32_MIN, INT32_MAX, s); rmode, scale, INT32_MIN, INT32_MAX, s);
} }
int64_t float64_to_int64_scalbn(float64 a, int rmode, int scale, int64_t float64_to_int64_scalbn(float64 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_int_and_pack(float64_unpack_canonical(a, s), return round_to_int_and_pack(float64_unpack_canonical(a, s),
@ -2273,8 +2275,9 @@ int64_t float64_to_int64_round_to_zero(float64 a, float_status *s)
* flag. * flag.
*/ */
static uint64_t round_to_uint_and_pack(FloatParts in, int rmode, int scale, static uint64_t round_to_uint_and_pack(FloatParts in, FloatRoundMode rmode,
uint64_t max, float_status *s) int scale, uint64_t max,
float_status *s)
{ {
int orig_flags = get_float_exception_flags(s); int orig_flags = get_float_exception_flags(s);
FloatParts p = round_to_int(in, rmode, scale, s); FloatParts p = round_to_int(in, rmode, scale, s);
@ -2319,63 +2322,63 @@ static uint64_t round_to_uint_and_pack(FloatParts in, int rmode, int scale,
} }
} }
uint16_t float16_to_uint16_scalbn(float16 a, int rmode, int scale, uint16_t float16_to_uint16_scalbn(float16 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_uint_and_pack(float16_unpack_canonical(a, s), return round_to_uint_and_pack(float16_unpack_canonical(a, s),
rmode, scale, UINT16_MAX, s); rmode, scale, UINT16_MAX, s);
} }
uint32_t float16_to_uint32_scalbn(float16 a, int rmode, int scale, uint32_t float16_to_uint32_scalbn(float16 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_uint_and_pack(float16_unpack_canonical(a, s), return round_to_uint_and_pack(float16_unpack_canonical(a, s),
rmode, scale, UINT32_MAX, s); rmode, scale, UINT32_MAX, s);
} }
uint64_t float16_to_uint64_scalbn(float16 a, int rmode, int scale, uint64_t float16_to_uint64_scalbn(float16 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_uint_and_pack(float16_unpack_canonical(a, s), return round_to_uint_and_pack(float16_unpack_canonical(a, s),
rmode, scale, UINT64_MAX, s); rmode, scale, UINT64_MAX, s);
} }
uint16_t float32_to_uint16_scalbn(float32 a, int rmode, int scale, uint16_t float32_to_uint16_scalbn(float32 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_uint_and_pack(float32_unpack_canonical(a, s), return round_to_uint_and_pack(float32_unpack_canonical(a, s),
rmode, scale, UINT16_MAX, s); rmode, scale, UINT16_MAX, s);
} }
uint32_t float32_to_uint32_scalbn(float32 a, int rmode, int scale, uint32_t float32_to_uint32_scalbn(float32 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_uint_and_pack(float32_unpack_canonical(a, s), return round_to_uint_and_pack(float32_unpack_canonical(a, s),
rmode, scale, UINT32_MAX, s); rmode, scale, UINT32_MAX, s);
} }
uint64_t float32_to_uint64_scalbn(float32 a, int rmode, int scale, uint64_t float32_to_uint64_scalbn(float32 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_uint_and_pack(float32_unpack_canonical(a, s), return round_to_uint_and_pack(float32_unpack_canonical(a, s),
rmode, scale, UINT64_MAX, s); rmode, scale, UINT64_MAX, s);
} }
uint16_t float64_to_uint16_scalbn(float64 a, int rmode, int scale, uint16_t float64_to_uint16_scalbn(float64 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_uint_and_pack(float64_unpack_canonical(a, s), return round_to_uint_and_pack(float64_unpack_canonical(a, s),
rmode, scale, UINT16_MAX, s); rmode, scale, UINT16_MAX, s);
} }
uint32_t float64_to_uint32_scalbn(float64 a, int rmode, int scale, uint32_t float64_to_uint32_scalbn(float64 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_uint_and_pack(float64_unpack_canonical(a, s), return round_to_uint_and_pack(float64_unpack_canonical(a, s),
rmode, scale, UINT32_MAX, s); rmode, scale, UINT32_MAX, s);
} }
uint64_t float64_to_uint64_scalbn(float64 a, int rmode, int scale, uint64_t float64_to_uint64_scalbn(float64 a, FloatRoundMode rmode, int scale,
float_status *s) float_status *s)
{ {
return round_to_uint_and_pack(float64_unpack_canonical(a, s), return round_to_uint_and_pack(float64_unpack_canonical(a, s),
@ -5715,6 +5718,11 @@ floatx80 floatx80_round_to_int(floatx80 a, float_status *status)
return return
aSign ? packFloatx80( 1, 0, 0 ) aSign ? packFloatx80( 1, 0, 0 )
: packFloatx80( 0, 0x3FFF, UINT64_C(0x8000000000000000)); : packFloatx80( 0, 0x3FFF, UINT64_C(0x8000000000000000));
case float_round_to_zero:
break;
default:
g_assert_not_reached();
} }
return packFloatx80( aSign, 0, 0 ); return packFloatx80( aSign, 0, 0 );
} }
@ -7047,6 +7055,9 @@ float128 float128_round_to_int(float128 a, float_status *status)
case float_round_to_odd: case float_round_to_odd:
return packFloat128(aSign, 0x3FFF, 0, 0); return packFloat128(aSign, 0x3FFF, 0, 0);
case float_round_to_zero:
break;
} }
return packFloat128( aSign, 0, 0, 0 ); return packFloat128( aSign, 0, 0, 0 );
} }

View File

@ -58,7 +58,8 @@ static inline void set_float_detect_tininess(bool val, float_status *status)
status->tininess_before_rounding = val; status->tininess_before_rounding = val;
} }
static inline void set_float_rounding_mode(int val, float_status *status) static inline void set_float_rounding_mode(FloatRoundMode val,
float_status *status)
{ {
status->float_rounding_mode = val; status->float_rounding_mode = val;
} }
@ -99,7 +100,7 @@ static inline bool get_float_detect_tininess(float_status *status)
return status->tininess_before_rounding; return status->tininess_before_rounding;
} }
static inline int get_float_rounding_mode(float_status *status) static inline FloatRoundMode get_float_rounding_mode(float_status *status)
{ {
return status->float_rounding_mode; return status->float_rounding_mode;
} }

View File

@ -123,7 +123,7 @@ typedef struct {
*Software IEC/IEEE floating-point rounding mode. *Software IEC/IEEE floating-point rounding mode.
*/ */
enum { typedef enum __attribute__((__packed__)) {
float_round_nearest_even = 0, float_round_nearest_even = 0,
float_round_down = 1, float_round_down = 1,
float_round_up = 2, float_round_up = 2,
@ -131,7 +131,7 @@ enum {
float_round_ties_away = 4, float_round_ties_away = 4,
/* Not an IEEE rounding mode: round to the closest odd mantissa value */ /* Not an IEEE rounding mode: round to the closest odd mantissa value */
float_round_to_odd = 5, float_round_to_odd = 5,
}; } FloatRoundMode;
/* /*
* Software IEC/IEEE floating-point exception flags. * Software IEC/IEEE floating-point exception flags.
@ -156,7 +156,7 @@ enum {
*/ */
typedef struct float_status { typedef struct float_status {
signed char float_rounding_mode; FloatRoundMode float_rounding_mode;
uint8_t float_exception_flags; uint8_t float_exception_flags;
signed char floatx80_rounding_precision; signed char floatx80_rounding_precision;
bool tininess_before_rounding; bool tininess_before_rounding;

View File

@ -186,9 +186,9 @@ float32 float16_to_float32(float16, bool ieee, float_status *status);
float16 float64_to_float16(float64 a, 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); float64 float16_to_float64(float16 a, bool ieee, float_status *status);
int16_t float16_to_int16_scalbn(float16, int, int, float_status *status); int16_t float16_to_int16_scalbn(float16, FloatRoundMode, int, float_status *);
int32_t float16_to_int32_scalbn(float16, int, int, float_status *status); int32_t float16_to_int32_scalbn(float16, FloatRoundMode, int, float_status *);
int64_t float16_to_int64_scalbn(float16, int, int, float_status *status); int64_t float16_to_int64_scalbn(float16, FloatRoundMode, int, float_status *);
int16_t float16_to_int16(float16, float_status *status); int16_t float16_to_int16(float16, float_status *status);
int32_t float16_to_int32(float16, float_status *status); int32_t float16_to_int32(float16, float_status *status);
@ -198,9 +198,12 @@ int16_t float16_to_int16_round_to_zero(float16, float_status *status);
int32_t float16_to_int32_round_to_zero(float16, float_status *status); int32_t float16_to_int32_round_to_zero(float16, float_status *status);
int64_t float16_to_int64_round_to_zero(float16, float_status *status); int64_t float16_to_int64_round_to_zero(float16, float_status *status);
uint16_t float16_to_uint16_scalbn(float16 a, int, int, float_status *status); uint16_t float16_to_uint16_scalbn(float16 a, FloatRoundMode,
uint32_t float16_to_uint32_scalbn(float16 a, int, int, float_status *status); int, float_status *status);
uint64_t float16_to_uint64_scalbn(float16 a, int, int, float_status *status); uint32_t float16_to_uint32_scalbn(float16 a, FloatRoundMode,
int, float_status *status);
uint64_t float16_to_uint64_scalbn(float16 a, FloatRoundMode,
int, float_status *status);
uint16_t float16_to_uint16(float16 a, float_status *status); uint16_t float16_to_uint16(float16 a, float_status *status);
uint32_t float16_to_uint32(float16 a, float_status *status); uint32_t float16_to_uint32(float16 a, float_status *status);
@ -298,9 +301,9 @@ float16 float16_default_nan(float_status *status);
| Software IEC/IEEE single-precision conversion routines. | Software IEC/IEEE single-precision conversion routines.
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
int16_t float32_to_int16_scalbn(float32, int, int, float_status *status); int16_t float32_to_int16_scalbn(float32, FloatRoundMode, int, float_status *);
int32_t float32_to_int32_scalbn(float32, int, int, float_status *status); int32_t float32_to_int32_scalbn(float32, FloatRoundMode, int, float_status *);
int64_t float32_to_int64_scalbn(float32, int, int, float_status *status); int64_t float32_to_int64_scalbn(float32, FloatRoundMode, int, float_status *);
int16_t float32_to_int16(float32, float_status *status); int16_t float32_to_int16(float32, float_status *status);
int32_t float32_to_int32(float32, float_status *status); int32_t float32_to_int32(float32, float_status *status);
@ -310,9 +313,9 @@ int16_t float32_to_int16_round_to_zero(float32, float_status *status);
int32_t float32_to_int32_round_to_zero(float32, float_status *status); int32_t float32_to_int32_round_to_zero(float32, float_status *status);
int64_t float32_to_int64_round_to_zero(float32, float_status *status); int64_t float32_to_int64_round_to_zero(float32, float_status *status);
uint16_t float32_to_uint16_scalbn(float32, int, int, float_status *status); uint16_t float32_to_uint16_scalbn(float32, FloatRoundMode, int, float_status *);
uint32_t float32_to_uint32_scalbn(float32, int, int, float_status *status); uint32_t float32_to_uint32_scalbn(float32, FloatRoundMode, int, float_status *);
uint64_t float32_to_uint64_scalbn(float32, int, int, float_status *status); uint64_t float32_to_uint64_scalbn(float32, FloatRoundMode, int, float_status *);
uint16_t float32_to_uint16(float32, float_status *status); uint16_t float32_to_uint16(float32, float_status *status);
uint32_t float32_to_uint32(float32, float_status *status); uint32_t float32_to_uint32(float32, float_status *status);
@ -455,9 +458,9 @@ float32 float32_default_nan(float_status *status);
| Software IEC/IEEE double-precision conversion routines. | Software IEC/IEEE double-precision conversion routines.
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
int16_t float64_to_int16_scalbn(float64, int, int, float_status *status); int16_t float64_to_int16_scalbn(float64, FloatRoundMode, int, float_status *);
int32_t float64_to_int32_scalbn(float64, int, int, float_status *status); int32_t float64_to_int32_scalbn(float64, FloatRoundMode, int, float_status *);
int64_t float64_to_int64_scalbn(float64, int, int, float_status *status); int64_t float64_to_int64_scalbn(float64, FloatRoundMode, int, float_status *);
int16_t float64_to_int16(float64, float_status *status); int16_t float64_to_int16(float64, float_status *status);
int32_t float64_to_int32(float64, float_status *status); int32_t float64_to_int32(float64, float_status *status);
@ -467,9 +470,9 @@ int16_t float64_to_int16_round_to_zero(float64, float_status *status);
int32_t float64_to_int32_round_to_zero(float64, float_status *status); int32_t float64_to_int32_round_to_zero(float64, float_status *status);
int64_t float64_to_int64_round_to_zero(float64, float_status *status); int64_t float64_to_int64_round_to_zero(float64, float_status *status);
uint16_t float64_to_uint16_scalbn(float64, int, int, float_status *status); uint16_t float64_to_uint16_scalbn(float64, FloatRoundMode, int, float_status *);
uint32_t float64_to_uint32_scalbn(float64, int, int, float_status *status); uint32_t float64_to_uint32_scalbn(float64, FloatRoundMode, int, float_status *);
uint64_t float64_to_uint64_scalbn(float64, int, int, float_status *status); uint64_t float64_to_uint64_scalbn(float64, FloatRoundMode, int, float_status *);
uint16_t float64_to_uint16(float64, float_status *status); uint16_t float64_to_uint16(float64, float_status *status);
uint32_t float64_to_uint32(float64, float_status *status); uint32_t float64_to_uint32(float64, float_status *status);

View File

@ -697,9 +697,9 @@ static bool round_to_inf(float_status *fpst, bool sign_bit)
return sign_bit; return sign_bit;
case float_round_to_zero: /* Round to Zero */ case float_round_to_zero: /* Round to Zero */
return false; return false;
default:
g_assert_not_reached();
} }
g_assert_not_reached();
} }
uint32_t HELPER(recpe_f16)(uint32_t input, void *fpstp) uint32_t HELPER(recpe_f16)(uint32_t input, void *fpstp)

View File

@ -149,7 +149,7 @@ void cpu_m68k_set_fpcr(CPUM68KState *env, uint32_t val)
void HELPER(fitrunc)(CPUM68KState *env, FPReg *res, FPReg *val) void HELPER(fitrunc)(CPUM68KState *env, FPReg *res, FPReg *val)
{ {
int rounding_mode = get_float_rounding_mode(&env->fp_status); FloatRoundMode rounding_mode = get_float_rounding_mode(&env->fp_status);
set_float_rounding_mode(float_round_to_zero, &env->fp_status); set_float_rounding_mode(float_round_to_zero, &env->fp_status);
res->d = floatx80_round_to_int(val->d, &env->fp_status); res->d = floatx80_round_to_int(val->d, &env->fp_status);
set_float_rounding_mode(rounding_mode, &env->fp_status); set_float_rounding_mode(rounding_mode, &env->fp_status);
@ -300,7 +300,7 @@ void HELPER(fdmul)(CPUM68KState *env, FPReg *res, FPReg *val0, FPReg *val1)
void HELPER(fsglmul)(CPUM68KState *env, FPReg *res, FPReg *val0, FPReg *val1) void HELPER(fsglmul)(CPUM68KState *env, FPReg *res, FPReg *val0, FPReg *val1)
{ {
int rounding_mode = get_float_rounding_mode(&env->fp_status); FloatRoundMode rounding_mode = get_float_rounding_mode(&env->fp_status);
floatx80 a, b; floatx80 a, b;
PREC_BEGIN(32); PREC_BEGIN(32);
@ -333,7 +333,7 @@ void HELPER(fddiv)(CPUM68KState *env, FPReg *res, FPReg *val0, FPReg *val1)
void HELPER(fsgldiv)(CPUM68KState *env, FPReg *res, FPReg *val0, FPReg *val1) void HELPER(fsgldiv)(CPUM68KState *env, FPReg *res, FPReg *val0, FPReg *val1)
{ {
int rounding_mode = get_float_rounding_mode(&env->fp_status); FloatRoundMode rounding_mode = get_float_rounding_mode(&env->fp_status);
floatx80 a, b; floatx80 a, b;
PREC_BEGIN(32); PREC_BEGIN(32);