Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jynkG-0005Di-UF for mharc-qemu-riscv@gnu.org; Thu, 23 Jul 2020 22:47:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53624) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jynkE-0005Cy-QY; Thu, 23 Jul 2020 22:47:34 -0400 Received: from smtp2200-217.mail.aliyun.com ([121.197.200.217]:40830) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jynkC-0002mj-MJ; Thu, 23 Jul 2020 22:47:34 -0400 X-Alimail-AntiSpam: AC=CONTINUE; BC=0.07436282|-1; CH=green; DM=|CONTINUE|false|; DS=CONTINUE|ham_system_inform|0.563953-6.42188e-05-0.435982; FP=0|0|0|0|0|-1|-1|-1; HT=e02c03307; MF=zhiwei_liu@c-sky.com; NM=1; PH=DS; RN=5; RT=5; SR=0; TI=SMTPD_---.I6id2jM_1595558846; Received: from 30.225.208.37(mailfrom:zhiwei_liu@c-sky.com fp:SMTPD_---.I6id2jM_1595558846) by smtp.aliyun-inc.com(10.147.40.200); Fri, 24 Jul 2020 10:47:26 +0800 Subject: Re: [PATCH v2 4/7] target/riscv: Check nanboxed inputs to fp helpers To: Richard Henderson , qemu-devel@nongnu.org Cc: frank.chang@sifive.com, qemu-riscv@nongnu.org, alistair23@gmail.com References: <20200724002807.441147-1-richard.henderson@linaro.org> <20200724002807.441147-5-richard.henderson@linaro.org> From: LIU Zhiwei Message-ID: Date: Fri, 24 Jul 2020 10:47:26 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200724002807.441147-5-richard.henderson@linaro.org> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Received-SPF: none client-ip=121.197.200.217; envelope-from=zhiwei_liu@c-sky.com; helo=smtp2200-217.mail.aliyun.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/23 22:31:14 X-ACL-Warn: Detected OS = Linux 3.x [generic] [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_NONE=0.001, UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-riscv@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Jul 2020 02:47:35 -0000 On 2020/7/24 8:28, Richard Henderson wrote: > If a 32-bit input is not properly nanboxed, then the input is > replaced with the default qnan. > > Signed-off-by: Richard Henderson > --- > target/riscv/internals.h | 11 +++++++ > target/riscv/fpu_helper.c | 64 ++++++++++++++++++++++++++++----------- > 2 files changed, 57 insertions(+), 18 deletions(-) > > diff --git a/target/riscv/internals.h b/target/riscv/internals.h > index 9f4ba7d617..f1a546dba6 100644 > --- a/target/riscv/internals.h > +++ b/target/riscv/internals.h > @@ -43,4 +43,15 @@ static inline uint64_t nanbox_s(float32 f) > return f | MAKE_64BIT_MASK(32, 32); > } > > +static inline float32 check_nanbox_s(uint64_t f) > +{ > + uint64_t mask = MAKE_64BIT_MASK(32, 32); > + > + if (likely((f & mask) == mask)) { > + return (uint32_t)f; > + } else { > + return 0x7fc00000u; /* default qnan */ > + } > +} > + If possible, +static inline float32 check_nanbox(uint64_t f, uint32_t flen) +{ + uint64_t mask = MAKE_64BIT_MASK(flen, 64 - flen); + + if (likely((f & mask) == mask)) { + return (uint32_t)f; + } else { + return (flen == 32) ? 0x7fc00000u : 0x7e00u; /* default qnan */ + } +} + Reviewed-by: LIU Zhiwei Zhiwei > #endif > diff --git a/target/riscv/fpu_helper.c b/target/riscv/fpu_helper.c > index 72541958a7..bb346a8249 100644 > --- a/target/riscv/fpu_helper.c > +++ b/target/riscv/fpu_helper.c > @@ -81,9 +81,12 @@ void helper_set_rounding_mode(CPURISCVState *env, uint32_t rm) > set_float_rounding_mode(softrm, &env->fp_status); > } > > -static uint64_t do_fmadd_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2, > - uint64_t frs3, int flags) > +static uint64_t do_fmadd_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2, > + uint64_t rs3, int flags) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > + float32 frs3 = check_nanbox_s(rs3); > return nanbox_s(float32_muladd(frs1, frs2, frs3, flags, &env->fp_status)); > } > > @@ -139,74 +142,97 @@ uint64_t helper_fnmadd_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2, > float_muladd_negate_product, &env->fp_status); > } > > -uint64_t helper_fadd_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) > +uint64_t helper_fadd_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > return nanbox_s(float32_add(frs1, frs2, &env->fp_status)); > } > > -uint64_t helper_fsub_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) > +uint64_t helper_fsub_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > return nanbox_s(float32_sub(frs1, frs2, &env->fp_status)); > } > > -uint64_t helper_fmul_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) > +uint64_t helper_fmul_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > return nanbox_s(float32_mul(frs1, frs2, &env->fp_status)); > } > > -uint64_t helper_fdiv_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) > +uint64_t helper_fdiv_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > return nanbox_s(float32_div(frs1, frs2, &env->fp_status)); > } > > -uint64_t helper_fmin_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) > +uint64_t helper_fmin_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > return nanbox_s(float32_minnum(frs1, frs2, &env->fp_status)); > } > > -uint64_t helper_fmax_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) > +uint64_t helper_fmax_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > return nanbox_s(float32_maxnum(frs1, frs2, &env->fp_status)); > } > > -uint64_t helper_fsqrt_s(CPURISCVState *env, uint64_t frs1) > +uint64_t helper_fsqrt_s(CPURISCVState *env, uint64_t rs1) > { > + float32 frs1 = check_nanbox_s(rs1); > return nanbox_s(float32_sqrt(frs1, &env->fp_status)); > } > > -target_ulong helper_fle_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) > +target_ulong helper_fle_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > return float32_le(frs1, frs2, &env->fp_status); > } > > -target_ulong helper_flt_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) > +target_ulong helper_flt_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > return float32_lt(frs1, frs2, &env->fp_status); > } > > -target_ulong helper_feq_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2) > +target_ulong helper_feq_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2) > { > + float32 frs1 = check_nanbox_s(rs1); > + float32 frs2 = check_nanbox_s(rs2); > return float32_eq_quiet(frs1, frs2, &env->fp_status); > } > > -target_ulong helper_fcvt_w_s(CPURISCVState *env, uint64_t frs1) > +target_ulong helper_fcvt_w_s(CPURISCVState *env, uint64_t rs1) > { > + float32 frs1 = check_nanbox_s(rs1); > return float32_to_int32(frs1, &env->fp_status); > } > > -target_ulong helper_fcvt_wu_s(CPURISCVState *env, uint64_t frs1) > +target_ulong helper_fcvt_wu_s(CPURISCVState *env, uint64_t rs1) > { > + float32 frs1 = check_nanbox_s(rs1); > return (int32_t)float32_to_uint32(frs1, &env->fp_status); > } > > #if defined(TARGET_RISCV64) > -uint64_t helper_fcvt_l_s(CPURISCVState *env, uint64_t frs1) > +uint64_t helper_fcvt_l_s(CPURISCVState *env, uint64_t rs1) > { > + float32 frs1 = check_nanbox_s(rs1); > return float32_to_int64(frs1, &env->fp_status); > } > > -uint64_t helper_fcvt_lu_s(CPURISCVState *env, uint64_t frs1) > +uint64_t helper_fcvt_lu_s(CPURISCVState *env, uint64_t rs1) > { > + float32 frs1 = check_nanbox_s(rs1); > return float32_to_uint64(frs1, &env->fp_status); > } > #endif > @@ -233,8 +259,9 @@ uint64_t helper_fcvt_s_lu(CPURISCVState *env, uint64_t rs1) > } > #endif > > -target_ulong helper_fclass_s(uint64_t frs1) > +target_ulong helper_fclass_s(uint64_t rs1) > { > + float32 frs1 = check_nanbox_s(rs1); > return fclass_s(frs1); > } > > @@ -275,7 +302,8 @@ uint64_t helper_fcvt_s_d(CPURISCVState *env, uint64_t rs1) > > uint64_t helper_fcvt_d_s(CPURISCVState *env, uint64_t rs1) > { > - return float32_to_float64(rs1, &env->fp_status); > + float32 frs1 = check_nanbox_s(rs1); > + return float32_to_float64(frs1, &env->fp_status); > } > > uint64_t helper_fsqrt_d(CPURISCVState *env, uint64_t frs1)