This source file includes following definitions.
- ieee754dp_sqrt
1
2
3
4
5
6
7
8
9
10 #include "ieee754dp.h"
11
12 static const unsigned int table[] = {
13 0, 1204, 3062, 5746, 9193, 13348, 18162, 23592,
14 29598, 36145, 43202, 50740, 58733, 67158, 75992,
15 85215, 83599, 71378, 60428, 50647, 41945, 34246,
16 27478, 21581, 16499, 12183, 8588, 5674, 3403,
17 1742, 661, 130
18 };
19
20 union ieee754dp ieee754dp_sqrt(union ieee754dp x)
21 {
22 struct _ieee754_csr oldcsr;
23 union ieee754dp y, z, t;
24 unsigned int scalx, yh;
25 COMPXDP;
26
27 EXPLODEXDP;
28 ieee754_clearcx();
29 FLUSHXDP;
30
31
32 switch (xc) {
33 case IEEE754_CLASS_SNAN:
34 return ieee754dp_nanxcpt(x);
35
36 case IEEE754_CLASS_QNAN:
37
38 return x;
39
40 case IEEE754_CLASS_ZERO:
41
42 return x;
43
44 case IEEE754_CLASS_INF:
45 if (xs) {
46
47 ieee754_setcx(IEEE754_INVALID_OPERATION);
48 return ieee754dp_indef();
49 }
50
51 return x;
52
53 case IEEE754_CLASS_DNORM:
54 DPDNORMX;
55
56
57 case IEEE754_CLASS_NORM:
58 if (xs) {
59
60 ieee754_setcx(IEEE754_INVALID_OPERATION);
61 return ieee754dp_indef();
62 }
63 break;
64 }
65
66
67 oldcsr = ieee754_csr;
68 ieee754_csr.mx &= ~IEEE754_INEXACT;
69 ieee754_csr.sx &= ~IEEE754_INEXACT;
70 ieee754_csr.rm = FPU_CSR_RN;
71
72
73 scalx = 0;
74 if (xe > 512) {
75 xe -= 512;
76 scalx += 256;
77 } else if (xe < -512) {
78 xe += 512;
79 scalx -= 256;
80 }
81
82 x = builddp(0, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
83 y = x;
84
85
86 yh = y.bits >> 32;
87 yh = (yh >> 1) + 0x1ff80000;
88 yh = yh - table[(yh >> 15) & 31];
89 y.bits = ((u64) yh << 32) | (y.bits & 0xffffffff);
90
91
92
93 t = ieee754dp_div(x, y);
94 y = ieee754dp_add(y, t);
95 y.bits -= 0x0010000600000000LL;
96 y.bits &= 0xffffffff00000000LL;
97
98
99
100 t = ieee754dp_mul(y, y);
101 z = t;
102 t.bexp += 0x001;
103 t = ieee754dp_add(t, z);
104 z = ieee754dp_mul(ieee754dp_sub(x, z), y);
105
106
107 t = ieee754dp_div(z, ieee754dp_add(t, x));
108 t.bexp += 0x001;
109 y = ieee754dp_add(y, t);
110
111
112
113
114 ieee754_csr.rm = FPU_CSR_RZ;
115 ieee754_csr.sx &= ~IEEE754_INEXACT;
116
117
118 t = ieee754dp_div(x, y);
119
120 if (ieee754_csr.sx & IEEE754_INEXACT || t.bits != y.bits) {
121
122 if (!(ieee754_csr.sx & IEEE754_INEXACT))
123
124 t.bits -= 1;
125
126
127 oldcsr.cx |= IEEE754_INEXACT;
128 oldcsr.sx |= IEEE754_INEXACT;
129
130 switch (oldcsr.rm) {
131 case FPU_CSR_RU:
132 y.bits += 1;
133
134 case FPU_CSR_RN:
135 t.bits += 1;
136 break;
137 }
138
139
140 y = ieee754dp_add(y, t);
141
142
143 scalx -= 1;
144 }
145
146
147 y.bexp += scalx;
148
149
150 ieee754_csr = oldcsr;
151
152 return y;
153 }