1 2 /*============================================================================ 3 4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic 5 Package, Release 3a, by John R. Hauser. 6 7 Copyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of 8 California. All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions are met: 12 13 1. Redistributions of source code must retain the above copyright notice, 14 this list of conditions, and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright notice, 17 this list of conditions, and the following disclaimer in the documentation 18 and/or other materials provided with the distribution. 19 20 3. Neither the name of the University nor the names of its contributors may 21 be used to endorse or promote products derived from this software without 22 specific prior written permission. 23 24 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE 27 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 35 =============================================================================*/ 36 37 #include <stdbool.h> 38 #include <stdint.h> 39 #include "platform.h" 40 #include "internals.h" 41 #include "specialize.h" 42 #include "softfloat.h" 43 44 extFloat80_t extF80_sqrt( extFloat80_t a ) 45 { 46 union { struct extFloat80M s; extFloat80_t f; } uA; 47 uint_fast16_t uiA64; 48 uint_fast64_t uiA0; 49 bool signA; 50 int_fast32_t expA; 51 uint_fast64_t sigA; 52 struct uint128 uiZ; 53 uint_fast16_t uiZ64; 54 uint_fast64_t uiZ0; 55 struct exp32_sig64 normExpSig; 56 int_fast32_t expZ; 57 uint_fast32_t sig32A, recipSqrt32, sig32Z; 58 struct uint128 rem; 59 uint_fast64_t q, sigZ, x64; 60 struct uint128 term; 61 uint_fast64_t sigZExtra; 62 union { struct extFloat80M s; extFloat80_t f; } uZ; 63 64 /*------------------------------------------------------------------------ 65 *------------------------------------------------------------------------*/ 66 uA.f = a; 67 uiA64 = uA.s.signExp; 68 uiA0 = uA.s.signif; 69 signA = signExtF80UI64( uiA64 ); 70 expA = expExtF80UI64( uiA64 ); 71 sigA = uiA0; 72 /*------------------------------------------------------------------------ 73 *------------------------------------------------------------------------*/ 74 if ( expA == 0x7FFF ) { 75 if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) { 76 uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, 0, 0 ); 77 uiZ64 = uiZ.v64; 78 uiZ0 = uiZ.v0; 79 goto uiZ; 80 } 81 if ( ! signA ) return a; 82 goto invalid; 83 } 84 /*------------------------------------------------------------------------ 85 *------------------------------------------------------------------------*/ 86 if ( signA ) { 87 if ( ! sigA ) goto zero; 88 goto invalid; 89 } 90 /*------------------------------------------------------------------------ 91 *------------------------------------------------------------------------*/ 92 if ( ! expA ) expA = 1; 93 if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) { 94 if ( ! sigA ) goto zero; 95 normExpSig = softfloat_normSubnormalExtF80Sig( sigA ); 96 expA += normExpSig.exp; 97 sigA = normExpSig.sig; 98 } 99 /*------------------------------------------------------------------------ 100 | (`sig32Z' is guaranteed to be a lower bound on the square root of 101 | `sig32A', which makes `sig32Z' also a lower bound on the square root of 102 | `sigA'.) 103 *------------------------------------------------------------------------*/ 104 expZ = ((expA - 0x3FFF)>>1) + 0x3FFF; 105 expA &= 1; 106 sig32A = sigA>>32; 107 recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A ); 108 sig32Z = ((uint_fast64_t) sig32A * recipSqrt32)>>32; 109 if ( expA ) { 110 sig32Z >>= 1; 111 rem = softfloat_shortShiftLeft128( 0, sigA, 61 ); 112 } else { 113 rem = softfloat_shortShiftLeft128( 0, sigA, 62 ); 114 } 115 rem.v64 -= (uint_fast64_t) sig32Z * sig32Z; 116 /*------------------------------------------------------------------------ 117 *------------------------------------------------------------------------*/ 118 q = ((uint_fast64_t) (uint32_t) (rem.v64>>2) * recipSqrt32)>>32; 119 sigZ = ((uint_fast64_t) sig32Z<<32) + (q<<3); 120 x64 = ((uint_fast64_t) sig32Z<<32) + sigZ; 121 term = softfloat_mul64ByShifted32To128( x64, q ); 122 rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 ); 123 rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 ); 124 /*------------------------------------------------------------------------ 125 *------------------------------------------------------------------------*/ 126 q = (((uint_fast64_t) (uint32_t) (rem.v64>>2) * recipSqrt32)>>32) + 2; 127 x64 = sigZ; 128 sigZ = (sigZ<<1) + (q>>25); 129 sigZExtra = (uint64_t) (q<<39); 130 /*------------------------------------------------------------------------ 131 *------------------------------------------------------------------------*/ 132 if ( (q & 0xFFFFFF) <= 2 ) { 133 q &= ~(uint_fast64_t) 0xFFFF; 134 sigZExtra = (uint64_t) (q<<39); 135 term = softfloat_mul64ByShifted32To128( x64 + (q>>27), q ); 136 x64 = (uint_fast64_t) (uint32_t) (q<<5) * (uint32_t) q; 137 term = softfloat_add128( term.v64, term.v0, 0, x64 ); 138 rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 28 ); 139 rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 ); 140 if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) { 141 if ( ! sigZExtra ) --sigZ; 142 --sigZExtra; 143 } else { 144 if ( rem.v64 | rem.v0 ) sigZExtra |= 1; 145 } 146 } 147 return 148 softfloat_roundPackToExtF80( 149 0, expZ, sigZ, sigZExtra, extF80_roundingPrecision ); 150 /*------------------------------------------------------------------------ 151 *------------------------------------------------------------------------*/ 152 invalid: 153 softfloat_raiseFlags( softfloat_flag_invalid ); 154 uiZ64 = defaultNaNExtF80UI64; 155 uiZ0 = defaultNaNExtF80UI0; 156 goto uiZ; 157 /*------------------------------------------------------------------------ 158 *------------------------------------------------------------------------*/ 159 zero: 160 uiZ64 = packToExtF80UI64( signA, 0 ); 161 uiZ0 = 0; 162 uiZ: 163 uZ.s.signExp = uiZ64; 164 uZ.s.signif = uiZ0; 165 return uZ.f; 166 167 } 168 169