From nobody Fri May 17 20:14:42 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=buhman.org ARC-Seal: i=1; a=rsa-sha256; t=1712248059; cv=none; d=zohomail.com; s=zohoarc; b=J+yCFBQ02hYRL4t/i/X6XxVbZQeZjXkTUyLqHZTv0SdPxMlJuzPn4slSp1FUCNTe3sapQaVh5FVJH45DyEOymiXmUSqp+SizPMzFRC1cyWvTK2F1RvcZ3uveEhC/aia4XXR4y5feYJ17xYKm7FkOug4guV+ehEcIQ6OBBNHIZLQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1712248059; 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=CrS2tH4Wo60nrFr/QboqFPCXl98MV0fk8Z6FADCpxLE=; b=PayKZtKoJIJh1J63NAg2z/IOq3AsODZISSo6HPsenVwd27Wt4+yeCx8zZfsd0Mk+DnfeTcVwXSQint4wHDyBm2AgArH80Em5gqJV5Xj7lJmgeQiFQYQ91yd7KL+OR0K1RYdmYB8uZ3/CQsTdtnccO2B5lW+QtYW+0XKk2q54LBY= 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 1712248058656148.16216775539897; Thu, 4 Apr 2024 09:27:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rsPvQ-0003GL-O7; Thu, 04 Apr 2024 12:26:52 -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 1rsPvO-0003FC-Sy for qemu-devel@nongnu.org; Thu, 04 Apr 2024 12:26:50 -0400 Received: from wfhigh2-smtp.messagingengine.com ([64.147.123.153]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rsPvL-0006VE-8T for qemu-devel@nongnu.org; Thu, 04 Apr 2024 12:26:49 -0400 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.west.internal (Postfix) with ESMTP id 8E1CF18001FE; Thu, 4 Apr 2024 12:26:44 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 04 Apr 2024 12:26:44 -0400 Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 4 Apr 2024 12:26:41 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=buhman.org; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1712248004; x= 1712334404; bh=CrS2tH4Wo60nrFr/QboqFPCXl98MV0fk8Z6FADCpxLE=; b=H Zun8PeornXEbquLwHxEF/91+PkYO+X4cGLrofZp3pYy7GiKDNlUnVBd2cD0WIrGC hmSP18/N3ufdZVZ9+1tMd3hzVP2H3I0IKVodjAvaE1EfD2YF5y8ps79n9VD4NMIk OqQTFGxkJ6V5KVwY8Bp+P6PcVvY2wXOMj5iM43E/uwAxTu4g9RTkL1q/unmmxh4o 3uiAn4FPjx2aSlMQeJpf0gcXCQBtMT9wTD078pHq+4pCT7rszcV6l6qnfy+o16h8 B5n887gEwqwwY6+Dkir3Lz9FDVREAlPox2WNN7yPuRKZ6x2cratapryL3+4X0iVx g/bbZ0S7Uc5MuC1zy5NIg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1712248004; x= 1712334404; bh=CrS2tH4Wo60nrFr/QboqFPCXl98MV0fk8Z6FADCpxLE=; b=C mZgXB4dP98zqgsL4X4EO3N2CovRky/ZC1MP5sg01viiP598aX3os6Ws5yr9b/5Tj NE9VPfDkRjmON1ZC7d7nJ4omAUuHtNiGi0+Uk1iAldyg0ysXTg1f1w4YpzrYRfzL kpBPz3SIVOqWbs9pZsmEDuIihb6QKUK5yF6tXpxrZqWz3P0rnR0pP9Lpj9/zca2i rMlfDTZ2S23yv3gq76uGx49ZPM4AtiY8lHGxRcfxzoD0SteNaixdZSQ2vIkZxPgf dOgTIa2aukrlFOt1Cbzl19LUZWhl7jDyfPzLzMCe8FXojGUtSq7G6ZWMpfP2X9LG Ow9Q7lXfstbDzXMYmSVHw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudefkedguddtvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvvefufffkofgjfhgggfestd ekredtredttdenucfhrhhomhepkggrtghkuceuuhhhmhgrnhcuoeiirggtkhessghuhhhm rghnrdhorhhgqeenucggtffrrghtthgvrhhnpeevhfehtdevfeevgfeiheeltdekgedvfe dvfedtffetheefjefhjeetiedtgfdtjeenucevlhhushhtvghrufhiiigvpedtnecurfgr rhgrmhepmhgrihhlfhhrohhmpeiirggtkhessghuhhhmrghnrdhorhhg X-ME-Proxy: Feedback-ID: i1541475f:Fastmail From: Zack Buhman To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Zack Buhman Subject: [PATCH v2] sh4: mac.l: implement saturation arithmetic logic Date: Fri, 5 Apr 2024 00:26:39 +0800 Message-ID: <20240404162641.27528-2-zack@buhman.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: 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=64.147.123.153; envelope-from=zack@buhman.org; helo=wfhigh2-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, 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 @buhman.org) X-ZM-MESSAGEID: 1712248059918100001 Content-Type: text/plain; charset="utf-8" The saturation arithmetic logic in helper_macl is not correct. I tested and verified this behavior on a SH7091, the general pattern is a code sequence such as: sets mov.l _mach,r2 lds r2,mach mov.l _macl,r2 lds r2,macl mova _n,r0 mov r0,r1 mova _m,r0 mac.l @r0+,@r1+ _mach: .long 0x00007fff _macl: .long 0x12345678 _m: .long 0x7fffffff _n: .long 0x7fffffff Test case 0: (no int64_t overflow) given; prior to saturation mac.l: mach =3D 0x00007fff macl =3D 0x12345678 @r0 =3D 0x7fffffff @r1 =3D 0x7fffffff expected saturation mac.l result: mach =3D 0x00007fff macl =3D 0xffffffff qemu saturation mac.l result (prior to this commit): mach =3D 0x00007ffe macl =3D 0x12345678 Test case 1: (no int64_t overflow) given; prior to saturation mac.l: mach =3D 0xffff8000 macl =3D 0x00000000 @r0 =3D 0xffffffff @r1 =3D 0x00000001 expected saturation mac.l result: mach =3D 0xffff8000 macl =3D 0x00000000 qemu saturation mac.l result (prior to this commit): mach =3D 0xffff7fff macl =3D 0xffffffff Test case 2: (int64_t addition overflow) given; prior to saturation mac.l: mach =3D 0x80000000 macl =3D 0x00000000 @r0 =3D 0xffffffff @r1 =3D 0x00000001 expected saturation mac.l result: mach =3D 0xffff8000 macl =3D 0x00000000 qemu saturation mac.l result (prior to this commit): mach =3D 0xffff7fff macl =3D 0xffffffff Test case 3: (int64_t addition overflow) given; prior to saturation mac.l: mach =3D 0x7fffffff macl =3D 0x00000000 @r0 =3D 0x7fffffff @r1 =3D 0x7fffffff expected saturation mac.l result: mach =3D 0x00007fff macl =3D 0xffffffff qemu saturation mac.l result (prior to this commit): mach =3D 0xfffffffe macl =3D 0x00000001 All of the above also matches the description of MAC.L as documented in cd00147165-sh-4-32-bit-cpu-core-architecture-stmicroelectronics.pdf Reviewed-by: Peter Maydell --- target/sh4/op_helper.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/target/sh4/op_helper.c b/target/sh4/op_helper.c index 4559d0d376..ee16524083 100644 --- a/target/sh4/op_helper.c +++ b/target/sh4/op_helper.c @@ -160,18 +160,29 @@ void helper_ocbi(CPUSH4State *env, uint32_t address) =20 void helper_macl(CPUSH4State *env, uint32_t arg0, uint32_t arg1) { - int64_t res; - - 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; + int32_t value0 =3D (int32_t)arg0; + int32_t value1 =3D (int32_t)arg1; + int64_t mul =3D ((int64_t)value0) * ((int64_t)value1); + int64_t mac =3D (((uint64_t)env->mach) << 32) | env->macl; + int64_t result; + bool overflow =3D sadd64_overflow(mac, mul, &result); + /* Perform 48-bit saturation arithmetic if the S flag is set */ if (env->sr & (1u << SR_S)) { - if (res < 0) - env->mach |=3D 0xffff0000; - else - env->mach &=3D 0x00007fff; + /* + * The sign bit of `mac + mul` may overflow. The MAC unit on + * real SH-4 hardware has equivalent carry/saturation logic: + */ + const int64_t upper_bound =3D ((1ull << 47) - 1); + const int64_t lower_bound =3D -((1ull << 47) - 0); + + if (overflow) { + result =3D (mac < 0) ? lower_bound : upper_bound; + } else { + result =3D MIN(MAX(result, lower_bound), upper_bound); + } } + env->macl =3D result; + env->mach =3D result >> 32; } =20 void helper_macw(CPUSH4State *env, uint32_t arg0, uint32_t arg1) --=20 2.41.0