Fix for logb/logbf/logbl (bugs 13954/13955/13956)
[glibc.git] / sysdeps / ieee754 / dbl-64 / branred.c
bloba32daa990d27ad146391df98292d71b69a73d492
1 /*
2 * IBM Accurate Mathematical Library
3 * Written by International Business Machines Corp.
4 * Copyright (C) 2001, 2011 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 /* */
21 /* MODULE_NAME: branred.c */
22 /* */
23 /* FUNCTIONS: branred */
24 /* */
25 /* FILES NEEDED: branred.h mydefs.h endian.h mpa.h */
26 /* mha.c */
27 /* */
28 /* Routine branred() performs range reduction of a double number */
29 /* x into Double length number a+aa,such that */
30 /* x=n*pi/2+(a+aa), abs(a+aa)<pi/4, n=0,+-1,+-2,.... */
31 /* Routine returns the integer (n mod 4) of the above description */
32 /* of x. */
33 /*******************************************************************/
35 #include "endian.h"
36 #include "mydefs.h"
37 #include "branred.h"
38 #include <math_private.h>
40 #ifndef SECTION
41 # define SECTION
42 #endif
45 /*******************************************************************/
46 /* Routine branred() performs range reduction of a double number */
47 /* x into Double length number a+aa,such that */
48 /* x=n*pi/2+(a+aa), abs(a+aa)<pi/4, n=0,+-1,+-2,.... */
49 /* Routine return integer (n mod 4) */
50 /*******************************************************************/
51 int
52 SECTION
53 __branred(double x, double *a, double *aa)
55 int i,k;
56 #if 0
57 int n;
58 #endif
59 mynumber u,gor;
60 #if 0
61 mynumber v;
62 #endif
63 double r[6],s,t,sum,b,bb,sum1,sum2,b1,bb1,b2,bb2,x1,x2,t1,t2;
65 x*=tm600.x;
66 t=x*split; /* split x to two numbers */
67 x1=t-(t-x);
68 x2=x-x1;
69 sum=0;
70 u.x = x1;
71 k = (u.i[HIGH_HALF]>>20)&2047;
72 k = (k-450)/24;
73 if (k<0)
74 k=0;
75 gor.x = t576.x;
76 gor.i[HIGH_HALF] -= ((k*24)<<20);
77 for (i=0;i<6;i++)
78 { r[i] = x1*toverp[k+i]*gor.x; gor.x *= tm24.x; }
79 for (i=0;i<3;i++) {
80 s=(r[i]+big.x)-big.x;
81 sum+=s;
82 r[i]-=s;
84 t=0;
85 for (i=0;i<6;i++)
86 t+=r[5-i];
87 bb=(((((r[0]-t)+r[1])+r[2])+r[3])+r[4])+r[5];
88 s=(t+big.x)-big.x;
89 sum+=s;
90 t-=s;
91 b=t+bb;
92 bb=(t-b)+bb;
93 s=(sum+big1.x)-big1.x;
94 sum-=s;
95 b1=b;
96 bb1=bb;
97 sum1=sum;
98 sum=0;
100 u.x = x2;
101 k = (u.i[HIGH_HALF]>>20)&2047;
102 k = (k-450)/24;
103 if (k<0)
104 k=0;
105 gor.x = t576.x;
106 gor.i[HIGH_HALF] -= ((k*24)<<20);
107 for (i=0;i<6;i++)
108 { r[i] = x2*toverp[k+i]*gor.x; gor.x *= tm24.x; }
109 for (i=0;i<3;i++) {
110 s=(r[i]+big.x)-big.x;
111 sum+=s;
112 r[i]-=s;
114 t=0;
115 for (i=0;i<6;i++)
116 t+=r[5-i];
117 bb=(((((r[0]-t)+r[1])+r[2])+r[3])+r[4])+r[5];
118 s=(t+big.x)-big.x;
119 sum+=s;
120 t-=s;
121 b=t+bb;
122 bb=(t-b)+bb;
123 s=(sum+big1.x)-big1.x;
124 sum-=s;
126 b2=b;
127 bb2=bb;
128 sum2=sum;
130 sum=sum1+sum2;
131 b=b1+b2;
132 bb = (ABS(b1)>ABS(b2))? (b1-b)+b2 : (b2-b)+b1;
133 if (b > 0.5)
134 {b-=1.0; sum+=1.0;}
135 else if (b < -0.5)
136 {b+=1.0; sum-=1.0;}
137 s=b+(bb+bb1+bb2);
138 t=((b-s)+bb)+(bb1+bb2);
139 b=s*split;
140 t1=b-(b-s);
141 t2=s-t1;
142 b=s*hp0.x;
143 bb=(((t1*mp1.x-b)+t1*mp2.x)+t2*mp1.x)+(t2*mp2.x+s*hp1.x+t*hp0.x);
144 s=b+bb;
145 t=(b-s)+bb;
146 *a=s;
147 *aa=t;
148 return ((int) sum)&3; /* return quater of unit circle */