From patchwork Mon May 27 08:21:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 799219 Delivered-To: patch@linaro.org Received: by 2002:a5d:6a47:0:b0:354:fb4b:99cd with SMTP id t7csp1760710wrw; Mon, 27 May 2024 01:24:11 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVPhCz7HTR0UOXOS8Z6ReesT47yswaggbQVz/BMfrblSYDzZcgHyBOWn/v49TIARn4CjK4giCESkmt9Prjs/yA2 X-Google-Smtp-Source: AGHT+IFZXYRe7ri07iHN81TcU7MXA/RIAxp46Ej8tT6hCUU8B7yhNlRYpF3QEbXC9OruWv3NK8J/ X-Received: by 2002:a05:620a:45ab:b0:792:ba14:28cc with SMTP id af79cd13be357-794aa7c4f5cmr1823883785a.1.1716798251354; Mon, 27 May 2024 01:24:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1716798251; cv=none; d=google.com; s=arc-20160816; b=s+0Kot0+ksEWeK/TzPxJf5IJO0+0mBqV9padtBaQ3zGO2X7a/6iNjfOXSk0ponPnv3 csQb2uBfDlwA3ziyF385gU/Ba5S0NcmkrojskQARdbUY6mr5yqJa145X9MNtbBBbPmIe WZIYEkZdfCoqwWzhCFPVmOW2hg736PIcvPcBWnJGVTobs7UnqpZGiYUsFpFJ6cSeuVlk CE6LRhUs8MOuB3uAj3Rt6zbx4P2A4mDYDUQX10SLkC210X5Tc7cJ2UR+/ym2vUoQzAD9 vgy3QCeAaH77vr6Jdnm+lneSbRqZ39FYCC+7WskA0C3FpSed9zu6LVPFP0Dm5Lu3RaJH urwA== 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; bh=h8jg3kgl62JrS08S6xwUepRYtIPermDG+Hf+hLBu3qM=; fh=q09TJcPvxotL9gCkIe59Ge7cfqcjMrdjel2txRsy8jw=; b=YDYdVeY1ZHtIZd824WhOKbnEZPPcS08+4TeKO61NKDx32GFhENZVcFSderUjycVDs+ AWOMrVh2AJcIEaFmMLc74dsl8lfAjGykXS0jW/9puTujKmMt29AMw4F33nAOL2GEeXt/ jLIy+CVnXI8Uij1uqiqTjr0ENwbPGvvmrl52DoTAhO6Va3KH2ASXivgvNPKpZcdazkFu Xyqfy3RORAmBxTJbqmmYZkwuKirhKg7zGDbU7Hx0qx4i4EFBCZSVfN+T9iRolqvKhpaN uYoRR2064CbokFnRKUKOQEyPysuRtnwPb4xdOCY70M/oF1khqoG4Qib/h8wPB0faaI7e 6ghA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; 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" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-794abd3c0efsi727036485a.464.2024.05.27.01.24.11 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 27 May 2024 01:24:11 -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; 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" Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBVeJ-0003ks-EO; Mon, 27 May 2024 04:24:07 -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 1sBVdH-0001Le-3o; Mon, 27 May 2024 04:23:03 -0400 Received: from isrv.corpit.ru ([86.62.121.231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBVd6-0000qA-PY; Mon, 27 May 2024 04:23:02 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id A66C96A56D; Mon, 27 May 2024 11:22:13 +0300 (MSK) Received: from tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with SMTP id D0A9ED850A; Mon, 27 May 2024 11:21:39 +0300 (MSK) Received: (nullmailer pid 66399 invoked by uid 1000); Mon, 27 May 2024 08:21:38 -0000 From: Michael Tokarev To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Paul Cercueil , Richard Henderson , Yoshinori Sato , Michael Tokarev Subject: [Stable-9.0.1 16/44] target/sh4: Fix SUBV opcode Date: Mon, 27 May 2024 11:21:07 +0300 Message-Id: <20240527082138.66217-16-mjt@tls.msk.ru> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -68 X-Spam_score: -6.9 X-Spam_bar: ------ X-Spam_report: (-6.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: Philippe Mathieu-Daudé The documentation says: SUBV Rm, Rn Rn - Rm -> Rn, underflow -> T The overflow / underflow can be calculated as: T = ((Rn ^ Rm) & (Result ^ Rn)) >> 31 However we were using the incorrect: T = ((Rn ^ Rm) & (Result ^ Rm)) >> 31 Fix by using the Rn register instead of Rm. Add tests provided by Paul Cercueil. Cc: qemu-stable@nongnu.org Fixes: ad8d25a11f ("target-sh4: implement addv and subv using TCG") Reported-by: Paul Cercueil Suggested-by: Paul Cercueil Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2318 Reviewed-by: Richard Henderson Signed-off-by: Philippe Mathieu-Daudé Reviewed-by: Yoshinori Sato Message-Id: <20240430163125.77430-3-philmd@linaro.org> (cherry picked from commit e88a856efd1d3c3ffa8e53da4831eff8da290808) Signed-off-by: Michael Tokarev diff --git a/target/sh4/translate.c b/target/sh4/translate.c index 4a1dd0d1f4..3e013b7c7c 100644 --- a/target/sh4/translate.c +++ b/target/sh4/translate.c @@ -933,7 +933,7 @@ static void _decode_opc(DisasContext * ctx) t0 = tcg_temp_new(); tcg_gen_sub_i32(t0, REG(B11_8), REG(B7_4)); t1 = tcg_temp_new(); - tcg_gen_xor_i32(t1, t0, REG(B7_4)); + tcg_gen_xor_i32(t1, t0, REG(B11_8)); t2 = tcg_temp_new(); tcg_gen_xor_i32(t2, REG(B11_8), REG(B7_4)); tcg_gen_and_i32(t1, t1, t2); diff --git a/tests/tcg/sh4/Makefile.target b/tests/tcg/sh4/Makefile.target index 521b8b0a76..7852fa62d8 100644 --- a/tests/tcg/sh4/Makefile.target +++ b/tests/tcg/sh4/Makefile.target @@ -20,3 +20,6 @@ TESTS += test-macw test-addv: CFLAGS += -O -g TESTS += test-addv + +test-subv: CFLAGS += -O -g +TESTS += test-subv diff --git a/tests/tcg/sh4/test-subv.c b/tests/tcg/sh4/test-subv.c new file mode 100644 index 0000000000..a3c2db96e4 --- /dev/null +++ b/tests/tcg/sh4/test-subv.c @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include +#include +#include + +static void subv(const int a, const int b, const int res, const int carry) +{ + int o = a, c; + + asm volatile("subv %2,%0\n" + "movt %1\n" + : "+r"(o), "=r"(c) : "r"(b) : ); + + if (c != carry || o != res) { + printf("SUBV %d, %d = %d/%d [T = %d/%d]\n", a, b, o, res, c, carry); + abort(); + } +} + +int main(void) +{ + subv(INT_MIN, 1, INT_MAX, 1); + subv(INT_MAX, -1, INT_MIN, 1); + subv(INT_MAX, 1, INT_MAX - 1, 0); + subv(0, 1, -1, 0); + subv(-1, -1, 0, 0); + + return 0; +}