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