From nobody Tue Feb 10 14:33:31 2026 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=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1626184181; cv=none; d=zohomail.com; s=zohoarc; b=fFO67I+LWzDB6aMuCM5lFEv2AFyJrOkH3A0tKDOBkRV4J7MFXjk75g7KOmAGvzLg4t5D9nxmieTgmXXTwy9poDOmH0Rr50K6Qj9jPr4H9MY48zjU7mZ6bVZZNfpdqtOEPk9YzEwoCgAjR84rMKKHOHRf25JKpEGC+9f+/8/ik7Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1626184181; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=hzoBamT9L0is33IwFMKLrmnDteA/k33y/OWd2P4v5ng=; b=KMGk9xQNxCpl+K/zNQBy58z/zlB1pKQ9pHZmzScE2/yNXBOoEHOkNX4Lpfug1AJfZ3k9LzTQrFw9f+VnQM+BCzA0FROHjIHyR7izoXwHXltv9/eK0nGcUVdaYAcQAwH1kbAXa7A7hmVD8sVu77a2v2tIquvlDGwzn6WlZUzlLX8= 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 1626184181797478.24095621664037; Tue, 13 Jul 2021 06:49:41 -0700 (PDT) Received: from localhost ([::1]:56284 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m3In6-0004yc-NO for importer@patchew.org; Tue, 13 Jul 2021 09:49:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54082) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m3IbW-0008SZ-1a for qemu-devel@nongnu.org; Tue, 13 Jul 2021 09:37:42 -0400 Received: from mail-wr1-x42a.google.com ([2a00:1450:4864:20::42a]:40490) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m3IbQ-0003dI-DR for qemu-devel@nongnu.org; Tue, 13 Jul 2021 09:37:41 -0400 Received: by mail-wr1-x42a.google.com with SMTP id l7so29611864wrv.7 for ; Tue, 13 Jul 2021 06:37:34 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id j6sm9827443wrm.97.2021.07.13.06.37.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jul 2021 06:37:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=hzoBamT9L0is33IwFMKLrmnDteA/k33y/OWd2P4v5ng=; b=rR55kHlQIl1wQrwUm7Xj4eNLUchExUoJuFxbGK2hPb/tq6itxRee7QxMeQXHH18+m5 dEuOfmI2C12ZOblK0FJgPYD37OBJ3lUMDj0A7dVJ5JcXxfSue26SQhPVQWKHMYmQU4Db irwul/UrxmiUeUo3SF0XtgJ1lHY3tCBTcjemaaydQ71Jtq6fNowUWZ35chPKdteooQ9n 8cGS9lX575VTDi1wnwhl872p3bNlF7JIJhbGYS4HpG1dBHpnIwu1YYbvcdYkYMliBZS5 40teNAimv/aDyVTaXQfw4E6Dv5CxhfBkUtwJ/bnPy+BPPPYexpZhmaX5PPckBBrGKIvx mSuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hzoBamT9L0is33IwFMKLrmnDteA/k33y/OWd2P4v5ng=; b=ceNHWq3wRJCyQV1XCASwavulXdKbZ92PAAu/ryT6agzgnuksj+VFqFBP1XudZSarX0 AxeH8Tg1uNv+W6MI8vRI7WKGTMFuQbjItKOQokyzNQnOsKJRIxFl/F31OkxYDdCr8llI O6t5+atgpLaFabmYrFOBxP9PuVxCdi3AzFVTIe57PjDsZcgZL83dZaFXzOSlreFmUaGR czg24MCpKTRyQltQYPdQe/Q7QsPH8yReriym0N8kvWKfj6f/AuiAo2TxZZuSbT3LSvZv Bjpe+ZBuoQrwYwr4tW2JDthARhZgSyNQBY3T0LPNku6QONnaK9gpPJKdgtoBO4ovzT/+ f38w== X-Gm-Message-State: AOAM532bsXdOcWnqpKM1NAjr3V0EuSna04+md5arzANuSx0tKmUKBId5 3rY0Yfhg1Lm9GwWGtKOKfN85vGU4jRfUGuBI X-Google-Smtp-Source: ABdhPJw2rxp2JLV20GQ2+1j7o80ISN4qJMoLbml6hXzdka1OlyyhsR19sZlkwh8aFnav9D+2tOPmxQ== X-Received: by 2002:adf:f9cb:: with SMTP id w11mr5862352wrr.57.1626183453988; Tue, 13 Jul 2021 06:37:33 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH for-6.2 06/34] target/arm: Fix 48-bit saturating shifts Date: Tue, 13 Jul 2021 14:36:58 +0100 Message-Id: <20210713133726.26842-7-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210713133726.26842-1-peter.maydell@linaro.org> References: <20210713133726.26842-1-peter.maydell@linaro.org> 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=2a00:1450:4864:20::42a; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x42a.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=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 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" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1626184183301100001 Content-Type: text/plain; charset="utf-8" In do_sqrshl48_d() and do_uqrshl48_d() we got some of the edge cases wrong and failed to saturate correctly: (1) In do_sqrshl48_d() we used the same code that do_shrshl_bhs() does to obtain the saturated most-negative and most-positive 48-bit signed values for the large-shift-left case. This gives (1 << 47) for saturate-to-most-negative, but we weren't sign-extending this value to the 64-bit output as the pseudocode requires. (2) For left shifts by less than 48, we copied the "8/16 bit" code from do_sqrshl_bhs() and do_uqrshl_bhs(). This doesn't do the right thing because it assumes the C type we're working with is at least twice the number of bits we're saturating to (so that a shift left by bits-1 can't shift anything off the top of the value). This isn't true for bits =3D=3D 48, so we would incorrectly return 0 rather than the most-positive value for situations like "shift (1 << 44) right by 20". Instead check for saturation by doing the shift and signextend and then testing whether shifting back left again gives the original value. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson --- target/arm/mve_helper.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/target/arm/mve_helper.c b/target/arm/mve_helper.c index 8cbfd3a8c53..f17e5a413fd 100644 --- a/target/arm/mve_helper.c +++ b/target/arm/mve_helper.c @@ -1579,9 +1579,8 @@ static inline int64_t do_sqrshl48_d(int64_t src, int6= 4_t shift, } return src >> -shift; } else if (shift < 48) { - int64_t val =3D src << shift; - int64_t extval =3D sextract64(val, 0, 48); - if (!sat || val =3D=3D extval) { + int64_t extval =3D sextract64(src << shift, 0, 48); + if (!sat || src =3D=3D (extval >> shift)) { return extval; } } else if (!sat || src =3D=3D 0) { @@ -1589,7 +1588,7 @@ static inline int64_t do_sqrshl48_d(int64_t src, int6= 4_t shift, } =20 *sat =3D 1; - return (1ULL << 47) - (src >=3D 0); + return sextract64((1ULL << 47) - (src >=3D 0), 0, 48); } =20 /* Operate on 64-bit values, but saturate at 48 bits */ @@ -1612,9 +1611,8 @@ static inline uint64_t do_uqrshl48_d(uint64_t src, in= t64_t shift, return extval; } } else if (shift < 48) { - uint64_t val =3D src << shift; - uint64_t extval =3D extract64(val, 0, 48); - if (!sat || val =3D=3D extval) { + uint64_t extval =3D extract64(src << shift, 0, 48); + if (!sat || src =3D=3D (extval >> shift)) { return extval; } } else if (!sat || src =3D=3D 0) { --=20 2.20.1