VirtualBox

source: vbox/trunk/src/libs/softfloat-3e/source/extF80_sqrt.c@ 94558

Last change on this file since 94558 was 94558, checked in by vboxsync, 3 years ago

VMM/IEM,libs/softfloat: Don't use global variables in SoftFloat, pass in a state pointer to (almost) all functions instead. Started on fsqrt instruction implementation. bugref:9898

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
1
2/*============================================================================
3
4This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5Package, Release 3e, by John R. Hauser.
6
7Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the
8University of California. All rights reserved.
9
10Redistribution and use in source and binary forms, with or without
11modification, 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
24THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31ON 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
33SOFTWARE, 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
44extFloat80_t extF80_sqrt( extFloat80_t a SOFTFLOAT_STATE_DECL_COMMA )
45{
46 union { struct extFloat80M s; extFloat80_t f; } uA;
47 uint_fast16_t uiA64;
48 uint_fast64_t uiA0;
49 bool signA;
50 int_fast32_t expA;
51 uint_fast64_t sigA;
52 struct uint128 uiZ;
53 uint_fast16_t uiZ64;
54 uint_fast64_t uiZ0;
55 struct exp32_sig64 normExpSig;
56 int_fast32_t expZ;
57 uint_fast32_t sig32A, recipSqrt32, sig32Z;
58 struct uint128 rem;
59 uint_fast64_t q, x64, sigZ;
60 struct uint128 y, term;
61 uint_fast64_t sigZExtra;
62 union { struct extFloat80M s; extFloat80_t f; } uZ;
63
64 /*------------------------------------------------------------------------
65 *------------------------------------------------------------------------*/
66 uA.f = a;
67 uiA64 = uA.s.signExp;
68 uiA0 = uA.s.signif;
69 signA = signExtF80UI64( uiA64 );
70 expA = expExtF80UI64( uiA64 );
71 sigA = uiA0;
72 /*------------------------------------------------------------------------
73 *------------------------------------------------------------------------*/
74 if ( expA == 0x7FFF ) {
75 if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {
76 uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, 0, 0 SOFTFLOAT_STATE_ARG_COMMA );
77 uiZ64 = uiZ.v64;
78 uiZ0 = uiZ.v0;
79 goto uiZ;
80 }
81 if ( ! signA ) return a;
82 goto invalid;
83 }
84 /*------------------------------------------------------------------------
85 *------------------------------------------------------------------------*/
86 if ( signA ) {
87 if ( ! sigA ) goto zero;
88 goto invalid;
89 }
90 /*------------------------------------------------------------------------
91 *------------------------------------------------------------------------*/
92 if ( ! expA ) expA = 1;
93 if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {
94 if ( ! sigA ) goto zero;
95 normExpSig = softfloat_normSubnormalExtF80Sig( sigA );
96 expA += normExpSig.exp;
97 sigA = normExpSig.sig;
98 }
99 /*------------------------------------------------------------------------
100 | (`sig32Z' is guaranteed to be a lower bound on the square root of
101 | `sig32A', which makes `sig32Z' also a lower bound on the square root of
102 | `sigA'.)
103 *------------------------------------------------------------------------*/
104 expZ = ((expA - 0x3FFF)>>1) + 0x3FFF;
105 expA &= 1;
106 sig32A = sigA>>32;
107 recipSqrt32 = softfloat_approxRecipSqrt32_1( expA, sig32A );
108 sig32Z = ((uint_fast64_t) sig32A * recipSqrt32)>>32;
109 if ( expA ) {
110 sig32Z >>= 1;
111 rem = softfloat_shortShiftLeft128( 0, sigA, 61 );
112 } else {
113 rem = softfloat_shortShiftLeft128( 0, sigA, 62 );
114 }
115 rem.v64 -= (uint_fast64_t) sig32Z * sig32Z;
116 /*------------------------------------------------------------------------
117 *------------------------------------------------------------------------*/
118 q = ((uint32_t) (rem.v64>>2) * (uint_fast64_t) recipSqrt32)>>32;
119 x64 = (uint_fast64_t) sig32Z<<32;
120 sigZ = x64 + (q<<3);
121 y = softfloat_shortShiftLeft128( rem.v64, rem.v0, 29 );
122 /*------------------------------------------------------------------------
123 | (Repeating this loop is a rare occurrence.)
124 *------------------------------------------------------------------------*/
125 for (;;) {
126 term = softfloat_mul64ByShifted32To128( x64 + sigZ, q );
127 rem = softfloat_sub128( y.v64, y.v0, term.v64, term.v0 );
128 if ( ! (rem.v64 & UINT64_C( 0x8000000000000000 )) ) break;
129 --q;
130 sigZ -= 1<<3;
131 }
132 /*------------------------------------------------------------------------
133 *------------------------------------------------------------------------*/
134 q = (((rem.v64>>2) * recipSqrt32)>>32) + 2;
135 x64 = sigZ;
136 sigZ = (sigZ<<1) + (q>>25);
137 sigZExtra = (uint64_t) (q<<39);
138 /*------------------------------------------------------------------------
139 *------------------------------------------------------------------------*/
140 if ( (q & 0xFFFFFF) <= 2 ) {
141 q &= ~(uint_fast64_t) 0xFFFF;
142 sigZExtra = (uint64_t) (q<<39);
143 term = softfloat_mul64ByShifted32To128( x64 + (q>>27), q );
144 x64 = (uint32_t) (q<<5) * (uint_fast64_t) (uint32_t) q;
145 term = softfloat_add128( term.v64, term.v0, 0, x64 );
146 rem = softfloat_shortShiftLeft128( rem.v64, rem.v0, 28 );
147 rem = softfloat_sub128( rem.v64, rem.v0, term.v64, term.v0 );
148 if ( rem.v64 & UINT64_C( 0x8000000000000000 ) ) {
149 if ( ! sigZExtra ) --sigZ;
150 --sigZExtra;
151 } else {
152 if ( rem.v64 | rem.v0 ) sigZExtra |= 1;
153 }
154 }
155 return
156 softfloat_roundPackToExtF80(
157 0, expZ, sigZ, sigZExtra, extF80_roundingPrecision SOFTFLOAT_STATE_ARG_COMMA );
158 /*------------------------------------------------------------------------
159 *------------------------------------------------------------------------*/
160 invalid:
161 softfloat_raiseFlags( softfloat_flag_invalid SOFTFLOAT_STATE_ARG_COMMA );
162 uiZ64 = defaultNaNExtF80UI64;
163 uiZ0 = defaultNaNExtF80UI0;
164 goto uiZ;
165 /*------------------------------------------------------------------------
166 *------------------------------------------------------------------------*/
167 zero:
168 uiZ64 = packToExtF80UI64( signA, 0 );
169 uiZ0 = 0;
170 uiZ:
171 uZ.s.signExp = uiZ64;
172 uZ.s.signif = uiZ0;
173 return uZ.f;
174
175}
176
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette