1 // SPDX-License-Identifier: GPL-2.0-only
2 /* IEEE754 floating point arithmetic
3 * double precision square root
6 * MIPS floating point support
7 * Copyright (C) 1994-2000 Algorithmics Ltd.
10 #include "ieee754dp.h"
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,
20 union ieee754dp
ieee754dp_sqrt(union ieee754dp x
)
22 struct _ieee754_csr oldcsr
;
23 union ieee754dp y
, z
, t
;
24 unsigned int scalx
, yh
;
31 /* x == INF or NAN? */
33 case IEEE754_CLASS_SNAN
:
34 return ieee754dp_nanxcpt(x
);
36 case IEEE754_CLASS_QNAN
:
40 case IEEE754_CLASS_ZERO
:
44 case IEEE754_CLASS_INF
:
46 /* sqrt(-Inf) = Nan */
47 ieee754_setcx(IEEE754_INVALID_OPERATION
);
48 return ieee754dp_indef();
50 /* sqrt(+Inf) = Inf */
53 case IEEE754_CLASS_DNORM
:
57 case IEEE754_CLASS_NORM
:
60 ieee754_setcx(IEEE754_INVALID_OPERATION
);
61 return ieee754dp_indef();
66 /* save old csr; switch off INX enable & flag; set RN rounding */
68 ieee754_csr
.mx
&= ~IEEE754_INEXACT
;
69 ieee754_csr
.sx
&= ~IEEE754_INEXACT
;
70 ieee754_csr
.rm
= FPU_CSR_RN
;
72 /* adjust exponent to prevent overflow */
74 if (xe
> 512) { /* x > 2**-512? */
75 xe
-= 512; /* x = x / 2**512 */
77 } else if (xe
< -512) { /* x < 2**-512? */
78 xe
+= 512; /* x = x * 2**512 */
82 x
= builddp(0, xe
+ DP_EBIAS
, xm
& ~DP_HIDDEN_BIT
);
85 /* magic initial approximation to almost 8 sig. bits */
87 yh
= (yh
>> 1) + 0x1ff80000;
88 yh
= yh
- table
[(yh
>> 15) & 31];
89 y
.bits
= ((u64
) yh
<< 32) | (y
.bits
& 0xffffffff);
91 /* Heron's rule once with correction to improve to ~18 sig. bits */
92 /* t=x/y; y=y+t; py[n0]=py[n0]-0x00100006; py[n1]=0; */
93 t
= ieee754dp_div(x
, y
);
94 y
= ieee754dp_add(y
, t
);
95 y
.bits
-= 0x0010000600000000LL
;
96 y
.bits
&= 0xffffffff00000000LL
;
98 /* triple to almost 56 sig. bits: y ~= sqrt(x) to within 1 ulp */
99 /* t=y*y; z=t; pt[n0]+=0x00100000; t+=z; z=(x-z)*y; */
100 t
= ieee754dp_mul(y
, y
);
103 t
= ieee754dp_add(t
, z
);
104 z
= ieee754dp_mul(ieee754dp_sub(x
, z
), y
);
106 /* t=z/(t+x) ; pt[n0]+=0x00100000; y+=t; */
107 t
= ieee754dp_div(z
, ieee754dp_add(t
, x
));
109 y
= ieee754dp_add(y
, t
);
111 /* twiddle last bit to force y correctly rounded */
113 /* set RZ, clear INEX flag */
114 ieee754_csr
.rm
= FPU_CSR_RZ
;
115 ieee754_csr
.sx
&= ~IEEE754_INEXACT
;
117 /* t=x/y; ...chopped quotient, possibly inexact */
118 t
= ieee754dp_div(x
, y
);
120 if (ieee754_csr
.sx
& IEEE754_INEXACT
|| t
.bits
!= y
.bits
) {
122 if (!(ieee754_csr
.sx
& IEEE754_INEXACT
))
126 /* add inexact to result status */
127 oldcsr
.cx
|= IEEE754_INEXACT
;
128 oldcsr
.sx
|= IEEE754_INEXACT
;
139 /* y=y+t; ...chopped sum */
140 y
= ieee754dp_add(y
, t
);
142 /* adjust scalx for correctly rounded sqrt(x) */
146 /* py[n0]=py[n0]+scalx; ...scale back y */
149 /* restore rounding mode, possibly set inexact */
150 ieee754_csr
= oldcsr
;