From nobody Tue May 21 00:21:21 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1553181042550723.1456978660473; Thu, 21 Mar 2019 08:10:42 -0700 (PDT) Received: from localhost ([127.0.0.1]:39227 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h6zKz-0002g3-Gs for importer@patchew.org; Thu, 21 Mar 2019 11:10:33 -0400 Received: from eggs.gnu.org ([209.51.188.92]:46952) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h6zBV-0004aW-Rk for qemu-devel@nongnu.org; Thu, 21 Mar 2019 11:00:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h6zBU-0002uX-JY for qemu-devel@nongnu.org; Thu, 21 Mar 2019 11:00:45 -0400 Received: from mail-pg1-x52e.google.com ([2607:f8b0:4864:20::52e]:43487) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h6zBU-0002sc-6z for qemu-devel@nongnu.org; Thu, 21 Mar 2019 11:00:44 -0400 Received: by mail-pg1-x52e.google.com with SMTP id l11so4357944pgq.10 for ; Thu, 21 Mar 2019 08:00:42 -0700 (PDT) Received: from localhost (60-250-203-158.HINET-IP.hinet.net. [60.250.203.158]) by smtp.gmail.com with ESMTPSA id m17sm8304368pff.170.2019.03.21.08.00.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 Mar 2019 08:00:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=subject:date:message-id:mime-version:content-transfer-encoding:cc :from:to; bh=JORoJzYgQFH/7WNMb1acrFIMBBGdNJO3f8a7htCYba0=; b=SnnKAfMN6GX24WcIlk7TgcjH+TMnChzRlFFZXOcUTQ1KmNAFqls3ccIw4LqSw/3DUz Zv8LqczXFmsmn4q7D5qCjn8YP/Lyg02ek8qh3FSIh+13qIf1H6xnuzpqFeM3ziIG6BQ2 XJdzYHCKNMryAziivuM+4lxLK6RS7NEAChohiGgPKLSnuaSgRDOBHog5iAy92JHoXn5v uuteJzrhlVW3LnkJqaWQ8WC9KpVRkl91prAa7VPnGVoQfvvNcbicymfEF9a2hmD30MNB tbXEuF32iEJ90tg5Tnf3AORRXXyXR7JKhFPAPyY8bOiZPOBUQUznOgn8kVpL45R4VZY0 jrWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:date:message-id:mime-version :content-transfer-encoding:cc:from:to; bh=JORoJzYgQFH/7WNMb1acrFIMBBGdNJO3f8a7htCYba0=; b=UA/z5qX0uVePB08wXXj/loLD9/DnIUw6zqdor7eHA1/FXpUM7mkgrKAvCTARjn99pF DERsDRJcUxnG4BJwx/NY9Ol9g2R3LfbTNq7/jfdFe4clt0wk5Zbyw5XQfQodkBUOrGv8 UEpsGEEnoLT4EmtCXGxYMJ9vMDQPE+12NgwQ+gRSdd5yDmaoPyHyto2bzLOppfnDVFl/ 91O9Avpk3/CAAYs8DqhrKxGTMp2Vk/gBgMVEfE+c9UzaZyoF2IxoZ5USKNM/eW7HoUdq bLa7jb+8rvzjciPhncfeIC9/5yfAeMmQoMopiRQV0rAd+4y/alqD/x2P04nkcJiKa4gW 6GdQ== X-Gm-Message-State: APjAAAVNjar9NJeorHPvIWin/wtLqgHEAOuTWZ5yPaMFCWcy5xMOMJEq 9DCUQZwCb2d3mrNsESNHd5KfiaH54Balou0j X-Google-Smtp-Source: APXvYqwfNHjNPG4AGfyDOuJU5cNhzZO5DU3mQc48Y4CO+DUILbKS9fdIKtsy3b5eX8RSA4HZ4JySag== X-Received: by 2002:aa7:8251:: with SMTP id e17mr3766682pfn.96.1553180440752; Thu, 21 Mar 2019 08:00:40 -0700 (PDT) Date: Thu, 21 Mar 2019 07:59:20 -0700 Message-Id: <20190321145920.20897-1-palmer@sifive.com> X-Mailer: git-send-email 2.19.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Palmer Dabbelt To: qemu-riscv@nongnu.org X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::52e Subject: [Qemu-devel] [PATCH] target/riscv: Zero extend the inputs of divuw and remuw X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Palmer Dabbelt , qemu-devel@nongnu.org, Kito Cheng Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" While running the GCC test suite against 4.0.0-rc0, Kito found a regression introduced by the decodetree conversion that caused divuw and remuw to sign-extend their inputs. The ISA manual says they are supposed to be zero extended: DIVW and DIVUW instructions are only valid for RV64, and divide the lower 32 bits of rs1 by the lower 32 bits of rs2, treating them as signed and unsigned integers respectively, placing the 32-bit quotient in rd, sign-extended to 64 bits. REMW and REMUW instructions are only valid for RV64, and provide the corresponding signed and unsigned remainder operations respectively. Both REMW and REMUW always sign-extend the 32-bit result to 64 bits, including on a divide by zero. Here's Kito's reduced test case from the GCC test suite unsigned calc_mp(unsigned mod) { unsigned a,b,c; c=3D-1; a=3Dc/mod; b=3D0-a*mod; if (b > mod) { a +=3D 1; b-=3Dmod; } return b; } int main(int argc, char *argv[]) { unsigned x =3D 1234; unsigned y =3D calc_mp(x); if ((sizeof (y) =3D=3D 4 && y !=3D 680) || (sizeof (y) =3D=3D 2 && y !=3D 134)) abort (); exit (0); } I haven't done any other testing on this, but it does fix the test case. Signed-off-by: Palmer Dabbelt Reviewed-by: Richard Henderson --- target/riscv/insn_trans/trans_rvm.inc.c | 4 ++-- target/riscv/translate.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/target/riscv/insn_trans/trans_rvm.inc.c b/target/riscv/insn_tr= ans/trans_rvm.inc.c index 204af225f8f3..47cd6edc72a1 100644 --- a/target/riscv/insn_trans/trans_rvm.inc.c +++ b/target/riscv/insn_trans/trans_rvm.inc.c @@ -103,7 +103,7 @@ static bool trans_divw(DisasContext *ctx, arg_divw *a) static bool trans_divuw(DisasContext *ctx, arg_divuw *a) { REQUIRE_EXT(ctx, RVM); - return gen_arith_div_w(ctx, a, &gen_divu); + return gen_arith_div_uw(ctx, a, &gen_divu); } =20 static bool trans_remw(DisasContext *ctx, arg_remw *a) @@ -115,6 +115,6 @@ static bool trans_remw(DisasContext *ctx, arg_remw *a) static bool trans_remuw(DisasContext *ctx, arg_remuw *a) { REQUIRE_EXT(ctx, RVM); - return gen_arith_div_w(ctx, a, &gen_remu); + return gen_arith_div_uw(ctx, a, &gen_remu); } #endif diff --git a/target/riscv/translate.c b/target/riscv/translate.c index 049fa65c6611..dd763647ea55 100644 --- a/target/riscv/translate.c +++ b/target/riscv/translate.c @@ -600,6 +600,27 @@ static bool gen_arith_div_w(DisasContext *ctx, arg_r *= a, return true; } =20 +static bool gen_arith_div_uw(DisasContext *ctx, arg_r *a, + void(*func)(TCGv, TCGv, TCGv)) +{ + TCGv source1, source2; + source1 =3D tcg_temp_new(); + source2 =3D tcg_temp_new(); + + gen_get_gpr(source1, a->rs1); + gen_get_gpr(source2, a->rs2); + tcg_gen_ext32u_tl(source1, source1); + tcg_gen_ext32u_tl(source2, source2); + + (*func)(source1, source1, source2); + + tcg_gen_ext32s_tl(source1, source1); + gen_set_gpr(a->rd, source1); + tcg_temp_free(source1); + tcg_temp_free(source2); + return true; +} + #endif =20 static bool gen_arith(DisasContext *ctx, arg_r *a, --=20 2.19.2