From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: richard.henderson@linaro.org
Subject: [PATCH 12/14] target/i386: use builtin popcnt or parity to compute PF, if available
Date: Sun, 20 Oct 2024 17:53:22 +0200 [thread overview]
Message-ID: <20241020155324.35273-13-pbonzini@redhat.com> (raw)
In-Reply-To: <20241020155324.35273-1-pbonzini@redhat.com>
This removes the 256 byte parity table from the executable on
x86, s390 and RISC-V/zbb hosts.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/qemu/host-utils.h | 16 ++++++++++++++++
target/i386/tcg/helper-tcg.h | 12 ++++++++++++
target/i386/tcg/cc_helper_template.h.inc | 20 ++++++++++----------
target/i386/tcg/cc_helper.c | 2 ++
target/i386/tcg/int_helper.c | 4 ++--
5 files changed, 42 insertions(+), 12 deletions(-)
diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index ead97d354d6..bd4c684e5b5 100644
--- a/include/qemu/host-utils.h
+++ b/include/qemu/host-utils.h
@@ -126,6 +126,13 @@ static inline uint64_t muldiv64_round_up(uint64_t a, uint32_t b, uint32_t c)
}
#endif
+#if defined __POPCNT__ || defined __s390x__|| defined __riscv_zbb
+#define HAVE_FAST_CTPOP 1
+#endif
+#if defined __i386__ || defined __x86_64__ || defined HAVE_FAST_CTPOP
+#define HAVE_FAST_PARITY8 1
+#endif
+
/**
* clz8 - count leading zeros in a 8-bit value.
* @val: The value to search
@@ -313,6 +320,15 @@ static inline int ctpop8(uint8_t val)
return __builtin_popcount(val);
}
+/*
+ * parity8 - return the parity (1 = odd) of an 8-bit value.
+ * @val: The value to search
+ */
+static inline int parity8(uint8_t val)
+{
+ return __builtin_parity(val);
+}
+
/**
* ctpop16 - count the population of one bits in a 16-bit value.
* @val: The value to search
diff --git a/target/i386/tcg/helper-tcg.h b/target/i386/tcg/helper-tcg.h
index 15d6c6f8b4f..fb6354873d1 100644
--- a/target/i386/tcg/helper-tcg.h
+++ b/target/i386/tcg/helper-tcg.h
@@ -21,6 +21,7 @@
#define I386_HELPER_TCG_H
#include "exec/exec-all.h"
+#include "qemu/host-utils.h"
/* Maximum instruction code size */
#define TARGET_MAX_INSN_SIZE 16
@@ -87,7 +88,18 @@ G_NORETURN void x86_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
#endif
/* cc_helper.c */
+#ifdef HAVE_FAST_PARITY8
+static inline unsigned int compute_pf(uint8_t x)
+{
+ return !parity8(x) * CC_P;
+}
+#else
extern const uint8_t parity_table[256];
+static inline unsigned int compute_pf(uint8_t x)
+{
+ return parity_table[x];
+}
+#endif
/* misc_helper.c */
void cpu_load_eflags(CPUX86State *env, int eflags, int update_mask);
diff --git a/target/i386/tcg/cc_helper_template.h.inc b/target/i386/tcg/cc_helper_template.h.inc
index 4cbbc73c3cd..8af8b8539f9 100644
--- a/target/i386/tcg/cc_helper_template.h.inc
+++ b/target/i386/tcg/cc_helper_template.h.inc
@@ -45,7 +45,7 @@ static uint32_t glue(compute_all_add, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
DATA_TYPE src2 = dst - src1;
cf = dst < src1;
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = (dst ^ src1 ^ src2) & CC_A;
zf = (dst == 0) * CC_Z;
sf = lshift(dst, 8 - DATA_BITS) & CC_S;
@@ -65,7 +65,7 @@ static uint32_t glue(compute_all_adc, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1,
DATA_TYPE src2 = dst - src1 - src3;
cf = (src3 ? dst <= src1 : dst < src1);
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = (dst ^ src1 ^ src2) & 0x10;
zf = (dst == 0) << 6;
sf = lshift(dst, 8 - DATA_BITS) & 0x80;
@@ -85,7 +85,7 @@ static uint32_t glue(compute_all_sub, SUFFIX)(DATA_TYPE dst, DATA_TYPE src2)
DATA_TYPE src1 = dst + src2;
cf = src1 < src2;
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = (dst ^ src1 ^ src2) & CC_A;
zf = (dst == 0) * CC_Z;
sf = lshift(dst, 8 - DATA_BITS) & CC_S;
@@ -107,7 +107,7 @@ static uint32_t glue(compute_all_sbb, SUFFIX)(DATA_TYPE dst, DATA_TYPE src2,
DATA_TYPE src1 = dst + src2 + src3;
cf = (src3 ? src1 <= src2 : src1 < src2);
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = (dst ^ src1 ^ src2) & 0x10;
zf = (dst == 0) << 6;
sf = lshift(dst, 8 - DATA_BITS) & 0x80;
@@ -128,7 +128,7 @@ static uint32_t glue(compute_all_logic, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
uint32_t cf, pf, af, zf, sf, of;
cf = 0;
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = 0;
zf = (dst == 0) * CC_Z;
sf = lshift(dst, 8 - DATA_BITS) & CC_S;
@@ -144,7 +144,7 @@ static uint32_t glue(compute_all_inc, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
cf = src1;
src1 = dst - 1;
src2 = 1;
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = (dst ^ src1 ^ src2) & CC_A;
zf = (dst == 0) * CC_Z;
sf = lshift(dst, 8 - DATA_BITS) & CC_S;
@@ -160,7 +160,7 @@ static uint32_t glue(compute_all_dec, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
cf = src1;
src1 = dst + 1;
src2 = 1;
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = (dst ^ src1 ^ src2) & CC_A;
zf = (dst == 0) * CC_Z;
sf = lshift(dst, 8 - DATA_BITS) & CC_S;
@@ -173,7 +173,7 @@ static uint32_t glue(compute_all_shl, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
uint32_t cf, pf, af, zf, sf, of;
cf = (src1 >> (DATA_BITS - 1)) & CC_C;
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = 0; /* undefined */
zf = (dst == 0) * CC_Z;
sf = lshift(dst, 8 - DATA_BITS) & CC_S;
@@ -192,7 +192,7 @@ static uint32_t glue(compute_all_sar, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1)
uint32_t cf, pf, af, zf, sf, of;
cf = src1 & 1;
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = 0; /* undefined */
zf = (dst == 0) * CC_Z;
sf = lshift(dst, 8 - DATA_BITS) & CC_S;
@@ -209,7 +209,7 @@ static uint32_t glue(compute_all_mul, SUFFIX)(DATA_TYPE dst, target_long src1)
uint32_t cf, pf, af, zf, sf, of;
cf = (src1 != 0);
- pf = parity_table[(uint8_t)dst];
+ pf = compute_pf(dst);
af = 0; /* undefined */
zf = (dst == 0) * CC_Z;
sf = lshift(dst, 8 - DATA_BITS) & CC_S;
diff --git a/target/i386/tcg/cc_helper.c b/target/i386/tcg/cc_helper.c
index c24e6a14c07..d65345f059d 100644
--- a/target/i386/tcg/cc_helper.c
+++ b/target/i386/tcg/cc_helper.c
@@ -22,6 +22,7 @@
#include "exec/helper-proto.h"
#include "helper-tcg.h"
+#ifndef HAVE_FAST_PARITY8
const uint8_t parity_table[256] = {
CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
@@ -56,6 +57,7 @@ const uint8_t parity_table[256] = {
CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
};
+#endif
#define SHIFT 0
#include "cc_helper_template.h.inc"
diff --git a/target/i386/tcg/int_helper.c b/target/i386/tcg/int_helper.c
index e1f92405282..1a02e9d8434 100644
--- a/target/i386/tcg/int_helper.c
+++ b/target/i386/tcg/int_helper.c
@@ -237,7 +237,7 @@ void helper_daa(CPUX86State *env)
env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
/* well, speed is not an issue here, so we compute the flags by hand */
eflags |= (al == 0) << 6; /* zf */
- eflags |= parity_table[al]; /* pf */
+ eflags |= compute_pf(al);
eflags |= (al & 0x80); /* sf */
CC_SRC = eflags;
CC_OP = CC_OP_EFLAGS;
@@ -269,7 +269,7 @@ void helper_das(CPUX86State *env)
env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
/* well, speed is not an issue here, so we compute the flags by hand */
eflags |= (al == 0) << 6; /* zf */
- eflags |= parity_table[al]; /* pf */
+ eflags |= compute_pf(al);
eflags |= (al & 0x80); /* sf */
CC_SRC = eflags;
CC_OP = CC_OP_EFLAGS;
--
2.46.2
next prev parent reply other threads:[~2024-10-20 15:55 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-20 15:53 [PATCH 00/14] target/i386: miscellaneous flags improvements Paolo Bonzini
2024-10-20 15:53 ` [PATCH 01/14] target/i386: use tcg_gen_ext_tl when applicable Paolo Bonzini
2024-10-20 19:50 ` Richard Henderson
2024-10-20 15:53 ` [PATCH 02/14] target/i386: Tidy cc_op_str usage Paolo Bonzini
2024-10-20 15:53 ` [PATCH 03/14] target/i386: remove CC_OP_CLR Paolo Bonzini
2024-10-20 19:55 ` Richard Henderson
2024-10-20 15:53 ` [PATCH 04/14] target/i386: Rearrange CCOp Paolo Bonzini
2024-10-20 15:53 ` [PATCH 05/14] target/i386: Introduce cc_op_size Paolo Bonzini
2024-10-20 15:53 ` [PATCH 06/14] target/i386: Wrap cc_op_live with a validity check Paolo Bonzini
2024-10-20 20:05 ` Richard Henderson
2024-10-20 15:53 ` [PATCH 07/14] target/i386: optimize computation of ZF from CC_OP_DYNAMIC Paolo Bonzini
2024-10-20 20:18 ` Richard Henderson
2024-10-20 15:53 ` [PATCH 08/14] target/i386: optimize TEST+Jxx sequences Paolo Bonzini
2024-10-20 20:19 ` Richard Henderson
2024-10-20 15:53 ` [PATCH 09/14] target/i386: add a few more trivial CCPrepare cases Paolo Bonzini
2024-10-20 20:23 ` Richard Henderson
2024-10-20 15:53 ` [PATCH 10/14] target/i386: add a note about gen_jcc1 Paolo Bonzini
2024-10-20 15:53 ` [PATCH 11/14] target/i386: make flag variables unsigned Paolo Bonzini
2024-10-20 20:26 ` Richard Henderson
2024-10-20 15:53 ` Paolo Bonzini [this message]
2024-10-20 20:40 ` [PATCH 12/14] target/i386: use builtin popcnt or parity to compute PF, if available Richard Henderson
2024-10-20 15:53 ` [PATCH 13/14] target/i386: use higher-precision arithmetic to compute CF Paolo Bonzini
2024-10-20 15:53 ` [PATCH 14/14] target/i386: use + to put flags together Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241020155324.35273-13-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).