Skip to contentSkip to conversation

target-alpha: An approach to fp insn qualifiers

Qemu-devel Richard Henderson 3 participants 9 messages Dec 14, 2009 – Dec 17, 2009

lore
Share & tools
lore
#0 lore raw

One of the other folks here (I'm sorry, I've forgotten who as I misplaced the mail) passed me a preliminary patch to tackle the missing fp rounding mode support. That patch added helpers to change the rounding mode, and injected them before and after every fp insn that forced a rounding mode.

After some thought I decided not to pursue this method for the simple reason that TCG does not have native floating point code generation. Given that we have to call one helper routine to perform the operation, we might as well arrange for that same routine to handle the rounding mode manipulation.

Indeed, the patch below doesn't just pass the rounding mode to the helper, but all of the qualifier fields. Thus we can avoid a large explosion of helper routines for addt{,/u,/su,sui}{,/c,/m,/d}.

To complete the patch I should add some symbolic defines for /s, /u, etc to avoid magic constants cluttering the code. I should figure out what I should pass to helper_excp for each arithmetic exception. I should actually implement the arithmetic exceptions.

That said, these two patches are enough to pass the gcc testsuite with no regressions over native hardware.

Comments?

r~

commit 14cb36374f15413abfafb4fc030dcab25a2e0d9a

Author: Richard Henderson <[email protected]>
Date:   Fri Dec 11 08:40:22 2009 -0800

Phase 1, adding a "Quals" argument to all of the fpu helpers.

target-alpha/helper.h +30 -30
index bedd3c0..1521a84 100644
@@ -41,33 +41,33 @@ DEF_HELPER_1(store_fpcr, void, i64)
 
 DEF_HELPER_1(f_to_memory, i32, i64)
 DEF_HELPER_1(memory_to_f, i64, i32)
-DEF_HELPER_2(addf, i64, i64, i64)
-DEF_HELPER_2(subf, i64, i64, i64)
-DEF_HELPER_2(mulf, i64, i64, i64)
-DEF_HELPER_2(divf, i64, i64, i64)
-DEF_HELPER_1(sqrtf, i64, i64)
+DEF_HELPER_3(addf, i64, i64, i64, i32)
+DEF_HELPER_3(subf, i64, i64, i64, i32)
+DEF_HELPER_3(mulf, i64, i64, i64, i32)
+DEF_HELPER_3(divf, i64, i64, i64, i32)
+DEF_HELPER_2(sqrtf, i64, i64, i32)
 
 DEF_HELPER_1(g_to_memory, i64, i64)
 DEF_HELPER_1(memory_to_g, i64, i64)
-DEF_HELPER_2(addg, i64, i64, i64)
-DEF_HELPER_2(subg, i64, i64, i64)
-DEF_HELPER_2(mulg, i64, i64, i64)
-DEF_HELPER_2(divg, i64, i64, i64)
-DEF_HELPER_1(sqrtg, i64, i64)
+DEF_HELPER_3(addg, i64, i64, i64, i32)
+DEF_HELPER_3(subg, i64, i64, i64, i32)
+DEF_HELPER_3(mulg, i64, i64, i64, i32)
+DEF_HELPER_3(divg, i64, i64, i64, i32)
+DEF_HELPER_2(sqrtg, i64, i64, i32)
 
 DEF_HELPER_1(s_to_memory, i32, i64)
 DEF_HELPER_1(memory_to_s, i64, i32)
-DEF_HELPER_2(adds, i64, i64, i64)
-DEF_HELPER_2(subs, i64, i64, i64)
-DEF_HELPER_2(muls, i64, i64, i64)
-DEF_HELPER_2(divs, i64, i64, i64)
-DEF_HELPER_1(sqrts, i64, i64)
-
-DEF_HELPER_2(addt, i64, i64, i64)
-DEF_HELPER_2(subt, i64, i64, i64)
-DEF_HELPER_2(mult, i64, i64, i64)
-DEF_HELPER_2(divt, i64, i64, i64)
-DEF_HELPER_1(sqrtt, i64, i64)
+DEF_HELPER_3(adds, i64, i64, i64, i32)
+DEF_HELPER_3(subs, i64, i64, i64, i32)
+DEF_HELPER_3(muls, i64, i64, i64, i32)
+DEF_HELPER_3(divs, i64, i64, i64, i32)
+DEF_HELPER_2(sqrts, i64, i64, i32)
+
+DEF_HELPER_3(addt, i64, i64, i64, i32)
+DEF_HELPER_3(subt, i64, i64, i64, i32)
+DEF_HELPER_3(mult, i64, i64, i64, i32)
+DEF_HELPER_3(divt, i64, i64, i64, i32)
+DEF_HELPER_2(sqrtt, i64, i64, i32)
 
 DEF_HELPER_2(cmptun, i64, i64, i64)
 DEF_HELPER_2(cmpteq, i64, i64, i64)
