target/i386: share some EFLAGS code between TCG and x86_emu
cover target/i386: share some EFLAGS code between TCG and x86_emu
This reduces by ~20% the size of helper_cc_compute_all, and also shares some code between TCG and x86_emu. Also, the line of code delta is negative despite adding a bunch of comments. :)
Paolo
Paolo Bonzini (3):
target/i386: tcg: simplify computation of AF after INC/DEC target/i386: emulate: microoptimize and explain
ADD_COUT_VEC/SUB_COUT_VEC
target/i386: tcg: use cout to commonize add/adc/sub/sbb cases
target/i386/cpu.h | 25 +++++++ target/i386/tcg/cc_helper_template.h.inc | 90 ++++++++---------------- target/i386/hvf/x86_flags.c | 9 --- 3 files changed, 56 insertions(+), 68 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/3 target/i386: tcg: simplify computation of AF after INC/DEC
No difference in generated code, but the XOR-based formula is easily understood on its own. This will make more sense once ADD/SUB stop using dst^src1^src2 to compute AF.
--- target/i386/tcg/cc_helper_template.h.inc | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
target/i386/tcg/cc_helper_template.h.inc +2 -8
@@ -175,13 +175,10 @@ static uint32_t glue(compute_all_logic, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1) static uint32_t glue(compute_all_inc, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1) { uint32_t cf, pf, af, zf, sf, of; - DATA_TYPE src2; cf = src1; - src1 = dst - 1; - src2 = 1; pf = compute_pf(dst); - af = (dst ^ src1 ^ src2) & CC_A; + af = (dst ^ (dst - 1)) & CC_A; /* bits 0..3 are all clear */ zf = (dst == 0) * CC_Z; sf = lshift(dst, 8 - DATA_BITS) & CC_S; of = (dst == SIGN_MASK) * CC_O;
@@ -191,13 +188,10 @@ static uint32_t glue(compute_all_inc, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1) static uint32_t glue(compute_all_dec, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1) { uint32_t cf, pf, af, zf, sf, of; - DATA_TYPE src2; cf = src1; - src1 = dst + 1; - src2 = 1; pf = compute_pf(dst); - af = (dst ^ src1 ^ src2) & CC_A; + af = (dst ^ (dst + 1)) & CC_A; /* bits 0..3 are all set */ zf = (dst == 0) * CC_Z; sf = lshift(dst, 8 - DATA_BITS) & CC_S; of = (dst == SIGN_MASK - 1) * CC_O;
Signature · 1 line
2.49.0
PATCH 2/3 target/i386: emulate: microoptimize and explain ADD_COUT_VEC/SUB_COUT_VEC
In reply to Paolo BonziniThe logic is the same, but the majority(NOT a, b, c) is brought out to a separate macro and implemented without NOT operations.
--- target/i386/hvf/x86_flags.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
target/i386/hvf/x86_flags.c +21 -5
@@ -45,14 +45,30 @@#define LF_MASK_CF (0x01 << LF_BIT_CF)#define LF_MASK_PO (0x01 << LF_BIT_PO) +/* majority(NOT a, b, c) = (a ^ b) ? b : c */ +#define MAJ_INV1 (a, b, c) ((((a) ^ (b)) & ((b) ^ (c))) ^ (c)) + +/* +* ADD_COUT_VEC(x, y) = majority((x + y) ^ x ^ y, x, y) +* +* If two corresponding bits in x and y are the same, that's the carry +* independent of the value (x+y)^x^y. Hence x^y can be replaced with +* 1 in (x+y)^x^y, resulting in majority(NOT (x+y), x, y) +*/ #define ADD_COUT_VEC (op1, op2, result) \ - (((op1) & (op2)) | (((op1) | (op2)) & (~(result)))) + MAJ_INV1(result, op1, op2) +/* +* SUB_COUT_VEC(x, y) = NOT majority(x, NOT y, (x - y) ^ x ^ NOT y) +* = majority(NOT x, y, (x - y) ^ x ^ y) +* +* Note that the carry out is actually a borrow, i.e. it is inverted. +* If two corresponding bits in x and y are different, the value that +* the bit has in (x-y)^x^y likewise does not Hence x^y can be replaced +* with 0 in (x-y)^x^y, resulting in majority(NOT x, y, x-y) +*/ #define SUB_COUT_VEC (op1, op2, result) \ - (((~(op1)) & (op2)) | (((~(op1)) ^ (op2)) & (result))) - -#define GET_ADD_OVERFLOW (op1, op2, result, mask) \ - ((((op1) ^ (result)) & ((op2) ^ (result))) & (mask)) + MAJ_INV1(op1, op2, result)/* ******************* */ /* OSZAPC */
Signature · 1 line
2.49.0
PATCH 3/3 target/i386: tcg: use cout to commonize add/adc/sub/sbb cases
In reply to Paolo BonziniUse the carry-out vector as the basis to compute AF, CF and OF. The cost is pretty much the same, because the carry-out is just four boolean operations, and the code is much smaller because add/adc/sub/sbb now share most of it.
A similar algorithm to what is used in target/i386/emulate can also be used for APX, in order to build the result of CCMP/CTEST with a new CC_OP_*. CCMP needs to place into the flags from either a subtraction or a constant value; CTEST likewise place into the flags either an AND or a constant value. The new CC_OP for CCMP and CTEST would store for a successful predcate:
- in DST and SRC2, the result of the operation;
- in SRC, a carry-out vector for CCMP or zero for CTEST;
If the default flag value is used, DST/SRC/SRC2 can be filled with constants:
- in DST the negated ZF;
- in SRC's top 2 bits, a value that results in the desired OF and CF;
- in SRC2 a suitable value (any of 0/1/~0/~1) that can be used instead of DST to compute the desired SF and PF.
--- target/i386/cpu.h | 25 ++++++++ target/i386/tcg/cc_helper_template.h.inc | 80 +++++++++--------------- target/i386/hvf/x86_flags.c | 25 -------- 3 files changed, 54 insertions(+), 76 deletions(-)
target/i386/cpu.h +25 -0
@@ -2843,4 +2843,29 @@ static inline bool ctl_has_irq(CPUX86State *env)# define TARGET_VSYSCALL_PAGE (UINT64_C(-10) << 20)#endif +/* majority(NOT a, b, c) = (a ^ b) ? b : c */ +#define MAJ_INV1 (a, b, c) ((((a) ^ (b)) & ((b) ^ (c))) ^ (c)) + +/* +* ADD_COUT_VEC(x, y) = majority((x + y) ^ x ^ y, x, y) +* +* If two corresponding bits in x and y are the same, that's the carry +* independent of the value (x+y)^x^y. Hence x^y can be replaced with +* 1 in (x+y)^x^y, resulting in majority(NOT (x+y), x, y) +*/ +#define ADD_COUT_VEC (op1, op2, result) \ + MAJ_INV1(result, op1, op2) + +/* +* SUB_COUT_VEC(x, y) = NOT majority(x, NOT y, (x - y) ^ x ^ NOT y) +* = majority(NOT x, y, (x - y) ^ x ^ y) +* +* Note that the carry out is actually a borrow, i.e. it is inverted. +* If two corresponding bits in x and y are different, the value that +* the bit has in (x-y)^x^y likewise does not Hence x^y can be replaced +* with 0 in (x-y)^x^y, resulting in majority(NOT x, y, x-y) +*/ +#define SUB_COUT_VEC (op1, op2, result) \ + MAJ_INV1(op1, op2, result) +#endif /* I386_CPU_H */
target/i386/tcg/cc_helper_template.h.inc +29 -51
141 lines not on this page · raw message
target/i386/hvf/x86_flags.c +0 -25
@@ -45,31 +45,6 @@#define LF_MASK_CF (0x01 << LF_BIT_CF)#define LF_MASK_PO (0x01 << LF_BIT_PO) -/* majority(NOT a, b, c) = (a ^ b) ? b : c */ -#define MAJ_INV1 (a, b, c) ((((a) ^ (b)) & ((b) ^ (c))) ^ (c)) - -/* -* ADD_COUT_VEC(x, y) = majority((x + y) ^ x ^ y, x, y) -* -* If two corresponding bits in x and y are the same, that's the carry -* independent of the value (x+y)^x^y. Hence x^y can be replaced with -* 1 in (x+y)^x^y, resulting in majority(NOT (x+y), x, y) -*/ -#define ADD_COUT_VEC (op1, op2, result) \ - MAJ_INV1(result, op1, op2) - -/* -* SUB_COUT_VEC(x, y) = NOT majority(x, NOT y, (x - y) ^ x ^ NOT y) -* = majority(NOT x, y, (x - y) ^ x ^ y) -* -* Note that the carry out is actually a borrow, i.e. it is inverted. -* If two corresponding bits in x and y are different, the value that -* the bit has in (x-y)^x^y likewise does not Hence x^y can be replaced -* with 0 in (x-y)^x^y, resulting in majority(NOT x, y, x-y) -*/ -#define SUB_COUT_VEC (op1, op2, result) \ - MAJ_INV1(op1, op2, result) -/* ******************* */ /* OSZAPC */ /* ******************* */
Signature · 1 line
2.49.0