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