From patchwork Thu Jul 29 11:14:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 488667 Delivered-To: patch@linaro.org Received: by 2002:a05:6638:1185:0:0:0:0 with SMTP id f5csp2066641jas; Thu, 29 Jul 2021 04:21:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzP0QIghUJ6FSqmPQiMFv61pRTIj5kS7COsZdXej8lPc340aMib1vpHjBNZcuEt5dyjNpJP X-Received: by 2002:a25:db0c:: with SMTP id g12mr5624146ybf.6.1627557662479; Thu, 29 Jul 2021 04:21:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627557662; cv=none; d=google.com; s=arc-20160816; b=02Q0wvGgusrCKURjNtH+Lj2J0z1rvJW4KxphEjOPYXeCsBbbYHK0d/hG+bUYLj3r6B +Uj3L+m/GYIhdyx61gp9n2jKe8TYCPdFZ3bNSFHAqTECnpW7yEv9N6CfzN1eiOpNAUON C0AMbWDKNW26ralIx78pdqnem9Xs4Fncf/eyTE1OMOdRAxamXci9CJJ4bzdq0A6V/Wtg Eql6dACKJyWxvaUdMQsPuLT49cPe54eDzEiXg2d7dlGC/i94vKWyht24xInMMQrf9Sbf zBi0B73FPzc0ydYIXEqPTtxksZn7IIEiipOqTKJb2w5vkdC+uprcM33R6YPUm9kctpVK FhVA== 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:to:from :dkim-signature; bh=SngGo1vVY/dtcXAMZ+FWnV5QnI/yJF6vZ4XQ33WiT+w=; b=RDsd9ZoGvGacFAs0wbE/tgJc+v76V/3BoRfOONIWazutdA5v/Qp2eyVtU8U5or61Wx fce6tRZCtMXgogo9a9ksOGfc2EZesKt6QSWPTz9QM9TBhAiji7EIMpZqNci0EicJS/lT tBgMoFvNVMfHkrO3XlbL9S+eok0PAGbZ3PzEWsd+6weUDU77+zP5ifomQVyJHrFCgO21 2pN05x7nFh3Kthqm5stTjDyQaLqjGWJIEda8tlSditgyvqZYndzCQDS5daTSD6ikv6Nq rCOEU8XEfiqdDi2idfHhXYGqqrsUu/HZmw6Kn16eQBfSPDg9hvDY30izjDR4EIMhTABZ 2qrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="BU/lZOxZ"; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id j1si2604258ybp.150.2021.07.29.04.21.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 29 Jul 2021 04:21:02 -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; dkim=pass header.i=@linaro.org header.s=google header.b="BU/lZOxZ"; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:60204 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m9461-0001Dh-SO for patch@linaro.org; Thu, 29 Jul 2021 07:21:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39754) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m940e-0000FY-TI for qemu-devel@nongnu.org; Thu, 29 Jul 2021 07:15:29 -0400 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]:41634) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m940W-0000ze-U9 for qemu-devel@nongnu.org; Thu, 29 Jul 2021 07:15:28 -0400 Received: by mail-wr1-x42f.google.com with SMTP id b7so6454411wri.8 for ; Thu, 29 Jul 2021 04:15:20 -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=SngGo1vVY/dtcXAMZ+FWnV5QnI/yJF6vZ4XQ33WiT+w=; b=BU/lZOxZuQ/YyalcmqPglVnC9D2ypjEIpi2DwBz16NjDeHsYMUSODtB9HJPxQ1u/91 VDrSsEBBBv/BbhiRO0i8+CHB9wV8PDGre4St7kn8+8kvEFKYIbqB8H3Vj9VNOi8z9cDM 4jERJ1CuAcvYN1ntH+3jcBGC+M2zcDJOe7QPhjbosu9lvjngnb7/2BLK2nJpkBZB5TsX sgOLNYJHS4Mwdfb2rUkChbRgZ+Dc9hUj3qfUDZr5oRdRHhHES2PcaY/raLYMNJMJL+Ua Q/VJoKW6++wf3g1ykqgG7aSu8aTXjSEO7tDoDEr2kx9ddLRVbwpgL0JwTZSzkiC5RWNW rJTw== 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=SngGo1vVY/dtcXAMZ+FWnV5QnI/yJF6vZ4XQ33WiT+w=; b=DZOm3ag+y8nPoumv3ALiTSqxJlQhieDTwTeJW3SQRYnaXq5JpEhHXnSWO5u5aC9lgV NmIMcPF8GPvyhIYo+1UjSP8m4vT+BJKBi0Qp7aDpIQnPCn529XwPCRh945XHB11OZ9MU rGuq9vPOg8daLgpMo0WXPallor9yvhHMxatKKgRF8L82o9d5RDzGEcmMrZ6Gs5yTceiy 0CKAh1ch6KaA8Z8+smTljWXfTXPMI/ou4+Xl0CFjRV7aJ7Ak6HBvexIm70gFL7RwOvsy 2kJEzcUnSMncrLg8tN+MarHUwnovAm6H7qMLI5q4CsOlGTAI/ahTOx0WH7JypRbCOLhn 94/Q== X-Gm-Message-State: AOAM5332akkmHkGUBTm7+zvZ2WsDK0cwOtYhmfdVItjGLzKLq24Qp4u1 R6oFVfXVHud6ydNaNs52Mb3gNg== X-Received: by 2002:adf:a409:: with SMTP id d9mr4284457wra.237.1627557319627; Thu, 29 Jul 2021 04:15:19 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id j140sm3037829wmj.37.2021.07.29.04.15.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 04:15:19 -0700 (PDT) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH for-6.2 06/53] target/arm: Fix 48-bit saturating shifts Date: Thu, 29 Jul 2021 12:14:25 +0100 Message-Id: <20210729111512.16541-7-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210729111512.16541-1-peter.maydell@linaro.org> References: <20210729111512.16541-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42f; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x42f.google.com X-Spam_score_int: -1 X-Spam_score: -0.2 X-Spam_bar: / X-Spam_report: (-0.2 / 5.0 requ) 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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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 == 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(-) -- 2.20.1 diff --git a/target/arm/mve_helper.c b/target/arm/mve_helper.c index 847ef5156ad..5730b48f35e 100644 --- a/target/arm/mve_helper.c +++ b/target/arm/mve_helper.c @@ -1576,9 +1576,8 @@ static inline int64_t do_sqrshl48_d(int64_t src, int64_t shift, } return src >> -shift; } else if (shift < 48) { - int64_t val = src << shift; - int64_t extval = sextract64(val, 0, 48); - if (!sat || val == extval) { + int64_t extval = sextract64(src << shift, 0, 48); + if (!sat || src == (extval >> shift)) { return extval; } } else if (!sat || src == 0) { @@ -1586,7 +1585,7 @@ static inline int64_t do_sqrshl48_d(int64_t src, int64_t shift, } *sat = 1; - return (1ULL << 47) - (src >= 0); + return src >= 0 ? MAKE_64BIT_MASK(0, 47) : MAKE_64BIT_MASK(47, 17); } /* Operate on 64-bit values, but saturate at 48 bits */ @@ -1609,9 +1608,8 @@ static inline uint64_t do_uqrshl48_d(uint64_t src, int64_t shift, return extval; } } else if (shift < 48) { - uint64_t val = src << shift; - uint64_t extval = extract64(val, 0, 48); - if (!sat || val == extval) { + uint64_t extval = extract64(src << shift, 0, 48); + if (!sat || src == (extval >> shift)) { return extval; } } else if (!sat || src == 0) {