blob: 0d8e7ffdba5ee6762ec7948c1b0671c730c63a0c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* IEEE754 floating point arithmetic
2 * double precision square root
3 */
4/*
5 * MIPS floating point support
6 * Copyright (C) 1994-2000 Algorithmics Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can distribute it and/or modify it
9 * under the terms of the GNU General Public License (Version 2) as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
Ralf Baechle3f7cac42014-04-26 01:49:14 +020019 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "ieee754dp.h"
23
24static const unsigned table[] = {
25 0, 1204, 3062, 5746, 9193, 13348, 18162, 23592,
26 29598, 36145, 43202, 50740, 58733, 67158, 75992,
27 85215, 83599, 71378, 60428, 50647, 41945, 34246,
28 27478, 21581, 16499, 12183, 8588, 5674, 3403,
29 1742, 661, 130
30};
31
Ralf Baechle2209bcb2014-04-16 01:31:11 +020032union ieee754dp ieee754dp_sqrt(union ieee754dp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000034 struct _ieee754_csr oldcsr;
Ralf Baechle2209bcb2014-04-16 01:31:11 +020035 union ieee754dp y, z, t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 unsigned scalx, yh;
37 COMPXDP;
38
39 EXPLODEXDP;
Ralf Baechle9e8bad12014-04-19 00:36:32 +020040 ieee754_clearcx();
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 FLUSHXDP;
42
43 /* x == INF or NAN? */
44 switch (xc) {
45 case IEEE754_CLASS_QNAN:
46 /* sqrt(Nan) = Nan */
Maciej W. Rozycki539bfb52015-04-03 23:25:30 +010047 return x;
Ralf Baechle3f7cac42014-04-26 01:49:14 +020048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case IEEE754_CLASS_SNAN:
Ralf Baechle9e8bad12014-04-19 00:36:32 +020050 ieee754_setcx(IEEE754_INVALID_OPERATION);
Ralf Baechle90efba32014-04-25 03:19:57 +020051 return ieee754dp_nanxcpt(ieee754dp_indef());
Ralf Baechle3f7cac42014-04-26 01:49:14 +020052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 case IEEE754_CLASS_ZERO:
54 /* sqrt(0) = 0 */
55 return x;
Ralf Baechle3f7cac42014-04-26 01:49:14 +020056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 case IEEE754_CLASS_INF:
58 if (xs) {
59 /* sqrt(-Inf) = Nan */
Ralf Baechle9e8bad12014-04-19 00:36:32 +020060 ieee754_setcx(IEEE754_INVALID_OPERATION);
Maciej W. Rozycki539bfb52015-04-03 23:25:30 +010061 return ieee754dp_indef();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63 /* sqrt(+Inf) = Inf */
64 return x;
Ralf Baechle3f7cac42014-04-26 01:49:14 +020065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 case IEEE754_CLASS_DNORM:
67 DPDNORMX;
68 /* fall through */
Ralf Baechle3f7cac42014-04-26 01:49:14 +020069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 case IEEE754_CLASS_NORM:
71 if (xs) {
72 /* sqrt(-x) = Nan */
Ralf Baechle9e8bad12014-04-19 00:36:32 +020073 ieee754_setcx(IEEE754_INVALID_OPERATION);
Maciej W. Rozycki539bfb52015-04-03 23:25:30 +010074 return ieee754dp_indef();
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76 break;
77 }
78
79 /* save old csr; switch off INX enable & flag; set RN rounding */
80 oldcsr = ieee754_csr;
81 ieee754_csr.mx &= ~IEEE754_INEXACT;
82 ieee754_csr.sx &= ~IEEE754_INEXACT;
Ralf Baechle56a64732014-04-30 11:21:55 +020083 ieee754_csr.rm = FPU_CSR_RN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /* adjust exponent to prevent overflow */
86 scalx = 0;
87 if (xe > 512) { /* x > 2**-512? */
88 xe -= 512; /* x = x / 2**512 */
89 scalx += 256;
Ralf Baechle70342282013-01-22 12:59:30 +010090 } else if (xe < -512) { /* x < 2**-512? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 xe += 512; /* x = x * 2**512 */
92 scalx -= 256;
93 }
94
95 y = x = builddp(0, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
96
97 /* magic initial approximation to almost 8 sig. bits */
98 yh = y.bits >> 32;
99 yh = (yh >> 1) + 0x1ff80000;
100 yh = yh - table[(yh >> 15) & 31];
101 y.bits = ((u64) yh << 32) | (y.bits & 0xffffffff);
102
103 /* Heron's rule once with correction to improve to ~18 sig. bits */
104 /* t=x/y; y=y+t; py[n0]=py[n0]-0x00100006; py[n1]=0; */
105 t = ieee754dp_div(x, y);
106 y = ieee754dp_add(y, t);
107 y.bits -= 0x0010000600000000LL;
108 y.bits &= 0xffffffff00000000LL;
109
110 /* triple to almost 56 sig. bits: y ~= sqrt(x) to within 1 ulp */
Ralf Baechle70342282013-01-22 12:59:30 +0100111 /* t=y*y; z=t; pt[n0]+=0x00100000; t+=z; z=(x-z)*y; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 z = t = ieee754dp_mul(y, y);
Ralf Baechle49548b02014-04-25 15:48:40 +0200113 t.bexp += 0x001;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 t = ieee754dp_add(t, z);
115 z = ieee754dp_mul(ieee754dp_sub(x, z), y);
116
Ralf Baechle70342282013-01-22 12:59:30 +0100117 /* t=z/(t+x) ; pt[n0]+=0x00100000; y+=t; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 t = ieee754dp_div(z, ieee754dp_add(t, x));
Ralf Baechle49548b02014-04-25 15:48:40 +0200119 t.bexp += 0x001;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 y = ieee754dp_add(y, t);
121
122 /* twiddle last bit to force y correctly rounded */
123
124 /* set RZ, clear INEX flag */
Ralf Baechle56a64732014-04-30 11:21:55 +0200125 ieee754_csr.rm = FPU_CSR_RZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 ieee754_csr.sx &= ~IEEE754_INEXACT;
127
128 /* t=x/y; ...chopped quotient, possibly inexact */
129 t = ieee754dp_div(x, y);
130
131 if (ieee754_csr.sx & IEEE754_INEXACT || t.bits != y.bits) {
132
133 if (!(ieee754_csr.sx & IEEE754_INEXACT))
134 /* t = t-ulp */
135 t.bits -= 1;
136
137 /* add inexact to result status */
138 oldcsr.cx |= IEEE754_INEXACT;
139 oldcsr.sx |= IEEE754_INEXACT;
140
141 switch (oldcsr.rm) {
Ralf Baechle56a64732014-04-30 11:21:55 +0200142 case FPU_CSR_RU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 y.bits += 1;
144 /* drop through */
Ralf Baechle56a64732014-04-30 11:21:55 +0200145 case FPU_CSR_RN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 t.bits += 1;
147 break;
148 }
149
150 /* y=y+t; ...chopped sum */
151 y = ieee754dp_add(y, t);
152
153 /* adjust scalx for correctly rounded sqrt(x) */
154 scalx -= 1;
155 }
156
157 /* py[n0]=py[n0]+scalx; ...scale back y */
Ralf Baechle49548b02014-04-25 15:48:40 +0200158 y.bexp += scalx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 /* restore rounding mode, possibly set inexact */
161 ieee754_csr = oldcsr;
162
163 return y;
164}