1 /* fmodq.c -- __float128 version of e_fmod.c.
2 * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
5 * ====================================================
6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 * Developed at SunPro, a Sun Microsystems, Inc. business.
9 * Permission to use, copy, modify, and distribute this
10 * software is freely granted, provided that this notice
12 * ====================================================
17 * Return x mod y in exact arithmetic
18 * Method: shift and subtract
21 #include "quadmath-imp.h"
23 static const __float128 one = 1.0, Zero[] = {0.0, -0.0,};
26 fmodq (__float128 x, __float128 y)
28 int64_t n,hx,hy,hz,ix,iy,sx,i;
31 GET_FLT128_WORDS64(hx,lx,x);
32 GET_FLT128_WORDS64(hy,ly,y);
33 sx = hx&0x8000000000000000ULL; /* sign of x */
35 hy &= 0x7fffffffffffffffLL; /* |y| */
37 /* purge off exception values */
38 if((hy|ly)==0||(hx>=0x7fff000000000000LL)|| /* y=0,or x not finite */
39 ((hy|((ly|-ly)>>63))>0x7fff000000000000LL)) /* or y is NaN */
42 if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */
44 return Zero[(uint64_t)sx>>63]; /* |x|=|y| return x*0*/
47 /* determine ix = ilogb(x) */
48 if(hx<0x0001000000000000LL) { /* subnormal x */
50 for (ix = -16431, i=lx; i>0; i<<=1) ix -=1;
52 for (ix = -16382, i=hx<<15; i>0; i<<=1) ix -=1;
54 } else ix = (hx>>48)-0x3fff;
56 /* determine iy = ilogb(y) */
57 if(hy<0x0001000000000000LL) { /* subnormal y */
59 for (iy = -16431, i=ly; i>0; i<<=1) iy -=1;
61 for (iy = -16382, i=hy<<15; i>0; i<<=1) iy -=1;
63 } else iy = (hy>>48)-0x3fff;
65 /* set up {hx,lx}, {hy,ly} and align y to x */
67 hx = 0x0001000000000000LL|(0x0000ffffffffffffLL&hx);
68 else { /* subnormal x, shift x to normal */
71 hx = (hx<<n)|(lx>>(64-n));
79 hy = 0x0001000000000000LL|(0x0000ffffffffffffLL&hy);
80 else { /* subnormal y, shift y to normal */
83 hy = (hy<<n)|(ly>>(64-n));
94 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
95 if(hz<0){hx = hx+hx+(lx>>63); lx = lx+lx;}
97 if((hz|lz)==0) /* return sign(x)*0 */
98 return Zero[(uint64_t)sx>>63];
99 hx = hz+hz+(lz>>63); lx = lz+lz;
102 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
103 if(hz>=0) {hx=hz;lx=lz;}
105 /* convert back to floating value and restore the sign */
106 if((hx|lx)==0) /* return sign(x)*0 */
107 return Zero[(uint64_t)sx>>63];
108 while(hx<0x0001000000000000LL) { /* normalize x */
109 hx = hx+hx+(lx>>63); lx = lx+lx;
112 if(iy>= -16382) { /* normalize output */
113 hx = ((hx-0x0001000000000000LL)|((iy+16383)<<48));
114 SET_FLT128_WORDS64(x,hx|sx,lx);
115 } else { /* subnormal output */
118 lx = (lx>>n)|((uint64_t)hx<<(64-n));
121 lx = (hx<<(64-n))|(lx>>n); hx = sx;
123 lx = hx>>(n-64); hx = sx;
125 SET_FLT128_WORDS64(x,hx|sx,lx);
126 x *= one; /* create necessary signal */
128 return x; /* exact output */