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 The Regents of the University of California. 8 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 float128_t f128_sqrt( float128_t a ) 45 { 46 union ui128_f128 uA; 47 uint_fast64_t uiA64, uiA0; 48 bool signA; 49 int_fast32_t expA; 50 struct uint128 sigA, uiZ; 51 struct exp32_sig128 normExpSig; 52 int_fast32_t expZ; 53 uint_fast32_t sig32A, recipSqrt32, sig32Z; 54 struct uint128 rem; 55 uint32_t qs[3]; 56 uint_fast32_t q; 57 uint_fast64_t x64, sig64Z; 58 struct uint128 term, y; 59 uint_fast64_t sigZExtra; 60 struct uint128 sigZ; 61 union ui128_f128 uZ; 62 63 /*------------------------------------------------------------------------ 64 *------------------------------------------------------------------------*/ 65 uA.f = a; 66 uiA64 = uA.ui.v64; 67 uiA0 = uA.ui.v0; 68 signA = signF128UI64( uiA64 ); 69 expA = expF128UI64( uiA64 ); 70 sigA.v64 = fracF128UI64( uiA64 ); 71 sigA.v0 = uiA0; 72 /*------------------------------------------------------------------------ 73 *------------------------------------------------------------------------*/ 74 if ( expA == 0x7FFF ) { 75 if ( sigA.v64 | sigA.v0 ) { 76 uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, 0, 0 ); 77 goto uiZ; 78 } 79 if ( ! signA ) return a; 80 goto invalid; 81 } 82 /*------------------------------------------------------------------------ 83 *------------------------------------------------------------------------*/ 84 if ( signA ) { 85 if ( ! (expA | sigA.v64 | sigA.v0) ) return a; 86 goto invalid; 87 } 88 /*------------------------------------------------------------------------ 89 *------------------------------------------------------------------------*/ 90 if ( ! expA ) { 91 if ( ! (sigA.v64 | sigA.v0) ) return a; 92 normExpSig = softfloat_normSubnormalF128Sig( sigA.v64, sigA.v0 ); 93 expA = normExpSig.exp; 94 sigA = normExpSig.sig; 95 } 96 /*------------------------------------------------------------------------ 97 | (`sig32Z' is guaranteed to be a lower bound on the square root of 98 | `sig32A', which makes `sig32Z' also a lower bound on the square root of 99 | `sigA'.) 100 *------------------------------------------------------------------------*/ 101 expZ = ((expA - 0x3FFF)>>1) + 0x3FFE; 102 expA &= 1; 103 sigA.v64 |= UINT64_C( 0x0001000000000000 ); 104 sig32A = sigA.v64>>17; 105 recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A ); 106 sig32Z = ((uint_fast64_t) sig32A * recipSqrt32)>>32; 107 if ( expA ) { 108 sig32Z >>= 1; 109 rem = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 12 ); 110 } else { 111 rem = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 13 ); 112 } 113 qs[2] = sig32Z; 114 rem.v64 -= (uint_fast64_t) sig32Z * sig32Z; 115 /*------------------------------------------------------------------------ 116 *------------------------------------------------------------------------*/ 117 q = ((uint_fast64_t) (uint32_t) (rem.v64>>2) * recipSqrt32)>>32; 118 qs[1] = q; 119 x64 = (uint_fast64_t) sig32Z<<32; 120 sig64Z = x64 + ((uint_fast64_t) q<<3); 121 x64 += sig64Z; 122 rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 ); 123 term = softfloat_mul64ByShifted32To128( x64, q ); 124 rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 ); 125 /*------------------------------------------------------------------------ 126 *------------------------------------------------------------------------*/ 127 q = ((uint_fast64_t) (uint32_t) (rem.v64>>2) * recipSqrt32)>>32; 128 y = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 ); 129 sig64Z <<= 1; 130 /*------------------------------------------------------------------------ 131 | (Repeating this loop is a rare occurrence.) 132 *------------------------------------------------------------------------*/ 133 for (;;) { 134 term = softfloat_shortShiftLeft128( 0, sig64Z, 32 ); 135 term = softfloat_add128( term.v64, term.v0, 0, (uint_fast64_t) q<<6 ); 136 term = softfloat_mul128By32( term.v64, term.v0, q ); 137 rem = softfloat_sub128( y.v64, y.v0, term.v64, term.v0 ); 138 if ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) ) break; 139 --q; 140 } 141 qs[0] = q; 142 /*------------------------------------------------------------------------ 143 *------------------------------------------------------------------------*/ 144 q = (((uint_fast64_t) (uint32_t) (rem.v64>>2) * recipSqrt32)>>32) + 2; 145 sigZExtra = (uint64_t) ((uint_fast64_t) q<<59); 146 term = softfloat_shortShiftLeft128( 0, qs[1], 53 ); 147 sigZ = 148 softfloat_add128( 149 (uint_fast64_t) qs[2]<<18, ((uint_fast64_t) qs[0]<<24) + (q>>5), 150 term.v64, term.v0 151 ); 152 /*------------------------------------------------------------------------ 153 *------------------------------------------------------------------------*/ 154 if ( (q & 0xF) <= 2 ) { 155 q &= ~3; 156 sigZExtra = (uint64_t) ((uint_fast64_t) q<<59); 157 y = softfloat_shortShiftLeft128( sigZ.v64, sigZ.v0, 6 ); 158 y.v0 |= sigZExtra>>58; 159 term = softfloat_sub128( y.v64, y.v0, 0, q ); 160 y = softfloat_mul64ByShifted32To128( term.v0, q ); 161 term = softfloat_mul64ByShifted32To128( term.v64, q ); 162 term = softfloat_add128( term.v64, term.v0, 0, y.v64 ); 163 rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 20 ); 164 term = softfloat_sub128( term.v64, term.v0, rem.v64, rem.v0 ); 165 /*-------------------------------------------------------------------- 166 | The concatenation of `term' and `y.v0' is now the negative remainder 167 | (3 words altogether). 168 *--------------------------------------------------------------------*/ 169 if ( term.v64 & UINT64_C( 0x8000000000000000 ) ) { 170 sigZExtra |= 1; 171 } else { 172 if ( term.v64 | term.v0 | y.v0 ) { 173 if ( sigZExtra ) { 174 --sigZExtra; 175 } else { 176 sigZ = softfloat_sub128( sigZ.v64, sigZ.v0, 0, 1 ); 177 sigZExtra = ~0; 178 } 179 } 180 } 181 } 182 return softfloat_roundPackToF128( 0, expZ, sigZ.v64, sigZ.v0, sigZExtra ); 183 /*------------------------------------------------------------------------ 184 *------------------------------------------------------------------------*/ 185 invalid: 186 softfloat_raiseFlags( softfloat_flag_invalid ); 187 uiZ.v64 = defaultNaNF128UI64; 188 uiZ.v0 = defaultNaNF128UI0; 189 uiZ: 190 uZ.ui = uiZ; 191 return uZ.f; 192 193 } 194 195