From nobody Tue Feb 10 12:59:03 2026 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=1712381954; cv=none; d=zohomail.com; s=zohoarc; b=YXm9vqh0o1jr61ejNuVZx0KYX6k4ZnP5KR0vUOgOhPisEtExvJYEHKkJ9wQMCfmbArTarl/yL+XzuObpetc5ocyeG0aUG0ewUXoivki863484mcBxHjPmdO0d+90miLGc5PcWsWZojewFmxJVzVMAyXwtwWWMWgLQgpmul1d9lQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1712381954; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eNDwqVcutDiIERmHFyRHN/ZgN4CmsTKIxQUGe7pIcjw=; b=buu8sNhiZ5RmVSe2C5Phie/j4KUz1m7/Ydx0iiJjVwSZwR/lgqonnY22DV3FGf/d795QNsVV6ykMwM5xqt8jnAl0rXAzp8Yinef8Apee2iHl5R1SH9QxDg1mDKmatHb8g1uv2lVzIsrxAD5FwvF2kwCCeHWbKJMLGn08Wn9qdkY= 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 1712381954797321.46216934889435; Fri, 5 Apr 2024 22:39:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rsykK-0002qt-Hp; Sat, 06 Apr 2024 01:37:44 -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 1rsykJ-0002qf-Jn for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:43 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rsykH-0006IY-Rj for qemu-devel@nongnu.org; Sat, 06 Apr 2024 01:37:43 -0400 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1e2b1cd446fso19519865ad.3 for ; Fri, 05 Apr 2024 22:37:41 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id u12-20020a170903124c00b001dc05535632sm2589378plh.170.2024.04.05.22.37.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 22:37:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712381860; x=1712986660; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eNDwqVcutDiIERmHFyRHN/ZgN4CmsTKIxQUGe7pIcjw=; b=yRlIYthNpN79MdYRCo2XKjSOzYr/8QIVeMD0zQuOWjiA0HKdcnzxcdej6vPA+q7idh W/ple7ODrlrKxNFI5HCjU91A+s17s8XAQC+27AldXFSFuEQdeA4RZe1lfUGZQmB7JoQ1 s2AjoyPpkbZzUXPYGxxLd+LdhbHjE0RjmFAsMKHU494Aic0iw1nkwcmULvh+yK/i+aBh MkbTm2anYyvolVOAp20Pg/XoYT9VUuBgTulXUpR5to4KFOmFKM6D+TRbwmB3QIlkjroW Ee8YJEOZAOFwmMbk6GOKbBgt7fuRlXnz8Ot91r57oFuuID/nYxp/LMQ/+NpBP4qYJO/p kCMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712381860; x=1712986660; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eNDwqVcutDiIERmHFyRHN/ZgN4CmsTKIxQUGe7pIcjw=; b=YRjf8LInQxAlA/Kdc28Wb8tblrJtgc8hFcNdHKTcOJoERhoC06bHEP4NzM4iLVPkxf xV0MFbs0PMZMTdkY8bp5ps49l9I8pOAOpZmlIrCTMDsk5BIpHfigQO1q4+X+HMHd8wUZ iUFXP6UD2pQENon7OEvVdS4kXULHnY90kW+9CntVJ8DGLEn9q7cCqiQENu+bGfeML68z 4UEM5EQC5IaT+YNV0lEuwanbRjnSI03+8Sb+0N30kH3I+XOS0CP888R4YL/GazKw7Ifr rDOKUg0Qib/HSLBdF8p2B8exDMOq83nLNMRtJYfxaOchO1+M912+AiJqm1ameGCjayyG Miuw== X-Gm-Message-State: AOJu0YwjRGkiXZ+TkSBit9pu3OFCUIWdcJfo+u9a1Ao1k3Ur8hv15gEo hZSFR/3UkLt7r05QkWfUJ5T/oqxtXHDLftwj9l7zgF39PezN7lxEkmUrOHW/D+Lv6Qg9Gr6koBd I X-Google-Smtp-Source: AGHT+IHnqjAFKupO/j+BXCydilO81rLvgIr5jftNW/sTUjjZ3H4JtseEja7Ji0dBrT9cYnuJ7MS1tQ== X-Received: by 2002:a17:902:a387:b0:1e0:d631:5563 with SMTP id x7-20020a170902a38700b001e0d6315563mr3201583pla.66.1712381860408; Fri, 05 Apr 2024 22:37:40 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: zack@buhman.org, peter.maydell@linaro.org, ysato@users.sourceforge.jp Subject: [PATCH v3 3/4] target/sh4: Fix mac.l with saturation enabled Date: Fri, 5 Apr 2024 19:37:31 -1000 Message-Id: <20240406053732.191398-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240406053732.191398-1-richard.henderson@linaro.org> References: <20240406053732.191398-1-richard.henderson@linaro.org> 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::62d; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1712381956924100007 Content-Type: text/plain; charset="utf-8" From: Zack Buhman The saturation arithmetic logic in helper_macl is not correct. I tested and verified this behavior on a SH7091. Signed-off-by: Zack Buhman Message-Id: <20240404162641.27528-2-zack@buhman.org> [rth: Reformat helper_macl, add a test case.] Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- target/sh4/helper.h | 2 +- target/sh4/op_helper.c | 23 ++++++------ tests/tcg/sh4/test-macl.c | 67 +++++++++++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 5 +++ 4 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 tests/tcg/sh4/test-macl.c diff --git a/target/sh4/helper.h b/target/sh4/helper.h index 8d792f6b55..64056e4a39 100644 --- a/target/sh4/helper.h +++ b/target/sh4/helper.h @@ -11,7 +11,7 @@ DEF_HELPER_3(movcal, void, env, i32, i32) DEF_HELPER_1(discard_movcal_backup, void, env) DEF_HELPER_2(ocbi, void, env, i32) =20 -DEF_HELPER_3(macl, void, env, i32, i32) +DEF_HELPER_3(macl, void, env, s32, s32) DEF_HELPER_3(macw, void, env, i32, i32) =20 DEF_HELPER_2(ld_fpscr, void, env, i32) diff --git a/target/sh4/op_helper.c b/target/sh4/op_helper.c index 4559d0d376..d0bae0cc00 100644 --- a/target/sh4/op_helper.c +++ b/target/sh4/op_helper.c @@ -158,20 +158,23 @@ void helper_ocbi(CPUSH4State *env, uint32_t address) } } =20 -void helper_macl(CPUSH4State *env, uint32_t arg0, uint32_t arg1) +void helper_macl(CPUSH4State *env, int32_t arg0, int32_t arg1) { + const int64_t min =3D -(1ll << 47); + const int64_t max =3D (1ll << 47) - 1; + int64_t mul =3D (int64_t)arg0 * arg1; + int64_t mac =3D env->mac; int64_t res; =20 - res =3D ((uint64_t) env->mach << 32) | env->macl; - res +=3D (int64_t) (int32_t) arg0 *(int64_t) (int32_t) arg1; - env->mach =3D (res >> 32) & 0xffffffff; - env->macl =3D res & 0xffffffff; - if (env->sr & (1u << SR_S)) { - if (res < 0) - env->mach |=3D 0xffff0000; - else - env->mach &=3D 0x00007fff; + if (!(env->sr & (1u << SR_S))) { + res =3D mac + mul; + } else if (sadd64_overflow(mac, mul, &res)) { + res =3D mac < 0 ? min : max; + } else { + res =3D MIN(MAX(res, min), max); } + + env->mac =3D res; } =20 void helper_macw(CPUSH4State *env, uint32_t arg0, uint32_t arg1) diff --git a/tests/tcg/sh4/test-macl.c b/tests/tcg/sh4/test-macl.c new file mode 100644 index 0000000000..b66c854365 --- /dev/null +++ b/tests/tcg/sh4/test-macl.c @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include +#include +#include + +#define MACL_S_MIN (-(1ll << 47)) +#define MACL_S_MAX ((1ll << 47) - 1) + +int64_t mac_l(int64_t mac, const int32_t *a, const int32_t *b) +{ + register uint32_t macl __asm__("macl") =3D mac; + register uint32_t mach __asm__("mach") =3D mac >> 32; + + asm volatile("mac.l @%0+,@%1+" + : "+r"(a), "+r"(b), "+x"(macl), "+x"(mach)); + + return ((uint64_t)mach << 32) | macl; +} + +typedef struct { + int64_t mac; + int32_t a, b; + int64_t res[2]; +} Test; + +__attribute__((noinline)) +void test(const Test *t, int sat) +{ + int64_t res; + + if (sat) { + asm volatile("sets"); + } else { + asm volatile("clrs"); + } + res =3D mac_l(t->mac, &t->a, &t->b); + + if (res !=3D t->res[sat]) { + fprintf(stderr, "%#llx + (%#x * %#x) =3D %#llx -- got %#llx\n", + t->mac, t->a, t->b, t->res[sat], res); + abort(); + } +} + +int main() +{ + static const Test tests[] =3D { + { 0x00007fff12345678ll, INT32_MAX, INT32_MAX, + { 0x40007ffe12345679ll, MACL_S_MAX } }, + { MACL_S_MIN, -1, 1, + { 0xffff7fffffffffffll, MACL_S_MIN } }, + { INT64_MIN, -1, 1, + { INT64_MAX, MACL_S_MIN } }, + { 0x00007fff00000000ll, INT32_MAX, INT32_MAX, + { 0x40007ffe00000001ll, MACL_S_MAX } }, + { 4, 1, 2, { 6, 6 } }, + { -4, -1, -2, { -2, -2 } }, + }; + + for (int i =3D 0; i < sizeof(tests) / sizeof(tests[0]); ++i) { + for (int j =3D 0; j < 2; ++j) { + test(&tests[i], j); + } + } + return 0; +} diff --git a/tests/tcg/sh4/Makefile.target b/tests/tcg/sh4/Makefile.target index 16eaa850a8..9a11c10924 100644 --- a/tests/tcg/sh4/Makefile.target +++ b/tests/tcg/sh4/Makefile.target @@ -9,3 +9,8 @@ run-signals: signals $(call skip-test, $<, "BROKEN") run-plugin-signals-with-%: $(call skip-test, $<, "BROKEN") + +VPATH +=3D $(SRC_PATH)/tests/tcg/sh4 + +test-macl: CFLAGS +=3D -O -g +TESTS +=3D test-macl --=20 2.34.1