From patchwork Thu Oct 4 17:56:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 148119 Delivered-To: patch@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp1283257lji; Thu, 4 Oct 2018 10:57:23 -0700 (PDT) X-Google-Smtp-Source: ACcGV63eaLbGY9gfhTPB1MEtV2lJBJhA76MhJOz7BhnZkKFfBQbFgJ78gn6J1UhvVZGa8i1wYO2I X-Received: by 2002:a0c:92da:: with SMTP id c26-v6mr6267409qvc.39.1538675843566; Thu, 04 Oct 2018 10:57:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538675843; cv=none; d=google.com; s=arc-20160816; b=V/cEyh1krFT39ngRplmBP/kz3SV6M2UFpNjdHpZIGkFfXnf4E68/Goh5u4vRzy0wTP U8sUdLXmZWYd/LvwQPXE4L0hS7+7YGbKajt8Lvez+BVhftEcU8zPJfAkOyoXmJZ1GnmB gV+j57jnnXKWj3X2qTddSqrhgxsdP8Ux7Y4qxiQTwr9N+heZCu3v80GfP0/QaKSWQzF6 PGZS6/re25Cpf01DWVN9rDIBLShAENG31sRwh71Pxi+g2MsuajsOGYYPhV0aSkTdqWt6 CwIGFS2PkX9OrK3+EF59G+s72kOlRAc4BQqNgZi9j37Yri4IYJi7iz8OS1Q43FaPL6ho Dc8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=eoQfmeIbqQYNxqFOPaK1rddW7v/35ufvgMSTkPh3uTI=; b=Wa+pKYCeDwvBvS4kyDhVA8bxp6pmaPNEL7fLke6pu1fSBiOjbwqTxTRX3nAs6AnfHX FhwUSndumuuZDbgDrG/x/TfsXIy84vaU4wi9KQjrWgWrhTz3DEHvDqbcwSS9bBf8F1PH K0PHG1KZOLaZEOeBirSlhA9VpXLIhukFpiaHfOf6qYH0CQHtMidrwhVqMf2yJhPWVfD/ ytZQl5RNInG9zv3dk3vE9LEqoTGYbS9pyQcFBpM6gRTD7w3pR3so1FwwY6metovNFiLf SBwnkVXcOjLUhUVBDO9fS5nbMAJdh+1rUosNuZzH3miTyxmKFvXiSqIJ51RWo8Jdn1fZ LjaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=DUQrFhX3; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id x93-v6si3654924qtd.54.2018.10.04.10.57.23 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 04 Oct 2018 10:57:23 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=DUQrFhX3; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:58100 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g87sJ-00069b-3s for patch@linaro.org; Thu, 04 Oct 2018 13:57:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60992) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g87s7-00069U-Vs for qemu-devel@nongnu.org; Thu, 04 Oct 2018 13:57:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g87s3-0005hG-JT for qemu-devel@nongnu.org; Thu, 04 Oct 2018 13:57:11 -0400 Received: from mail-ot1-x333.google.com ([2607:f8b0:4864:20::333]:43881) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g87s3-0005g0-CO for qemu-devel@nongnu.org; Thu, 04 Oct 2018 13:57:07 -0400 Received: by mail-ot1-x333.google.com with SMTP id e21-v6so10059508otk.10 for ; Thu, 04 Oct 2018 10:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eoQfmeIbqQYNxqFOPaK1rddW7v/35ufvgMSTkPh3uTI=; b=DUQrFhX3vA8iB5oP8MZ5MWYhqtMWPbny+UoK0JAAxd1qAbrLaZTC4DNxytvxRg8nXn mof7MKArrvm1oUsnp71XP5OIHRWQykDEaxaWph5jjZg6EEjCCsTS45FWNBJI43/UX2Cp p36XlSsJu6fgtUDgdzVioKlyO/i94Qr2iiugI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eoQfmeIbqQYNxqFOPaK1rddW7v/35ufvgMSTkPh3uTI=; b=knlLzn802FA603qIHX8eK6B7aLW5sfGVvalQuoQzQ4P8jF9rGwZYZc5XEy/dFdDJru 0ZHaI7cgu3DnJ7OjtPf/8k8fQZ7U47/bVhGlqFJ0FhT5EHP38hHZEVgo0FEHYjw3AEcP a0eualiZPE8Go/5lPLnJXn08mllYtYLK+kR85nRSiY3pf764qrSImgQkVuaZjmBac73x /ZbBahCE/t0qsxUzdHlnvJg9bGVbVEPRJjz0K4+IL6ZKyK92qcEXb/YCqvNKgl/QNRRi 3zm/vA8Wsr2ITy+APi3i2+xzLqhrHMy/pWm+X6nzcc7uH7BzQfM6ZQdsgUG9D1iCLktu zKaQ== X-Gm-Message-State: ABuFfohw30OwGMZNMNpCsX6GvT8v2HNZDAzv4A9N6O2Xy51HDmrqqBF4 +j8TwmOfuzz01SW9QxXFCkIIDb3asoE3iQ== X-Received: by 2002:a9d:76d:: with SMTP id 100mr4486917ote.153.1538675825753; Thu, 04 Oct 2018 10:57:05 -0700 (PDT) Received: from cloudburst.twiddle.net ([187.217.227.243]) by smtp.gmail.com with ESMTPSA id f84-v6sm1830649oia.44.2018.10.04.10.57.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 04 Oct 2018 10:57:04 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 4 Oct 2018 12:56:57 -0500 Message-Id: <20181004175700.20847-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181004175700.20847-1-richard.henderson@linaro.org> References: <20181004175700.20847-1-richard.henderson@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::333 Subject: [Qemu-devel] [PATCH v3 1/4] softfloat: Fix division X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: cota@braap.org, alex.bennee@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" The __udiv_qrnnd primitive that we nicked from gmp requires its inputs to be normalized. We were not doing that. Because the inputs are nearly normalized already, finishing that is trivial. Replace div128to64 with a "proper" udiv_qrnnd, so that this remains a reusable primitive. Fixes: cf07323d494 Fixes: https://bugs.launchpad.net/qemu/+bug/1793119 Tested-by: Emilio G. Cota Tested-by: Alex Bennée Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- include/fpu/softfloat-macros.h | 34 ++++++++++++++++++++++++--------- fpu/softfloat.c | 35 ++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 17 deletions(-) -- 2.17.1 diff --git a/include/fpu/softfloat-macros.h b/include/fpu/softfloat-macros.h index edc682139e..a1d99c730d 100644 --- a/include/fpu/softfloat-macros.h +++ b/include/fpu/softfloat-macros.h @@ -329,15 +329,30 @@ static inline void | pieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'. *----------------------------------------------------------------------------*/ -static inline void - shortShift128Left( - uint64_t a0, uint64_t a1, int count, uint64_t *z0Ptr, uint64_t *z1Ptr) +static inline void shortShift128Left(uint64_t a0, uint64_t a1, int count, + uint64_t *z0Ptr, uint64_t *z1Ptr) { + *z1Ptr = a1 << count; + *z0Ptr = count == 0 ? a0 : (a0 << count) | (a1 >> (-count & 63)); +} - *z1Ptr = a1<>( ( - count ) & 63 ) ); +/*---------------------------------------------------------------------------- +| Shifts the 128-bit value formed by concatenating `a0' and `a1' left by the +| number of bits given in `count'. Any bits shifted off are lost. The value +| of `count' may be greater than 64. The result is broken into two 64-bit +| pieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'. +*----------------------------------------------------------------------------*/ +static inline void shift128Left(uint64_t a0, uint64_t a1, int count, + uint64_t *z0Ptr, uint64_t *z1Ptr) +{ + if (count < 64) { + *z1Ptr = a1 << count; + *z0Ptr = count == 0 ? a0 : (a0 << count) | (a1 >> (-count & 63)); + } else { + *z1Ptr = 0; + *z0Ptr = a1 << (count - 64); + } } /*---------------------------------------------------------------------------- @@ -619,7 +634,8 @@ static inline uint64_t estimateDiv128To64(uint64_t a0, uint64_t a1, uint64_t b) * * Licensed under the GPLv2/LGPLv3 */ -static inline uint64_t div128To64(uint64_t n0, uint64_t n1, uint64_t d) +static inline uint64_t udiv_qrnnd(uint64_t *r, uint64_t n1, + uint64_t n0, uint64_t d) { uint64_t d0, d1, q0, q1, r1, r0, m; @@ -658,8 +674,8 @@ static inline uint64_t div128To64(uint64_t n0, uint64_t n1, uint64_t d) } r0 -= m; - /* Return remainder in LSB */ - return (q1 << 32) | q0 | (r0 != 0); + *r = r0; + return (q1 << 32) | q0; } /*---------------------------------------------------------------------------- diff --git a/fpu/softfloat.c b/fpu/softfloat.c index 71da0f68bb..46ae206172 100644 --- a/fpu/softfloat.c +++ b/fpu/softfloat.c @@ -1112,19 +1112,38 @@ static FloatParts div_floats(FloatParts a, FloatParts b, float_status *s) bool sign = a.sign ^ b.sign; if (a.cls == float_class_normal && b.cls == float_class_normal) { - uint64_t temp_lo, temp_hi; + uint64_t n0, n1, q, r; int exp = a.exp - b.exp; + + /* + * We want a 2*N / N-bit division to produce exactly an N-bit + * result, so that we do not lose any precision and so that we + * do not have to renormalize afterward. If A.frac < B.frac, + * then division would produce an (N-1)-bit result; shift A left + * by one to produce the an N-bit result, and decrement the + * exponent to match. + * + * The udiv_qrnnd algorithm that we're using requires normalization, + * i.e. the msb of the denominator must be set. Since we know that + * DECOMPOSED_BINARY_POINT is msb-1, the inputs must be shifted left + * by one (more), and the remainder must be shifted right by one. + */ if (a.frac < b.frac) { exp -= 1; - shortShift128Left(0, a.frac, DECOMPOSED_BINARY_POINT + 1, - &temp_hi, &temp_lo); + shift128Left(0, a.frac, DECOMPOSED_BINARY_POINT + 2, &n1, &n0); } else { - shortShift128Left(0, a.frac, DECOMPOSED_BINARY_POINT, - &temp_hi, &temp_lo); + shift128Left(0, a.frac, DECOMPOSED_BINARY_POINT + 1, &n1, &n0); } - /* LSB of quot is set if inexact which roundandpack will use - * to set flags. Yet again we re-use a for the result */ - a.frac = div128To64(temp_lo, temp_hi, b.frac); + q = udiv_qrnnd(&r, n1, n0, b.frac << 1); + + /* + * Set lsb if there is a remainder, to set inexact. + * As mentioned above, to find the actual value of the remainder we + * would need to shift right, but (1) we are only concerned about + * non-zero-ness, and (2) the remainder will always be even because + * both inputs to the division primitive are even. + */ + a.frac = q | (r != 0); a.sign = sign; a.exp = exp; return a;