From patchwork Fri Sep 6 06:53:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 825914 Delivered-To: patch@linaro.org Received: by 2002:adf:a345:0:b0:367:895a:4699 with SMTP id d5csp662290wrb; Thu, 5 Sep 2024 23:57:33 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWGcujkx4HJfld8M/s7xhALZdhN6Q1LxiRpL8XlZTc2dqi4psxaquBM7b192yvf2PvJiY7V5w==@linaro.org X-Google-Smtp-Source: AGHT+IGgRRecffVkorb+b181fEsDc2z1Pej+bKpr9607lZRZ76x+8kfAaBDkOWp8Uuj/c8dVl37C X-Received: by 2002:a05:6214:5544:b0:6c3:62bc:5dd8 with SMTP id 6a1803df08f44-6c528500482mr23740976d6.27.1725605852795; Thu, 05 Sep 2024 23:57:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1725605852; cv=none; d=google.com; s=arc-20240605; b=BBMf9tkO5dwEYDRnMEgaS2Ug5hVJRgoxKVOC87wgTZsG7ZspLDCOa08NRbjCtWAj+j 5DUzY2tID+/NiCahy7FeTTJFq97UQiPRraJQVWdCV34tF9jYZfnDRe2e11pLcUW4kP6t MO18QpnJvoB8SCR33x6ii256TdhaqaLSTHFieZFLDfCK7ZTgLMizJSA9TEzT21d+yNsZ K7kxrTtZJ97py0+qsquhYmXBYUpvIg/yGkeAzl53Mo5LQ/rxkQcRTZ0+wfL7LiycnJ4T qcHRylMvowugFcZQwSKa7JBGvD5G0J9XOkOwPYcE9vZmg3kvviZHalsSYJXPa1R5RHOl DMnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=XOxtLhZ5tCE/URPeqJzT+qG9oFSUPUp/UoLzaMkVVtc=; fh=xJ1URYKcMN3TM0/XAv5v+aCN+5tIbzAdcfBx5UNgoLw=; b=Q1fM9/IMnS2FSslKI+PLklkmU47nO44InRnCkCw+8/g6WMWEE2Nd/nST/EDf1X77DU EVC55NBNwREhR+nyFhluufhekhAQdoUDiLpUI/29GqNQX4PAofAolKzI5Q9Ph78PE+59 vFWwHYYIUNiPJQD9lpbeuwVygUJLrp4BPmyRx2tmUnKMUvVAnG1Gz5uIBRAc82vKaVv0 RGLfHC1cKm9S5YsYrqAWocZD1uGT9KuNKGwm/5PVMhAOMcM3J5S8Zkbk60CZR5EOoDAh wjnLbqKncOc4pPVkbP/qiJCBm+DcmZyw6zVu8dI63ypUOsM8NAuKczmWubLUjwOD4vNp JfzQ==; 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 6a1803df08f44-6c520512783si36726876d6.434.2024.09.05.23.57.32 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 05 Sep 2024 23:57:32 -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 1smStL-0003rV-D0; Fri, 06 Sep 2024 02:56:23 -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 1smStI-0003b7-Ra; Fri, 06 Sep 2024 02:56:20 -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 1smStG-0003Mj-Ve; Fri, 06 Sep 2024 02:56:20 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id 114AB8C24A; Fri, 6 Sep 2024 09:53: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 CECA5133402; Fri, 6 Sep 2024 09:54:30 +0300 (MSK) Received: (nullmailer pid 43470 invoked by uid 1000); Fri, 06 Sep 2024 06:54:29 -0000 From: Michael Tokarev To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Peter Maydell , Richard Henderson , Michael Tokarev Subject: [Stable-8.2.7 21/53] target/arm: Fix UMOPA/UMOPS of 16-bit values Date: Fri, 6 Sep 2024 09:53:51 +0300 Message-Id: <20240906065429.42415-21-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: -57 X-Spam_score: -5.8 X-Spam_bar: ----- X-Spam_report: (-5.8 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, THIS_AD=1.099, 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: Peter Maydell The UMOPA/UMOPS instructions are supposed to multiply unsigned 8 or 16 bit elements and accumulate the products into a 64-bit element. In the Arm ARM pseudocode, this is done with the usual infinite-precision signed arithmetic. However our implementation doesn't quite get it right, because in the DEF_IMOP_64() macro we do: sum += (NTYPE)(n >> 0) * (MTYPE)(m >> 0); where NTYPE and MTYPE are uint16_t or int16_t. In the uint16_t case, the C usual arithmetic conversions mean the values are converted to "int" type and the multiply is done as a 32-bit multiply. This means that if the inputs are, for example, 0xffff and 0xffff then the result is 0xFFFE0001 as an int, which is then promoted to uint64_t for the accumulation into sum; this promotion incorrectly sign extends the multiply. Avoid the incorrect sign extension by casting to int64_t before the multiply, so we do the multiply as 64-bit signed arithmetic, which is a type large enough that the multiply can never overflow into the sign bit. (The equivalent 8-bit operations in DEF_IMOP_32() are fine, because the 8-bit multiplies can never overflow into the sign bit of a 32-bit integer.) Cc: qemu-stable@nongnu.org Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2372 Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Message-id: 20240722172957.1041231-3-peter.maydell@linaro.org (cherry picked from commit ea3f5a90f036734522e9af3bffd77e69e9f47355) Signed-off-by: Michael Tokarev diff --git a/target/arm/tcg/sme_helper.c b/target/arm/tcg/sme_helper.c index 5a6dd76489..f9001f5213 100644 --- a/target/arm/tcg/sme_helper.c +++ b/target/arm/tcg/sme_helper.c @@ -1146,10 +1146,10 @@ static uint64_t NAME(uint64_t n, uint64_t m, uint64_t a, uint8_t p, bool neg) \ uint64_t sum = 0; \ /* Apply P to N as a mask, making the inactive elements 0. */ \ n &= expand_pred_h(p); \ - sum += (NTYPE)(n >> 0) * (MTYPE)(m >> 0); \ - sum += (NTYPE)(n >> 16) * (MTYPE)(m >> 16); \ - sum += (NTYPE)(n >> 32) * (MTYPE)(m >> 32); \ - sum += (NTYPE)(n >> 48) * (MTYPE)(m >> 48); \ + sum += (int64_t)(NTYPE)(n >> 0) * (MTYPE)(m >> 0); \ + sum += (int64_t)(NTYPE)(n >> 16) * (MTYPE)(m >> 16); \ + sum += (int64_t)(NTYPE)(n >> 32) * (MTYPE)(m >> 32); \ + sum += (int64_t)(NTYPE)(n >> 48) * (MTYPE)(m >> 48); \ return neg ? a - sum : a + sum; \ }