Skip to contentSkip to conversation

target/arm: Fix some FEAT_AFP corner cases

PATCH In review Peter Maydell 3 participants 11 messages May 21 – May 22

lore
Share & tools
lore
#0 lore raw

cover target/arm: Fix some FEAT_AFP corner cases

There are a few corner cases where when we implemented FEAT_AFP we didn't update the instructions to get the FPCR.AH = 1 handling right:
* SVE2 FMAXP and FMINP need to do the AH=1 style max/min
* SVE FCVTLT to halfprec needs to use the F16 FPCR
* FLOGB needs to report when it consumed a denormal

This patchset implements fixes for these.

thanks
-- PMM

Peter Maydell (3):
target/arm: SVE2 FMAXP, FMINP must honour AH=1 target/arm: Use FPST_A64_F16 for SVE FCVTLT_hs target/arm: Set correct fp flags for FLOGB when FPCR.AH = 1

 target/arm/tcg/helper-sve-defs.h | 14 ++++++++++++++
 target/arm/tcg/sve_helper.c      | 11 +++++++++++
 target/arm/tcg/translate-sve.c   |  6 +++---
 3 files changed, 28 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 target/arm: SVE2 FMAXP, FMINP must honour AH=1

The behaviour of floating-point maximum and minimum insns has some odd special cases when FPCR.AH=1. We get this right in most places (for instance, the ASIMD FMAXP, FMINP) but forgot about it for the SVE2 versions of FMAXP and FMINP.

Fixes 384433e70983 ("target/arm: Implement FPCR.AH semantics for FMINP and FMAXP")
---
 target/arm/tcg/helper-sve-defs.h | 14 ++++++++++++++
 target/arm/tcg/sve_helper.c      |  8 ++++++++
 target/arm/tcg/translate-sve.c   |  4 ++--
 3 files changed, 24 insertions(+), 2 deletions(-)
