From patchwork Fri Sep 6 05:16:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 825899 Delivered-To: patch@linaro.org Received: by 2002:adf:a345:0:b0:367:895a:4699 with SMTP id d5csp637878wrb; Thu, 5 Sep 2024 22:20:01 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVpyvs8t9BJx0ME2+MiCSwRr9gtH0KGFQ1APgjqLqMTMcR2yD8QKnWGAhidbYCWCXWtI5YJyw==@linaro.org X-Google-Smtp-Source: AGHT+IFEC81ZiyM7AFxwySJmj6rz6dDppaEUy6sGCWTKCFkMTDrXEo3R9Fjq4h7yT7sFWtL2XD1C X-Received: by 2002:a05:690c:3585:b0:664:8646:4d02 with SMTP id 00721157ae682-6db44d9fc35mr20071907b3.12.1725600001259; Thu, 05 Sep 2024 22:20:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1725600001; cv=none; d=google.com; s=arc-20240605; b=fbvgHMIR1ZRfYToo2oMIcTiIAuDpOnGpdGRLTFKX/D24obGxHkUgIz6keKHPU/WyVR ZrYJb1AyRgqhamGony7nhjQ+ns5qz40XA9G4zKsR+8N/3AXvTzHGR8EbPlMElaKoqfRO nCH/6UFVPd/+Q1WczXjz5CMx/DlvcNFF1urtBv73uxlQL/x1tCu1dvhxBmMz3LCpXVhh 7T2MKG0kBXXTjyb19D1p1+cYLfcfP3Wf7dliWPVVzaGXZLylKFIjC4QV8rtmdmHKNkLs j+5OsxI4V+wXbu9Kr1Es8ftuAHrdlA1Gt1tcd4MEvvFe7LqWVTy+Z4HNix2yQzThlghw GsLg== 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=f3JwBPK+TLlGpWXnv/Pwp9XES+mq3I741tvjHEE1yfw=; fh=xJ1URYKcMN3TM0/XAv5v+aCN+5tIbzAdcfBx5UNgoLw=; b=je8Pe8+fKMg1evSFapPfuZwZdlUyZQAkaG4lxJWRq6ChhuUCSKdqxRJ53EFFEyU7Km dgImHGd4WGaScHQgesNbEn9X6PnxdbgI65Xr0u1rpUEM28eIaoDwvm+a3n/Yt8EseZJn OJcZG7dnlegXZeDtUbOETLMdFGgTb7iP6npnSry9Dm+tK3GgLcUzRZ+J4WOZpou/UUNp 4R9ZZ3Kp5WW8A8dJYylUC+ANyKWpJ+tU0YRnMhzWb3NeTp4/kPX3G4HHHqk/zYAAUu0F Z2B94nFI1P6//yOttTWkPRYVKw6hHS3B2T7GW1Wd8Zrc0RPD6alEH511EH5A+u/SB/gx +fZw==; 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-7a98ef1e714si360502385a.68.2024.09.05.22.20.01 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 05 Sep 2024 22:20:01 -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 1smRLz-0006Nr-GV; Fri, 06 Sep 2024 01:17:51 -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 1smRLs-0005jE-QO; Fri, 06 Sep 2024 01:17:44 -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 1smRLq-000829-JJ; Fri, 06 Sep 2024 01:17:44 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id 3AFBB8C124; Fri, 6 Sep 2024 08:15:17 +0300 (MSK) Received: from tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with SMTP id DF64313336A; Fri, 6 Sep 2024 08:16:34 +0300 (MSK) Received: (nullmailer pid 10433 invoked by uid 1000); Fri, 06 Sep 2024 05:16:33 -0000 From: Michael Tokarev To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Peter Maydell , Richard Henderson , Michael Tokarev Subject: [Stable-7.2.14 16/40] target/arm: Fix UMOPA/UMOPS of 16-bit values Date: Fri, 6 Sep 2024 08:16:04 +0300 Message-Id: <20240906051633.10288-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: -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/sme_helper.c b/target/arm/sme_helper.c index e55bc51d69..f12f3288fd 100644 --- a/target/arm/sme_helper.c +++ b/target/arm/sme_helper.c @@ -1167,10 +1167,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; \ }