2.9
[glibc/nacl-glibc.git] / sysdeps / ieee754 / dbl-64 / e_remainder.c
blobcc06e18ce8bb5d2a6ce190bfa852aaaa2d884b87
1 /*
2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001 Free Software Foundation
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.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 urem.c */
22 /* */
23 /* FUNCTION: uremainder */
24 /* */
25 /* An ultimate remainder routine. Given two IEEE double machine numbers x */
26 /* ,y it computes the correctly rounded (to nearest) value of remainder */
27 /* of dividing x by y. */
28 /* Assumption: Machine arithmetic operations are performed in */
29 /* round to nearest mode of IEEE 754 standard. */
30 /* */
31 /* ************************************************************************/
33 #include "endian.h"
34 #include "mydefs.h"
35 #include "urem.h"
36 #include "MathLib.h"
37 #include "math_private.h"
39 /**************************************************************************/
40 /* An ultimate remainder routine. Given two IEEE double machine numbers x */
41 /* ,y it computes the correctly rounded (to nearest) value of remainder */
42 /**************************************************************************/
43 double __ieee754_remainder(double x, double y)
45 double z,d,xx;
46 #if 0
47 double yy;
48 #endif
49 int4 kx,ky,n,nn,n1,m1,l;
50 #if 0
51 int4 m;
52 #endif
53 mynumber u,t,w={{0,0}},v={{0,0}},ww={{0,0}},r;
54 u.x=x;
55 t.x=y;
56 kx=u.i[HIGH_HALF]&0x7fffffff; /* no sign for x*/
57 t.i[HIGH_HALF]&=0x7fffffff; /*no sign for y */
58 ky=t.i[HIGH_HALF];
59 /*------ |x| < 2^1023 and 2^-970 < |y| < 2^1024 ------------------*/
60 if (kx<0x7fe00000 && ky<0x7ff00000 && ky>=0x03500000) {
61 if (kx+0x00100000<ky) return x;
62 if ((kx-0x01500000)<ky) {
63 z=x/t.x;
64 v.i[HIGH_HALF]=t.i[HIGH_HALF];
65 d=(z+big.x)-big.x;
66 xx=(x-d*v.x)-d*(t.x-v.x);
67 if (d-z!=0.5&&d-z!=-0.5) return (xx!=0)?xx:((x>0)?ZERO.x:nZERO.x);
68 else {
69 if (ABS(xx)>0.5*t.x) return (z>d)?xx-t.x:xx+t.x;
70 else return xx;
72 } /* (kx<(ky+0x01500000)) */
73 else {
74 r.x=1.0/t.x;
75 n=t.i[HIGH_HALF];
76 nn=(n&0x7ff00000)+0x01400000;
77 w.i[HIGH_HALF]=n;
78 ww.x=t.x-w.x;
79 l=(kx-nn)&0xfff00000;
80 n1=ww.i[HIGH_HALF];
81 m1=r.i[HIGH_HALF];
82 while (l>0) {
83 r.i[HIGH_HALF]=m1-l;
84 z=u.x*r.x;
85 w.i[HIGH_HALF]=n+l;
86 ww.i[HIGH_HALF]=(n1)?n1+l:n1;
87 d=(z+big.x)-big.x;
88 u.x=(u.x-d*w.x)-d*ww.x;
89 l=(u.i[HIGH_HALF]&0x7ff00000)-nn;
91 r.i[HIGH_HALF]=m1;
92 w.i[HIGH_HALF]=n;
93 ww.i[HIGH_HALF]=n1;
94 z=u.x*r.x;
95 d=(z+big.x)-big.x;
96 u.x=(u.x-d*w.x)-d*ww.x;
97 if (ABS(u.x)<0.5*t.x) return (u.x!=0)?u.x:((x>0)?ZERO.x:nZERO.x);
98 else
99 if (ABS(u.x)>0.5*t.x) return (d>z)?u.x+t.x:u.x-t.x;
100 else
101 {z=u.x/t.x; d=(z+big.x)-big.x; return ((u.x-d*w.x)-d*ww.x);}
104 } /* (kx<0x7fe00000&&ky<0x7ff00000&&ky>=0x03500000) */
105 else {
106 if (kx<0x7fe00000&&ky<0x7ff00000&&(ky>0||t.i[LOW_HALF]!=0)) {
107 y=ABS(y)*t128.x;
108 z=__ieee754_remainder(x,y)*t128.x;
109 z=__ieee754_remainder(z,y)*tm128.x;
110 return z;
112 else {
113 if ((kx&0x7ff00000)==0x7fe00000&&ky<0x7ff00000&&(ky>0||t.i[LOW_HALF]!=0)) {
114 y=ABS(y);
115 z=2.0*__ieee754_remainder(0.5*x,y);
116 d = ABS(z);
117 if (d <= ABS(d-y)) return z;
118 else return (z>0)?z-y:z+y;
120 else { /* if x is too big */
121 if (kx == 0x7ff00000 && u.i[LOW_HALF] == 0 && y == 1.0)
122 return x / x;
123 if (kx>=0x7ff00000||(ky==0&&t.i[LOW_HALF]==0)||ky>0x7ff00000||
124 (ky==0x7ff00000&&t.i[LOW_HALF]!=0))
125 return (u.i[HIGH_HALF]&0x80000000)?nNAN.x:NAN.x;
126 else return x;