All of lore.kernel.org
 help / color / mirror / Atom feed
From: Taylor Simpson <tsimpson@quicinc.com>
To: qemu-devel@nongnu.org
Cc: tsimpson@quicinc.com, richard.henderson@linaro.org,
	philmd@linaro.org, peter.maydell@linaro.org, bcain@quicinc.com,
	quic_mathbern@quicinc.com, stefanha@redhat.com,
	"Niccolò Izzo" <nizzo@rev.ng>,
	"Alessandro Di Federico" <ale@rev.ng>,
	"Anton Johansson" <anjo@rev.ng>
Subject: [PULL 16/21] target/hexagon: introduce new helper functions
Date: Fri, 16 Dec 2022 12:48:40 -0800	[thread overview]
Message-ID: <20221216204845.19290-17-tsimpson@quicinc.com> (raw)
In-Reply-To: <20221216204845.19290-1-tsimpson@quicinc.com>

From: Niccolò Izzo <nizzo@rev.ng>

These helpers will be employed by the idef-parser generated code, to
correctly implement instruction semantics. "Helper" functions, in the
context of this patch, refers to functions which provide a manual TCG
implementation of certain features.

Signed-off-by: Alessandro Di Federico <ale@rev.ng>
Signed-off-by: Niccolò Izzo <nizzo@rev.ng>
Signed-off-by: Anton Johansson <anjo@rev.ng>
Signed-off-by: Taylor Simpson <tsimpson@quicinc.com>
Reviewed-by: Taylor Simpson <tsimpson@quicinc.com>
Message-Id: <20220923173831.227551-6-anjo@rev.ng>
---
 target/hexagon/genptr.h |  13 ++++
 target/hexagon/macros.h |   9 +++
 target/hexagon/genptr.c | 146 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 166 insertions(+), 2 deletions(-)

diff --git a/target/hexagon/genptr.h b/target/hexagon/genptr.h
index e126c46255..591b059698 100644
--- a/target/hexagon/genptr.h
+++ b/target/hexagon/genptr.h
@@ -33,9 +33,22 @@ void gen_store1i(TCGv_env cpu_env, TCGv vaddr, int32_t src, uint32_t slot);
 void gen_store2i(TCGv_env cpu_env, TCGv vaddr, int32_t src, uint32_t slot);
 void gen_store4i(TCGv_env cpu_env, TCGv vaddr, int32_t src, uint32_t slot);
 void gen_store8i(TCGv_env cpu_env, TCGv vaddr, int64_t src, uint32_t slot);
+TCGv gen_read_reg(TCGv result, int num);
 TCGv gen_read_preg(TCGv pred, uint8_t num);
 void gen_log_reg_write(int rnum, TCGv val);
 void gen_log_pred_write(DisasContext *ctx, int pnum, TCGv val);
+void gen_set_usr_field(int field, TCGv val);
+void gen_set_usr_fieldi(int field, int x);
+void gen_set_usr_field_if(int field, TCGv val);
+void gen_sat_i32(TCGv dest, TCGv source, int width);
+void gen_sat_i32_ovfl(TCGv ovfl, TCGv dest, TCGv source, int width);
+void gen_satu_i32(TCGv dest, TCGv source, int width);
+void gen_satu_i32_ovfl(TCGv ovfl, TCGv dest, TCGv source, int width);
+void gen_sat_i64(TCGv_i64 dest, TCGv_i64 source, int width);
+void gen_sat_i64_ovfl(TCGv ovfl, TCGv_i64 dest, TCGv_i64 source, int width);
+void gen_satu_i64(TCGv_i64 dest, TCGv_i64 source, int width);
+void gen_satu_i64_ovfl(TCGv ovfl, TCGv_i64 dest, TCGv_i64 source, int width);
+void gen_add_sat_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b);
 TCGv gen_8bitsof(TCGv result, TCGv value);
 void gen_set_byte_i64(int N, TCGv_i64 result, TCGv src);
 TCGv gen_get_byte(TCGv result, int N, TCGv src, bool sign);
diff --git a/target/hexagon/macros.h b/target/hexagon/macros.h
index 1a31542010..cd64bb8eec 100644
--- a/target/hexagon/macros.h
+++ b/target/hexagon/macros.h
@@ -197,7 +197,16 @@
 #define MEM_STORE8(VA, DATA, SLOT) log_store64(env, VA, DATA, 8, SLOT)
 #endif
 