@@ -81,15 +81,15 @@ DEF_HELPER_2(cpys, i64, i64, i64)
 DEF_HELPER_2(cpysn, i64, i64, i64)
 DEF_HELPER_2(cpyse, i64, i64, i64)
 
-DEF_HELPER_1(cvtts, i64, i64)
-DEF_HELPER_1(cvtst, i64, i64)
-DEF_HELPER_1(cvttq, i64, i64)
-DEF_HELPER_1(cvtqs, i64, i64)
-DEF_HELPER_1(cvtqt, i64, i64)
-DEF_HELPER_1(cvtqf, i64, i64)
-DEF_HELPER_1(cvtgf, i64, i64)
-DEF_HELPER_1(cvtgq, i64, i64)
-DEF_HELPER_1(cvtqg, i64, i64)
+DEF_HELPER_2(cvtts, i64, i64, i32)
+DEF_HELPER_2(cvtst, i64, i64, i32)
+DEF_HELPER_2(cvttq, i64, i64, i32)
+DEF_HELPER_2(cvtqs, i64, i64, i32)
+DEF_HELPER_2(cvtqt, i64, i64, i32)
+DEF_HELPER_2(cvtqf, i64, i64, i32)
+DEF_HELPER_2(cvtgf, i64, i64, i32)
+DEF_HELPER_2(cvtgq, i64, i64, i32)
+DEF_HELPER_2(cvtqg, i64, i64, i32)
 DEF_HELPER_1(cvtlq, i64, i64)
 DEF_HELPER_1(cvtql, i64, i64)
 DEF_HELPER_1(cvtqlv, i64, i64)
target-alpha/op_helper.c +313 -37

597 lines not on this page · raw message

target-alpha/translate.c +138 -110

397 lines not on this page · raw message

commit a0cc18be5837339ad6da257a93597fc81a28c042

Author: Richard Henderson <[email protected]>
Date:   Mon Dec 14 09:31:22 2009 -0800

target-alpha: Fix cvttq.

The alpha fp-integer conversion instruction truncates instead of saturating like the generic IEEE version does.

target-alpha/op_helper.c +105 -8

127 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · Richard Henderson

One of the other folks here (I'm sorry, I've forgotten who as I misplaced the mail) passed me a preliminary patch to tackle the missing fp rounding mode support.  That patch added helpers to change the rounding mode, and injected them before and after every fp insn that forced a rounding mode.

That was me.

Quoted 2 lines

After some thought I decided not to pursue this method for the simple reason that TCG does not have native floating point code generation.

I have started implementing TCG FP, but lack the time and desire to complete it. The speed up was pretty good when running on an ARM host with a fast FPU.

Quoted 7 lines

Given that we
have to call one helper routine to perform the operation, we might as well arrange for that same routine to handle the rounding mode manipulation.

Indeed, the patch below doesn't just pass the rounding mode to the helper, but all of the qualifier fields.  Thus we can avoid a large explosion of helper routines for addt{,/u,/su,sui}{,/c,/m,/d}.

I don't really like passing parts of opcodes to helpers, but as you say that prevents explosion of helpers. OTOH you could do lazy calls to helpers that set rounding modes with my approach of separating them from computation.

I'll take a closer look at your patch tomorrow.

Quoted 7 lines

To complete the patch I should add some symbolic defines for /s, /u, etc to avoid magic constants cluttering the code.  I should figure out what I should pass to helper_excp for each arithmetic exception.  I should actually implement the arithmetic exceptions.

That said, these two patches are enough to pass the gcc testsuite with no regressions over native hardware.

Can you give SPECint 2K equake a try? The symptom was the presence of many NaN's. If you don't have access to SPEC2K I'll try it.

Laurent

#2 lore raw
Quoted 4 lines · Laurent Desnogues

