2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001-2013 Free Software Foundation, Inc.
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, see <http://www.gnu.org/licenses/>.
19 /*********************************************************************/
20 /* MODULE_NAME: uroot.c */
24 /* FILES NEEDED: dla.h endian.h mydefs.h uroot.h */
27 /* An ultimate sqrt routine. Given an IEEE double machine number x */
28 /* it computes the correctly rounded (to nearest) value of square */
30 /* Assumption: Machine arithmetic operations are performed in */
31 /* round to nearest mode of IEEE 754 standard. */
33 /*********************************************************************/
35 #include <math_private.h>
37 typedef unsigned int int4
;
38 typedef union {int4 i
[4]; long double x
; double d
[2]; } mynumber
;
41 t512
= {{0x5ff00000, 0x00000000, 0x00000000, 0x00000000 }}, /* 2^512 */
42 tm256
= {{0x2ff00000, 0x00000000, 0x00000000, 0x00000000 }}; /* 2^-256 */
44 two54
= 1.80143985094819840000e+16, /* 0x4350000000000000 */
45 twom54
= 5.55111512312578270212e-17; /* 0x3C90000000000000 */
47 /*********************************************************************/
48 /* An ultimate sqrt routine. Given an IEEE double machine number x */
49 /* it computes the correctly rounded (to nearest) value of square */
51 /*********************************************************************/
52 long double __ieee754_sqrtl(long double x
)
54 static const long double big
= 134217728.0, big1
= 134217729.0;
62 k
=a
.i
[0] & 0x7fffffff;
63 /*----------------- 2^-1022 <= | x |< 2^1024 -----------------*/
64 if (k
>0x000fffff && k
<0x7ff00000) {
65 if (x
< 0) return (big1
-big1
)/(big
-big
);
66 l
= (k
&0x001fffff)|0x3fe00000;
67 if (((a
.i
[2] & 0x7fffffff) | a
.i
[3]) != 0) {
68 n
= (int) ((l
- k
) * 2) >> 21;
69 m
= (a
.i
[2] >> 20) & 0x7ff;
72 m
= ((a
.i
[2] >> 20) & 0x7ff) - 54;
76 a
.i
[2] = (a
.i
[2] & 0x800fffff) | (m
<< 20);
77 else if ((int) m
<= -54) {
82 a
.i
[2] = (a
.i
[2] & 0x800fffff) | (m
<< 20);
88 d
= __ieee754_sqrt (a
.d
[0]);
89 c
.i
[0] = 0x20000000+((k
&0x7fe00000)>>1);
94 t
= 0.5L * (i
+ s
/ i
);
95 i
= 0.5L * (t
+ s
/ t
);
100 if (a
.i
[0] == 0xfff00000 && a
.i
[1] == 0)
101 return (big1
-big1
)/(big
-big
); /* sqrt (-Inf) = NaN. */
102 return x
; /* sqrt (NaN) = NaN, sqrt (+Inf) = +Inf. */
104 if (x
== 0) return x
;
105 if (x
< 0) return (big1
-big1
)/(big
-big
);
106 return tm256
.x
*__ieee754_sqrtl(x
*t512
.x
);
109 strong_alias (__ieee754_sqrtl
, __sqrtl_finite
)