xref: /optee_os/lib/libutils/isoc/arch/arm/softfloat/source/extF80M_sqrt.c (revision 9403c583381528e7fb391e3769644cc9653cfbb6)
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 #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 rem[4], sig32A, recipSqrt32, sig32Z, q;
64     uint64_t sig64Z, x64;
65     uint32_t term[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(
104         rem64, 30 - expA, &rem[indexMultiwordHi( 4, 3 )] );
105     sig32A = rem64>>32;
106     recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A );
107     sig32Z = ((uint64_t) sig32A * recipSqrt32)>>32;
108     if ( expA ) sig32Z >>= 1;
109     rem64 =
110         ((uint64_t) rem[indexWord( 4, 3 )]<<32 | rem[indexWord( 4, 2 )])
111             - (uint64_t) sig32Z * sig32Z;
112     rem[indexWord( 4, 3 )] = rem64>>32;
113     rem[indexWord( 4, 2 )] = rem64;
114     /*------------------------------------------------------------------------
115     *------------------------------------------------------------------------*/
116     q = ((uint32_t) (rem64>>2) * (uint64_t) recipSqrt32)>>32;
117     sig64Z = ((uint64_t) sig32Z<<32) + ((uint64_t) q<<3);
118     x64 = ((uint64_t) sig32Z<<32) + sig64Z;
119     term[indexWord( 3, 2 )] = 0;
120     term[indexWord( 3, 1 )] = x64>>32;
121     term[indexWord( 3, 0 )] = x64;
122     softfloat_remStep96MBy32(
123         &rem[indexMultiwordHi( 4, 3 )],
124         29,
125         term,
126         q,
127         &rem[indexMultiwordHi( 4, 3 )]
128     );
129     rem64 = (uint64_t) rem[indexWord( 4, 3 )]<<32 | rem[indexWord( 4, 2 )];
130     /*------------------------------------------------------------------------
131     *------------------------------------------------------------------------*/
132     q = (((uint32_t) (rem64>>2) * (uint64_t) recipSqrt32)>>32) + 2;
133     x64 = (uint64_t) q<<7;
134     extSigZ[indexWord( 3, 0 )] = x64;
135     x64 = (sig64Z<<1) + (x64>>32);
136     extSigZ[indexWord( 3, 2 )] = x64>>32;
137     extSigZ[indexWord( 3, 1 )] = x64;
138     /*------------------------------------------------------------------------
139     *------------------------------------------------------------------------*/
140     if ( (q & 0xFFFFFF) <= 2 ) {
141         q &= ~(uint32_t) 0xFFFF;
142         extSigZ[indexWordLo( 3 )] = q<<7;
143         x64 = sig64Z + (q>>27);
144         term[indexWord( 4, 3 )] = 0;
145         term[indexWord( 4, 2 )] = x64>>32;
146         term[indexWord( 4, 1 )] = x64;
147         term[indexWord( 4, 0 )] = q<<5;
148         rem[indexWord( 4, 0 )] = 0;
149         softfloat_remStep128MBy32( rem, 28, term, q, rem );
150         q = rem[indexWordHi( 4 )];
151         if ( q & 0x80000000 ) {
152             softfloat_sub1X96M( extSigZ );
153         } else {
154             if ( q || rem[indexWord( 4, 1 )] || rem[indexWord( 4, 2 )] ) {
155                 extSigZ[indexWordLo( 3 )] |= 1;
156             }
157         }
158     }
159     softfloat_roundPackMToExtF80M(
160         0, expZ, extSigZ, extF80_roundingPrecision, zSPtr );
161     return;
162     /*------------------------------------------------------------------------
163     *------------------------------------------------------------------------*/
164  invalid:
165     softfloat_invalidExtF80M( zSPtr );
166     return;
167     /*------------------------------------------------------------------------
168     *------------------------------------------------------------------------*/
169  copyA:
170     zSPtr->signExp = uiA64;
171     zSPtr->signif  = rem64;
172 
173 }
174 
175 #endif
176 
177