diff mbox series

[11/17] target/hexagon: Use float32_muladd for helper_sffm[as]_lib

Message ID 20241208224844.570491-12-richard.henderson@linaro.org
State New
Headers show
Series softfloat, hexagon: Cleanup fmaf | expand

Commit Message

Richard Henderson Dec. 8, 2024, 10:48 p.m. UTC
There are multiple special cases for this instruction.
(1) The saturate to normal maximum instead of overflow to infinity is
    handled by the new float_round_nearest_even_max rounding mode.
(2) The 0 * n + c special case is handled by the new
    float_muladd_suppress_add_product_zero flag.
(3) The Inf - Inf -> 0 special case can be detected after the fact
    by examining float_flag_invalid_isi.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 target/hexagon/op_helper.c | 105 +++++++++----------------------------
 1 file changed, 26 insertions(+), 79 deletions(-)

Comments

Brian Cain Dec. 10, 2024, 3:29 p.m. UTC | #1
On 12/8/2024 4:48 PM, Richard Henderson wrote:
> There are multiple special cases for this instruction.
> (1) The saturate to normal maximum instead of overflow to infinity is
>      handled by the new float_round_nearest_even_max rounding mode.
> (2) The 0 * n + c special case is handled by the new
>      float_muladd_suppress_add_product_zero flag.
> (3) The Inf - Inf -> 0 special case can be detected after the fact
>      by examining float_flag_invalid_isi.
>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>   target/hexagon/op_helper.c | 105 +++++++++----------------------------
>   1 file changed, 26 insertions(+), 79 deletions(-)
>
> diff --git a/target/hexagon/op_helper.c b/target/hexagon/op_helper.c
> index eb010422bf..26e329f7b9 100644
> --- a/target/hexagon/op_helper.c
> +++ b/target/hexagon/op_helper.c
> @@ -1171,24 +1171,6 @@ float32 HELPER(sffma)(CPUHexagonState *env, float32 RxV,
>       return RxV;
>   }
>   
> -static bool is_zero_prod(float32 a, float32 b)
> -{
> -    return ((float32_is_zero(a) && is_finite(b)) ||
> -            (float32_is_zero(b) && is_finite(a)));
> -}
> -
> -static float32 check_nan(float32 dst, float32 x, float_status *fp_status)
> -{
> -    float32 ret = dst;
> -    if (float32_is_any_nan(x)) {
> -        if (extract32(x, 22, 1) == 0) {
> -            float_raise(float_flag_invalid, fp_status);
> -        }
> -        ret = make_float32(0xffffffff);    /* nan */
> -    }
> -    return ret;
> -}
> -
>   float32 HELPER(sffma_sc)(CPUHexagonState *env, float32 RxV,
>                            float32 RsV, float32 RtV, float32 PuV)
>   {
> @@ -1210,78 +1192,43 @@ float32 HELPER(sffms)(CPUHexagonState *env, float32 RxV,
>       return RxV;
>   }
>   
> -static bool is_inf_prod(int32_t a, int32_t b)
> +static float32 do_sffma_lib(CPUHexagonState *env, float32 RxV,
> +                            float32 RsV, float32 RtV, int negate)
>   {
> -    return (float32_is_infinity(a) && float32_is_infinity(b)) ||
> -           (float32_is_infinity(a) && is_finite(b) && !float32_is_zero(b)) ||
> -           (float32_is_infinity(b) && is_finite(a) && !float32_is_zero(a));
> +    int flags;
> +
> +    arch_fpop_start(env);
> +
> +    set_float_rounding_mode(float_round_nearest_even_max, &env->fp_status);
> +    RxV = float32_muladd(RsV, RtV, RxV,
> +                         negate | float_muladd_suppress_add_product_zero,
> +                         &env->fp_status);
> +
> +    flags = get_float_exception_flags(&env->fp_status);
> +    if (flags) {
> +        /* Flags are suppressed by this instruction. */
> +        set_float_exception_flags(0, &env->fp_status);
> +
> +        /* Return 0 for Inf - Inf. */
> +        if (flags & float_flag_invalid_isi) {
> +            RxV = 0;
> +        }
> +    }
> +
> +    arch_fpop_end(env);
> +    return RxV;
>   }
>   
>   float32 HELPER(sffma_lib)(CPUHexagonState *env, float32 RxV,
>                             float32 RsV, float32 RtV)
>   {
> -    bool infinp;
> -    bool infminusinf;
> -    float32 tmp;
> -
> -    arch_fpop_start(env);
> -    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
> -    infminusinf = float32_is_infinity(RxV) &&
> -                  is_inf_prod(RsV, RtV) &&
> -                  (fGETBIT(31, RsV ^ RxV ^ RtV) != 0);
> -    infinp = float32_is_infinity(RxV) ||
> -             float32_is_infinity(RtV) ||
> -             float32_is_infinity(RsV);
> -    RxV = check_nan(RxV, RxV, &env->fp_status);
> -    RxV = check_nan(RxV, RsV, &env->fp_status);
> -    RxV = check_nan(RxV, RtV, &env->fp_status);
> -    tmp = internal_fmafx(RsV, RtV, RxV, 0, &env->fp_status);
> -    if (!(float32_is_zero(RxV) && is_zero_prod(RsV, RtV))) {
> -        RxV = tmp;
> -    }
> -    set_float_exception_flags(0, &env->fp_status);
> -    if (float32_is_infinity(RxV) && !infinp) {
> -        RxV = RxV - 1;
> -    }
> -    if (infminusinf) {
> -        RxV = 0;
> -    }
> -    arch_fpop_end(env);
> -    return RxV;
> +    return do_sffma_lib(env, RxV, RsV, RtV, 0);
>   }
>   
>   float32 HELPER(sffms_lib)(CPUHexagonState *env, float32 RxV,
>                             float32 RsV, float32 RtV)
>   {
> -    bool infinp;
> -    bool infminusinf;
> -    float32 tmp;
> -
> -    arch_fpop_start(env);
> -    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
> -    infminusinf = float32_is_infinity(RxV) &&
> -                  is_inf_prod(RsV, RtV) &&
> -                  (fGETBIT(31, RsV ^ RxV ^ RtV) == 0);
> -    infinp = float32_is_infinity(RxV) ||
> -             float32_is_infinity(RtV) ||
> -             float32_is_infinity(RsV);
> -    RxV = check_nan(RxV, RxV, &env->fp_status);
> -    RxV = check_nan(RxV, RsV, &env->fp_status);
> -    RxV = check_nan(RxV, RtV, &env->fp_status);
> -    float32 minus_RsV = float32_sub(float32_zero, RsV, &env->fp_status);
> -    tmp = internal_fmafx(minus_RsV, RtV, RxV, 0, &env->fp_status);
> -    if (!(float32_is_zero(RxV) && is_zero_prod(RsV, RtV))) {
> -        RxV = tmp;
> -    }
> -    set_float_exception_flags(0, &env->fp_status);
> -    if (float32_is_infinity(RxV) && !infinp) {
> -        RxV = RxV - 1;
> -    }
> -    if (infminusinf) {
> -        RxV = 0;
> -    }
> -    arch_fpop_end(env);
> -    return RxV;
> +    return do_sffma_lib(env, RxV, RsV, RtV, float_muladd_negate_product);
>   }
>   
Reviewed-by: Brian Cain <brian.cain@oss.qualcomm.com>
>   float64 HELPER(dfmpyfix)(CPUHexagonState *env, float64 RssV, float64 RttV)
diff mbox series

Patch

diff --git a/target/hexagon/op_helper.c b/target/hexagon/op_helper.c
index eb010422bf..26e329f7b9 100644
--- a/target/hexagon/op_helper.c
+++ b/target/hexagon/op_helper.c
@@ -1171,24 +1171,6 @@  float32 HELPER(sffma)(CPUHexagonState *env, float32 RxV,
     return RxV;
 }
 
-static bool is_zero_prod(float32 a, float32 b)
-{
-    return ((float32_is_zero(a) && is_finite(b)) ||
-            (float32_is_zero(b) && is_finite(a)));
-}
-
-static float32 check_nan(float32 dst, float32 x, float_status *fp_status)
-{
-    float32 ret = dst;
-    if (float32_is_any_nan(x)) {
-        if (extract32(x, 22, 1) == 0) {
-            float_raise(float_flag_invalid, fp_status);
-        }
-        ret = make_float32(0xffffffff);    /* nan */
-    }
-    return ret;
-}
-
 float32 HELPER(sffma_sc)(CPUHexagonState *env, float32 RxV,
                          float32 RsV, float32 RtV, float32 PuV)
 {
@@ -1210,78 +1192,43 @@  float32 HELPER(sffms)(CPUHexagonState *env, float32 RxV,
     return RxV;
 }
 
-static bool is_inf_prod(int32_t a, int32_t b)
+static float32 do_sffma_lib(CPUHexagonState *env, float32 RxV,
+                            float32 RsV, float32 RtV, int negate)
 {
-    return (float32_is_infinity(a) && float32_is_infinity(b)) ||
-           (float32_is_infinity(a) && is_finite(b) && !float32_is_zero(b)) ||
-           (float32_is_infinity(b) && is_finite(a) && !float32_is_zero(a));
+    int flags;
+
+    arch_fpop_start(env);
+
+    set_float_rounding_mode(float_round_nearest_even_max, &env->fp_status);
+    RxV = float32_muladd(RsV, RtV, RxV,
+                         negate | float_muladd_suppress_add_product_zero,
+                         &env->fp_status);
+
+    flags = get_float_exception_flags(&env->fp_status);
+    if (flags) {
+        /* Flags are suppressed by this instruction. */
+        set_float_exception_flags(0, &env->fp_status);
+
+        /* Return 0 for Inf - Inf. */
+        if (flags & float_flag_invalid_isi) {
+            RxV = 0;
+        }
+    }
+
+    arch_fpop_end(env);
+    return RxV;
 }
 
 float32 HELPER(sffma_lib)(CPUHexagonState *env, float32 RxV,
                           float32 RsV, float32 RtV)
 {
-    bool infinp;
-    bool infminusinf;
-    float32 tmp;
-
-    arch_fpop_start(env);
-    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
-    infminusinf = float32_is_infinity(RxV) &&
-                  is_inf_prod(RsV, RtV) &&
-                  (fGETBIT(31, RsV ^ RxV ^ RtV) != 0);
-    infinp = float32_is_infinity(RxV) ||
-             float32_is_infinity(RtV) ||
-             float32_is_infinity(RsV);
-    RxV = check_nan(RxV, RxV, &env->fp_status);
-    RxV = check_nan(RxV, RsV, &env->fp_status);
-    RxV = check_nan(RxV, RtV, &env->fp_status);
-    tmp = internal_fmafx(RsV, RtV, RxV, 0, &env->fp_status);
-    if (!(float32_is_zero(RxV) && is_zero_prod(RsV, RtV))) {
-        RxV = tmp;
-    }
-    set_float_exception_flags(0, &env->fp_status);
-    if (float32_is_infinity(RxV) && !infinp) {
-        RxV = RxV - 1;
-    }
-    if (infminusinf) {
-        RxV = 0;
-    }
-    arch_fpop_end(env);
-    return RxV;
+    return do_sffma_lib(env, RxV, RsV, RtV, 0);
 }
 
 float32 HELPER(sffms_lib)(CPUHexagonState *env, float32 RxV,
                           float32 RsV, float32 RtV)
 {
-    bool infinp;
-    bool infminusinf;
-    float32 tmp;
-
-    arch_fpop_start(env);
-    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
-    infminusinf = float32_is_infinity(RxV) &&
-                  is_inf_prod(RsV, RtV) &&
-                  (fGETBIT(31, RsV ^ RxV ^ RtV) == 0);
-    infinp = float32_is_infinity(RxV) ||
-             float32_is_infinity(RtV) ||
-             float32_is_infinity(RsV);
-    RxV = check_nan(RxV, RxV, &env->fp_status);
-    RxV = check_nan(RxV, RsV, &env->fp_status);
-    RxV = check_nan(RxV, RtV, &env->fp_status);
-    float32 minus_RsV = float32_sub(float32_zero, RsV, &env->fp_status);
-    tmp = internal_fmafx(minus_RsV, RtV, RxV, 0, &env->fp_status);
-    if (!(float32_is_zero(RxV) && is_zero_prod(RsV, RtV))) {
-        RxV = tmp;
-    }
-    set_float_exception_flags(0, &env->fp_status);
-    if (float32_is_infinity(RxV) && !infinp) {
-        RxV = RxV - 1;
-    }
-    if (infminusinf) {
-        RxV = 0;
-    }
-    arch_fpop_end(env);
-    return RxV;
+    return do_sffma_lib(env, RxV, RsV, RtV, float_muladd_negate_product);
 }
 
 float64 HELPER(dfmpyfix)(CPUHexagonState *env, float64 RssV, float64 RttV)