From nobody Mon Feb 9 01:51:51 2026 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 1553583801427714.4562443289451; Tue, 26 Mar 2019 00:03:21 -0700 (PDT) Received: from localhost ([127.0.0.1]:53905 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8g78-0003Hl-9U for importer@patchew.org; Tue, 26 Mar 2019 03:03:14 -0400 Received: from eggs.gnu.org ([209.51.188.92]:40411) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8g59-00024s-C5 for qemu-devel@nongnu.org; Tue, 26 Mar 2019 03:01:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h8fyK-0002jU-FE for qemu-devel@nongnu.org; Tue, 26 Mar 2019 02:54:09 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]:37527) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h8fyK-0002io-94 for qemu-devel@nongnu.org; Tue, 26 Mar 2019 02:54:08 -0400 Received: by mail-pl1-x634.google.com with SMTP id q6so1185530pll.4 for ; Mon, 25 Mar 2019 23:54:08 -0700 (PDT) Received: from localhost (2001-b011-7001-1270-b809-36ef-31ed-7980.dynamic-ip6.hinet.net. [2001:b011:7001:1270:b809:36ef:31ed:7980]) by smtp.gmail.com with ESMTPSA id 5sm17111618pfh.5.2019.03.25.23.54.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Mar 2019 23:54:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding:cc:from:to; bh=vUMtH/5yGUYau7/QJGeMKHa++U5mkr6c61IYfcEVBV8=; b=aEUOX+Q9G1SUTgRRAFiLKgvmayWBgCk7isp8VF40uD0whRfhlDnlI97cQU7G6Syynw 3FaAiMziLvuH2qp4hPz5e4J5ydXkIy2ckMM4xFDiX5PXsU+ru1sCDYbxDGTXhQhx2aFS bh5yzkhM+fXGpPGaGVMFQ+soXvgnbVAwTNr/1ueXFhQIbcJh1OMNFKZHA8+3FZw8S0J1 zsroDVzVtmYL+hIK0I+YogPzF7rd32uP3AVdbaKS1WpuvR7f9sI+LE8NmO1X3Dvw59Ko 20mbTmTBn+VqP0XMtoGgm5mF8oaAVlLwjOcCkoPXdTsCay/tb06KZONNY3Nn5meaWOf5 0dnQ== 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:in-reply-to:references :mime-version:content-transfer-encoding:cc:from:to; bh=vUMtH/5yGUYau7/QJGeMKHa++U5mkr6c61IYfcEVBV8=; b=S9ZZ8DpabO/bfBF2iWkP71QxbpmsoT7L9Z2EFKYyaX5vKf/N92uk/YDqDIXy6LLDgg ZZGHOqfgEGCz71oPV1TwTZxiZanKxP0hdWvgJgcduB1kxeIbAbUV+0bUL9fWje6ocuY1 RHjPwB9MKaKg+wDg4vJEIcgKIfAcHKr5Yr7Fmodw2/IvWpKKgRW/XES+64y5Q8QQSOam Tof5Je806bMltEWG5b6oMKPo0yVA3ZFx5yVyyMGpaGf/8i2nKgRKuYcAf+0rRDj3nAv+ 5Zmzu3560fNmsxSD1cpDw3FV69Tr5+47/5mquwcZWo/tuzSCYUZQTg1f9NVIHZPcN7je BE2A== X-Gm-Message-State: APjAAAWXipgrUD0cZ5oa4lAM7tiJf1Hgp7wy3rUtuPOjqwv6RuhptZhW gXUtPCngAVYofyrILV0tdZ8wCQbBYwp5GALS X-Google-Smtp-Source: APXvYqxt7NbrTSDf218z1iE2wO3yTBfVo2rVOBR1seQuh9ktyiDPmyS67Zn8MalXonmzBxC6coNzcA== X-Received: by 2002:a17:902:a9c8:: with SMTP id b8mr29716159plr.12.1553583247132; Mon, 25 Mar 2019 23:54:07 -0700 (PDT) Date: Mon, 25 Mar 2019 22:56:07 -0700 Message-Id: <20190326055607.14694-2-palmer@sifive.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190326055607.14694-1-palmer@sifive.com> References: <20190326055607.14694-1-palmer@sifive.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Palmer Dabbelt To: Peter Maydell X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::634 Subject: [Qemu-devel] [PULL] 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-riscv@nongnu.org, qemu-devel@nongnu.org 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. Reviewed-by: Richard Henderson Signed-off-by: Palmer Dabbelt --- 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