From patchwork Wed Jan 24 13:13:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 125659 Delivered-To: patch@linaro.org Received: by 10.46.66.141 with SMTP id h13csp419218ljf; Wed, 24 Jan 2018 05:34:42 -0800 (PST) X-Google-Smtp-Source: AH8x227huHwfH6N4u3A1Eh23Bm8UG1MOkezU2CmtvdvEwQVI0T72+PwU+gW26kYvz849Y6mZNzeB X-Received: by 10.129.72.209 with SMTP id v200mr5419652ywa.119.1516800882604; Wed, 24 Jan 2018 05:34:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516800882; cv=none; d=google.com; s=arc-20160816; b=nCO7fM4la1xnjW9SfFE6E8X8Sf0PN/v2uzwwAZ8JYYqDfx87CMn2FzCvdbpFF3kLci tUF3u8yoVwmZ2CzEFbomsVW3zbL4Ce2OFr61I+68PEbcWKhO1z3dXPvUaCI3lx6H7rct G8QYT7BBWNyBhrwnRTnE0XsI+SPq7i1yt4voCzhIXviwAnID2ZJ3XfpcLfphF9wMT6nS ku9DwGjh1mFqWelHmzKfbtYJOJUF5L6T5J2Be3NlzZcUPgd/ZLrthHfJHO8D+IiiRs1J dm4QDlNESV5J5SaLiqC0TwG/cvNqsiHEhKNwDjJAuvYP//mSrY8ZzlIyyEV4yJYLHV2t nyHw== 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:arc-authentication-results; bh=rbojFxZhoZdMo0X7aAP704RnOyYmJf1o91fZZfAIXvg=; b=K5AfT4Uoi1Ha7pmavaRvUFeAWRz+Z7nna84hCdyBEwzS6r3IKBLeoDI5hVaMEVWQ8t THVFQ2tQ88rj4kc1Fd4n8gIU5EfNVEZfMJtMoUqmDiMK/iY8MgEsbTXVs3jE4yZrVdCs XfDOsUlmYa0o4eSLBiRVEjV2tyas7ujquzvcGPLMe+qWtN1HnId10MhSnylWUIlpH+WH EDuiN6+Pe/TUci+OcL234syZ8XLQYzoRnfB72PMvBr6FQ9/NathrWt0fRrYyUViTNUpd rRWsSm4axBaSA378MW9j4lebHIinUAVcx2s6AyCCeuPqvfohmyRqVlOT/asEKt8Z74YV hwcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=jMqisZGr; 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 n8si50177ywm.6.2018.01.24.05.34.42 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 24 Jan 2018 05:34:42 -0800 (PST) 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=jMqisZGr; 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]:48857 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eeLCL-0002Ey-UV for patch@linaro.org; Wed, 24 Jan 2018 08:34:42 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57531) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eeKwz-0007sL-G4 for qemu-devel@nongnu.org; Wed, 24 Jan 2018 08:18:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eeKwx-00018G-V0 for qemu-devel@nongnu.org; Wed, 24 Jan 2018 08:18:49 -0500 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:42626) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eeKwx-00017S-LY for qemu-devel@nongnu.org; Wed, 24 Jan 2018 08:18:47 -0500 Received: by mail-wm0-x243.google.com with SMTP id r71so8503435wmd.1 for ; Wed, 24 Jan 2018 05:18:47 -0800 (PST) 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=rbojFxZhoZdMo0X7aAP704RnOyYmJf1o91fZZfAIXvg=; b=jMqisZGrtndfAxkPq2Dz7Rld3d149DVMCMR4SPOuYg7r4BK9PxiINiajaRjx/FmlpH AH8JeN6A1nMmU10NxPo+87fs5+lC4+H0kwSL3usKuI99HGboEpMpggRe+PjYb5dRxzPo nCjbsIl+lc3Y+3lrSlJUh/AUvtCJ9rDievl7I= 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=rbojFxZhoZdMo0X7aAP704RnOyYmJf1o91fZZfAIXvg=; b=CyAzBuVqdsjuF71yyYETd7xLGEg/wsj8jORoXrB3JYbMi8vpQQYFceEREhe55fCFRQ nIDb0KYS+YA8TLIVTINLQor7w8ahnS2JmkQ+MhTAbv6BN6elDnJufpfX6jYJnFjLbFpd T9jUOg1OsU2Ap4b1LVtvrVwEB4rhFlcrt4xoQ2AZEKrnqXMhFNAO150Umu78sEWfcdSP 6dUMmdF2/DvsSxfXsJW71uGSaplgkvm8lfnbIrh0cb6sT65/oILwqFQsPAbCK+ahnv0d u33eKY9aCYTIXAMOkail9ZPPW3rRuOHiPkTfh33V583ykV/YglySyEoqaF4Y/5mqt2fX +nnQ== X-Gm-Message-State: AKwxytfcg5rU8Eta5Cf3doJlb1WYA/z5VOzqAjYWwh2/7JTo8vWTlUz0 whKU/6TNv+EyWO/Fwv9QhdAHIA== X-Received: by 10.28.54.221 with SMTP id y90mr4532303wmh.156.1516799926487; Wed, 24 Jan 2018 05:18:46 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id w14sm2670389wrc.63.2018.01.24.05.18.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jan 2018 05:18:41 -0800 (PST) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 570833E0D1C; Wed, 24 Jan 2018 13:13:17 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: richard.henderson@linaro.org, peter.maydell@linaro.org, laurent@vivier.eu, bharata@linux.vnet.ibm.com, andrew@andrewdutcher.com Date: Wed, 24 Jan 2018 13:13:15 +0000 Message-Id: <20180124131315.30567-23-alex.bennee@linaro.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180124131315.30567-1-alex.bennee@linaro.org> References: <20180124131315.30567-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [PATCH v3 22/22] fpu/softfloat: re-factor sqrt 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: =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-devel@nongnu.org, Aurelien Jarno Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This is a little bit of a departure from softfloat's original approach as we skip the estimate step in favour of a straight iteration. Suggested-by: Richard Henderson Signed-off-by: Alex Bennée --- v3 - added to series - fixed renames of structs --- fpu/softfloat.c | 205 ++++++++++++++++++++++-------------------------- include/fpu/softfloat.h | 1 + 2 files changed, 95 insertions(+), 111 deletions(-) -- 2.15.1 diff --git a/fpu/softfloat.c b/fpu/softfloat.c index 8ee110abba..d0c91d5a9a 100644 --- a/fpu/softfloat.c +++ b/fpu/softfloat.c @@ -1886,6 +1886,100 @@ float64 float64_scalbn(float64 a, int n, float_status *status) return float64_round_pack_canonical(pr, status); } +/* sqrt */ + +/* + * Returns an approximation to the square root of the fraction given + * by `a'. Considered as an integer, `a' must be at least 2^31. If bit + * 0 of `aExp' (the least significant bit) is 1, the integer returned + * approximates 2^31*sqrt(`a'/2^31), where `a' is considered an + * integer. If bit 0 of `aExp' is 0, the integer returned approximates + * 2^31*sqrt(`a'/2^30). In either case, the approximation returned + * lies strictly within +/-2 of the exact value. + */ + +static FloatParts sqrt_float(FloatParts a, float_status *s, + const FloatFmt *p) +{ + uint64_t a_frac, r_frac, s_frac; + int bit, last_bit; + + if (a.cls >= float_class_qnan) { + return pick_nan_parts(a, a, s); + } + if (a.cls == float_class_zero) { + return a; /* sqrt(+-0) = +-0 */ + } + if (a.sign) { + s->float_exception_flags |= float_flag_invalid; + a.cls = float_class_dnan; + return a; + } + if (a.cls == float_class_inf) { + return a; /* sqrt(+inf) = +inf */ + } + + assert(a.cls == float_class_normal); + + /* We need two overflow bits at the top. Adding room for that is + a right shift. If the exponent is odd, we can discard the low + bit by multiplying the fraction by 2; that's a left shift. + Combine those and we shift right if the exponent is even. */ + a_frac = a.frac; + if (!(a.exp & 1)) { + a_frac >>= 1; + } + a.exp >>= 1; + + /* Bit-by-bit computation of sqrt. */ + r_frac = 0; + s_frac = 0; + + /* Iterate from implicit bit down to the 3 extra bits to compute a + * properly rounded result. Remember we've inserted one more bit + * at the top, so these positions are one less. */ + bit = DECOMPOSED_BINARY_POINT - 1; + last_bit = MAX(p->frac_shift - 4, 0); + do { + uint64_t q = 1ULL << bit; + uint64_t t_frac = s_frac + q; + if (t_frac <= a_frac) { + s_frac = t_frac + q; + a_frac -= t_frac; + r_frac += q; + } + a_frac <<= 1; + } while (--bit >= last_bit); + + /* Undo the right shift done above. If there is any remaining + fraction, the result is inexact. Set the sticky bit. */ + a.frac = (r_frac << 1) + (a_frac != 0); + + return a; +} + +float16 float16_sqrt(float16 a, float_status *status) +{ + FloatParts pa = float16_unpack_canonical(a, status); + FloatParts pr = sqrt_float(pa, status, &float16_params); + return float16_round_pack_canonical(pr, status); +} + +float32 float32_sqrt(float32 a, float_status *status) +{ + FloatParts pa = float32_unpack_canonical(a, status); + FloatParts pr = sqrt_float(pa, status, &float32_params); + return float32_round_pack_canonical(pr, status); +} + +float64 float64_sqrt(float64 a, float_status *status) +{ + FloatParts pa = float64_unpack_canonical(a, status); + FloatParts pr = sqrt_float(pa, status, &float64_params); + return float64_round_pack_canonical(pr, status); +} + + /*---------------------------------------------------------------------------- | Takes a 64-bit fixed-point value `absZ' with binary point between bits 6 | and 7, and returns the properly rounded 32-bit integer corresponding to the @@ -3293,62 +3387,6 @@ float32 float32_rem(float32 a, float32 b, float_status *status) } -/*---------------------------------------------------------------------------- -| Returns the square root of the single-precision floating-point value `a'. -| The operation is performed according to the IEC/IEEE Standard for Binary -| Floating-Point Arithmetic. -*----------------------------------------------------------------------------*/ - -float32 float32_sqrt(float32 a, float_status *status) -{ - flag aSign; - int aExp, zExp; - uint32_t aSig, zSig; - uint64_t rem, term; - a = float32_squash_input_denormal(a, status); - - aSig = extractFloat32Frac( a ); - aExp = extractFloat32Exp( a ); - aSign = extractFloat32Sign( a ); - if ( aExp == 0xFF ) { - if (aSig) { - return propagateFloat32NaN(a, float32_zero, status); - } - if ( ! aSign ) return a; - float_raise(float_flag_invalid, status); - return float32_default_nan(status); - } - if ( aSign ) { - if ( ( aExp | aSig ) == 0 ) return a; - float_raise(float_flag_invalid, status); - return float32_default_nan(status); - } - if ( aExp == 0 ) { - if ( aSig == 0 ) return float32_zero; - normalizeFloat32Subnormal( aSig, &aExp, &aSig ); - } - zExp = ( ( aExp - 0x7F )>>1 ) + 0x7E; - aSig = ( aSig | 0x00800000 )<<8; - zSig = estimateSqrt32( aExp, aSig ) + 2; - if ( ( zSig & 0x7F ) <= 5 ) { - if ( zSig < 2 ) { - zSig = 0x7FFFFFFF; - goto roundAndPack; - } - aSig >>= aExp & 1; - term = ( (uint64_t) zSig ) * zSig; - rem = ( ( (uint64_t) aSig )<<32 ) - term; - while ( (int64_t) rem < 0 ) { - --zSig; - rem += ( ( (uint64_t) zSig )<<1 ) | 1; - } - zSig |= ( rem != 0 ); - } - shift32RightJamming( zSig, 1, &zSig ); - roundAndPack: - return roundAndPackFloat32(0, zExp, zSig, status); - -} /*---------------------------------------------------------------------------- | Returns the binary exponential of the single-precision floating-point value @@ -4192,61 +4230,6 @@ float64 float64_rem(float64 a, float64 b, float_status *status) } - -/*---------------------------------------------------------------------------- -| Returns the square root of the double-precision floating-point value `a'. -| The operation is performed according to the IEC/IEEE Standard for Binary -| Floating-Point Arithmetic. -*----------------------------------------------------------------------------*/ - -float64 float64_sqrt(float64 a, float_status *status) -{ - flag aSign; - int aExp, zExp; - uint64_t aSig, zSig, doubleZSig; - uint64_t rem0, rem1, term0, term1; - a = float64_squash_input_denormal(a, status); - - aSig = extractFloat64Frac( a ); - aExp = extractFloat64Exp( a ); - aSign = extractFloat64Sign( a ); - if ( aExp == 0x7FF ) { - if (aSig) { - return propagateFloat64NaN(a, a, status); - } - if ( ! aSign ) return a; - float_raise(float_flag_invalid, status); - return float64_default_nan(status); - } - if ( aSign ) { - if ( ( aExp | aSig ) == 0 ) return a; - float_raise(float_flag_invalid, status); - return float64_default_nan(status); - } - if ( aExp == 0 ) { - if ( aSig == 0 ) return float64_zero; - normalizeFloat64Subnormal( aSig, &aExp, &aSig ); - } - zExp = ( ( aExp - 0x3FF )>>1 ) + 0x3FE; - aSig |= LIT64( 0x0010000000000000 ); - zSig = estimateSqrt32( aExp, aSig>>21 ); - aSig <<= 9 - ( aExp & 1 ); - zSig = estimateDiv128To64( aSig, 0, zSig<<32 ) + ( zSig<<30 ); - if ( ( zSig & 0x1FF ) <= 5 ) { - doubleZSig = zSig<<1; - mul64To128( zSig, zSig, &term0, &term1 ); - sub128( aSig, 0, term0, term1, &rem0, &rem1 ); - while ( (int64_t) rem0 < 0 ) { - --zSig; - doubleZSig -= 2; - add128( rem0, rem1, zSig>>63, doubleZSig | 1, &rem0, &rem1 ); - } - zSig |= ( ( rem0 | rem1 ) != 0 ); - } - return roundAndPackFloat64(0, zExp, zSig, status); - -} - /*---------------------------------------------------------------------------- | Returns the binary log of the double-precision floating-point value `a'. | The operation is performed according to the IEC/IEEE Standard for Binary diff --git a/include/fpu/softfloat.h b/include/fpu/softfloat.h index cebe37b716..9b7b5e34e2 100644 --- a/include/fpu/softfloat.h +++ b/include/fpu/softfloat.h @@ -251,6 +251,7 @@ float16 float16_minnum(float16, float16, float_status *status); float16 float16_maxnum(float16, float16, float_status *status); float16 float16_minnummag(float16, float16, float_status *status); float16 float16_maxnummag(float16, float16, float_status *status); +float16 float16_sqrt(float16, float_status *status); int float16_compare(float16, float16, float_status *status); int float16_compare_quiet(float16, float16, float_status *status);