2 /*============================================================================
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3d, by John R. Hauser.
7 Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the
8 University of California. All rights reserved.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
13 1. Redistributions of source code must retain the above copyright notice,
14 this list of conditions, and the following disclaimer.
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.
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.
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.
35 =============================================================================*/
40 #include "internals.h"
41 #include "specialize.h"
42 #include "softfloat.h"
44 float128_t
f128_sqrt( float128_t a
)
47 uint_fast64_t uiA64
, uiA0
;
50 struct uint128 sigA
, uiZ
;
51 struct exp32_sig128 normExpSig
;
53 uint_fast32_t sig32A
, recipSqrt32
, sig32Z
;
57 uint_fast64_t x64
, sig64Z
;
58 struct uint128 y
, term
;
59 uint_fast64_t sigZExtra
;
63 /*------------------------------------------------------------------------
64 *------------------------------------------------------------------------*/
68 signA
= signF128UI64( uiA64
);
69 expA
= expF128UI64( uiA64
);
70 sigA
.v64
= fracF128UI64( uiA64
);
72 /*------------------------------------------------------------------------
73 *------------------------------------------------------------------------*/
74 if ( expA
== 0x7FFF ) {
75 if ( sigA
.v64
| sigA
.v0
) {
76 uiZ
= softfloat_propagateNaNF128UI( uiA64
, uiA0
, 0, 0 );
79 if ( ! signA
) return a
;
82 /*------------------------------------------------------------------------
83 *------------------------------------------------------------------------*/
85 if ( ! (expA
| sigA
.v64
| sigA
.v0
) ) return a
;
88 /*------------------------------------------------------------------------
89 *------------------------------------------------------------------------*/
91 if ( ! (sigA
.v64
| sigA
.v0
) ) return a
;
92 normExpSig
= softfloat_normSubnormalF128Sig( sigA
.v64
, sigA
.v0
);
93 expA
= normExpSig
.exp
;
94 sigA
= normExpSig
.sig
;
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
100 *------------------------------------------------------------------------*/
101 expZ
= ((expA
- 0x3FFF)>>1) + 0x3FFE;
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;
109 rem
= softfloat_shortShiftLeft128( sigA
.v64
, sigA
.v0
, 12 );
111 rem
= softfloat_shortShiftLeft128( sigA
.v64
, sigA
.v0
, 13 );
114 rem
.v64
-= (uint_fast64_t) sig32Z
* sig32Z
;
115 /*------------------------------------------------------------------------
116 *------------------------------------------------------------------------*/
117 q
= ((uint32_t) (rem
.v64
>>2) * (uint_fast64_t) recipSqrt32
)>>32;
118 x64
= (uint_fast64_t) sig32Z
<<32;
119 sig64Z
= x64
+ ((uint_fast64_t) q
<<3);
120 y
= softfloat_shortShiftLeft128( rem
.v64
, rem
.v0
, 29 );
121 /*------------------------------------------------------------------------
122 | (Repeating this loop is a rare occurrence.)
123 *------------------------------------------------------------------------*/
125 term
= softfloat_mul64ByShifted32To128( x64
+ sig64Z
, q
);
126 rem
= softfloat_sub128( y
.v64
, y
.v0
, term
.v64
, term
.v0
);
127 if ( ! (rem
.v64
& UINT64_C( 0x8000000000000000 )) ) break;
132 /*------------------------------------------------------------------------
133 *------------------------------------------------------------------------*/
134 q
= ((rem
.v64
>>2) * recipSqrt32
)>>32;
135 y
= softfloat_shortShiftLeft128( rem
.v64
, rem
.v0
, 29 );
137 /*------------------------------------------------------------------------
138 | (Repeating this loop is a rare occurrence.)
139 *------------------------------------------------------------------------*/
141 term
= softfloat_shortShiftLeft128( 0, sig64Z
, 32 );
142 term
= softfloat_add128( term
.v64
, term
.v0
, 0, (uint_fast64_t) q
<<6 );
143 term
= softfloat_mul128By32( term
.v64
, term
.v0
, q
);
144 rem
= softfloat_sub128( y
.v64
, y
.v0
, term
.v64
, term
.v0
);
145 if ( ! (rem
.v64
& UINT64_C( 0x8000000000000000 )) ) break;
149 /*------------------------------------------------------------------------
150 *------------------------------------------------------------------------*/
151 q
= (((rem
.v64
>>2) * recipSqrt32
)>>32) + 2;
152 sigZExtra
= (uint64_t) ((uint_fast64_t) q
<<59);
153 term
= softfloat_shortShiftLeft128( 0, qs
[1], 53 );
156 (uint_fast64_t) qs
[2]<<18, ((uint_fast64_t) qs
[0]<<24) + (q
>>5),
159 /*------------------------------------------------------------------------
160 *------------------------------------------------------------------------*/
161 if ( (q
& 0xF) <= 2 ) {
163 sigZExtra
= (uint64_t) ((uint_fast64_t) q
<<59);
164 y
= softfloat_shortShiftLeft128( sigZ
.v64
, sigZ
.v0
, 6 );
165 y
.v0
|= sigZExtra
>>58;
166 term
= softfloat_sub128( y
.v64
, y
.v0
, 0, q
);
167 y
= softfloat_mul64ByShifted32To128( term
.v0
, q
);
168 term
= softfloat_mul64ByShifted32To128( term
.v64
, q
);
169 term
= softfloat_add128( term
.v64
, term
.v0
, 0, y
.v64
);
170 rem
= softfloat_shortShiftLeft128( rem
.v64
, rem
.v0
, 20 );
171 term
= softfloat_sub128( term
.v64
, term
.v0
, rem
.v64
, rem
.v0
);
172 /*--------------------------------------------------------------------
173 | The concatenation of `term' and `y.v0' is now the negative remainder
174 | (3 words altogether).
175 *--------------------------------------------------------------------*/
176 if ( term
.v64
& UINT64_C( 0x8000000000000000 ) ) {
179 if ( term
.v64
| term
.v0
| y
.v0
) {
183 sigZ
= softfloat_sub128( sigZ
.v64
, sigZ
.v0
, 0, 1 );
189 return softfloat_roundPackToF128( 0, expZ
, sigZ
.v64
, sigZ
.v0
, sigZExtra
);
190 /*------------------------------------------------------------------------
191 *------------------------------------------------------------------------*/
193 softfloat_raiseFlags( softfloat_flag_invalid
);
194 uiZ
.v64
= defaultNaNF128UI64
;
195 uiZ
.v0
= defaultNaNF128UI0
;