From nobody Fri Nov 7 13:02:05 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538763368250815.6769658513894; Fri, 5 Oct 2018 11:16:08 -0700 (PDT) Received: from localhost ([::1]:36469 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g8Udz-00065d-2m for importer@patchew.org; Fri, 05 Oct 2018 14:16:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38628) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g8UQg-0000Wv-K9 for qemu-devel@nongnu.org; Fri, 05 Oct 2018 14:02:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g8UQd-0004vB-Km for qemu-devel@nongnu.org; Fri, 05 Oct 2018 14:02:22 -0400 Received: from mail-oi1-x244.google.com ([2607:f8b0:4864:20::244]:43202) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g8UQc-0004KX-KN for qemu-devel@nongnu.org; Fri, 05 Oct 2018 14:02:19 -0400 Received: by mail-oi1-x244.google.com with SMTP id s69-v6so11088230oie.10 for ; Fri, 05 Oct 2018 11:02:16 -0700 (PDT) Received: from cloudburst.twiddle.net ([187.217.230.84]) by smtp.gmail.com with ESMTPSA id d37-v6sm2601302otb.0.2018.10.05.11.02.13 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 05 Oct 2018 11:02:14 -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=Y4urqlPQlb0iVk4XHPAq48tfS6akXHDuAR9jHvemwZbleKD07q3fLctEHQjkV1u5MB EnuN22uKCoAhZdkA6+QEHuOMIrM7+rU+9ryQ6AN8omaZq3HALCciT1QeApwZEyM497bn ThP+srKswDydp+zFuWcTHcmSlcGtesOSNfJQk= 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=mq9fZwPUBaeeWZ9kshn6TA1H5AcyZYNdZt9biWS4YMDcZy8OxRdOqafbKkGPYTImYC BoV/k5BxCN1yshF5PmDVw/I7c2FVRL5VgXWJw5Hz0GC8Tc/Kdf8bHm/G9DlhoOh+08tG YeZ90WzZ7jiFXHk/OXobpkoATLiX/St3rGUXxCblDowh1CxxgWd+b2jQ/8Ew79lbVEHF xP7kaobQ5+zr10jxlATIpX69gfPQSZmC3Wr6tKplPJVunmqY6MSERWi5EjeJp1+DoL1j cwSzdZcFTBK7A9lTLlN8kglfOcMfHT2KN645b55f4+W6nCwfHdFe7J9OnoUroZOB7/UV I+EQ== X-Gm-Message-State: ABuFfoibAKYyNqMuV0n1ZUSWcr2fSFZYHowy+P1DG9QXr14OS4ceGj+6 5SephnJtT7V2zYUabd/MEPFH0nQfpTF2B7wOkEA= X-Google-Smtp-Source: ACcGV60vnwCSldEwEJ11MRtlt71Zt6fVPplhiz+Z8moNCV1aW+Hs63OeVZy6d0otNFCsShLDJsosyw== X-Received: by 2002:aca:c402:: with SMTP id u2-v6mr23988oif.141.1538762535269; Fri, 05 Oct 2018 11:02:15 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 5 Oct 2018 13:01:58 -0500 Message-Id: <20181005180201.11387-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181005180201.11387-1-richard.henderson@linaro.org> References: <20181005180201.11387-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::244 Subject: [Qemu-devel] [PULL 5/8] 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: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_0 Z_629925259 SPT_0 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=C3=A9e Reviewed-by: Alex Benn=C3=A9e Signed-off-by: Richard Henderson --- include/fpu/softfloat-macros.h | 34 ++++++++++++++++++++++++--------- fpu/softfloat.c | 35 ++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 17 deletions(-) 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 `z1Pt= r'. *-------------------------------------------------------------------------= ---*/ =20 -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 =3D a1 << count; + *z0Ptr =3D count =3D=3D 0 ? a0 : (a0 << count) | (a1 >> (-count & 63)); +} =20 - *z1Ptr =3D a1<>( ( - count ) & 63= ) ); +/*------------------------------------------------------------------------= ---- +| Shifts the 128-bit value formed by concatenating `a0' and `a1' left by t= he +| number of bits given in `count'. Any bits shifted off are lost. The va= lue +| 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 `z1Pt= r'. +*-------------------------------------------------------------------------= ---*/ =20 +static inline void shift128Left(uint64_t a0, uint64_t a1, int count, + uint64_t *z0Ptr, uint64_t *z1Ptr) +{ + if (count < 64) { + *z1Ptr =3D a1 << count; + *z0Ptr =3D count =3D=3D 0 ? a0 : (a0 << count) | (a1 >> (-count & = 63)); + } else { + *z1Ptr =3D 0; + *z0Ptr =3D a1 << (count - 64); + } } =20 /*------------------------------------------------------------------------= ---- @@ -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; =20 @@ -658,8 +674,8 @@ static inline uint64_t div128To64(uint64_t n0, uint64_t= n1, uint64_t d) } r0 -=3D m; =20 - /* Return remainder in LSB */ - return (q1 << 32) | q0 | (r0 !=3D 0); + *r =3D r0; + return (q1 << 32) | q0; } =20 /*------------------------------------------------------------------------= ---- 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, FloatPar= ts b, float_status *s) bool sign =3D a.sign ^ b.sign; =20 if (a.cls =3D=3D float_class_normal && b.cls =3D=3D float_class_normal= ) { - uint64_t temp_lo, temp_hi; + uint64_t n0, n1, q, r; int exp =3D 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 normalizatio= n, + * 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 le= ft + * by one (more), and the remainder must be shifted right by one. + */ if (a.frac < b.frac) { exp -=3D 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 =3D div128To64(temp_lo, temp_hi, b.frac); + q =3D 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 =3D q | (r !=3D 0); a.sign =3D sign; a.exp =3D exp; return a; --=20 2.17.1