Fix TCG error in gen_avr_ptr.
[qemu] / target-ppc / op_helper.c
index d76ec0a..876b517 100644 (file)
@@ -338,9 +338,10 @@ void helper_stsw(target_ulong addr, uint32_t nb, uint32_t reg)
        addr = addr_add(addr, 4);
     }
     if (unlikely(nb > 0)) {
-        for (sh = 24; nb > 0; nb--, sh -= 8)
+        for (sh = 24; nb > 0; nb--, sh -= 8) {
             stb(addr, (env->gpr[reg] >> sh) & 0xFF);
-           addr = addr_add(addr, 1);
+            addr = addr_add(addr, 1);
+        }
     }
 }
 
@@ -554,15 +555,6 @@ uint32_t helper_float64_to_float32(uint64_t arg)
     return f.l;
 }
 
-static always_inline int fpisneg (float64 d)
-{
-    CPU_DoubleU u;
-
-    u.d = d;
-
-    return u.ll >> 63 != 0;
-}
-
 static always_inline int isden (float64 d)
 {
     CPU_DoubleU u;
@@ -572,53 +564,13 @@ static always_inline int isden (float64 d)
     return ((u.ll >> 52) & 0x7FF) == 0;
 }
 
-static always_inline int iszero (float64 d)
-{
-    CPU_DoubleU u;
-
-    u.d = d;
-
-    return (u.ll & ~0x8000000000000000ULL) == 0;
-}
-
-static always_inline int isinfinity (float64 d)
-{
-    CPU_DoubleU u;
-
-    u.d = d;
-
-    return ((u.ll >> 52) & 0x7FF) == 0x7FF &&
-        (u.ll & 0x000FFFFFFFFFFFFFULL) == 0;
-}
-
-#ifdef CONFIG_SOFTFLOAT
-static always_inline int isfinite (float64 d)
-{
-    CPU_DoubleU u;
-
-    u.d = d;
-
-    return (((u.ll >> 52) & 0x7FF) != 0x7FF);
-}
-
-static always_inline int isnormal (float64 d)
-{
-    CPU_DoubleU u;
-
-    u.d = d;
-
-    uint32_t exp = (u.ll >> 52) & 0x7FF;
-    return ((0 < exp) && (exp < 0x7FF));
-}
-#endif
-
 uint32_t helper_compute_fprf (uint64_t arg, uint32_t set_fprf)
 {
     CPU_DoubleU farg;
     int isneg;
     int ret;
     farg.ll = arg;
-    isneg = fpisneg(farg.d);
+    isneg = float64_is_neg(farg.d);
     if (unlikely(float64_is_nan(farg.d))) {
         if (float64_is_signaling_nan(farg.d)) {
             /* Signaling NaN: flags are undefined */
@@ -627,14 +579,14 @@ uint32_t helper_compute_fprf (uint64_t arg, uint32_t set_fprf)
             /* Quiet NaN */
             ret = 0x11;
         }
-    } else if (unlikely(isinfinity(farg.d))) {
+    } else if (unlikely(float64_is_infinity(farg.d))) {
         /* +/- infinity */
         if (isneg)
             ret = 0x09;
         else
             ret = 0x05;
     } else {
-        if (iszero(farg.d)) {
+        if (float64_is_zero(farg.d)) {
             /* +/- zero */
             if (isneg)
                 ret = 0x12;
@@ -671,15 +623,13 @@ static always_inline uint64_t fload_invalid_op_excp (int op)
     int ve;
 
     ve = fpscr_ve;
-    if (op & POWERPC_EXCP_FP_VXSNAN) {
-        /* Operation on signaling NaN */
+    switch (op) {
+    case POWERPC_EXCP_FP_VXSNAN:
         env->fpscr |= 1 << FPSCR_VXSNAN;
-    }
-    if (op & POWERPC_EXCP_FP_VXSOFT) {
-        /* Software-defined condition */
+       break;
+    case POWERPC_EXCP_FP_VXSOFT:
         env->fpscr |= 1 << FPSCR_VXSOFT;
-    }
-    switch (op & ~(POWERPC_EXCP_FP_VXSOFT | POWERPC_EXCP_FP_VXSNAN)) {
+       break;
     case POWERPC_EXCP_FP_VXISI:
         /* Magnitude subtraction of infinities */
         env->fpscr |= 1 << FPSCR_VXISI;
@@ -718,7 +668,7 @@ static always_inline uint64_t fload_invalid_op_excp (int op)
         env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
         if (ve == 0) {
             /* Set the result to quiet NaN */
-            ret = UINT64_MAX;
+            ret = 0xFFF8000000000000ULL;
             env->fpscr &= ~(0xF << FPSCR_FPCC);
             env->fpscr |= 0x11 << FPSCR_FPCC;
         }
@@ -729,7 +679,7 @@ static always_inline uint64_t fload_invalid_op_excp (int op)
         env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
         if (ve == 0) {
             /* Set the result to quiet NaN */
-            ret = UINT64_MAX;
+            ret = 0xFFF8000000000000ULL;
             env->fpscr &= ~(0xF << FPSCR_FPCC);
             env->fpscr |= 0x11 << FPSCR_FPCC;
         }
@@ -748,7 +698,7 @@ static always_inline uint64_t fload_invalid_op_excp (int op)
     return ret;
 }
 
-static always_inline uint64_t float_zero_divide_excp (uint64_t arg1, uint64_t arg2)
+static always_inline void float_zero_divide_excp (void)
 {
     env->fpscr |= 1 << FPSCR_ZX;
     env->fpscr &= ~((1 << FPSCR_FR) | (1 << FPSCR_FI));
@@ -761,12 +711,7 @@ static always_inline uint64_t float_zero_divide_excp (uint64_t arg1, uint64_t ar
             helper_raise_exception_err(POWERPC_EXCP_PROGRAM,
                                        POWERPC_EXCP_FP | POWERPC_EXCP_FP_ZX);
         }
-    } else {
-        /* Set the result to infinity */
-        arg1 = ((arg1 ^ arg2) & 0x8000000000000000ULL);
-        arg1 |= 0x7FFULL << 52;
     }
-    return arg1;
 }
 
 static always_inline void float_overflow_excp (void)
@@ -843,6 +788,24 @@ static always_inline void fpscr_set_rounding_mode (void)
     set_float_rounding_mode(rnd_type, &env->fp_status);
 }
 
+void helper_fpscr_clrbit (uint32_t bit)
+{
+    int prev;
+
+    prev = (env->fpscr >> bit) & 1;
+    env->fpscr &= ~(1 << bit);
+    if (prev == 1) {
+        switch (bit) {
+        case FPSCR_RN1:
+        case FPSCR_RN:
+            fpscr_set_rounding_mode();
+            break;
+        default:
+            break;
+        }
+    }
+}
+
 void helper_fpscr_setbit (uint32_t bit)
 {
     int prev;
@@ -968,9 +931,9 @@ void helper_store_fpscr (uint64_t arg, uint32_t mask)
 
     prev = env->fpscr;
     new = (uint32_t)arg;
-    new &= ~0x90000000;
-    new |= prev & 0x90000000;
-    for (i = 0; i < 7; i++) {
+    new &= ~0x60000000;
+    new |= prev & 0x60000000;
+    for (i = 0; i < 8; i++) {
         if (mask & (1 << i)) {
             env->fpscr &= ~(0xF << (4 * i));
             env->fpscr |= new & (0xF << (4 * i));
@@ -1000,12 +963,17 @@ void helper_float_check_status (void)
         /* Differred floating-point exception after target FPR update */
         if (msr_fe0 != 0 || msr_fe1 != 0)
             helper_raise_exception_err(env->exception_index, env->error_code);
-    } else if (env->fp_status.float_exception_flags & float_flag_overflow) {
-        float_overflow_excp();
-    } else if (env->fp_status.float_exception_flags & float_flag_underflow) {
-        float_underflow_excp();
-    } else if (env->fp_status.float_exception_flags & float_flag_inexact) {
-        float_inexact_excp();
+    } else {
+        int status = get_float_exception_flags(&env->fp_status);
+        if (status & float_flag_divbyzero) {
+            float_zero_divide_excp();
+        } else if (status & float_flag_overflow) {
+            float_overflow_excp();
+        } else if (status & float_flag_underflow) {
+            float_underflow_excp();
+        } else if (status & float_flag_inexact) {
+            float_inexact_excp();
+        }
     }
 #else
     if (env->exception_index == POWERPC_EXCP_PROGRAM &&
@@ -1020,7 +988,7 @@ void helper_float_check_status (void)
 #ifdef CONFIG_SOFTFLOAT
 void helper_reset_fpstatus (void)
 {
-    env->fp_status.float_exception_flags = 0;
+    set_float_exception_flags(0, &env->fp_status);
 }
 #endif
 
@@ -1036,12 +1004,12 @@ uint64_t helper_fadd (uint64_t arg1, uint64_t arg2)
                  float64_is_signaling_nan(farg2.d))) {
         /* sNaN addition */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
-    } else if (likely(isfinite(farg1.d) || isfinite(farg2.d) ||
-                      fpisneg(farg1.d) == fpisneg(farg2.d))) {
-        farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
-    } else {
+    } else if (unlikely(float64_is_infinity(farg1.d) && float64_is_infinity(farg2.d) &&
+                      float64_is_neg(farg1.d) != float64_is_neg(farg2.d))) {
         /* Magnitude subtraction of infinities */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
+    } else {
+        farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
     }
 #else
     farg1.d = float64_add(farg1.d, farg2.d, &env->fp_status);
@@ -1062,12 +1030,12 @@ uint64_t helper_fsub (uint64_t arg1, uint64_t arg2)
                  float64_is_signaling_nan(farg2.d))) {
         /* sNaN subtraction */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
-    } else if (likely(isfinite(farg1.d) || isfinite(farg2.d) ||
-                      fpisneg(farg1.d) != fpisneg(farg2.d))) {
-        farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
-    } else {
+    } else if (unlikely(float64_is_infinity(farg1.d) && float64_is_infinity(farg2.d) &&
+                      float64_is_neg(farg1.d) == float64_is_neg(farg2.d))) {
         /* Magnitude subtraction of infinities */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
+    } else {
+        farg1.d = float64_sub(farg1.d, farg2.d, &env->fp_status);
     }
 }
 #else
@@ -1088,8 +1056,8 @@ uint64_t helper_fmul (uint64_t arg1, uint64_t arg2)
                  float64_is_signaling_nan(farg2.d))) {
         /* sNaN multiplication */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
-    } else if (unlikely((isinfinity(farg1.d) && iszero(farg2.d)) ||
-                        (iszero(farg1.d) && isinfinity(farg2.d)))) {
+    } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
+                        (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
         /* Multiplication of zero by infinity */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
     } else {
@@ -1113,17 +1081,12 @@ uint64_t helper_fdiv (uint64_t arg1, uint64_t arg2)
                  float64_is_signaling_nan(farg2.d))) {
         /* sNaN division */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
-    } else if (unlikely(isinfinity(farg1.d) && isinfinity(farg2.d))) {
+    } else if (unlikely(float64_is_infinity(farg1.d) && float64_is_infinity(farg2.d))) {
         /* Division of infinity by infinity */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIDI);