target/arm/tcg/helper-sve-defs.h +14 -0
index c3541a8ca8..cd05dd0fb4 100644
@@ -2914,6 +2914,20 @@ DEF_HELPER_FLAGS_6(sve2_fminp_zpzz_s, TCG_CALL_NO_RWG,
 DEF_HELPER_FLAGS_6(sve2_fminp_zpzz_d, TCG_CALL_NO_RWG,
                    void, ptr, ptr, ptr, ptr, fpst, i32)
 
+DEF_HELPER_FLAGS_6(sve2_ah_fmaxp_zpzz_h, TCG_CALL_NO_RWG,
+                   void, ptr, ptr, ptr, ptr, fpst, i32)
+DEF_HELPER_FLAGS_6(sve2_ah_fmaxp_zpzz_s, TCG_CALL_NO_RWG,
+                   void, ptr, ptr, ptr, ptr, fpst, i32)
+DEF_HELPER_FLAGS_6(sve2_ah_fmaxp_zpzz_d, TCG_CALL_NO_RWG,
+                   void, ptr, ptr, ptr, ptr, fpst, i32)
+
+DEF_HELPER_FLAGS_6(sve2_ah_fminp_zpzz_h, TCG_CALL_NO_RWG,
+                   void, ptr, ptr, ptr, ptr, fpst, i32)
+DEF_HELPER_FLAGS_6(sve2_ah_fminp_zpzz_s, TCG_CALL_NO_RWG,
+                   void, ptr, ptr, ptr, ptr, fpst, i32)
+DEF_HELPER_FLAGS_6(sve2_ah_fminp_zpzz_d, TCG_CALL_NO_RWG,
+                   void, ptr, ptr, ptr, ptr, fpst, i32)
+
 DEF_HELPER_FLAGS_5(sve2_eor3, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr, i32)
 DEF_HELPER_FLAGS_5(sve2_bcax, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr, i32)
 DEF_HELPER_FLAGS_5(sve2_bsl1n, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr, i32)
target/arm/tcg/sve_helper.c +8 -0
index 062d8881bd..179cbd74fb 100644
@@ -778,6 +778,14 @@ DO_ZPZZ_PAIR_FP(sve2_fminp_zpzz_h, float16, H1_2, float16_min)
 DO_ZPZZ_PAIR_FP(sve2_fminp_zpzz_s, float32, H1_4, float32_min)
 DO_ZPZZ_PAIR_FP(sve2_fminp_zpzz_d, float64, H1_8, float64_min)
 
+DO_ZPZZ_PAIR_FP(sve2_ah_fmaxp_zpzz_h, float16, H1_2, helper_vfp_ah_maxh)
+DO_ZPZZ_PAIR_FP(sve2_ah_fmaxp_zpzz_s, float32, H1_4, helper_vfp_ah_maxs)
+DO_ZPZZ_PAIR_FP(sve2_ah_fmaxp_zpzz_d, float64, H1_8, helper_vfp_ah_maxd)
+
+DO_ZPZZ_PAIR_FP(sve2_ah_fminp_zpzz_h, float16, H1_2, helper_vfp_ah_minh)
+DO_ZPZZ_PAIR_FP(sve2_ah_fminp_zpzz_s, float32, H1_4, helper_vfp_ah_mins)
+DO_ZPZZ_PAIR_FP(sve2_ah_fminp_zpzz_d, float64, H1_8, helper_vfp_ah_mind)
+
 #undef DO_ZPZZ_PAIR_FP
 
 /* Three-operand expander, controlled by a predicate, in which the
target/arm/tcg/translate-sve.c +2 -2
index aa7d72a35e..bf9f0ae179 100644
@@ -7667,8 +7667,8 @@ TRANS_FEAT_NONSTREAMING(HISTSEG, aa64_sve2, gen_gvec_ool_arg_zzz,
 DO_ZPZZ_FP(FADDP, aa64_sme_or_sve2, sve2_faddp_zpzz)
 DO_ZPZZ_FP(FMAXNMP, aa64_sme_or_sve2, sve2_fmaxnmp_zpzz)
 DO_ZPZZ_FP(FMINNMP, aa64_sme_or_sve2, sve2_fminnmp_zpzz)
-DO_ZPZZ_FP(FMAXP, aa64_sme_or_sve2, sve2_fmaxp_zpzz)
-DO_ZPZZ_FP(FMINP, aa64_sme_or_sve2, sve2_fminp_zpzz)
+DO_ZPZZ_AH_FP(FMAXP, aa64_sme_or_sve2, sve2_fmaxp_zpzz, sve2_ah_fmaxp_zpzz)
+DO_ZPZZ_AH_FP(FMINP, aa64_sme_or_sve2, sve2_fminp_zpzz, sve2_ah_fminp_zpzz)
 
 static bool do_fmmla(DisasContext *s, arg_rrrr_esz *a,
                      gen_helper_gvec_4_ptr *fn)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] target/arm: SVE2 FMAXP, FMINP must honour AH=1

Quoted 8 lines · Peter Maydell

The behaviour of floating-point maximum and minimum insns has some odd special cases when FPCR.AH=1. We get this right in most places (for instance, the ASIMD FMAXP, FMINP) but forgot about it for the SVE2 versions of FMAXP and FMINP.

Fixes 384433e70983 ("target/arm: Implement FPCR.AH semantics for FMINP and FMAXP")
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#3 lore raw

PATCH 2/3 target/arm: Use FPST_A64_F16 for SVE FCVTLT_hs

In reply to Peter Maydell

The SVE FCVTLT_hs operation produces a halfprec result, so we should use the FPST_A64_F16 fpstatus for it. Getting this wrong means we will incorrectly set FPCR.IDC for input-denormals when FPCR.AH=1.

We missed this instruction when we updated the halfproc-to-other conversion insns to use FPST_A64_F16 in commit e07b48995aaa as part of implementing FEAT_AHP.

Fixes e07b48995aaa ("target/arm: Use FPST_A64_F16 for halfprec-to-other conversions")a
---
 target/arm/tcg/translate-sve.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/tcg/translate-sve.c +1 -1
index bf9f0ae179..59b8c494a8 100644
@@ -7827,7 +7827,7 @@ TRANS_FEAT(BFCVTNT, aa64_sme_sve_bf16, gen_gvec_fpst_arg_zpz,
            s->fpcr_ah ? FPST_AH : FPST_A64)
 
 TRANS_FEAT(FCVTLT_hs, aa64_sme_or_sve2, gen_gvec_fpst_arg_zpz,
-           gen_helper_sve2_fcvtlt_hs, a, 0, FPST_A64)
+           gen_helper_sve2_fcvtlt_hs, a, 0, FPST_A64_F16)
 TRANS_FEAT(FCVTLT_sd, aa64_sme_or_sve2, gen_gvec_fpst_arg_zpz,
            gen_helper_sve2_fcvtlt_sd, a, 0, FPST_A64)
 
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/3] target/arm: Use FPST_A64_F16 for SVE FCVTLT_hs

Quoted 2 lines · Peter Maydell

The SVE FCVTLT_hs operation produces a halfprec result, so we should use the FPST_A64_F16 fpstatus for it.

Maybe I'm reading the Arm ARM wrong (I can't find FCVTLT_hs but I can see FCVTLT in the SVE encodings). But these are all widening operations so I can see halfprec to single, not the other way around.

Quoted 26 lines

Getting this wrong means we
will incorrectly set FPCR.IDC for input-denormals when FPCR.AH=1.

We missed this instruction when we updated the halfproc-to-other conversion insns to use FPST_A64_F16 in commit e07b48995aaa as part of implementing FEAT_AHP.

Fixes e07b48995aaa ("target/arm: Use FPST_A64_F16 for halfprec-to-other conversions")a
---
 target/arm/tcg/translate-sve.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/tcg/translate-sve.c +1 -1
index bf9f0ae179..59b8c494a8 100644
@@ -7827,7 +7827,7 @@ TRANS_FEAT(BFCVTNT, aa64_sme_sve_bf16, gen_gvec_fpst_arg_zpz,
            s->fpcr_ah ? FPST_AH : FPST_A64)
 
 TRANS_FEAT(FCVTLT_hs, aa64_sme_or_sve2, gen_gvec_fpst_arg_zpz,
-           gen_helper_sve2_fcvtlt_hs, a, 0, FPST_A64)
+           gen_helper_sve2_fcvtlt_hs, a, 0, FPST_A64_F16)
 TRANS_FEAT(FCVTLT_sd, aa64_sme_or_sve2, gen_gvec_fpst_arg_zpz,
            gen_helper_sve2_fcvtlt_sd, a, 0, FPST_A64)
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#5 lore raw

Re: [PATCH 2/3] target/arm: Use FPST_A64_F16 for SVE FCVTLT_hs

Quoted 9 lines · Alex Bennée
Quoted 2 lines · Peter Maydell

The SVE FCVTLT_hs operation produces a halfprec result, so we should use the FPST_A64_F16 fpstatus for it.

Maybe I'm reading the Arm ARM wrong (I can't find FCVTLT_hs but I can see FCVTLT in the SVE encodings). But these are all widening operations so I can see halfprec to single, not the other way around.

Yes, I got the commit message wrong here; this is half to single, and we need to use the A64_F16 fpstatus for conversions from halfprec, not conversions to it. The e07b48995aaa commit message does a better job of explaining this than I did this time around.

The _hs is how the QEMU decode file splits the half to single and the single to double encodings, which are the same insn mnememonic in the architecture.

Improved commit message:

===begin===
We should be using the F16-specific float_status for conversions from half-precision, because halfprec inputs never set Input Denormal. If we use the FPST_A64 fpstatus then we will incorrectly set FPCR.IDC for input-denormals when FPCR.AH=1.

In commit e07b48995aaa we updated most of the halfprec-to-other conversion insns to use FPST_A64_F16 as part of implementing FEAT_AHP. However we missed the SVE FCVTLT instruction, which has a halfprec-to-single encoding.

Correct the FPST we use for the hs variant of FCVTLT.

[cc, fixes, &c]
===endit===

thanks
-- PMM

Reviewed-by
#6 lore raw

Re: [PATCH 2/3] target/arm: Use FPST_A64_F16 for SVE FCVTLT_hs

Quoted 33 lines · Peter Maydell
Quoted 9 lines · Alex Bennée
Quoted 2 lines · Peter Maydell

The SVE FCVTLT_hs operation produces a halfprec result, so we should use the FPST_A64_F16 fpstatus for it.

Maybe I'm reading the Arm ARM wrong (I can't find FCVTLT_hs but I can see FCVTLT in the SVE encodings). But these are all widening operations so I can see halfprec to single, not the other way around.

Yes, I got the commit message wrong here; this is half to single, and we need to use the A64_F16 fpstatus for conversions from halfprec, not conversions to it. The e07b48995aaa commit message does a better job of explaining this than I did this time around.

The _hs is how the QEMU decode file splits the half to single and the single to double encodings, which are the same insn mnememonic in the architecture.

Improved commit message:

===begin===
We should be using the F16-specific float_status for conversions from half-precision, because halfprec inputs never set Input Denormal. If we use the FPST_A64 fpstatus then we will incorrectly set FPCR.IDC for input-denormals when FPCR.AH=1.

In commit e07b48995aaa we updated most of the halfprec-to-other conversion insns to use FPST_A64_F16 as part of implementing FEAT_AHP. However we missed the SVE FCVTLT instruction, which has a halfprec-to-single encoding.

Quoted 5 lines

Correct the FPST we use for the hs variant of FCVTLT.

[cc, fixes, &c]
===endit===

Quoted 3 lines

thanks
-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] target/arm: Use FPST_A64_F16 for SVE FCVTLT_hs

In reply to Peter Maydell
Quoted 1 line · Peter Maydell

The SVE FCVTLT_hs operation produces a halfprec result, so we should

halfprec input.

Quoted 19 lines

use the FPST_A64_F16 fpstatus for it. Getting this wrong means we will incorrectly set FPCR.IDC for input-denormals when FPCR.AH=1.

We missed this instruction when we updated the halfproc-to-other conversion insns to use FPST_A64_F16 in commit e07b48995aaa as part of implementing FEAT_AHP.

Fixes e07b48995aaa ("target/arm: Use FPST_A64_F16 for halfprec-to-other conversions")a
---
  target/arm/tcg/translate-sve.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/tcg/translate-sve.c +0 -0
index bf9f0ae179..59b8c494a8 100644

target/arm/tcg/translate-sve.c TRANS_FEAT(BFCVTNT, aa64_sme_sve_bf16, gen_gvec_fpst_arg_zpz, · line 7833

Quoted 8 lines
target/arm/tcg/translate-sve.c +1 -1
@@ -7827,7 +7827,7 @@ TRANS_FEAT(BFCVTNT, aa64_sme_sve_bf16, gen_gvec_fpst_arg_zpz,
             s->fpcr_ah ? FPST_AH : FPST_A64)
 
  TRANS_FEAT(FCVTLT_hs, aa64_sme_or_sve2, gen_gvec_fpst_arg_zpz,
-           gen_helper_sve2_fcvtlt_hs, a, 0, FPST_A64)
+           gen_helper_sve2_fcvtlt_hs, a, 0, FPST_A64_F16)
  TRANS_FEAT(FCVTLT_sd, aa64_sme_or_sve2, gen_gvec_fpst_arg_zpz,
             gen_helper_sve2_fcvtlt_sd, a, 0, FPST_A64)
 

Otherwise,

r~

#8 lore raw

PATCH 3/3 target/arm: Set correct fp flags for FLOGB when FPCR.AH = 1

In reply to Peter Maydell

Our implementation of the FLOGB insn does the operations entirely in the helper function, without needing to use fpu functions. This means it needs to handle all the fp status flags itself. We aren't setting float_flag_input_denormal_used when we use (i.e. do not flush to zero) an input denormal, which means that FPCR.IDC isn't set when it should be for FPCR.AH=1. We missed this when we added float_flag_input_denormal_used and made the fpu/ code set it.

Add the missing float_raise().

Fixes d38a57a3f ("target/arm: Enable FEAT_AFP for '-cpu max'")
---
 target/arm/tcg/sve_helper.c | 3 +++
 1 file changed, 3 insertions(+)
target/arm/tcg/sve_helper.c +3 -0
index 179cbd74fb..d884ba474f 100644
@@ -5036,6 +5036,7 @@ static int16_t do_float16_logb_as_int(float16 a, float_status *s)
         if (frac != 0) {
             if (!get_flush_inputs_to_zero(s)) {
                 /* denormal: bias - fractional_zeros */
+                float_raise(float_flag_input_denormal_used, s);
                 return -15 - clz32(frac);
             }
             /* flush to zero */