+#ifdef QEMU_GENERATE
+static inline void gen_cancel(uint32_t slot)
+{
+    tcg_gen_ori_tl(hex_slot_cancelled, hex_slot_cancelled, 1 << slot);
+}
+
+#define CANCEL gen_cancel(slot);
+#else
 #define CANCEL cancel_slot(env, slot)
+#endif
 
 #define LOAD_CANCEL(EA) do { CANCEL; } while (0)
 
diff --git a/target/hexagon/genptr.c b/target/hexagon/genptr.c
index 3fe279ce8c..6cf2e0ed43 100644
--- a/target/hexagon/genptr.c
+++ b/target/hexagon/genptr.c
@@ -31,6 +31,12 @@
 #include "gen_tcg_hvx.h"
 #include "genptr.h"
 
+TCGv gen_read_reg(TCGv result, int num)
+{
+    tcg_gen_mov_tl(result, hex_gpr[num]);
+    return result;
+}
+
 TCGv gen_read_preg(TCGv pred, uint8_t num)
 {
     tcg_gen_mov_tl(pred, hex_pred[num]);
@@ -506,7 +512,7 @@ static void gen_write_new_pc_pcrel(DisasContext *ctx, int pc_off,
     }
 }
 
-static void gen_set_usr_field(int field, TCGv val)
+void gen_set_usr_field(int field, TCGv val)
 {
     tcg_gen_deposit_tl(hex_new_value[HEX_REG_USR], hex_new_value[HEX_REG_USR],
                        val,
@@ -514,7 +520,7 @@ static void gen_set_usr_field(int field, TCGv val)
                        reg_field_info[field].width);
 }
 
