cavatools: initialize repository
[cavatools.git] / softfloat / source / extF80M_sqrt.c
1
2 /*============================================================================
3
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3e, by John R. Hauser.
6
7 Copyright 2011, 2012, 2013, 2014, 2015, 2017 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 #ifdef SOFTFLOAT_FAST_INT64
45
46 void extF80M_sqrt( const extFloat80_t *aPtr, extFloat80_t *zPtr )
47 {
48
49 *zPtr = extF80_sqrt( *aPtr );
50
51 }
52
53 #else
54
55 void extF80M_sqrt( const extFloat80_t *aPtr, extFloat80_t *zPtr )
56 {
57 const struct extFloat80M *aSPtr;
58 struct extFloat80M *zSPtr;
59 uint_fast16_t uiA64, signUI64;
60 int32_t expA;
61 uint64_t rem64;
62 int32_t expZ;
63 uint32_t rem96[3], sig32A, recipSqrt32, sig32Z, q;
64 uint64_t sig64Z, x64;
65 uint32_t rem32, term[4], rem[4], extSigZ[3];
66
67 /*------------------------------------------------------------------------
68 *------------------------------------------------------------------------*/
69 aSPtr = (const struct extFloat80M *) aPtr;
70 zSPtr = (struct extFloat80M *) zPtr;
71 /*------------------------------------------------------------------------
72 *------------------------------------------------------------------------*/
73 uiA64 = aSPtr->signExp;
74 signUI64 = uiA64 & packToExtF80UI64( 1, 0 );
75 expA = expExtF80UI64( uiA64 );
76 rem64 = aSPtr->signif;
77 /*------------------------------------------------------------------------
78 *------------------------------------------------------------------------*/
79 if ( expA == 0x7FFF ) {
80 if ( rem64 & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {
81 softfloat_propagateNaNExtF80M( aSPtr, 0, zSPtr );
82 return;
83 }
84 if ( signUI64 ) goto invalid;
85 rem64 = UINT64_C( 0x8000000000000000 );
86 goto copyA;
87 }
88 /*------------------------------------------------------------------------
89 *------------------------------------------------------------------------*/
90 if ( ! expA ) expA = 1;
91 if ( ! (rem64 & UINT64_C( 0x8000000000000000 )) ) {
92 if ( ! rem64 ) {
93 uiA64 = signUI64;
94 goto copyA;
95 }
96 expA += softfloat_normExtF80SigM( &rem64 );
97 }
98 if ( signUI64 ) goto invalid;
99 /*------------------------------------------------------------------------
100 *------------------------------------------------------------------------*/
101 expZ = ((expA - 0x3FFF)>>1) + 0x3FFF;
102 expA &= 1;
103 softfloat_shortShiftLeft64To96M( rem64, 30 - expA, rem96 );
104 sig32A = rem64>>32;
105 recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A );
106 sig32Z = ((uint64_t) sig32A * recipSqrt32)>>32;
107 if ( expA ) sig32Z >>= 1;
108 rem64 =
109 ((uint64_t) rem96[indexWord( 3, 2 )]<<32 | rem96[indexWord( 3, 1 )])
110 - (uint64_t) sig32Z * sig32Z;
111 rem96[indexWord( 3, 2 )] = rem64>>32;
112 rem96[indexWord( 3, 1 )] = rem64;
113 /*------------------------------------------------------------------------
114 *------------------------------------------------------------------------*/
115 q = ((uint32_t) (rem64>>2) * (uint64_t) recipSqrt32)>>32;
116 sig64Z = ((uint64_t) sig32Z<<32) + ((uint64_t) q<<3);
117 term[indexWord( 3, 2 )] = 0;
118 /*------------------------------------------------------------------------
119 | (Repeating this loop is a rare occurrence.)
120 *------------------------------------------------------------------------*/
121 for (;;) {
122 x64 = ((uint64_t) sig32Z<<32) + sig64Z;
123 term[indexWord( 3, 1 )] = x64>>32;
124 term[indexWord( 3, 0 )] = x64;
125 softfloat_remStep96MBy32(
126 rem96, 29, term, q, &rem[indexMultiwordHi( 4, 3 )] );
127 rem32 = rem[indexWord( 4, 3 )];
128 if ( ! (rem32 & 0x80000000) ) break;
129 --q;
130 sig64Z -= 1<<3;
131 }
132 rem64 = (uint64_t) rem32<<32 | rem[indexWord( 4, 2 )];
133 /*------------------------------------------------------------------------
134 *------------------------------------------------------------------------*/
135 q = (((uint32_t) (rem64>>2) * (uint64_t) recipSqrt32)>>32) + 2;
136 if ( rem64>>34 ) q += recipSqrt32;
137 x64 = (uint64_t) q<<7;
138 extSigZ[indexWord( 3, 0 )] = x64;
139 x64 = (sig64Z<<1) + (x64>>32);
140 extSigZ[indexWord( 3, 2 )] = x64>>32;
141 extSigZ[indexWord( 3, 1 )] = x64;
142 /*------------------------------------------------------------------------
143 *------------------------------------------------------------------------*/
144 if ( (q & 0xFFFFFF) <= 2 ) {
145 q &= ~(uint32_t) 0xFFFF;
146 extSigZ[indexWordLo( 3 )] = q<<7;
147 x64 = sig64Z + (q>>27);
148 term[indexWord( 4, 3 )] = 0;
149 term[indexWord( 4, 2 )] = x64>>32;
150 term[indexWord( 4, 1 )] = x64;
151 term[indexWord( 4, 0 )] = q<<5;
152 rem[indexWord( 4, 0 )] = 0;
153 softfloat_remStep128MBy32( rem, 28, term, q, rem );
154 q = rem[indexWordHi( 4 )];
155 if ( q & 0x80000000 ) {
156 softfloat_sub1X96M( extSigZ );
157 } else {
158 if ( q || rem[indexWord( 4, 1 )] || rem[indexWord( 4, 2 )] ) {
159 extSigZ[indexWordLo( 3 )] |= 1;
160 }
161 }
162 }
163 softfloat_roundPackMToExtF80M(
164 0, expZ, extSigZ, extF80_roundingPrecision, zSPtr );
165 return;
166 /*------------------------------------------------------------------------
167 *------------------------------------------------------------------------*/
168 invalid:
169 softfloat_invalidExtF80M( zSPtr );
170 return;
171 /*------------------------------------------------------------------------
172 *------------------------------------------------------------------------*/
173 copyA:
174 zSPtr->signExp = uiA64;
175 zSPtr->signif = rem64;
176
177 }
178
179 #endif
180