]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/ieee754/dbl-64/halfulp.c
Update.
[thirdparty/glibc.git] / sysdeps / ieee754 / dbl-64 / halfulp.c
1
2 /*
3 * IBM Accurate Mathematical Library
4 * Copyright (c) International Business Machines Corp., 2001
5 *
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 of the License, or
9 * (at your option) any later version.
10 *
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 General Public License for more details.
15 *
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.
19 */
20 /************************************************************************/
21 /* */
22 /* MODULE_NAME:halfulp.c */
23 /* */
24 /* FUNCTIONS:halfulp */
25 /* FILES NEEDED: mydefs.h dla.h endian.h */
26 /* uroot.c */
27 /* */
28 /*Routine halfulp(double x, double y) computes x^y where result does */
29 /*not need rounding. If the result is closer to 0 than can be */
30 /*represented it returns 0. */
31 /* In the following cases the function does not compute anything */
32 /*and returns a negative number: */
33 /*1. if the result needs rounding, */
34 /*2. if y is outside the interval [0, 2^20-1], */
35 /*3. if x can be represented by x=2**n for some integer n. */
36 /************************************************************************/
37
38 #include "endian.h"
39 #include "mydefs.h"
40 #include "dla.h"
41
42 double __ieee754_sqrt(double x);
43
44 int4 tab54[32] = {
45 262143, 11585, 1782, 511, 210, 107, 63, 42,
46 30, 22, 17, 14, 12, 10, 9, 7,
47 7, 6, 5, 5, 5, 4, 4, 4,
48 3, 3, 3, 3, 3, 3, 3, 3 };
49
50
51 double __halfulp(double x, double y)
52 {
53 mynumber v;
54 double z,u,uu,j1,j2,j3,j4,j5;
55 int4 k,l,m,n;
56 if (y <= 0) { /*if power is negative or zero */
57 v.x = y;
58 if (v.i[LOW_HALF] != 0) return -10.0;
59 v.x = x;
60 if (v.i[LOW_HALF] != 0) return -10.0;
61 if ((v.i[HIGH_HALF]&0x000fffff) != 0) return -10; /* if x =2 ^ n */
62 k = ((v.i[HIGH_HALF]&0x7fffffff)>>20)-1023; /* find this n */
63 z = (double) k;
64 return (z*y == -1075.0)?0: -10.0;
65 }
66 /* if y > 0 */
67 v.x = y;
68 if (v.i[LOW_HALF] != 0) return -10.0;
69
70 v.x=x;
71 /* case where x = 2**n for some integer n */
72 if (((v.i[HIGH_HALF]&0x000fffff)|v.i[LOW_HALF]) == 0) {
73 k=(v.i[HIGH_HALF]>>20)-1023;
74 return (((double) k)*y == -1075.0)?0:-10.0;
75 }
76
77 v.x = y;
78 k = v.i[HIGH_HALF];
79 m = k<<12;
80 l = 0;
81 while (m)
82 {m = m<<1; l++; }
83 n = (k&0x000fffff)|0x00100000;
84 n = n>>(20-l); /* n is the odd integer of y */
85 k = ((k>>20) -1023)-l; /* y = n*2**k */
86 if (k>5) return -10.0;
87 if (k>0) for (;k>0;k--) n *= 2;
88 if (n > 34) return -10.0;
89 k = -k;
90 if (k>5) return -10.0;
91
92 /* now treat x */
93 while (k>0) {
94 z = __ieee754_sqrt(x);
95 EMULV(z,z,u,uu,j1,j2,j3,j4,j5);
96 if (((u-x)+uu) != 0) break;
97 x = z;
98 k--;
99 }
100 if (k) return -10.0;
101
102 /* it is impossible that n == 2, so the mantissa of x must be short */
103
104 v.x = x;
105 if (v.i[LOW_HALF]) return -10.0;
106 k = v.i[HIGH_HALF];
107 m = k<<12;
108 l = 0;
109 while (m) {m = m<<1; l++; }
110 m = (k&0x000fffff)|0x00100000;
111 m = m>>(20-l); /* m is the odd integer of x */
112
113 /* now check whether the length of m**n is at most 54 bits */
114
115 if (m > tab54[n-3]) return -10.0;
116
117 /* yes, it is - now compute x**n by simple multiplications */
118
119 u = x;
120 for (k=1;k<n;k++) u = u*x;
121 return u;
122 }