From patchwork Tue Apr 9 19:35:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 787235 Delivered-To: patch@linaro.org Received: by 2002:adf:fdd2:0:b0:346:15ad:a2a with SMTP id i18csp383299wrs; Tue, 9 Apr 2024 12:36:46 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW0Je2eFNQa3lop3j+/NE3FgKXltagkKmyKNNlYkEqGN8ZpIKepStAhHUtpDYbxyGaoVviSUXDdmX88CM/yCebO X-Google-Smtp-Source: AGHT+IGlyFsEHM/2zLR9aDoa8hYysybZ9uB7O0IeUXcNjDAYlf9ug85gO/zfOrbFy8L31Alm5kaZ X-Received: by 2002:a05:622a:1ba2:b0:434:668b:32d1 with SMTP id bp34-20020a05622a1ba200b00434668b32d1mr844058qtb.9.1712691406577; Tue, 09 Apr 2024 12:36:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712691406; cv=none; d=google.com; s=arc-20160816; b=OLPgbkwN+X/BSAau2osnd28LjUOS4NscFOXbIHnZeRx2yIrETiTIB5pl6F9Za0hQVi 3U6eAeorj3svIk5bvzbsCTL15MF4rsA4hPf+FCkKVu7gm2E/JB7vgQ5OQFmJDu0uhTc3 llPyQDsCChBRgPAuXHgA7oZDb7MikKl87+2UoAYthn6VEWItfGW8z8sG1KSnrepbT+YW QwuKTXr9BJuugellkOw1qsQuEqb/FeXuq4uJjBTOBUwwDshG3w927mQ/NHcrh9FV6SZH F/ClDFBYxjpur0b6o+Gy6vp95crtO9mLxrF4GtPyrLhOJwi/LEmPGgnyEP9t3bDAqHZv 1JPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=kLktQTH6TNyDdnNQxMnhQfTJfTanNKRfEbSAV10avAE=; fh=HJpP7WWWMf63xDEKZYjQL9Y0mK13c8dar/udiJ/djJ4=; b=w2CwpwZxNDxclgnfTZeZaHwsVt/QeVqCfbkDlvgL+6O0H0dotn8mXJ8pNsp2BbsOz9 8Epv1l8qZDZhpmVrvK3/AEEM+IbcR6inh7hS4M5EOAgkDuHtOSDJIhmUEjlBhQ2Ub3f7 N1Kp2WNU1wyXSlPOFu6k9+nv8G8yte3C4VFZYWY/BUt0E1NFRtNGSyHBS04sRkobt1VR E+F7ONyO0RxHWz5DQBjviO5DlLVtViK0pK7e67GAOrbUIIIn25B71Gy08kthmQxvl5// 9hGpwTH13AijkS8cC+SjTcDjzcBVQ9Fdv0jRt+ku4LzVtTDv5xSVjKJ/C9ALQu1n6Ans 8urQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dnh+ZjsJ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id z10-20020ac87f8a000000b004348b9e8431si6130289qtj.598.2024.04.09.12.36.46 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 09 Apr 2024 12:36:46 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dnh+ZjsJ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ruHGW-0000Zr-Hy; Tue, 09 Apr 2024 15:36:20 -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 1ruHGU-0000ZE-Rb for qemu-devel@nongnu.org; Tue, 09 Apr 2024 15:36:18 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ruHGT-0004dq-1P for qemu-devel@nongnu.org; Tue, 09 Apr 2024 15:36:18 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1e3c7549078so26647645ad.0 for ; Tue, 09 Apr 2024 12:36:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712691376; x=1713296176; 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=kLktQTH6TNyDdnNQxMnhQfTJfTanNKRfEbSAV10avAE=; b=dnh+ZjsJlns20lT2lNHhCO3aGUQdDzne+R01XIRDmXp11Zx70gK8Udc8M9jacuintM egkWeZ8hjoAkFDZ73k0sEz+tXFYPT5PvGpjLz9CH14xlX8juey9Q5SOfzsKH4Wtv9YEj dSdKVXUaSJ+KGBiOzw+uPDnsiQ84owbQneopjGl/LV8irkDo9byT3KhpCmRAM/my2fLW wEkNAqt064IKuFVMGiJxb4VCB7D/UoeMPIG6n5BpEEHJMqAFcz+Tj5Jy/QjhgE4csrdp qs7poTN1w45fZFTQPNS8B5G8qHvZmoj8DGFfvowjTF7uTjLd5FsYGJNVrowx83Vti76J XL+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712691376; x=1713296176; 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=kLktQTH6TNyDdnNQxMnhQfTJfTanNKRfEbSAV10avAE=; b=GRmsMCqPdipiGjnlJa0JQEVwElKv0VncU1vuurwe1TLEscoRrdxSRb6KWYCBHcobQc pkzmGb54n94diARiQRBFEABCQkIfVDsHhxb8Xm5oZmaCuJNZDH3aDEJYKzoanL/9j4BX P0qA6jUyRnumA+gw6LPCltxPwPODMdkW1BUICx3wR3cE56rgG2l4VCVmi3f8CmtYbkYm 07sRxQjGDgUYLO68tM+j71tNBmwfHXsN5mBXNW+zlau4e76IvODTU34JdyeZPEu5rcfF m2NEgJ1geYQGAM85qaeDL0kenZVoYPTtBsM26KTT3K6+ZnrMB+Ry6odo5wr8Xj5QP3T4 X+Ww== X-Gm-Message-State: AOJu0YyHHD9okGOr8X5vEj2xtpxMRhl6MD49wjMg9YJrVpCSu7wSQ3HL L5ikuND68Y64vJdGSKrxxmC9ZiJfwkbsK2OQpuEylUnBtDtM0N9n3wccK8JEXjTED4mEHjf1rvv l X-Received: by 2002:a17:903:185:b0:1e2:7734:63dd with SMTP id z5-20020a170903018500b001e2773463ddmr5742263plg.30.1712691375703; Tue, 09 Apr 2024 12:36:15 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id c17-20020a170902d49100b001e1071cf0bbsm6065843plg.302.2024.04.09.12.36.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 12:36:15 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Zack Buhman , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PULL v2 07/20] target/sh4: Fix mac.l with saturation enabled Date: Tue, 9 Apr 2024 09:35:50 -1000 Message-Id: <20240409193603.1703216-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240409193603.1703216-1-richard.henderson@linaro.org> References: <20240409193603.1703216-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x632.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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org 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é --- 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) -DEF_HELPER_3(macl, void, env, i32, i32) +DEF_HELPER_3(macl, void, env, s32, s32) DEF_HELPER_3(macw, void, env, i32, i32) 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) } } -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 = -(1ll << 47); + const int64_t max = (1ll << 47) - 1; + int64_t mul = (int64_t)arg0 * arg1; + int64_t mac = env->mac; int64_t res; - res = ((uint64_t) env->mach << 32) | env->macl; - res += (int64_t) (int32_t) arg0 *(int64_t) (int32_t) arg1; - env->mach = (res >> 32) & 0xffffffff; - env->macl = res & 0xffffffff; - if (env->sr & (1u << SR_S)) { - if (res < 0) - env->mach |= 0xffff0000; - else - env->mach &= 0x00007fff; + if (!(env->sr & (1u << SR_S))) { + res = mac + mul; + } else if (sadd64_overflow(mac, mul, &res)) { + res = mac < 0 ? min : max; + } else { + res = MIN(MAX(res, min), max); } + + env->mac = res; } 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") = mac; + register uint32_t mach __asm__("mach") = 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 = mac_l(t->mac, &t->a, &t->b); + + if (res != t->res[sat]) { + fprintf(stderr, "%#llx + (%#x * %#x) = %#llx -- got %#llx\n", + t->mac, t->a, t->b, t->res[sat], res); + abort(); + } +} + +int main() +{ + static const Test tests[] = { + { 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 = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) { + for (int j = 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 += $(SRC_PATH)/tests/tcg/sh4 + +test-macl: CFLAGS += -O -g +TESTS += test-macl