target-arm: Implement UDIV/SDIV and fused multiply-accumulate
v2 cover target-arm: Implement UDIV/SDIV and fused multiply-accumulate
This patch series implements support in QEMU for some (user-mode) instructions which are present on some recent processors (eg Cortex-A15).
The new instructions are UDIV/SDIV (previously only available as a Thumb encoding for R and M-class cores, now an optional extension to ARMv7A), and the fused-multiply-accumulate instructions VFMA, VFMS, VFNMA, VFNMS. We don't (yet) support a specific processor for system mode with these extensions, so they are enabled only for ARM_CPUID_ANY.
The most interesting part of this is the fused multiply-accumulate support which I have added to softfloat. Although this is only used by ARM at the moment I've put in some flags which should be sufficient for the variants provided by other architectures. (Specifically, x86, Itanium and SPARC64 VI all need the "negate product" flag since they have "-(a*b)+c" instructions. PPC needs the "negate result" and "negate addend" flags.)
Changes v1 -> v2:
* incorporated RTH's suggestions in the main "implement fused multiply-add" patch (except the one about #ifdef placement, which I disagree with)
* added a patch renaming ARM_FEATURE_DIV to ARM_FEATURE_THUMB_DIV; this splits "core implements Thumb encodings of UDIV,SDIV" from "core implements ARM encodings of UDIV,SDIV"; we don't have any cores at the moment where this would make a difference, but they are architecturally distinct and the split will avoid having to rejig things for R4 support (R4 has only the Thumb encodings)
* rebased (and dropped the 'reinstate target-specific NaN handling' patch as it has been committed)
Peter Maydell (5):
target-arm: v6 media multiply space: UNDEF on unassigned encodings
target-arm: Rename ARM_FEATURE_DIV to _THUMB_DIV
target-arm: Add ARM UDIV/SDIV support
softfloat: Implement fused multiply-add
target-arm: Implement VFPv4 fused multiply-accumulate insns
fpu/softfloat-specialize.h | 178 ++++++++++++++++++ fpu/softfloat.c | 427 ++++++++++++++++++++++++++++++++++++++++++++ fpu/softfloat.h | 14 ++ target-arm/cpu.h | 4 +- target-arm/helper.c | 21 ++- target-arm/helper.h | 3 + target-arm/translate.c | 118 ++++++++++++- 7 files changed, 757 insertions(+), 8 deletions(-)
PATCH v2 1/5 target-arm: v6 media multiply space: UNDEF on unassigned encodings
Clean up the decoding of the v6 media multiply space so that we UNDEF on unassigned encodings rather than randomly interpreting them as some instruction in this space.
--- target-arm/translate.c | 24 ++++++++++++++++++++---- 1 files changed, 20 insertions(+), 4 deletions(-)
target-arm/translate.c +20 -4
@@ -7569,11 +7569,16 @@ static void disas_arm_insn(CPUState * env, DisasContext *s) }break ;case 2 :/* Multiplies (Type 3). */ - tmp =load_reg (s, rm); - tmp2 =load_reg (s, rs); -if (insn & (1 <<20 )) { +switch ((insn >>20 ) &0x7 ) { +case 5 : +if (((insn >>6 ) ^ (insn >>7 )) &1 ) { +/* op2 not 00x or 11x : UNDEF */ + goto illegal_op; + }/* Signed multiply most significant [accumulate]. (SMMUL, SMMLA, SMMLS) */ + tmp =load_reg (s, rm); + tmp2 =load_reg (s, rs); tmp64 =gen_muls_i64_i32 (tmp, tmp2);if (rd !=15 ) {
@@ -7592,7 +7597,15 @@ static void disas_arm_insn(CPUState * env, DisasContext *s)tcg_gen_trunc_i64_i32 (tmp, tmp64);tcg_temp_free_i64 (tmp64);store_reg (s, rn, tmp); - }else { +break ; +case 0 : +case 4 : +/* SMLAD, SMUAD, SMLSD, SMUSD, SMLALD, SMLSLD */ +if (insn & (1 <<7 )) { + goto illegal_op; + } + tmp =load_reg (s, rm); + tmp2 =load_reg (s, rs);if (insn & (1 <<5 ))gen_swap_half (tmp2);gen_smul_dual (tmp, tmp2);
@@ -7625,6 +7638,9 @@ static void disas_arm_insn(CPUState * env, DisasContext *s) }store_reg (s, rn, tmp); } +break ; +default : + goto illegal_op; }break ;case 3 :
Signature · 1 line
1.7.1
PATCH v2 2/5 target-arm: Rename ARM_FEATURE_DIV to _THUMB_DIV
In reply to Peter MaydellRename the ARM_FEATURE_DIV feature bit to _THUMB_DIV, to make room for a new feature switch enabling DIV in the ARM encoding. (Cores may implement either (a) no divide insns
(b) divide insns in Thumb encodings only (c) divide insns in both ARM and Thumb encodings.)
--- target-arm/cpu.h | 2 +- target-arm/helper.c | 4 ++-- target-arm/translate.c | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-)
target-arm/cpu.h +1 -1
@@ -366,7 +366,7 @@ enum arm_features {ARM_FEATURE_VFP3 ,ARM_FEATURE_VFP_FP16 ,ARM_FEATURE_NEON , -ARM_FEATURE_DIV , +ARM_FEATURE_THUMB_DIV ,/* divide supported in Thumb encoding */ ARM_FEATURE_M ,/* Microcontroller profile. */ ARM_FEATURE_OMAPCP ,/* OMAP specific CP15 ops handling. */ ARM_FEATURE_THUMB2EE,
target-arm/helper.c +2 -2
@@ -193,7 +193,7 @@ static void cpu_reset_model_id(CPUARMState *env, uint32_t id)set_feature (env,ARM_FEATURE_THUMB2 );set_feature (env,ARM_FEATURE_V7 );set_feature (env,ARM_FEATURE_M ); -set_feature (env,ARM_FEATURE_DIV ); +set_feature (env,ARM_FEATURE_THUMB_DIV );break ;case ARM_CPUID_ANY :/* For userspace emulation. */ set_feature (env,ARM_FEATURE_V4T );
@@ -207,7 +207,7 @@ static void cpu_reset_model_id(CPUARMState *env, uint32_t id)set_feature (env,ARM_FEATURE_VFP_FP16 );set_feature (env,ARM_FEATURE_NEON );set_feature (env,ARM_FEATURE_THUMB2EE ); -set_feature (env,ARM_FEATURE_DIV ); +set_feature (env,ARM_FEATURE_THUMB_DIV );set_feature (env,ARM_FEATURE_V7MP );break ;case ARM_CPUID_TI915T :
target-arm/translate.c +2 -1
@@ -8513,8 +8513,9 @@ static int disas_thumb2_insn(CPUState *env, DisasContext *s, uint16_t insn_hw1) tmp2 =load_reg (s, rm);if ((op &0x50 ) ==0x10 ) {/* sdiv, udiv */ -if (!arm_feature (env,ARM_FEATURE_DIV )) +if (!arm_feature (env,ARM_FEATURE_THUMB_DIV )) { goto illegal_op; + }if (op &0x20 )gen_helper_udiv (tmp, tmp, tmp2);else
Signature · 1 line
1.7.1
PATCH v2 3/5 target-arm: Add ARM UDIV/SDIV support
In reply to Peter MaydellAdd support for UDIV and SDIV in ARM mode. This is a new optional feature for A profile cores (Thumb mode has had UDIV and SDIV for M profile cores for some time).
--- target-arm/cpu.h | 1 + target-arm/helper.c | 5 ++++- target-arm/translate.c | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletions(-)
target-arm/cpu.h +1 -0
@@ -375,6 +375,7 @@ enum arm_features {ARM_FEATURE_V5 ,ARM_FEATURE_STRONGARM ,ARM_FEATURE_VAPA ,/* cp15 VA to PA lookups */ + ARM_FEATURE_ARM_DIV, /* divide supported in ARM encoding */ };static inline int arm_feature (CPUARMState *env,int feature)
target-arm/helper.c +4 -1
@@ -207,7 +207,7 @@ static void cpu_reset_model_id(CPUARMState *env, uint32_t id)set_feature (env,ARM_FEATURE_VFP_FP16 );set_feature (env,ARM_FEATURE_NEON );set_feature (env,ARM_FEATURE_THUMB2EE ); -set_feature (env,ARM_FEATURE_THUMB_DIV ); +set_feature (env,ARM_FEATURE_ARM_DIV );set_feature (env,ARM_FEATURE_V7MP );break ;case ARM_CPUID_TI915T :
@@ -261,6 +261,9 @@ static void cpu_reset_model_id(CPUARMState *env, uint32_t id)if (arm_feature (env,ARM_FEATURE_V7 )) {set_feature (env,ARM_FEATURE_VAPA ); } +if (arm_feature (env,ARM_FEATURE_ARM_DIV )) { +set_feature (env,ARM_FEATURE_THUMB_DIV ); + } }void cpu_reset (CPUARMState *env)
target-arm/translate.c +19 -0
@@ -7639,6 +7639,25 @@ static void disas_arm_insn(CPUState * env, DisasContext *s)store_reg (s, rn, tmp); }break ; +case 1 : +case 3 : +/* SDIV, UDIV */ +if (!arm_feature (env,ARM_FEATURE_ARM_DIV )) { + goto illegal_op; + } +if (((insn >>5 ) &7 ) || (rd !=15 )) { + goto illegal_op; + } + tmp =load_reg (s, rm); + tmp2 =load_reg (s, rs); +if (insn & (1 <<21 )) { +gen_helper_udiv (tmp, tmp, tmp2); + }else { +gen_helper_sdiv (tmp, tmp, tmp2); + } +tcg_temp_free_i32 (tmp2); +store_reg (s, rn, tmp); +break ;default : goto illegal_op; }
Signature · 1 line
1.7.1
PATCH v2 4/5 softfloat: Implement fused multiply-add
In reply to Peter MaydellImplement fused multiply-add as a softfloat primitive. This implements "a+b*c" as a single step without any intermediate rounding; it is specified in IEEE 754-2008 and implemented in a number of CPUs.
--- fpu/softfloat-specialize.h | 178 ++++++++++++++++++ fpu/softfloat.c | 427 ++++++++++++++++++++++++++++++++++++++++++++ fpu/softfloat.h | 14 ++ 3 files changed, 619 insertions(+), 0 deletions(-)
fpu/softfloat-specialize.h +178 -0
@@ -420,6 +420,82 @@ static int pickNaN(flag aIsQNaN, flag aIsSNaN, flag bIsQNaN, flag bIsSNaN,#endif /*---------------------------------------------------------------------------- +| Select which NaN to propagate for a three-input operation. +| For the moment we assume that no CPU needs the 'larger significand' +| information. +| Return values : 0 : a; 1 : b; 2 : c; 3 : default-NaN +*----------------------------------------------------------------------------*/ +#if defined(TARGET_ARM ) +static int pickNaNMulAdd (flag aIsQNaN,flag aIsSNaN,flag bIsQNaN,flag bIsSNaN, + flag cIsQNaN, flag cIsSNaN, flag infzero STATUS_PARAM) +{ +/* For ARM, the (inf,zero,qnan) case sets InvalidOp and returns +* the default NaN +*/ +if (infzero && cIsQNaN) { + float_raise(float_flag_invalid STATUS_VAR); +return 3 ; + } + +/* This looks different from the ARM ARM pseudocode, because the ARM ARM +* puts the operands to a fused mac operation (a*b)+c in the order c,a,b. +*/ +if (cIsSNaN) { +return 2 ; + }else if (aIsSNaN) { +return 0 ; + }else if (bIsSNaN) { +return 1 ; + }else if (cIsQNaN) { +return 2 ; + }else if (aIsQNaN) { +return 0 ; + }else { +return 1 ; + } +} +#elif defined(TARGET_PPC ) +static int pickNaNMulAdd (flag aIsQNaN,flag aIsSNaN,flag bIsQNaN,flag bIsSNaN, + flag cIsQNaN, flag cIsSNaN, flag infzero STATUS_PARAM) +{ +/* For PPC, the (inf,zero,qnan) case sets InvalidOp, but we prefer +* to return an input NaN if we have one (ie c) rather than generating +* a default NaN +*/ +if (infzero) { + float_raise(float_flag_invalid STATUS_VAR); +return 2 ; + } + +/* If fRA is a NaN return it; otherwise if fRB is a NaN return it; +* otherwise return fRC. Note that muladd on PPC is (fRA * fRC) + frB +*/ +if (aIsSNaN || aIsQNaN) { +return 0 ; + }else if (cIsSNaN || cIsQNaN) { +return 2 ; + }else { +return 1 ; + } +} +#else +/* A default implementation: prefer a to b to c. +* This is unlikely to actually match any real implementation. +*/ +static int pickNaNMulAdd (flag aIsQNaN,flag aIsSNaN,flag bIsQNaN,flag bIsSNaN, + flag cIsQNaN, flag cIsSNaN, flag infzero STATUS_PARAM) +{ +if (aIsSNaN || aIsQNaN) { +return 0 ; + }else if (bIsSNaN || bIsQNaN) { +return 1 ; + }else { +return 2 ; + } +} +#endif + +/*---------------------------------------------------------------------------- | Takes two single-precision floating-point values `a' and `b', one of which | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a | signaling NaN, the invalid exception is raised.
@@ -460,6 +536,57 @@ static float32 propagateFloat32NaN( float32 a, float32 b STATUS_PARAM) }/*---------------------------------------------------------------------------- +| Takes three single-precision floating-point values `a', `b' and `c', one of +| which is a NaN, and returns the appropriate NaN result. If any of `a', +| `b' or `c' is a signaling NaN, the invalid exception is raised. +| The input infzero indicates whether a*b was 0*inf or inf*0 (in which case +| obviously c is a NaN, and whether to propagate c or some other NaN is +| implementation defined). +*----------------------------------------------------------------------------*/ + +static float32 propagateFloat32MulAddNaN (float32 a,float32 b, + float32 c, flag infzero STATUS_PARAM) +{ +flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN, + cIsQuietNaN, cIsSignalingNaN; +int which; + + aIsQuietNaN =float32_is_quiet_nan (a); + aIsSignalingNaN =float32_is_signaling_nan (a); + bIsQuietNaN =float32_is_quiet_nan (b); + bIsSignalingNaN =float32_is_signaling_nan (b); + cIsQuietNaN =float32_is_quiet_nan (c); + cIsSignalingNaN =float32_is_signaling_nan (c); + +if (aIsSignalingNaN | bIsSignalingNaN | cIsSignalingNaN) { + float_raise(float_flag_invalid STATUS_VAR); + } + + which =pickNaNMulAdd (aIsQuietNaN, aIsSignalingNaN, + bIsQuietNaN, bIsSignalingNaN, + cIsQuietNaN, cIsSignalingNaN, infzero STATUS_VAR); + +if (STATUS (default_nan_mode)) { +/* Note that this check is after pickNaNMulAdd so that function +* has an opportunity to set the Invalid flag. +*/ +return float32_default_nan; + } + +switch (which) { +case 0 : +return float32_maybe_silence_nan (a); +case 1 : +return float32_maybe_silence_nan (b); +case 2 : +return float32_maybe_silence_nan (c); +case 3 : +default : +return float32_default_nan; + } +} + +/*---------------------------------------------------------------------------- | Returns 1 if the double-precision floating-point value `a' is a quiet | NaN; otherwise returns 0. *----------------------------------------------------------------------------*/
@@ -596,6 +723,57 @@ static float64 propagateFloat64NaN( float64 a, float64 b STATUS_PARAM) }/*---------------------------------------------------------------------------- +| Takes three double-precision floating-point values `a', `b' and `c', one of +| which is a NaN, and returns the appropriate NaN result. If any of `a', +| `b' or `c' is a signaling NaN, the invalid exception is raised. +| The input infzero indicates whether a*b was 0*inf or inf*0 (in which case +| obviously c is a NaN, and whether to propagate c or some other NaN is +| implementation defined). +*----------------------------------------------------------------------------*/ + +static float64 propagateFloat64MulAddNaN (float64 a,float64 b, + float64 c, flag infzero STATUS_PARAM) +{ +flag aIsQuietNaN, aIsSignalingNaN, bIsQuietNaN, bIsSignalingNaN, + cIsQuietNaN, cIsSignalingNaN; +int which; + + aIsQuietNaN =float64_is_quiet_nan (a); + aIsSignalingNaN =float64_is_signaling_nan (a); + bIsQuietNaN =float64_is_quiet_nan (b); + bIsSignalingNaN =float64_is_signaling_nan (b); + cIsQuietNaN =float64_is_quiet_nan (c); + cIsSignalingNaN =float64_is_signaling_nan (c); + +if (aIsSignalingNaN | bIsSignalingNaN | cIsSignalingNaN) { + float_raise(float_flag_invalid STATUS_VAR); + } + + which =pickNaNMulAdd (aIsQuietNaN, aIsSignalingNaN, + bIsQuietNaN, bIsSignalingNaN, + cIsQuietNaN, cIsSignalingNaN, infzero STATUS_VAR); + +if (STATUS (default_nan_mode)) { +/* Note that this check is after pickNaNMulAdd so that function +* has an opportunity to set the Invalid flag. +*/ +return float64_default_nan; + } + +switch (which) { +case 0 : +return float64_maybe_silence_nan (a); +case 1 : +return float64_maybe_silence_nan (b); +case 2 : +return float64_maybe_silence_nan (c); +case 3 : +default : +return float64_default_nan; + } +} + +/*---------------------------------------------------------------------------- | Returns 1 if the extended double-precision floating-point value `a' is a | quiet NaN; otherwise returns 0. This slightly differs from the same | function for other types as floatx80 has an explicit bit.
fpu/softfloat.c +427 -0
439 lines not on this page · raw message
fpu/softfloat.h +14 -0
@@ -212,6 +212,18 @@ void set_floatx80_rounding_precision(int val STATUS_PARAM); void float_raise( int8 flags STATUS_PARAM);/*---------------------------------------------------------------------------- +| Options to indicate which negations to perform in float*_muladd() +| Using these differs from negating an input or output before calling +| the muladd function in that this means that a NaN doesn't have its +| sign bit inverted before it is propagated. +*----------------------------------------------------------------------------*/ +enum { + float_muladd_negate_c =1 , + float_muladd_negate_product =2 , + float_muladd_negate_result =3 , +}; + +/*---------------------------------------------------------------------------- | Software IEC/IEEE integer-to-floating-point conversion routines. *----------------------------------------------------------------------------*/ float32 int32_to_float32 (int32 STATUS_PARAM );
@@ -269,6 +281,7 @@ float32 float32_sub( float32, float32 STATUS_PARAM );float32 float32_mul (float32 ,float32 STATUS_PARAM );float32 float32_div (float32 ,float32 STATUS_PARAM );float32 float32_rem (float32 ,float32 STATUS_PARAM ); +float32 float32_muladd (float32 ,float32 ,float32 ,int STATUS_PARAM );float32 float32_sqrt (float32 STATUS_PARAM );float32 float32_exp2 (float32 STATUS_PARAM );float32 float32_log2 (float32 STATUS_PARAM );
@@ -375,6 +388,7 @@ float64 float64_sub( float64, float64 STATUS_PARAM );float64 float64_mul (float64 ,float64 STATUS_PARAM );float64 float64_div (float64 ,float64 STATUS_PARAM );float64 float64_rem (float64 ,float64 STATUS_PARAM ); +float64 float64_muladd (float64 ,float64 ,float64 ,int STATUS_PARAM );float64 float64_sqrt (float64 STATUS_PARAM );float64 float64_log2 (float64 STATUS_PARAM );int float64_eq (float64 ,float64 STATUS_PARAM );
Signature · 1 line
1.7.1
PATCH v2 5/5 target-arm: Implement VFPv4 fused multiply-accumulate insns
In reply to Peter MaydellImplement the fused multiply-accumulate instructions (VFMA, VFMS, VFNMA, VFNMS) which are new in VFPv4.
--- target-arm/cpu.h | 1 + target-arm/helper.c | 14 +++++++++ target-arm/helper.h | 3 ++ target-arm/translate.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 0 deletions(-)
target-arm/cpu.h +1 -0
@@ -376,6 +376,7 @@ enum arm_features {ARM_FEATURE_STRONGARM ,ARM_FEATURE_VAPA ,/* cp15 VA to PA lookups */ ARM_FEATURE_ARM_DIV ,/* divide supported in ARM encoding */ + ARM_FEATURE_VFP4, /* VFPv4 (implies that NEON is v2) */ };static inline int arm_feature (CPUARMState *env,int feature)
target-arm/helper.c +14 -0
@@ -204,6 +204,7 @@ static void cpu_reset_model_id(CPUARMState *env, uint32_t id)set_feature (env,ARM_FEATURE_THUMB2 );set_feature (env,ARM_FEATURE_VFP );set_feature (env,ARM_FEATURE_VFP3 ); +set_feature (env,ARM_FEATURE_VFP4 );set_feature (env,ARM_FEATURE_VFP_FP16 );set_feature (env,ARM_FEATURE_NEON );set_feature (env,ARM_FEATURE_THUMB2EE );
@@ -3085,6 +3086,19 @@ uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)return 0x80000000 | ((float64_val (f64) >>21 ) &0x7fffffff ); } +/* VFPv4 fused multiply-accumulate */ +float32 VFP_HELPER (muladd ,s )(float32 a,float32 b,float32 c,void *fpstp) +{ +float_status *fpst = fpstp; +return float32_muladd (a, b, c,0 , fpst); +} + +float64 VFP_HELPER (muladd ,d )(float64 a,float64 b,float64 c,void *fpstp) +{ +float_status *fpst = fpstp; +return float64_muladd (a, b, c,0 , fpst); +} +void HELPER (set_teecr )(CPUState *env,uint32_t val) { val &=1 ;
target-arm/helper.h +3 -0
@@ -132,6 +132,9 @@ DEF_HELPER_2(vfp_fcvt_f32_to_f16, i32, f32, env) DEF_HELPER_2(neon_fcvt_f16_to_f32, f32, i32, env) DEF_HELPER_2(neon_fcvt_f32_to_f16, i32, f32, env) +DEF_HELPER_4(vfp_muladdd, f64, f64, f64, f64, ptr) +DEF_HELPER_4(vfp_muladds, f32, f32, f32, f32, ptr) + DEF_HELPER_3(recps_f32, f32, f32, f32, env) DEF_HELPER_3(rsqrts_f32, f32, f32, f32, env)DEF_HELPER_2 (recpe_f32, f32, f32, env)
target-arm/translate.c +72 -0
102 lines not on this page · raw message
Signature · 1 line
1.7.1