-static void gen_set_usr_fieldi(int field, int x)
+void gen_set_usr_fieldi(int field, int x)
 {
     if (reg_field_info[field].width == 1) {
         target_ulong bit = 1 << reg_field_info[field].offset;
@@ -1028,5 +1034,141 @@ void probe_noshuf_load(TCGv va, int s, int mi)
     gen_helper_probe_noshuf_load(cpu_env, va, size, mem_idx);
 }
 
+/*
+ * Note: Since this function might branch, `val` is
+ * required to be a `tcg_temp_local`.
+ */
+void gen_set_usr_field_if(int field, TCGv val)
+{
+    /* Sets the USR field if `val` is non-zero */
+    if (reg_field_info[field].width == 1) {
+        TCGv tmp = tcg_temp_new();
+        tcg_gen_extract_tl(tmp, val, 0, reg_field_info[field].width);
+        tcg_gen_shli_tl(tmp, tmp, reg_field_info[field].offset);
+        tcg_gen_or_tl(hex_new_value[HEX_REG_USR],
+                      hex_new_value[HEX_REG_USR],
+                      tmp);
+        tcg_temp_free(tmp);
+    } else {
+        TCGLabel *skip_label = gen_new_label();
+        tcg_gen_brcondi_tl(TCG_COND_EQ, val, 0, skip_label);
+        gen_set_usr_field(field, val);
+        gen_set_label(skip_label);
+    }
+}
+
+void gen_sat_i32(TCGv dest, TCGv source, int width)
+{
+    TCGv max_val = tcg_constant_tl((1 << (width - 1)) - 1);
+    TCGv min_val = tcg_constant_tl(-(1 << (width - 1)));
+    tcg_gen_smin_tl(dest, source, max_val);
+    tcg_gen_smax_tl(dest, dest, min_val);
+}
+
+void gen_sat_i32_ovfl(TCGv ovfl, TCGv dest, TCGv source, int width)
+{
+    gen_sat_i32(dest, source, width);
+    tcg_gen_setcond_tl(TCG_COND_NE, ovfl, source, dest);
+}
+
+void gen_satu_i32(TCGv dest, TCGv source, int width)
+{
+    TCGv max_val = tcg_constant_tl((1 << width) - 1);
+    TCGv zero = tcg_constant_tl(0);
+    tcg_gen_movcond_tl(TCG_COND_GTU, dest, source, max_val, max_val, source);
+    tcg_gen_movcond_tl(TCG_COND_LT, dest, source, zero, zero, dest);
+}
+
+void gen_satu_i32_ovfl(TCGv ovfl, TCGv dest, TCGv source, int width)
+{
+    gen_satu_i32(dest, source, width);
+    tcg_gen_setcond_tl(TCG_COND_NE, ovfl, source, dest);
+}
+
+void gen_sat_i64(TCGv_i64 dest, TCGv_i64 source, int width)
+{
+    TCGv_i64 max_val = tcg_constant_i64((1LL << (width - 1)) - 1LL);
+    TCGv_i64 min_val = tcg_constant_i64(-(1LL << (width - 1)));
+    tcg_gen_smin_i64(dest, source, max_val);
+    tcg_gen_smax_i64(dest, dest, min_val);
+}
+
+void gen_sat_i64_ovfl(TCGv ovfl, TCGv_i64 dest, TCGv_i64 source, int width)
+{
+    TCGv_i64 ovfl_64;
+    gen_sat_i64(dest, source, width);
+    ovfl_64 = tcg_temp_new_i64();
+    tcg_gen_setcond_i64(TCG_COND_NE, ovfl_64, dest, source);
+    tcg_gen_trunc_i64_tl(ovfl, ovfl_64);
+    tcg_temp_free_i64(ovfl_64);
+}
+
+void gen_satu_i64(TCGv_i64 dest, TCGv_i64 source, int width)
+{
+    TCGv_i64 max_val = tcg_constant_i64((1LL << width) - 1LL);
+    TCGv_i64 zero = tcg_constant_i64(0);
+    tcg_gen_movcond_i64(TCG_COND_GTU, dest, source, max_val, max_val, source);
+    tcg_gen_movcond_i64(TCG_COND_LT, dest, source, zero, zero, dest);
+}
+
+void gen_satu_i64_ovfl(TCGv ovfl, TCGv_i64 dest, TCGv_i64 source, int width)
+{
+    TCGv_i64 ovfl_64;
+    gen_satu_i64(dest, source, width);
+    ovfl_64 = tcg_temp_new_i64();
+    tcg_gen_setcond_i64(TCG_COND_NE, ovfl_64, dest, source);
+    tcg_gen_trunc_i64_tl(ovfl, ovfl_64);
+    tcg_temp_free_i64(ovfl_64);
+}
+
+/* Implements the fADDSAT64 macro in TCG */
+void gen_add_sat_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b)
+{
+    TCGv_i64 sum = tcg_temp_local_new_i64();
+    TCGv_i64 xor = tcg_temp_new_i64();
+    TCGv_i64 cond1 = tcg_temp_new_i64();
+    TCGv_i64 cond2 = tcg_temp_local_new_i64();
+    TCGv_i64 cond3 = tcg_temp_new_i64();
+    TCGv_i64 mask = tcg_constant_i64(0x8000000000000000ULL);
+    TCGv_i64 max_pos = tcg_constant_i64(0x7FFFFFFFFFFFFFFFLL);
+    TCGv_i64 max_neg = tcg_constant_i64(0x8000000000000000LL);
+    TCGv_i64 zero = tcg_constant_i64(0);
+    TCGLabel *no_ovfl_label = gen_new_label();
+    TCGLabel *ovfl_label = gen_new_label();
+    TCGLabel *ret_label = gen_new_label();
+
+    tcg_gen_add_i64(sum, a, b);
+    tcg_gen_xor_i64(xor, a, b);
+
+    /* if (xor & mask) */
+    tcg_gen_and_i64(cond1, xor, mask);
+    tcg_temp_free_i64(xor);
+    tcg_gen_brcondi_i64(TCG_COND_NE, cond1, 0, no_ovfl_label);
+    tcg_temp_free_i64(cond1);
+
+    /* else if ((a ^ sum) & mask) */
+    tcg_gen_xor_i64(cond2, a, sum);
+    tcg_gen_and_i64(cond2, cond2, mask);
+    tcg_gen_brcondi_i64(TCG_COND_NE, cond2, 0, ovfl_label);
+    tcg_temp_free_i64(cond2);
+    /* fallthrough to no_ovfl_label branch */
+
+    /* if branch */
+    gen_set_label(no_ovfl_label);
+    tcg_gen_mov_i64(ret, sum);
+    tcg_gen_br(ret_label);
+
+    /* else if branch */
+    gen_set_label(ovfl_label);
+    tcg_gen_and_i64(cond3, sum, mask);
+    tcg_temp_free_i64(mask);
+    tcg_temp_free_i64(sum);
+    tcg_gen_movcond_i64(TCG_COND_NE, ret, cond3, zero, max_pos, max_neg);
+    tcg_temp_free_i64(cond3);
+    SET_USR_FIELD(USR_OVF, 1);
+
+    gen_set_label(ret_label);
+}
+
 #include "tcg_funcs_generated.c.inc"
 #include "tcg_func_table_generated.c.inc"
