2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001, 2004, 2006 Free Software Foundation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /*********************************************************************/
21 /* MODULE_NAME: uroot.c */
25 /* FILES NEEDED: dla.h endian.h mydefs.h uroot.h */
28 /* An ultimate sqrt routine. Given an IEEE double machine number x */
29 /* it computes the correctly rounded (to nearest) value of square */
31 /* Assumption: Machine arithmetic operations are performed in */
32 /* round to nearest mode of IEEE 754 standard. */
34 /*********************************************************************/
36 #include <math_private.h>
38 typedef unsigned int int4
;
39 typedef union {int4 i
[4]; long double x
; double d
[2]; } mynumber
;
42 t512
= {{0x5ff00000, 0x00000000, 0x00000000, 0x00000000 }}, /* 2^512 */
43 tm256
= {{0x2ff00000, 0x00000000, 0x00000000, 0x00000000 }}; /* 2^-256 */
45 two54
= 1.80143985094819840000e+16, /* 0x4350000000000000 */
46 twom54
= 5.55111512312578270212e-17; /* 0x3C90000000000000 */
48 /*********************************************************************/
49 /* An ultimate sqrt routine. Given an IEEE double machine number x */
50 /* it computes the correctly rounded (to nearest) value of square */
52 /*********************************************************************/
53 long double __ieee754_sqrtl(long double x
)
55 static const long double big
= 134217728.0, big1
= 134217729.0;
63 k
=a
.i
[0] & 0x7fffffff;
64 /*----------------- 2^-1022 <= | x |< 2^1024 -----------------*/
65 if (k
>0x000fffff && k
<0x7ff00000) {
66 if (x
< 0) return (big1
-big1
)/(big
-big
);
67 l
= (k
&0x001fffff)|0x3fe00000;
68 if (((a
.i
[2] & 0x7fffffff) | a
.i
[3]) != 0) {
69 n
= (int) ((l
- k
) * 2) >> 21;
70 m
= (a
.i
[2] >> 20) & 0x7ff;
73 m
= ((a
.i
[2] >> 20) & 0x7ff) - 54;
77 a
.i
[2] = (a
.i
[2] & 0x800fffff) | (m
<< 20);
83 a
.i
[2] = (a
.i
[2] & 0x800fffff) | (m
<< 20);
89 d
= __ieee754_sqrt (a
.d
[0]);
90 c
.i
[0] = 0x20000000+((k
&0x7fe00000)>>1);
95 t
= 0.5L * (i
+ s
/ i
);
96 i
= 0.5L * (t
+ s
/ t
);
101 if (a
.i
[0] == 0xfff00000 && a
.i
[1] == 0)
102 return (big1
-big1
)/(big
-big
); /* sqrt (-Inf) = NaN. */
103 return x
; /* sqrt (NaN) = NaN, sqrt (+Inf) = +Inf. */
105 if (x
== 0) return x
;
106 if (x
< 0) return (big1
-big1
)/(big
-big
);
107 return tm256
.x
*__ieee754_sqrtl(x
*t512
.x
);