Skip to contentSkip to conversation

target/alpha: do not trap on Inf inputs to bare FP arithmetic

PATCH In review Matt Turner 3 participants 4 messages Jul 26 – Jul 27

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/alpha: do not trap on Inf inputs to bare FP arithmetic

helper_ieee_input is called for each operand of bare (non-/S) T-floating arithmetic instructions. It was raising EXC_M_INV for any value with exp == 0x7ff, which covers both NaN and Infinity.

This is wrong for Infinity. Infinity is a valid arithmetic operand; operations like Inf + finite, Inf + Inf, or Inf * finite produce well- defined IEEE results with no invalid-operation exception. Only NaN inputs and indeterminate forms (Inf - Inf, 0 * Inf) are invalid.

Indeterminate forms are already handled: the softfloat helpers set float_flag_invalid in those cases, which soft_to_fpcr_exc converts to FPCR_INV in env->error_code, and helper_fp_exc_raise then raises the trap. No pre-operation check for Inf is needed.

Change the exp == 0x7ff check to also require frac != 0, matching the existing behavior of helper_ieee_input_cmp.

Fixes b99e80694cc6 ("target-alpha: Raise EXC_M_INV properly for fp inputs")
---
 target/alpha/fpu_helper.c       |   7 +-
 tests/tcg/alpha/Makefile.target |   2 +-
 tests/tcg/alpha/test-fp-ieee.c  | 173 ++++++++++++++++++++++++++++++++
 3 files changed, 177 insertions(+), 5 deletions(-)
 create mode 100644 tests/tcg/alpha/test-fp-ieee.c
target/alpha/fpu_helper.c +3 -4
index 5b7e4bd5eb..6c26e3349f 100644
@@ -105,8 +105,7 @@ void helper_fp_exc_raise_s(CPUAlphaState *env, uint32_t ignore, uint32_t regno)
     }
 }
 
-/* Input handing without software completion.  Trap for all
-   non-finite numbers.  */
+/* Input handling without software completion.  Trap for NaN and denormals.  */
 void helper_ieee_input(CPUAlphaState *env, uint64_t val)
 {
     uint32_t exp = (uint32_t)(val >> 52) & 0x7ff;
@@ -117,8 +116,8 @@ void helper_ieee_input(CPUAlphaState *env, uint64_t val)
         if (frac != 0) {
             arith_excp(env, GETPC(), EXC_M_INV, 0);
         }
-    } else if (exp == 0x7ff) {
-        /* Infinity or NaN.  */
+    } else if (exp == 0x7ff && frac) {
+        /* NaN.  */
         env->fpcr |= FPCR_INV;
         arith_excp(env, GETPC(), EXC_M_INV, 0);
     }
tests/tcg/alpha/Makefile.target +1 -1
index 36d8ed1eae..2636921aeb 100644
@@ -5,7 +5,7 @@
 ALPHA_SRC=$(SRC_PATH)/tests/tcg/alpha
 VPATH+=$(ALPHA_SRC)
 
-ALPHA_TESTS=hello-alpha test-cond test-cmov test-ovf test-cvttq
+ALPHA_TESTS=hello-alpha test-cond test-cmov test-ovf test-cvttq test-fp-ieee
 TESTS+=$(ALPHA_TESTS)
 
 test-cmov: EXTRA_CFLAGS=-DTEST_CMOV
tests/tcg/alpha/test-fp-ieee.c new file +173 -0

173 lines not on this page · raw message

Signature · 1 line
2.54.0
#1 lore raw
Quoted 8 lines · Matt Turner

helper_ieee_input is called for each operand of bare (non-/S) T-floating arithmetic instructions. It was raising EXC_M_INV for any value with exp == 0x7ff, which covers both NaN and Infinity.

This is wrong for Infinity. Infinity is a valid arithmetic operand; operations like Inf + finite, Inf + Inf, or Inf * finite produce well- defined IEEE results with no invalid-operation exception. Only NaN inputs and indeterminate forms (Inf - Inf, 0 * Inf) are invalid.

No, this is modeling actual Alpha floating-point, not IEEE arithmetic. Only normal finites are handled in hardware; everything else needs software assitance.

r~

Quoted 11 lines

Indeterminate forms are already handled: the softfloat helpers set float_flag_invalid in those cases, which soft_to_fpcr_exc converts to FPCR_INV in env->error_code, and helper_fp_exc_raise then raises the trap. No pre-operation check for Inf is needed.

Change the exp == 0x7ff check to also require frac != 0, matching the existing behavior of helper_ieee_input_cmp.

Fixes b99e80694cc6 ("target-alpha: Raise EXC_M_INV properly for fp inputs")
#2 lore raw
Quoted 6 lines · Richard Henderson
Quoted 4 lines · Matt Turner

helper_ieee_input is called for each operand of bare (non-/S) T-floating arithmetic instructions. It was raising EXC_M_INV for any value with exp == 0x7ff, which covers both NaN and Infinity.

Quoted 7 lines
Quoted 4 lines

This is wrong for Infinity. Infinity is a valid arithmetic operand; operations like Inf + finite, Inf + Inf, or Inf * finite produce well- defined IEEE results with no invalid-operation exception. Only NaN inputs and indeterminate forms (Inf - Inf, 0 * Inf) are invalid.

No, this is modeling actual Alpha floating-point, not IEEE arithmetic. Only normal finites are handled in hardware; everything else needs software assitance.

Do we emulate that software-assist in linux-user ? I couldn't see any code for that at a quick glance.

-- PMM

#3 lore raw
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Richard Henderson
Quoted 8 lines · Matt Turner

helper_ieee_input is called for each operand of bare (non-/S) T-floating arithmetic instructions. It was raising EXC_M_INV for any value with exp == 0x7ff, which covers both NaN and Infinity.

This is wrong for Infinity. Infinity is a valid arithmetic operand; operations like Inf + finite, Inf + Inf, or Inf * finite produce well- defined IEEE results with no invalid-operation exception. Only NaN inputs and indeterminate forms (Inf - Inf, 0 * Inf) are invalid.

Quoted 5 lines
Quoted 2 lines

No, this is modeling actual Alpha floating-point, not IEEE arithmetic. Only normal finites are handled in hardware; everything else needs software assitance.

Do we emulate that software-assist in linux-user ? I couldn't see any code for that at a quick glance.

Well, that's complicated by the fact that there are both software assist (/s) and non-software assist (non-/s) instructions.

For software assist, we skip the input trap handling in linux-user:

#ifndef CONFIG_USER_ONLY
/* In system mode, raise exceptions for denormals like real
hardware. In user mode, proceed as if the OS completion handler is handling the denormal as per spec. */
gen_helper_ieee_input_s(tcg_env, val);
#endif

For non-software assist, per above, there's no difference: always trap.

Some truly horrible decisions were made back in the 80's in the name of FOPS.

r~