-    } else if (unlikely(iszero(farg2.d))) {
-        if (iszero(farg1.d)) {
-            /* Division of zero by zero */
-            farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ);
-        } else {
-            /* Division by zero */
-            farg1.ll = float_zero_divide_excp(farg1.d, farg2.d);
-        }
+    } else if (unlikely(float64_is_zero(farg1.d) && float64_is_zero(farg2.d))) {
+        /* Division of zero by zero */
+        farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXZDZ);
     } else {
         farg1.d = float64_div(farg1.d, farg2.d, &env->fp_status);
     }
@@ -1173,7 +1136,7 @@ uint64_t helper_fctiw (uint64_t arg)
     if (unlikely(float64_is_signaling_nan(farg.d))) {
         /* sNaN conversion */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
-    } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
+    } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
         /* qNan / infinity conversion */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
     } else {
@@ -1197,7 +1160,7 @@ uint64_t helper_fctiwz (uint64_t arg)
     if (unlikely(float64_is_signaling_nan(farg.d))) {
         /* sNaN conversion */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
-    } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
+    } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
         /* qNan / infinity conversion */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
     } else {
@@ -1230,7 +1193,7 @@ uint64_t helper_fctid (uint64_t arg)
     if (unlikely(float64_is_signaling_nan(farg.d))) {
         /* sNaN conversion */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
-    } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
+    } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
         /* qNan / infinity conversion */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
     } else {
@@ -1248,7 +1211,7 @@ uint64_t helper_fctidz (uint64_t arg)
     if (unlikely(float64_is_signaling_nan(farg.d))) {
         /* sNaN conversion */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
-    } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
+    } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
         /* qNan / infinity conversion */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
     } else {
@@ -1267,7 +1230,7 @@ static always_inline uint64_t do_fri (uint64_t arg, int rounding_mode)
     if (unlikely(float64_is_signaling_nan(farg.d))) {
         /* sNaN round */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN | POWERPC_EXCP_FP_VXCVI);
-    } else if (unlikely(float64_is_nan(farg.d) || isinfinity(farg.d))) {
+    } else if (unlikely(float64_is_nan(farg.d) || float64_is_infinity(farg.d))) {
         /* qNan / infinity round */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXCVI);
     } else {
@@ -1313,6 +1276,10 @@ uint64_t helper_fmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
                  float64_is_signaling_nan(farg3.d))) {
         /* sNaN operation */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
+    } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
+                        (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
+        /* Multiplication of zero by infinity */
+        farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
     } else {
 #ifdef FLOAT128
         /* This is the way the PowerPC specification defines it */
@@ -1321,9 +1288,15 @@ uint64_t helper_fmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
-        ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
-        ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
-        farg1.d = float128_to_float64(ft0_128, &env->fp_status);
+        if (unlikely(float128_is_infinity(ft0_128) && float64_is_infinity(farg3.d) &&
+                     float128_is_neg(ft0_128) != float64_is_neg(farg3.d))) {
+            /* Magnitude subtraction of infinities */
+            farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
+        } else {
+            ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
+            ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
+            farg1.d = float128_to_float64(ft0_128, &env->fp_status);
+        }
 #else
         /* This is OK on x86 hosts */
         farg1.d = (farg1.d * farg2.d) + farg3.d;
@@ -1350,6 +1323,10 @@ uint64_t helper_fmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
                  float64_is_signaling_nan(farg3.d))) {
         /* sNaN operation */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
+    } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
+                        (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
+        /* Multiplication of zero by infinity */
+        farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
     } else {
 #ifdef FLOAT128
         /* This is the way the PowerPC specification defines it */
@@ -1358,9 +1335,15 @@ uint64_t helper_fmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
-        ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
-        ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
-        farg1.d = float128_to_float64(ft0_128, &env->fp_status);
+        if (unlikely(float128_is_infinity(ft0_128) && float64_is_infinity(farg3.d) &&
+                     float128_is_neg(ft0_128) == float64_is_neg(farg3.d))) {
+            /* Magnitude subtraction of infinities */
+            farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
+        } else {
+            ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
+            ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
+            farg1.d = float128_to_float64(ft0_128, &env->fp_status);
+        }
 #else
         /* This is OK on x86 hosts */
         farg1.d = (farg1.d * farg2.d) - farg3.d;
@@ -1387,6 +1370,10 @@ uint64_t helper_fnmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
                  float64_is_signaling_nan(farg3.d))) {
         /* sNaN operation */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
+    } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
+                        (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
+        /* Multiplication of zero by infinity */
+        farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
     } else {
 #if USE_PRECISE_EMULATION
 #ifdef FLOAT128
@@ -1396,9 +1383,15 @@ uint64_t helper_fnmadd (uint64_t arg1, uint64_t arg2, uint64_t arg3)
         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
-        ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
-        ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
-        farg1.d= float128_to_float64(ft0_128, &env->fp_status);
+        if (unlikely(float128_is_infinity(ft0_128) && float64_is_infinity(farg3.d) &&
+                     float128_is_neg(ft0_128) != float64_is_neg(farg3.d))) {
+            /* Magnitude subtraction of infinities */
+            farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
+        } else {
+            ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
+            ft0_128 = float128_add(ft0_128, ft1_128, &env->fp_status);
+            farg1.d = float128_to_float64(ft0_128, &env->fp_status);
+        }
 #else
         /* This is OK on x86 hosts */
         farg1.d = (farg1.d * farg2.d) + farg3.d;
@@ -1427,6 +1420,10 @@ uint64_t helper_fnmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
                  float64_is_signaling_nan(farg3.d))) {
         /* sNaN operation */
         farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
+    } else if (unlikely((float64_is_infinity(farg1.d) && float64_is_zero(farg2.d)) ||
+                        (float64_is_zero(farg1.d) && float64_is_infinity(farg2.d)))) {
+        /* Multiplication of zero by infinity */
+        farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXIMZ);
     } else {
 #if USE_PRECISE_EMULATION
 #ifdef FLOAT128
@@ -1436,9 +1433,15 @@ uint64_t helper_fnmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
         ft0_128 = float64_to_float128(farg1.d, &env->fp_status);
         ft1_128 = float64_to_float128(farg2.d, &env->fp_status);
         ft0_128 = float128_mul(ft0_128, ft1_128, &env->fp_status);
-        ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
-        ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
-        farg1.d = float128_to_float64(ft0_128, &env->fp_status);
+        if (unlikely(float128_is_infinity(ft0_128) && float64_is_infinity(farg3.d) &&
+                     float128_is_neg(ft0_128) == float64_is_neg(farg3.d))) {
+            /* Magnitude subtraction of infinities */
+            farg1.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXISI);
+        } else {
+            ft1_128 = float64_to_float128(farg3.d, &env->fp_status);
+            ft0_128 = float128_sub(ft0_128, ft1_128, &env->fp_status);
+            farg1.d = float128_to_float64(ft0_128, &env->fp_status);
+        }
 #else
         /* This is OK on x86 hosts */
         farg1.d = (farg1.d * farg2.d) - farg3.d;