-- 
2.17.1


  parent reply	other threads:[~2022-12-16 20:49 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-16 20:48 [PULL 00/21] Hexagon update: bug fixes, performance, idef-parser Taylor Simpson
2022-12-16 20:48 ` [PULL 01/21] Hexagon (target/hexagon) Add pkt and insn to DisasContext Taylor Simpson
2022-12-16 20:48 ` [PULL 02/21] Hexagon (target/hexagon) Fix predicated assignment to .tmp and .cur Taylor Simpson
2022-12-16 20:48 ` [PULL 03/21] Hexagon (target/hexagon) Add overrides for S2_asr_r_r_sat/S2_asl_r_r_sat Taylor Simpson
2022-12-16 20:48 ` [PULL 04/21] Hexagon (target/hexagon) Only use branch_taken when packet has multi cof Taylor Simpson
2022-12-16 20:48 ` [PULL 05/21] Hexagon (target/hexagon) Remove PC from the runtime state Taylor Simpson
2022-12-16 20:48 ` [PULL 06/21] Hexagon (target/hexagon) Remove next_PC from " Taylor Simpson
2022-12-16 20:48 ` [PULL 07/21] Hexagon (target/hexagon) Add overrides for direct call instructions Taylor Simpson
2022-12-16 20:48 ` [PULL 08/21] Hexagon (target/hexagon) Add overrides for compound compare and jump Taylor Simpson
2022-12-16 20:48 ` [PULL 09/21] Hexagon (target/hexagon) Add overrides for various forms of jump Taylor Simpson
2022-12-16 20:48 ` [PULL 10/21] Hexagon (target/hexagon) Use direct block chaining for direct jump/branch Taylor Simpson
2022-12-16 20:48 ` [PULL 11/21] Hexagon (target/hexagon) Use direct block chaining for tight loops Taylor Simpson
2022-12-16 20:48 ` [PULL 12/21] target/hexagon: update MAINTAINERS for idef-parser Taylor Simpson
2022-12-16 20:48 ` [PULL 13/21] target/hexagon: import README " Taylor Simpson
2022-12-16 20:48 ` [PULL 14/21] target/hexagon: make slot number an unsigned Taylor Simpson
2022-12-16 20:48 ` [PULL 15/21] target/hexagon: make helper functions non-static Taylor Simpson
2022-12-16 20:48 ` Taylor Simpson [this message]
2022-12-16 20:48 ` [PULL 17/21] target/hexagon: prepare input for the idef-parser Taylor Simpson
2022-12-29 10:41   ` Thomas Huth
2022-12-31 14:00     ` Alessandro Di Federico via
2022-12-16 20:48 ` [PULL 18/21] target/hexagon: import lexer for idef-parser Taylor Simpson
2022-12-16 20:48 ` [PULL 19/21] target/hexagon: import parser " Taylor Simpson
2023-06-23 12:50   ` Peter Maydell
2023-06-26  9:54     ` Anton Johansson via
2022-12-16 20:48 ` [PULL 20/21] target/hexagon: call idef-parser functions Taylor Simpson
2022-12-16 20:48 ` [PULL 21/21] target/hexagon: import additional tests Taylor Simpson
2022-12-17 21:21 ` [PULL 00/21] Hexagon update: bug fixes, performance, idef-parser Peter Maydell
2022-12-18  9:33   ` Anton Johansson via
2022-12-18 13:52 ` Peter Maydell
2022-12-18 15:34   ` Anton Johansson via
2022-12-18 16:53   ` Richard Henderson
2022-12-18 17:01     ` Peter Maydell
2022-12-19 11:18       ` Philippe Mathieu-Daudé
2022-12-19 10:28 ` Peter Maydell
2022-12-19 18:54   ` Taylor Simpson
2022-12-20 12:49     ` Alessandro Di Federico via
2022-12-19 23:19 ` Philippe Mathieu-Daudé
2022-12-20  7:30   ` Philippe Mathieu-Daudé
2022-12-20 12:51     ` Alessandro Di Federico via
2022-12-20 14:19       ` Philippe Mathieu-Daudé
2022-12-20 18:13         ` Taylor Simpson

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=20221216204845.19290-17-tsimpson@quicinc.com \
    --to=tsimpson@quicinc.com \
    --cc=ale@rev.ng \
    --cc=anjo@rev.ng \
    --cc=bcain@quicinc.com \
    --cc=nizzo@rev.ng \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=quic_mathbern@quicinc.com \
    --cc=richard.henderson@linaro.org \
    --cc=stefanha@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.