I don't really like passing parts of opcodes to helpers, but as you say that prevents explosion of helpers. OTOH you could do lazy calls to helpers that set rounding modes with my approach of separating them from computation.

I don't particularly like it either, I just thought it was a bit more efficient, given the current state of TCG.

If it's not acceptable, I can try to do some macro magic in both op_helper.c and helper.h to do function replication on the /u,/su,/sui bits and then follow your lead on the lazy rounding mode setting.

Quoted 2 lines

Can you give SPECint 2K equake a try? The symptom was the presence of many NaN's. If you don't have access to SPEC2K I'll try it.

Yes, I can. I'll see about setting this up tonight.

r~

#3 lore raw
In reply to Laurent Desnogues
Quoted 1 line · Laurent Desnogues

I'll take a closer look at your patch tomorrow.

For the record, I believe this finishes what I had in mind for the exception handling there in op_handler.c.

r~

commit ce6c2abc1d5d437dde980b4addc7da0f0f5de252

Author: Richard Henderson <[email protected]>
Date:   Mon Dec 14 16:27:39 2009 -0800

target-alpha: Implement arithmetic exceptions for IEEE fp.

target-alpha/cpu.h +7 -3
index c0dff4b..c1c0470 100644
@@ -430,9 +430,13 @@ enum {
 };
 
 /* Arithmetic exception */