@@ -1457,6 +1460,7 @@ uint64_t helper_fnmsub (uint64_t arg1, uint64_t arg2, uint64_t arg3)
 uint64_t helper_frsp (uint64_t arg)
 {
     CPU_DoubleU farg;
+    float32 f32;
     farg.ll = arg;
 
 #if USE_PRECISE_EMULATION
@@ -1464,10 +1468,12 @@ uint64_t helper_frsp (uint64_t arg)
         /* sNaN square root */
        farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
     } else {
-       farg.d = float64_to_float32(farg.d, &env->fp_status);
+       f32 = float64_to_float32(farg.d, &env->fp_status);
+       farg.d = float32_to_float64(f32, &env->fp_status);
     }
 #else
-    farg.d = float64_to_float32(farg.d, &env->fp_status);
+    f32 = float64_to_float32(farg.d, &env->fp_status);
+    farg.d = float32_to_float64(f32, &env->fp_status);
 #endif
     return farg.ll;
 }
@@ -1481,7 +1487,7 @@ uint64_t helper_fsqrt (uint64_t arg)
     if (unlikely(float64_is_signaling_nan(farg.d))) {
         /* sNaN square root */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
-    } else if (unlikely(fpisneg(farg.d) && !iszero(farg.d))) {
+    } else if (unlikely(float64_is_neg(farg.d) && !float64_is_zero(farg.d))) {
         /* Square root of a negative nonzero number */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
     } else {
@@ -1493,29 +1499,15 @@ uint64_t helper_fsqrt (uint64_t arg)
 /* fre - fre. */
 uint64_t helper_fre (uint64_t arg)
 {
-    CPU_DoubleU farg;
+    CPU_DoubleU fone, farg;
+    fone.ll = 0x3FF0000000000000ULL; /* 1.0 */
     farg.ll = arg;
 
     if (unlikely(float64_is_signaling_nan(farg.d))) {
         /* sNaN reciprocal */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
-    } else if (unlikely(iszero(farg.d))) {
-        /* Zero reciprocal */
-        farg.ll = float_zero_divide_excp(1.0, farg.d);
-    } else if (likely(isnormal(farg.d))) {
-        farg.d = float64_div(1.0, farg.d, &env->fp_status);
     } else {
-        if (farg.ll == 0x8000000000000000ULL) {
-            farg.ll = 0xFFF0000000000000ULL;
-        } else if (farg.ll == 0x0000000000000000ULL) {
-            farg.ll = 0x7FF0000000000000ULL;
-        } else if (float64_is_nan(farg.d)) {
-            farg.ll = 0x7FF8000000000000ULL;
-        } else if (fpisneg(farg.d)) {
-            farg.ll = 0x8000000000000000ULL;
-        } else {
-            farg.ll = 0x0000000000000000ULL;
-        }
+        farg.d = float64_div(fone.d, farg.d, &env->fp_status);
     }
     return farg.d;
 }
@@ -1523,34 +1515,18 @@ uint64_t helper_fre (uint64_t arg)
 /* fres - fres. */
 uint64_t helper_fres (uint64_t arg)
 {
-    CPU_DoubleU farg;
+    CPU_DoubleU fone, farg;
+    float32 f32;
+    fone.ll = 0x3FF0000000000000ULL; /* 1.0 */
     farg.ll = arg;
 
     if (unlikely(float64_is_signaling_nan(farg.d))) {
         /* sNaN reciprocal */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
-    } else if (unlikely(iszero(farg.d))) {
-        /* Zero reciprocal */
-        farg.ll = float_zero_divide_excp(1.0, farg.d);
-    } else if (likely(isnormal(farg.d))) {
-#if USE_PRECISE_EMULATION
-        farg.d = float64_div(1.0, farg.d, &env->fp_status);
-        farg.d = float64_to_float32(farg.d, &env->fp_status);
-#else
-        farg.d = float32_div(1.0, farg.d, &env->fp_status);
-#endif
     } else {
-        if (farg.ll == 0x8000000000000000ULL) {
-            farg.ll = 0xFFF0000000000000ULL;
-        } else if (farg.ll == 0x0000000000000000ULL) {
-            farg.ll = 0x7FF0000000000000ULL;
-        } else if (float64_is_nan(farg.d)) {
-            farg.ll = 0x7FF8000000000000ULL;
-        } else if (fpisneg(farg.d)) {
-            farg.ll = 0x8000000000000000ULL;
-        } else {
-            farg.ll = 0x0000000000000000ULL;
-        }
+        farg.d = float64_div(fone.d, farg.d, &env->fp_status);
+        f32 = float64_to_float32(farg.d, &env->fp_status);
+        farg.d = float32_to_float64(f32, &env->fp_status);
     }
     return farg.ll;
 }
@@ -1558,30 +1534,22 @@ uint64_t helper_fres (uint64_t arg)
 /* frsqrte  - frsqrte. */
 uint64_t helper_frsqrte (uint64_t arg)
 {
-    CPU_DoubleU farg;
+    CPU_DoubleU fone, farg;
+    float32 f32;
+    fone.ll = 0x3FF0000000000000ULL; /* 1.0 */
     farg.ll = arg;
 
     if (unlikely(float64_is_signaling_nan(farg.d))) {
         /* sNaN reciprocal square root */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
-    } else if (unlikely(fpisneg(farg.d) && !iszero(farg.d))) {
+    } else if (unlikely(float64_is_neg(farg.d) && !float64_is_zero(farg.d))) {
         /* Reciprocal square root of a negative nonzero number */
         farg.ll = fload_invalid_op_excp(POWERPC_EXCP_FP_VXSQRT);
-    } else if (likely(isnormal(farg.d))) {
-        farg.d = float64_sqrt(farg.d, &env->fp_status);
-        farg.d = float32_div(1.0, farg.d, &env->fp_status);
     } else {
-        if (farg.ll == 0x8000000000000000ULL) {
-            farg.ll = 0xFFF0000000000000ULL;
-        } else if (farg.ll == 0x0000000000000000ULL) {
-            farg.ll = 0x7FF0000000000000ULL;
-        } else if (float64_is_nan(farg.d)) {
-            farg.ll |= 0x000FFFFFFFFFFFFFULL;
-        } else if (fpisneg(farg.d)) {
-            farg.ll = 0x7FF8000000000000ULL;
-        } else {
-            farg.ll = 0x0000000000000000ULL;
-        }
+        farg.d = float64_sqrt(farg.d, &env->fp_status);
+        farg.d = float64_div(fone.d, farg.d, &env->fp_status);
+        f32 = float64_to_float32(farg.d, &env->fp_status);
+        farg.d = float32_to_float64(f32, &env->fp_status);
     }
     return farg.ll;
 }
@@ -1589,44 +1557,46 @@ uint64_t helper_frsqrte (uint64_t arg)
 /* fsel - fsel. */
 uint64_t helper_fsel (uint64_t arg1, uint64_t arg2, uint64_t arg3)
 {
-    CPU_DoubleU farg1, farg2, farg3;
+    CPU_DoubleU farg1;
 
     farg1.ll = arg1;
-    farg2.ll = arg2;
-    farg3.ll = arg3;
 
-    if (!fpisneg(farg1.d) || iszero(farg1.d))
-        return farg2.ll;
+    if ((!float64_is_neg(farg1.d) || float64_is_zero(farg1.d)) && !float64_is_nan(farg1.d))
+        return arg2;
     else
-        return farg2.ll;
+        return arg3;
 }
 
-uint32_t helper_fcmpu (uint64_t arg1, uint64_t arg2)
+void helper_fcmpu (uint64_t arg1, uint64_t arg2, uint32_t crfD)
 {
     CPU_DoubleU farg1, farg2;
     uint32_t ret = 0;
     farg1.ll = arg1;
     farg2.ll = arg2;
 
-    if (unlikely(float64_is_signaling_nan(farg1.d) ||
-                 float64_is_signaling_nan(farg2.d))) {
-        /* sNaN comparison */
-        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
+    if (unlikely(float64_is_nan(farg1.d) ||
+                 float64_is_nan(farg2.d))) {
+        ret = 0x01UL;
+    } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
+        ret = 0x08UL;
+    } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
+        ret = 0x04UL;
     } else {
-        if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
-            ret = 0x08UL;
-        } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
-            ret = 0x04UL;
-        } else {
-            ret = 0x02UL;
-        }
+        ret = 0x02UL;
     }