@@ -5064,6 +5065,7 @@ static int32_t do_float32_logb_as_int(float32 a, float_status *s)
         if (frac != 0) {
             if (!get_flush_inputs_to_zero(s)) {
                 /* denormal: bias - fractional_zeros */
+                float_raise(float_flag_input_denormal_used, s);
                 return -127 - clz32(frac);
             }
             /* flush to zero */
@@ -5092,6 +5094,7 @@ static int64_t do_float64_logb_as_int(float64 a, float_status *s)
         if (frac != 0) {
             if (!get_flush_inputs_to_zero(s)) {
                 /* denormal: bias - fractional_zeros */
+                float_raise(float_flag_input_denormal_used, s);
                 return -1023 - clz64(frac);
             }
             /* flush to zero */
Signature · 1 line
2.43.0
Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] target/arm: Set correct fp flags for FLOGB when FPCR.AH = 1

Quoted 9 lines · Peter Maydell

Our implementation of the FLOGB insn does the operations entirely in the helper function, without needing to use fpu functions. This means it needs to handle all the fp status flags itself. We aren't setting float_flag_input_denormal_used when we use (i.e. do not flush to zero) an input denormal, which means that FPCR.IDC isn't set when it should be for FPCR.AH=1. We missed this when we added float_flag_input_denormal_used and made the fpu/ code set it.

Quoted 5 lines

Add the missing float_raise().

Fixes d38a57a3f ("target/arm: Enable FEAT_AFP for '-cpu max'")
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] target/arm: Set correct fp flags for FLOGB when FPCR.AH = 1

In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

Our implementation of the FLOGB insn does the operations entirely in the helper function, without needing to use fpu functions. This means it needs to handle all the fp status flags itself. We aren't setting float_flag_input_denormal_used when we use (i.e. do not flush to zero) an input denormal, which means that FPCR.IDC isn't set when it should be for FPCR.AH=1. We missed this when we added float_flag_input_denormal_used and made the fpu/ code set it.

Quoted 8 lines

Add the missing float_raise().

Fixes d38a57a3f ("target/arm: Enable FEAT_AFP for '-cpu max'")
---
  target/arm/tcg/sve_helper.c | 3 +++
  1 file changed, 3 insertions(+)

r~