-enum {
-    EXCP_ARITH_OVERFLOW,
-};
+#define EXC_M_IOV	(1<<16)		/* Integer Overflow */
+#define EXC_M_INE	(1<<15)		/* Inexact result */
+#define EXC_M_UNF	(1<<14)		/* Underflow */
+#define EXC_M_FOV	(1<<13)		/* Overflow */
+#define EXC_M_DZE	(1<<12)		/* Division by zero */
+#define EXC_M_INV	(1<<11)		/* Invalid operation */
+#define EXC_M_SWC	(1<<10)		/* Software completion */
 
 enum {
     IR_V0   = 0,
target-alpha/helper.c +15 -35

74 lines not on this page · raw message

target-alpha/op_helper.c +282 -234

840 lines not on this page · raw message

#4 lore raw
Quoted 5 lines · Richard Henderson
Quoted 1 line · Laurent Desnogues

I'll take a closer look at your patch tomorrow.

For the record, I believe this finishes what I had in mind for the exception handling there in op_handler.c.

Hmph. One more patch for correctness. With this 183.equake runs correctly. I couldn't remember all the hoops to get runspec.pl to work, to do the whole testsuite, but I did run this one by hand.

./quake-amd64: Done. Terminating the simulation.

real 0m34.943s
user 0m34.913s
sys 0m0.024s

./quake-axp: Done. Terminating the simulation.

real 33m24.105s
user 33m23.674s
sys 0m0.116s

with identical output.

r~

commit daf11ad5cd50c56d44e36e4ea334c660f8fe4c16

Author: Richard Henderson <[email protected]>
Date:   Mon Dec 14 19:46:57 2009 -0800

target-alpha: Don't ever saturate cvttq.

The previous patch tried allowing saturation if /S; that doesn't match what the kernels generate.

target-alpha/op_helper.c +76 -90
index d031f56..2d1c3d5 100644
@@ -1220,120 +1220,106 @@ uint64_t helper_cvtqs (uint64_t a, uint32_t quals)
    is used by the compiler to get unsigned conversion for free with
    the same instruction.  */
 
-static uint64_t cvttq_noqual_internal(uint64_t a, uint32_t rounding_mode)
+static uint64_t cvttq_internal(uint64_t a)
 {
     uint64_t frac, ret = 0;
-    uint32_t exp, sign;
+    uint32_t exp, sign, exc = 0;
     int shift;
 
     sign = (a >> 63);
     exp = (uint32_t)(a >> 52) & 0x7ff;
     frac = a & 0xfffffffffffffull;
 
-    /* We already handled denormals in remap_ieee_input; infinities and
-       nans are defined to return zero as per truncation.  */
-    if (exp == 0 || exp == 0x7ff)
-        return 0;
-
-    /* Restore implicit bit.  */
-    frac |= 0x10000000000000ull;
-
-    /* Note that neither overflow exceptions nor inexact exceptions
-       are desired.  This lets us streamline the checks quite a bit.  */
-    shift = exp - 1023 - 52;
-    if (shift >= 0) {
-        /* In this case the number is so large that we must shift
-           the fraction left.  There is no rounding to do.  */
-        if (shift < 63) {
-            ret = frac << shift;
-        }
+    if (exp == 0) {
+        if (unlikely(frac != 0))
+            goto do_underflow;
+    } else if (exp == 0x7ff) {
+        if (frac == 0)
+            exc = float_flag_overflow;
+        else
+            exc = float_flag_invalid;
     } else {
-        uint64_t round;
-
-        /* In this case the number is smaller than the fraction as
-           represented by the 52 bit number.  Here we must think 
-           about rounding the result.  Handle this by shifting the
-           fractional part of the number into the high bits of ROUND.
-           This will let us efficiently handle round-to-nearest.  */
-        shift = -shift;
-        if (shift < 63) {
-            ret = frac >> shift;
-            round = frac << (64 - shift);
+        /* Restore implicit bit.  */
+        frac |= 0x10000000000000ull;
+
+        /* Note that neither overflow exceptions nor inexact exceptions
+           are desired.  This lets us streamline the checks quite a bit.  */
+        shift = exp - 1023 - 52;
+        if (shift >= 0) {
+            /* In this case the number is so large that we must shift
+               the fraction left.  There is no rounding to do.  */
+            if (shift < 63) {
+                ret = frac << shift;
+                if ((ret >> shift) != frac)
+                    exc = float_flag_overflow;
+            }
         } else {
-            /* The exponent is so small we shift out everything.
-               Leave a sticky bit for proper rounding below.  */
-            round = 1;
-        }
+            uint64_t round;
+
+            /* In this case the number is smaller than the fraction as
+               represented by the 52 bit number.  Here we must think 
+               about rounding the result.  Handle this by shifting the
+               fractional part of the number into the high bits of ROUND.
+               This will let us efficiently handle round-to-nearest.  */
+            shift = -shift;
+            if (shift < 63) {
+                ret = frac >> shift;
+                round = frac << (64 - shift);
+            } else {
+                /* The exponent is so small we shift out everything.
+                   Leave a sticky bit for proper rounding below.  */
+            do_underflow:
+                round = 1;
+            }
 
-        if (round) {
-            switch (rounding_mode) {
-            case float_round_nearest_even:
-                if (round == (1ull << 63)) {
-                    /* Remaining fraction is exactly 0.5; round to even.  */
-                    ret += (ret & 1);
-                } else if (round > (1ull << 63)) {
-                    ret += 1;
+            if (round) {
+                exc = float_flag_inexact;
+                switch (FP_STATUS.float_rounding_mode) {
+                case float_round_nearest_even:
+                    if (round == (1ull << 63)) {
+                        /* Fraction is exactly 0.5; round to even.  */
+                        ret += (ret & 1);
+                    } else if (round > (1ull << 63)) {
+                        ret += 1;
+                    }
+                    break;
+                case float_round_to_zero:
+                    break;
+                case float_round_up:
+                    if (!sign)
+                        ret += 1;
+                    break;
+                case float_round_down:
+                    if (sign)
+                        ret += 1;
+                    break;
                 }
-                break;
-            case float_round_to_zero:
-                break;
-            case float_round_up:
-                if (!sign)
-                    ret += 1;
-                break;
-            case float_round_down:
-                if (sign)
-                    ret += 1;
-                break;
             }
         }
+        if (sign)
+            ret = -ret;
     }
+    if (unlikely(exc))
+        float_raise(exc, &FP_STATUS);
 
-    if (sign)
-        ret = -ret;
     return ret;
 }
 
 uint64_t helper_cvttq (uint64_t a, uint32_t quals)
 {
     uint64_t ret;
+    uint32_t token;
 
-    a = remap_ieee_input(quals, a);
-
-    if (quals & QUAL_V) {
-        float64 fa = t_to_float64(a);
-        uint32_t token;
-
-        token = begin_fp_exception();
-        if ((quals & QUAL_RM_MASK) == QUAL_RM_C) {
-            ret = float64_to_int64_round_to_zero(fa, &FP_STATUS);
-        } else {
-            token |= begin_fp_roundmode(quals);
-            ret = float64_to_int64(fa, &FP_STATUS);
-            end_fp_roundmode(token);
-        }
-        end_fp_exception(quals, token);
-    } else {
-        uint32_t round_mode;
-
-        switch (quals & QUAL_RM_MASK) {
-        case QUAL_RM_N:
-            round_mode = float_round_nearest_even;
-            break;
-        case QUAL_RM_C:
-        default:
-            round_mode = float_round_to_zero;
-            break;
-        case QUAL_RM_M:
-            round_mode = float_round_down;
-            break;
-        case QUAL_RM_D:
-            round_mode = FP_STATUS.float_rounding_mode;
-            break;
-        }
+    /* ??? There's an arugument to be made that when /S is enabled, we
+       should provide the standard IEEE saturated result, instead of
+       the truncated result that we *must* provide when /V is disabled.
+       However, that's not how either the Tru64 or Linux completion
+       handlers actually work, and GCC knows it.  */
 
-        ret = cvttq_noqual_internal(a, round_mode);
-    }
+    token = begin_fp(quals);
+    a = remap_ieee_input(quals, a);
+    ret = cvttq_internal(a);
+    end_fp(quals, token);
 
     return ret;
 }
#5 lore raw
Quoted 19 lines · Richard Henderson
Quoted 7 lines · Richard Henderson
Quoted 2 lines · Laurent Desnogues

I'll take a closer look at your patch tomorrow.

For the record, I believe this finishes what I had in mind for the exception handling there in op_handler.c.

Hmph.  One more patch for correctness.  With this 183.equake runs correctly. I couldn't remember all the hoops to get runspec.pl to work, to do the whole testsuite, but I did run this one by hand.

./quake-amd64: Done. Terminating the simulation.

real    0m34.943s
user    0m34.913s
sys     0m0.024s

Quoted 7 lines

./quake-axp: Done. Terminating the simulation.

real    33m24.105s
user    33m23.674s
sys     0m0.116s

with identical output.

Well equake works me with the last set of patches you sent (that is without using this FPU patch you just sent). Can you confirm?

Laurent

#6 lore raw
Quoted 2 lines · Laurent Desnogues

Well equake works me with the last set of patches you sent (that is without using this FPU patch you just sent). Can you confirm?

GCC testsuite breaks with the saturating cvttq so I didn't even try. If equake works, it just means that it doesn't do any conversions to unsigned long.

r~

#7 lore raw
In reply to Richard Henderson
Quoted 5 lines · Richard Henderson

On 12/14/2009 04:31 PM, Richard Henderson wrote:

Hmph. One more patch for correctness. With this 183.equake runs correctly. I couldn't remember all the hoops to get runspec.pl to work, to do the whole testsuite, but I did run this one by hand.

This is great! About a year ago I had been steadily working on getting all of spec2k to run under alpha qemu but then other projects came up and I didn't have time.

I'll re-run once the patches get applied to see if any more of the benchmarks have issues. My notes from before your patches showed the following benchmarks had issues:

perlbmk.perfect   WRONG_OUTPUT
eon.rushmeier     wrong_results
eon.kajiya        wrong_results
eon.cook          wrong_results
art.110           never finished
art.470           never finished
vpr.place         Arithmetic trap.
vpr.route         Arithmetic trap.
mesa              Arithmetic trap.
wupwise           wrong results (NaN)
lucas             Arithmetic trap.
equake            wrong results (NaN)
sixtrack          Fortran runtime error: Invalid string input in item 1
facerec           Arithmetic trap.
fma3d             MMU data fault
ammp              Arithmetic trap.
galgel            broken?
twolf             Arithmetic trap.
apsi              MMU data fault

Vince

#8 lore raw
In reply to Richard Henderson
Quoted 3 lines · Richard Henderson

On 12/14/2009 04:31 PM, Richard Henderson wrote:

Hmph. One more patch for correctness. With this 183.equake runs correctly.

I just finished running all of spec2k Alpha through Qemu.

With these patches installed (the 4 fpu ones, and the 5 from the earlier thread) on top of current Qemu, pretty much all of the floating point issues have been fixed!

Before, at least 19 of the spec2k benchmarks didn't run for various reasons. Now only two fail, sixtrack and fma3d. And I think those two are probably syscall related, though I haven't had chance to look in detail.

#sixtrack At line 343 of file daten.f Fortran runtime error: End of file
#fma3d FMA-3D> Fatal Error Reported. Job Terminated.

So it would be great if the alpha patches could be merged.

Vince