From nobody Sat May 18 10:48:36 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1666433885; cv=none; d=zohomail.com; s=zohoarc; b=YSWbHOZ7ORefx6aRXL4pr6XmL6AJHCgrBG5wrhK+ZLxXEXrycKSxHMBIlJYlJySRCoJaZwOFkG5vP9wDG0WX1mazdM7wE2TiVXLxndURSBfNUsX5CShxF/RF4Och47+0YLXcl7S4l61U5QMzjBQ4AL+iDL6yZUejsLbZT+uUQCc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666433885; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=RU8/5HTo8+baKMeHrUEQiBWbmsr/936MisDwlU3iBGk=; b=bwrLphjUaKfGuS6IL5LXhO09PtANhG0onETrF+QCe5CMp3cBJaj02+3mLI+y+STi26JXFrMEqaPr7BPsvG7+rNL0grDodgjqZ/vmCQ/hCA6Bw21rFhvvEKVizaDQO4/Bj8OFWxQeo4CTzyFoesxFUML009p2PdWA/cMPw8b8bOA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666433885395620.6566531441348; Sat, 22 Oct 2022 03:18:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1omBHj-0004aU-Qy; Sat, 22 Oct 2022 05:59:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1omBHi-0004aM-F7 for qemu-devel@nongnu.org; Sat, 22 Oct 2022 05:59:18 -0400 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1omBHT-0008FD-7t for qemu-devel@nongnu.org; Sat, 22 Oct 2022 05:59:18 -0400 Received: by mail-pg1-x533.google.com with SMTP id f9so246483pgj.2 for ; Sat, 22 Oct 2022 02:59:02 -0700 (PDT) Received: from localhost.localdomain ([149.135.10.35]) by smtp.gmail.com with ESMTPSA id 129-20020a621987000000b0056b3c863950sm2021564pfz.8.2022.10.22.02.58.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Oct 2022 02:59:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=RU8/5HTo8+baKMeHrUEQiBWbmsr/936MisDwlU3iBGk=; b=m+HclE7BndO+171z9rIM5f+KdAsuQQwcekZv5h0GAIUIYxV1BvJndK+zpQZPe4T7sa o4LdYXvgfBlEduVXcPXw8z0G7MAkDMTXkonGjlM1uL5YC1CmJ9GBnfx4Jsc8wjpCFeKh 5rZiy9WR0936WanKNzwHeudWvmwBdswwKVxnJhdB/HoNqIxjgfmP24lDPgrZxEIdmpM0 rvx7ZM32MWjhn/TgBFOEXr1BQZGYWPJ7/8WvX8AdWplTzcjPbRXllTATwrzhPAEEgvMa xeUomqcp10JpVUIXfygszLOXgpQWOFb1EoGEGhuSosK1+vQ0FUO68gtcCI+OVwc40TiH glIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=RU8/5HTo8+baKMeHrUEQiBWbmsr/936MisDwlU3iBGk=; b=m7QC7RQVdiCjum4nj/13IsaZQ4J1cmGFfbkfXvdECPJkqwedVuoKYIsgxeDTjOuYii ImChr1gdzAzljDZRV/AXj7iizUWreXTfsaRrtmK6tgAvP3JQ5nG/TAwuJV01p+fUbjTA kYgdcfJGi0fyVttD+G8lxo7qXCGigLcKgcnzhmFcvPNan38x/hw2BixQg5QTwHN2xd9W bl8Je9rmAjQHfFYHA1jo1VQ5P1fUy7xc/bcasnaro4T+DNJO+HtSeky++X+PKaWekN8Z eIzu3kDT/8ADUVRa3byOzH6ov1zk24LbRa+glpwDoSv0TBgnIydM+hGMyvOuKPnJaSbT tLeQ== X-Gm-Message-State: ACrzQf2ZrtTTsVslf9Gd1sPTKa/SSNSggB0uDQlssY8Yxg0SHMuxBS7u lLMHICmwXXkzpYuTX+TBbE7DgAI+uSOOJQo0 X-Google-Smtp-Source: AMsMyM7wpT23k+x74M/B8jO4WigDRTCxoZaYInGTBWeodb80VqIvB7Iq+88z3waJiUVBEua9mpZc+A== X-Received: by 2002:a05:6a00:174c:b0:565:c73a:9117 with SMTP id j12-20020a056a00174c00b00565c73a9117mr23288425pfc.23.1666432741344; Sat, 22 Oct 2022 02:59:01 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Alistair.Francis@wdc.com, qemu-riscv@nongnu.org, palmer@dabbelt.com, LIU Zhiwei Subject: [PATCH] tcg/riscv: Fix range matched by TCG_CT_CONST_M12 Date: Sat, 22 Oct 2022 19:58:21 +1000 Message-Id: <20221022095821.2441874-1-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=2607:f8b0:4864:20::533; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x533.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1666433887357100003 Content-Type: text/plain; charset="utf-8" We were matching a signed 13-bit range, not a 12-bit range. Expand the commentary within the function and be explicit about all of the ranges. Reported-by: LIU Zhiwei Signed-off-by: Richard Henderson Reviewed-by: Alistair Francis Reviewed-by: LIU Zhiwei --- tcg/riscv/tcg-target.c.inc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tcg/riscv/tcg-target.c.inc b/tcg/riscv/tcg-target.c.inc index 1cdaf7b57b..2a84c57bec 100644 --- a/tcg/riscv/tcg-target.c.inc +++ b/tcg/riscv/tcg-target.c.inc @@ -154,13 +154,26 @@ static bool tcg_target_const_match(int64_t val, TCGTy= pe type, int ct) if ((ct & TCG_CT_CONST_ZERO) && val =3D=3D 0) { return 1; } - if ((ct & TCG_CT_CONST_S12) && val =3D=3D sextreg(val, 0, 12)) { + /* + * Sign extended from 12 bits: [-0x800, 0x7ff]. + * Used for most arithmetic, as this is the isa field. + */ + if ((ct & TCG_CT_CONST_S12) && val >=3D -0x800 && val <=3D 0x7ff) { return 1; } - if ((ct & TCG_CT_CONST_N12) && -val =3D=3D sextreg(-val, 0, 12)) { + /* + * Sign extended from 12 bits, negated: [-0x7ff, 0x800]. + * Used for subtraction, where a constant must be handled by ADDI. + */ + if ((ct & TCG_CT_CONST_N12) && val >=3D -0x7ff && val <=3D 0x800) { return 1; } - if ((ct & TCG_CT_CONST_M12) && val >=3D -0xfff && val <=3D 0xfff) { + /* + * Sign extended from 12 bits, +/- matching: [-0x7ff, 0x7ff]. + * Used by addsub2, which may need the negative operation, + * and requires the modified constant to be representable. + */ + if ((ct & TCG_CT_CONST_M12) && val >=3D -0x7ff && val <=3D 0x7ff) { return 1; } return 0; --=20 2.34.1