From: LIU Zhiwei <zhiwei_liu@c-sky.com>
To: Chih-Min Chao <chihmin.chao@sifive.com>,
Richard Henderson <richard.henderson@linaro.org>
Cc: Frank Chang <frank.chang@sifive.com>,
Alistair Francis <alistair23@gmail.com>,
"open list:RISC-V" <qemu-riscv@nongnu.org>,
"qemu-devel@nongnu.org Developers" <qemu-devel@nongnu.org>
Subject: Re: [PATCH v2 5/7] target/riscv: Check nanboxed inputs in trans_rvf.inc.c
Date: Sun, 9 Aug 2020 07:06:05 +0800 [thread overview]
Message-ID: <cd6908d3-75eb-b5ee-35fb-b1d20b9c28d8@c-sky.com> (raw)
In-Reply-To: <560581be-926e-c303-85a6-b15d4c187ad6@c-sky.com>
[-- Attachment #1: Type: text/plain, Size: 7293 bytes --]
On 2020/8/8 22:18, LIU Zhiwei wrote:
>
>
> On 2020/8/8 4:24, Chih-Min Chao wrote:
>> On Fri, Jul 24, 2020 at 8:28 AM Richard Henderson
>> <richard.henderson@linaro.org <mailto:richard.henderson@linaro.org>>
>> wrote:
>>
>> If a 32-bit input is not properly nanboxed, then the input is
>> replaced
>> with the default qnan. The only inline expansion is for the
>> sign-changing
>> set of instructions: FSGNJ.S, FSGNJX.S, FSGNJN.S.
>>
>> Signed-off-by: Richard Henderson <richard.henderson@linaro.org
>> <mailto:richard.henderson@linaro.org>>
>> ---
>> target/riscv/insn_trans/trans_rvf.inc.c | 71
>> +++++++++++++++++++------
>> target/riscv/translate.c | 18 +++++++
>> 2 files changed, 73 insertions(+), 16 deletions(-)
>>
>> diff --git a/target/riscv/insn_trans/trans_rvf.inc.c
>> b/target/riscv/insn_trans/trans_rvf.inc.c
>> index 264d3139f1..f9a9e0643a 100644
>> --- a/target/riscv/insn_trans/trans_rvf.inc.c
>> +++ b/target/riscv/insn_trans/trans_rvf.inc.c
>> @@ -161,47 +161,86 @@ static bool trans_fsgnj_s(DisasContext
>> *ctx, arg_fsgnj_s *a)
>> {
>> REQUIRE_FPU;
>> REQUIRE_EXT(ctx, RVF);
>> +
>> if (a->rs1 == a->rs2) { /* FMOV */
>> - tcg_gen_mov_i64(cpu_fpr[a->rd], cpu_fpr[a->rs1]);
>> + gen_check_nanbox_s(cpu_fpr[a->rd], cpu_fpr[a->rs1]);
>> } else { /* FSGNJ */
>> - tcg_gen_deposit_i64(cpu_fpr[a->rd], cpu_fpr[a->rs2],
>> cpu_fpr[a->rs1],
>> - 0, 31);
>> + TCGv_i64 rs1 = tcg_temp_new_i64();
>> + TCGv_i64 rs2 = tcg_temp_new_i64();
>> +
>> + gen_check_nanbox_s(rs1, cpu_fpr[a->rs1]);
>> + gen_check_nanbox_s(rs2, cpu_fpr[a->rs2]);
>> +
>> + /* This formulation retains the nanboxing of rs2. */
>> + tcg_gen_deposit_i64(cpu_fpr[a->rd], rs2, rs1, 0, 31);
>> + tcg_temp_free_i64(rs1);
>> + tcg_temp_free_i64(rs2);
>> }
>> - gen_nanbox_s(cpu_fpr[a->rd], cpu_fpr[a->rd]);
>> mark_fs_dirty(ctx);
>> return true;
>> }
>>
>> static bool trans_fsgnjn_s(DisasContext *ctx, arg_fsgnjn_s *a)
>> {
>> + TCGv_i64 rs1, rs2, mask;
>> +
>> REQUIRE_FPU;
>> REQUIRE_EXT(ctx, RVF);
>> +
>> + rs1 = tcg_temp_new_i64();
>> + gen_check_nanbox_s(rs1, cpu_fpr[a->rs1]);
>> +
>> if (a->rs1 == a->rs2) { /* FNEG */
>> - tcg_gen_xori_i64(cpu_fpr[a->rd], cpu_fpr[a->rs1],
>> INT32_MIN);
>> + tcg_gen_xori_i64(cpu_fpr[a->rd], rs1,
>> MAKE_64BIT_MASK(31, 1));
>> } else {
>> - TCGv_i64 t0 = tcg_temp_new_i64();
>> - tcg_gen_not_i64(t0, cpu_fpr[a->rs2]);
>> - tcg_gen_deposit_i64(cpu_fpr[a->rd], t0, cpu_fpr[a->rs1],
>> 0, 31);
>> - tcg_temp_free_i64(t0);
>> + rs2 = tcg_temp_new_i64();
>> + gen_check_nanbox_s(rs2, cpu_fpr[a->rs2]);
>> +
>> + /*
>> + * Replace bit 31 in rs1 with inverse in rs2.
>> + * This formulation retains the nanboxing of rs1.
>> + */
>> + mask = tcg_const_i64(~MAKE_64BIT_MASK(31, 1));
>> + tcg_gen_andc_i64(rs2, mask, rs2);
>>
>>
>> should be
>> tcg_gen_not_i64(rs2, rs2); // forget to inverse rs2
>> tcg_gen_andc_i64(rs2, rs2, mask); //mask needs to be
>> inverted to get only sign
> Hi Chih-Min,
>
> Thanks for pointing it out. It's a bug here. However, I think it
> should be
>
> tcg_gen_andc_i64(rs2, rs2, mask); // only get rs2 bit 31
> tcg_gen_not_i64(rs2, rs2); // inverse rs2
>
Hi Chih-Min,
Sorry, your code is right.
Zhiwei
> Best Regards,
> Zhiwei
>>
>> Chih-Min Chao
>>
>> + tcg_gen_and_i64(rs1, mask, rs1);
>> + tcg_gen_or_i64(cpu_fpr[a->rd], rs1, rs2);
>> +
>> + tcg_temp_free_i64(mask);
>> + tcg_temp_free_i64(rs2);
>> }
>> - gen_nanbox_s(cpu_fpr[a->rd], cpu_fpr[a->rd]);
>> + tcg_temp_free_i64(rs1);
>> +
>> mark_fs_dirty(ctx);
>> return true;
>> }
>>
>> static bool trans_fsgnjx_s(DisasContext *ctx, arg_fsgnjx_s *a)
>> {
>> + TCGv_i64 rs1, rs2;
>> +
>> REQUIRE_FPU;
>> REQUIRE_EXT(ctx, RVF);
>> +
>> + rs1 = tcg_temp_new_i64();
>> + gen_check_nanbox_s(rs1, cpu_fpr[a->rs1]);
>> +
>> if (a->rs1 == a->rs2) { /* FABS */
>> - tcg_gen_andi_i64(cpu_fpr[a->rd], cpu_fpr[a->rs1],
>> ~INT32_MIN);
>> + tcg_gen_andi_i64(cpu_fpr[a->rd], rs1,
>> ~MAKE_64BIT_MASK(31, 1));
>> } else {
>> - TCGv_i64 t0 = tcg_temp_new_i64();
>> - tcg_gen_andi_i64(t0, cpu_fpr[a->rs2], INT32_MIN);
>> - tcg_gen_xor_i64(cpu_fpr[a->rd], cpu_fpr[a->rs1], t0);
>> - tcg_temp_free_i64(t0);
>> + rs2 = tcg_temp_new_i64();
>> + gen_check_nanbox_s(rs2, cpu_fpr[a->rs2]);
>> +
>> + /*
>> + * Xor bit 31 in rs1 with that in rs2.
>> + * This formulation retains the nanboxing of rs1.
>> + */
>> + tcg_gen_andi_i64(rs2, rs2, MAKE_64BIT_MASK(31, 1));
>> + tcg_gen_xor_i64(cpu_fpr[a->rd], rs1, rs2);
>> +
>> + tcg_temp_free_i64(rs2);
>> }
>> - gen_nanbox_s(cpu_fpr[a->rd], cpu_fpr[a->rd]);
>> + tcg_temp_free_i64(rs1);
>> +
>> mark_fs_dirty(ctx);
>> return true;
>> }
>> diff --git a/target/riscv/translate.c b/target/riscv/translate.c
>> index 12a746da97..bf35182776 100644
>> --- a/target/riscv/translate.c
>> +++ b/target/riscv/translate.c
>> @@ -101,6 +101,24 @@ static void gen_nanbox_s(TCGv_i64 out,
>> TCGv_i64 in)
>> tcg_gen_ori_i64(out, in, MAKE_64BIT_MASK(32, 32));
>> }
>>
>> +/*
>> + * A narrow n-bit operation, where n < FLEN, checks that input
>> operands
>> + * are correctly Nan-boxed, i.e., all upper FLEN - n bits are 1.
>> + * If so, the least-significant bits of the input are used,
>> otherwise the
>> + * input value is treated as an n-bit canonical NaN (v2.2
>> section 9.2).
>> + *
>> + * Here, the result is always nan-boxed, even the canonical nan.
>> + */
>> +static void gen_check_nanbox_s(TCGv_i64 out, TCGv_i64 in)
>> +{
>> + TCGv_i64 t_max = tcg_const_i64(0xffffffff00000000ull);
>> + TCGv_i64 t_nan = tcg_const_i64(0xffffffff7fc00000ull);
>> +
>> + tcg_gen_movcond_i64(TCG_COND_GEU, out, in, t_max, in, t_nan);
>> + tcg_temp_free_i64(t_max);
>> + tcg_temp_free_i64(t_nan);
>> +}
>> +
>> static void generate_exception(DisasContext *ctx, int excp)
>> {
>> tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next);
>> --
>> 2.25.1
>>
>>
>
[-- Attachment #2: Type: text/html, Size: 11685 bytes --]
next prev parent reply other threads:[~2020-08-08 23:06 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-24 0:28 [PATCH v2 0/7] target/riscv: NaN-boxing for multiple precison Richard Henderson
2020-07-24 0:28 ` [PATCH v2 1/7] target/riscv: Generate nanboxed results from fp helpers Richard Henderson
2020-07-24 2:35 ` LIU Zhiwei
2020-07-24 3:55 ` Richard Henderson
2020-07-24 6:05 ` LIU Zhiwei
2020-08-06 6:09 ` Chih-Min Chao
2020-08-06 7:05 ` LIU Zhiwei
2020-08-06 8:42 ` Chih-Min Chao
2020-08-06 10:02 ` LIU Zhiwei
2020-07-24 0:28 ` [PATCH v2 2/7] target/riscv: Generalize gen_nanbox_fpr to gen_nanbox_s Richard Henderson
2020-07-24 2:39 ` LIU Zhiwei
2020-08-06 6:24 ` Chih-Min Chao
2020-07-24 0:28 ` [PATCH v2 3/7] target/riscv: Generate nanboxed results from trans_rvf.inc.c Richard Henderson
2020-07-24 2:41 ` LIU Zhiwei
2020-08-06 6:24 ` Chih-Min Chao
2020-07-24 0:28 ` [PATCH v2 4/7] target/riscv: Check nanboxed inputs to fp helpers Richard Henderson
2020-07-24 2:47 ` LIU Zhiwei
2020-07-24 3:59 ` Richard Henderson
2020-08-06 6:26 ` Chih-Min Chao
2020-07-24 0:28 ` [PATCH v2 5/7] target/riscv: Check nanboxed inputs in trans_rvf.inc.c Richard Henderson
2020-07-24 6:04 ` LIU Zhiwei
2020-08-06 6:27 ` Chih-Min Chao
2020-08-07 20:24 ` Chih-Min Chao
2020-08-08 14:18 ` LIU Zhiwei
2020-08-08 23:06 ` LIU Zhiwei [this message]
2020-07-24 0:28 ` [PATCH v2 6/7] target/riscv: Clean up fmv.w.x Richard Henderson
2020-08-06 6:28 ` Chih-Min Chao
2020-07-24 0:28 ` [PATCH v2 7/7] target/riscv: check before allocating TCG temps Richard Henderson
2020-08-06 6:28 ` Chih-Min Chao
2020-07-24 2:31 ` [PATCH v2 0/7] target/riscv: NaN-boxing for multiple precison LIU Zhiwei
2020-07-27 23:37 ` Alistair Francis
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=cd6908d3-75eb-b5ee-35fb-b1d20b9c28d8@c-sky.com \
--to=zhiwei_liu@c-sky.com \
--cc=alistair23@gmail.com \
--cc=chihmin.chao@sifive.com \
--cc=frank.chang@sifive.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-riscv@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).