+
     env->fpscr &= ~(0x0F << FPSCR_FPRF);
     env->fpscr |= ret << FPSCR_FPRF;
-    return ret;
+    env->crf[crfD] = ret;
+    if (unlikely(ret == 0x01UL
+                 && (float64_is_signaling_nan(farg1.d) ||
+                     float64_is_signaling_nan(farg2.d)))) {
+        /* sNaN comparison */
+        fload_invalid_op_excp(POWERPC_EXCP_FP_VXSNAN);
+    }
 }
 
-uint32_t helper_fcmpo (uint64_t arg1, uint64_t arg2)
+void helper_fcmpo (uint64_t arg1, uint64_t arg2, uint32_t crfD)
 {
     CPU_DoubleU farg1, farg2;
     uint32_t ret = 0;
@@ -1635,6 +1605,19 @@ uint32_t helper_fcmpo (uint64_t arg1, uint64_t arg2)
 
     if (unlikely(float64_is_nan(farg1.d) ||
                  float64_is_nan(farg2.d))) {
+        ret = 0x01UL;
+    } else if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
+        ret = 0x08UL;
+    } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
+        ret = 0x04UL;
+    } else {
+        ret = 0x02UL;
+    }
+
+    env->fpscr &= ~(0x0F << FPSCR_FPRF);
+    env->fpscr |= ret << FPSCR_FPRF;
+    env->crf[crfD] = ret;
+    if (unlikely (ret == 0x01UL)) {
         if (float64_is_signaling_nan(farg1.d) ||
             float64_is_signaling_nan(farg2.d)) {
             /* sNaN comparison */
@@ -1644,18 +1627,7 @@ uint32_t helper_fcmpo (uint64_t arg1, uint64_t arg2)
             /* qNaN comparison */
             fload_invalid_op_excp(POWERPC_EXCP_FP_VXVC);
         }
-    } else {
-        if (float64_lt(farg1.d, farg2.d, &env->fp_status)) {
-            ret = 0x08UL;
-        } else if (!float64_le(farg1.d, farg2.d, &env->fp_status)) {
-            ret = 0x04UL;
-        } else {
-            ret = 0x02UL;
-        }
     }
