blob: b7c098a86f951013f74d5d3838fef5ae014cbd9b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* IEEE754 floating point arithmetic
2 * single 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 "ieee754sp.h"
23
Ralf Baechle2209bcb2014-04-16 01:31:11 +020024union ieee754sp ieee754sp_sqrt(union ieee754sp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 int ix, s, q, m, t, i;
27 unsigned int r;
28 COMPXSP;
29
30 /* take care of Inf and NaN */
31
32 EXPLODEXSP;
Ralf Baechle9e8bad12014-04-19 00:36:32 +020033 ieee754_clearcx();
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 FLUSHXSP;
35
36 /* x == INF or NAN? */
37 switch (xc) {
38 case IEEE754_CLASS_QNAN:
39 /* sqrt(Nan) = Nan */
Ralf Baechle90efba32014-04-25 03:19:57 +020040 return ieee754sp_nanxcpt(x);
Ralf Baechle3f7cac42014-04-26 01:49:14 +020041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 case IEEE754_CLASS_SNAN:
Ralf Baechle9e8bad12014-04-19 00:36:32 +020043 ieee754_setcx(IEEE754_INVALID_OPERATION);
Ralf Baechle90efba32014-04-25 03:19:57 +020044 return ieee754sp_nanxcpt(ieee754sp_indef());
Ralf Baechle3f7cac42014-04-26 01:49:14 +020045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 case IEEE754_CLASS_ZERO:
47 /* sqrt(0) = 0 */
48 return x;
Ralf Baechle3f7cac42014-04-26 01:49:14 +020049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case IEEE754_CLASS_INF:
51 if (xs) {
52 /* sqrt(-Inf) = Nan */
Ralf Baechle9e8bad12014-04-19 00:36:32 +020053 ieee754_setcx(IEEE754_INVALID_OPERATION);
Ralf Baechle90efba32014-04-25 03:19:57 +020054 return ieee754sp_nanxcpt(ieee754sp_indef());
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56 /* sqrt(+Inf) = Inf */
57 return x;
Ralf Baechle3f7cac42014-04-26 01:49:14 +020058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 case IEEE754_CLASS_DNORM:
60 case IEEE754_CLASS_NORM:
61 if (xs) {
62 /* sqrt(-x) = Nan */
Ralf Baechle9e8bad12014-04-19 00:36:32 +020063 ieee754_setcx(IEEE754_INVALID_OPERATION);
Ralf Baechle90efba32014-04-25 03:19:57 +020064 return ieee754sp_nanxcpt(ieee754sp_indef());
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
66 break;
67 }
68
69 ix = x.bits;
70
71 /* normalize x */
72 m = (ix >> 23);
73 if (m == 0) { /* subnormal x */
74 for (i = 0; (ix & 0x00800000) == 0; i++)
75 ix <<= 1;
76 m -= i - 1;
77 }
78 m -= 127; /* unbias exponent */
79 ix = (ix & 0x007fffff) | 0x00800000;
80 if (m & 1) /* odd m, double x to make it even */
81 ix += ix;
82 m >>= 1; /* m = [m/2] */
83
84 /* generate sqrt(x) bit by bit */
85 ix += ix;
86 q = s = 0; /* q = sqrt(x) */
87 r = 0x01000000; /* r = moving bit from right to left */
88
89 while (r != 0) {
90 t = s + r;
91 if (t <= ix) {
92 s = t + r;
93 ix -= t;
94 q += r;
95 }
96 ix += ix;
97 r >>= 1;
98 }
99
100 if (ix != 0) {
Ralf Baechle9e8bad12014-04-19 00:36:32 +0200101 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 switch (ieee754_csr.rm) {
Ralf Baechle56a64732014-04-30 11:21:55 +0200103 case FPU_CSR_RU:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 q += 2;
105 break;
Ralf Baechle56a64732014-04-30 11:21:55 +0200106 case FPU_CSR_RN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 q += (q & 1);
108 break;
109 }
110 }
111 ix = (q >> 1) + 0x3f000000;
112 ix += (m << 23);
113 x.bits = ix;
114 return x;
115}