1 /* IEEE754 floating point arithmetic
2 * single precision square root
5 * MIPS floating point support
6 * Copyright (C) 1994-2000 Algorithmics Ltd.
8 * 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.
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
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.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "ieee754sp.h"
24 union ieee754sp ieee754sp_sqrt(union ieee754sp x)
26 int ix, s, q, m, t, i;
30 /* take care of Inf and NaN */
36 /* x == INF or NAN? */
38 case IEEE754_CLASS_QNAN:
40 return ieee754sp_nanxcpt(x);
42 case IEEE754_CLASS_SNAN:
43 ieee754_setcx(IEEE754_INVALID_OPERATION);
44 return ieee754sp_nanxcpt(ieee754sp_indef());
46 case IEEE754_CLASS_ZERO:
50 case IEEE754_CLASS_INF:
52 /* sqrt(-Inf) = Nan */
53 ieee754_setcx(IEEE754_INVALID_OPERATION);
54 return ieee754sp_nanxcpt(ieee754sp_indef());
56 /* sqrt(+Inf) = Inf */
59 case IEEE754_CLASS_DNORM:
60 case IEEE754_CLASS_NORM:
63 ieee754_setcx(IEEE754_INVALID_OPERATION);
64 return ieee754sp_nanxcpt(ieee754sp_indef());
73 if (m == 0) { /* subnormal x */
74 for (i = 0; (ix & 0x00800000) == 0; i++)
78 m -= 127; /* unbias exponent */
79 ix = (ix & 0x007fffff) | 0x00800000;
80 if (m & 1) /* odd m, double x to make it even */
82 m >>= 1; /* m = [m/2] */
84 /* generate sqrt(x) bit by bit */
86 q = s = 0; /* q = sqrt(x) */
87 r = 0x01000000; /* r = moving bit from right to left */
101 ieee754_setcx(IEEE754_INEXACT);
102 switch (ieee754_csr.rm) {
111 ix = (q >> 1) + 0x3f000000;