From patchwork Mon Apr 8 17:49:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 786949 Delivered-To: patch@linaro.org Received: by 2002:adf:fb4d:0:b0:346:15ad:a2a with SMTP id c13csp2116wrs; Mon, 8 Apr 2024 10:56:37 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVELYbJD6hSfkz6gUHdXFEZhUwcp6aHkyXxnGcaFVLtHNAhHGWqT9YoCmOLtuLDgT3pbawXfW2G0mVrpDnAunR2 X-Google-Smtp-Source: AGHT+IFgwJd0Ml+SMtFD7awv/BJceVKYujQCUnUM6S5fLsKNsD0qoDLlVj12nBaZ4gL5cc1PCTWR X-Received: by 2002:a54:4896:0:b0:3c5:f721:128c with SMTP id r22-20020a544896000000b003c5f721128cmr2753566oic.14.1712598996931; Mon, 08 Apr 2024 10:56:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712598996; cv=none; d=google.com; s=arc-20160816; b=IcqMqPxYHe54+FFVELBgh5AwThPkz2wrPSoPdDh7Qljm9KdWs5dk+zeIVFHiXvrbhX 1E5T/78j3hhIXcIKClvfkA9b+hu6Vq9VwI034XtN4+28JCbVRZl8oBStzQrvK24Fuj/E kAE/AZVNcD36c6sUVb82qHMmuJ/bYUQhmb5eg+IcFNb2bDh55g2OAOXvDHpGLXPLKO59 MUg/BtmhRvUEdcqCN7TG6WAD1uN6ZiYtG2fFymls2lbw7vnQkGg2mETPClYSpV4yqVkA TYdvXnXWOnvu6It0xaAXLswX92Rggzl8Th3XxWmfm7wc3lQkNhKgLKl3v/nFYIEXpzmu USQw== 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=/A33LtPdfhd6ZXxcd3sa54LWhQX38sygsNQTelB9lyU=; fh=UxcOinUvtH9RCUG8YjsmzGmJmYoXEUythTaZQ7RVwi0=; b=CHb3/3gRJ9XX0j4oFYCEQsAL9HYhEJwm1D8EW3aJgTiye2V07YcwslROHHYF1DfyXB lR70nyIsIqWqdngIOWFzpLuUN52/in2w7uMIVKot1SBz56/CokDqTc7ISCv5kkmY/Nhb FodvHh5ykWp0vq14S4yOIDqoOf+RVv9e50tQVAJndOifweYBkKrcLGhY9kSRCmIy1iSU k5DnwX0qh0tcTXoylYKVNhQovOJa/n1H/JWjR8lQ+F5Zqcx4V0yTPAcYnoJ5slPJ0G+7 PawHyotEUH71IEGMdfnFdG/AEr3xYn2ywNfblq0RKNfSdxo2jhRxDaA/i1czs1SL9q6p XBBw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Pub0Ka1b; 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 z7-20020a05622a060700b004317c1ea75bsi8791864qta.134.2024.04.08.10.56.36 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 08 Apr 2024 10:56:36 -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=Pub0Ka1b; 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 1rtt84-00022T-4l; Mon, 08 Apr 2024 13:50:00 -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 1rtt7y-000203-5W for qemu-devel@nongnu.org; Mon, 08 Apr 2024 13:49:54 -0400 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rtt7w-0003eD-EW for qemu-devel@nongnu.org; Mon, 08 Apr 2024 13:49:53 -0400 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-5d4d15ec7c5so3810946a12.1 for ; Mon, 08 Apr 2024 10:49:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712598591; x=1713203391; 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=/A33LtPdfhd6ZXxcd3sa54LWhQX38sygsNQTelB9lyU=; b=Pub0Ka1bBmoTMN8gYNWU8cZuP1sW5EytI37CJPrlNg0gj1BrgwWzSc4X8IzgCeLtXo rdymg94ESqe7+pcKOqRF7NOJSg8M4Oogl6FxqKAhFYLQL1iL8t5dOlJ2EV4eE8Mjzk9m cOexCGDpyeYpscqvrF8ThqayPFgORLlFY5eSW3s+cuUwv2z8qPR5NVeBNUBme586zrP8 C2p3UZiktj8cdTsjohI840IT4F8OZzFJ9zwzRiwlxtq0LZiSIMFelIPWaXEauPOJK8Xp hZCIEJNKCBWlQHpU0gK25dN8/3jpnhRHcHRfC5mP/Duu+ysLT2Xn0eimkZuPNxHuDDM6 K9QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712598591; x=1713203391; 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=/A33LtPdfhd6ZXxcd3sa54LWhQX38sygsNQTelB9lyU=; b=cyWaHQEzd0Id8qurdEySv+yVPh9c166a5mgZO9gLKZrh3LfF1UTRjJle7r9pBLHvu9 Or2hPZaWFTPIEtpZySy/DhQRC/tU7sDMEhLWEz5wKRLa77EIwdv7eYA35ue7Gmo3n3vm LURM1Xj1ohGqgiikAMv/Us9dFu0y4e7L8v8rbIc6qlYhEfYZz3cTLVhcBmUo7tPPiaG5 RRJoyCm29EhKnSbllAO4BDy9WDAjN+XvuGSwucax5QhbA1x00VsGadrp6dq/mxFvzwju POLZOeW3ceKD0rgrcdufcA70pMWpRNjQy+/DjRZn0CyJzeF+pPO5M3XJTqpUf9AfmBYN fcFA== X-Gm-Message-State: AOJu0YwEwfWlm0y/f8IK8nBrzi22c0m6tI6XGG8WZf31pnHz6Fycm7x2 Yf6fQZDDvnGXLlt7OeXsjqeNNGycOMciYDghEwN9vqQu8K0szwGTgvGrYHQRCKv2Hth7fjh+mFv / X-Received: by 2002:a17:90a:3904:b0:2a2:bbdb:c4aa with SMTP id y4-20020a17090a390400b002a2bbdbc4aamr8015375pjb.30.1712598590798; Mon, 08 Apr 2024 10:49:50 -0700 (PDT) Received: from stoup.. (098-147-007-212.res.spectrum.com. [98.147.7.212]) by smtp.gmail.com with ESMTPSA id ga15-20020a17090b038f00b0029c3bac0aa8sm8658432pjb.4.2024.04.08.10.49.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 10:49:50 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Zack Buhman , Yoshinori Sato , =?utf-8?q?Philippe_Mathieu-Dau?= =?utf-8?q?d=C3=A9?= Subject: [PULL 13/35] target/sh4: Fix mac.w with saturation enabled Date: Mon, 8 Apr 2024 07:49:07 -1000 Message-Id: <20240408174929.862917-14-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240408174929.862917-1-richard.henderson@linaro.org> References: <20240408174929.862917-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52b; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52b.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_macw is not correct. I tested and verified this behavior on a SH7091. Reviewd-by: Yoshinori Sato Signed-off-by: Zack Buhman Message-Id: <20240405233802.29128-3-zack@buhman.org> [rth: Reformat helper_macw, add a test case.] Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- target/sh4/helper.h | 2 +- target/sh4/op_helper.c | 28 +++++++++------- tests/tcg/sh4/test-macw.c | 61 +++++++++++++++++++++++++++++++++++ tests/tcg/sh4/Makefile.target | 3 ++ 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 tests/tcg/sh4/test-macw.c diff --git a/target/sh4/helper.h b/target/sh4/helper.h index 64056e4a39..29011d3dbb 100644 --- a/target/sh4/helper.h +++ b/target/sh4/helper.h @@ -12,7 +12,7 @@ DEF_HELPER_1(discard_movcal_backup, void, env) DEF_HELPER_2(ocbi, void, env, i32) DEF_HELPER_3(macl, void, env, s32, s32) -DEF_HELPER_3(macw, void, env, i32, i32) +DEF_HELPER_3(macw, void, env, s32, s32) DEF_HELPER_2(ld_fpscr, void, env, i32) diff --git a/target/sh4/op_helper.c b/target/sh4/op_helper.c index d0bae0cc00..99394b714c 100644 --- a/target/sh4/op_helper.c +++ b/target/sh4/op_helper.c @@ -177,22 +177,28 @@ void helper_macl(CPUSH4State *env, int32_t arg0, int32_t arg1) env->mac = res; } -void helper_macw(CPUSH4State *env, uint32_t arg0, uint32_t arg1) +void helper_macw(CPUSH4State *env, int32_t arg0, int32_t arg1) { - int64_t res; + /* Inputs are already sign-extended from 16 bits. */ + int32_t mul = arg0 * arg1; - res = ((uint64_t) env->mach << 32) | env->macl; - res += (int64_t) (int16_t) arg0 *(int64_t) (int16_t) arg1; - env->mach = (res >> 32) & 0xffffffff; - env->macl = res & 0xffffffff; if (env->sr & (1u << SR_S)) { - if (res < -0x80000000) { + /* + * In saturation arithmetic mode, the accumulator is 32-bit + * with carry. MACH is not considered during the addition + * operation nor the 32-bit saturation logic. + */ + int32_t res, macl = env->macl; + + if (sadd32_overflow(macl, mul, &res)) { + res = macl < 0 ? INT32_MIN : INT32_MAX; + /* If overflow occurs, the MACH register is set to 1. */ env->mach = 1; - env->macl = 0x80000000; - } else if (res > 0x000000007fffffff) { - env->mach = 1; - env->macl = 0x7fffffff; } + env->macl = res; + } else { + /* In non-saturation arithmetic mode, the accumulator is 64-bit */ + env->mac += mul; } } diff --git a/tests/tcg/sh4/test-macw.c b/tests/tcg/sh4/test-macw.c new file mode 100644 index 0000000000..4eceec8634 --- /dev/null +++ b/tests/tcg/sh4/test-macw.c @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include +#include +#include + +int64_t mac_w(int64_t mac, const int16_t *a, const int16_t *b) +{ + register uint32_t macl __asm__("macl") = mac; + register uint32_t mach __asm__("mach") = mac >> 32; + + asm volatile("mac.w @%0+,@%1+" + : "+r"(a), "+r"(b), "+x"(macl), "+x"(mach)); + + return ((uint64_t)mach << 32) | macl; +} + +typedef struct { + int64_t mac; + int16_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_w(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[] = { + { 0, 2, 3, { 6, 6 } }, + { 0x123456787ffffffell, 2, -3, + { 0x123456787ffffff8ll, 0x123456787ffffff8ll } }, + { 0xabcdef127ffffffall, 2, 3, + { 0xabcdef1280000000ll, 0x000000017fffffffll } }, + { 0xfffffffffll, INT16_MAX, INT16_MAX, + { 0x103fff0000ll, 0xf3fff0000ll } }, + }; + + 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 9a11c10924..4d09291c0c 100644 --- a/tests/tcg/sh4/Makefile.target +++ b/tests/tcg/sh4/Makefile.target @@ -14,3 +14,6 @@ VPATH += $(SRC_PATH)/tests/tcg/sh4 test-macl: CFLAGS += -O -g TESTS += test-macl + +test-macw: CFLAGS += -O -g +TESTS += test-macw