-    env->fpscr &= ~(0x0F << FPSCR_FPRF);
-    env->fpscr |= ret << FPSCR_FPRF;
-    return ret;
 }
 
 #if !defined (CONFIG_USER_ONLY)
@@ -1862,30 +1834,16 @@ void helper_rfsvc (void)
 /* 602 specific instructions */
 /* mfrom is the most crazy instruction ever seen, imho ! */
 /* Real implementation uses a ROM table. Do the same */
+/* Extremly decomposed:
+ *                      -arg / 256
+ * return 256 * log10(10           + 1.0) + 0.5
+ */
 #if !defined (CONFIG_USER_ONLY)
-#define USE_MFROM_ROM_TABLE
 target_ulong helper_602_mfrom (target_ulong arg)
 {
     if (likely(arg < 602)) {
-#if defined(USE_MFROM_ROM_TABLE)
 #include "mfrom_table.c"
         return mfrom_ROM_table[arg];
-#else
-        double d;
-        /* Extremly decomposed:
-         *                      -arg / 256
-         * return 256 * log10(10           + 1.0) + 0.5
-         */
-        d = arg;
-        d = float64_div(d, 256, &env->fp_status);
-        d = float64_chs(d);
-        d = exp10(d); // XXX: use float emulation function
-        d = float64_add(d, 1.0, &env->fp_status);
-        d = log10(d); // XXX: use float emulation function
-        d = float64_mul(d, 256, &env->fp_status);
-        d = float64_add(d, 0.5, &env->fp_status);
-        return float64_round_to_int(d, &env->fp_status);
-#endif
     } else {
         return 0;
     }