1 /* IEEE754 floating point arithmetic
2 * single precision square root
5 * MIPS floating point support
6 * Copyright (C) 1994-2000 Algorithmics Ltd.
7 * http://www.algor.co.uk
9 * ########################################################################
11 * This program is free software; you can distribute it and/or modify it
12 * under the terms of the GNU General Public License (Version 2) as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
24 * ########################################################################
28 #include "ieee754sp.h"
30 ieee754sp
ieee754sp_sqrt(ieee754sp x
)
32 int ix
, s
, q
, m
, t
, i
;
36 /* take care of Inf and NaN */
42 /* x == INF or NAN? */
44 case IEEE754_CLASS_QNAN
:
46 return ieee754sp_nanxcpt(x
, "sqrt");
47 case IEEE754_CLASS_SNAN
:
48 SETCX(IEEE754_INVALID_OPERATION
);
49 return ieee754sp_nanxcpt(ieee754sp_indef(), "sqrt");
50 case IEEE754_CLASS_ZERO
:
53 case IEEE754_CLASS_INF
:
55 /* sqrt(-Inf) = Nan */
56 SETCX(IEEE754_INVALID_OPERATION
);
57 return ieee754sp_nanxcpt(ieee754sp_indef(), "sqrt");
59 /* sqrt(+Inf) = Inf */
61 case IEEE754_CLASS_DNORM
:
62 case IEEE754_CLASS_NORM
:
65 SETCX(IEEE754_INVALID_OPERATION
);
66 return ieee754sp_nanxcpt(ieee754sp_indef(), "sqrt");
75 if (m
== 0) { /* subnormal x */
76 for (i
= 0; (ix
& 0x00800000) == 0; i
++)
80 m
-= 127; /* unbias exponent */
81 ix
= (ix
& 0x007fffff) | 0x00800000;
82 if (m
& 1) /* odd m, double x to make it even */
84 m
>>= 1; /* m = [m/2] */
86 /* generate sqrt(x) bit by bit */
88 q
= s
= 0; /* q = sqrt(x) */
89 r
= 0x01000000; /* r = moving bit from right to left */
103 SETCX(IEEE754_INEXACT
);
104 switch (ieee754_csr
.rm
) {
113 ix
= (q
>> 1) + 0x3f000000;