149393c00Smartynas /* @(#)e_fmod.c 1.3 95/01/18 */
249393c00Smartynas /*-
349393c00Smartynas * ====================================================
449393c00Smartynas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
549393c00Smartynas *
649393c00Smartynas * Developed at SunSoft, a Sun Microsystems, Inc. business.
749393c00Smartynas * Permission to use, copy, modify, and distribute this
849393c00Smartynas * software is freely granted, provided that this notice
949393c00Smartynas * is preserved.
1049393c00Smartynas * ====================================================
1149393c00Smartynas */
1249393c00Smartynas
1349393c00Smartynas #include <sys/types.h>
1449393c00Smartynas #include <machine/ieee.h>
1549393c00Smartynas
1649393c00Smartynas #include <float.h>
1749393c00Smartynas #include <math.h>
1849393c00Smartynas #include <stdint.h>
1949393c00Smartynas
2049393c00Smartynas #include "math_private.h"
2149393c00Smartynas
2249393c00Smartynas #define BIAS (LDBL_MAX_EXP - 1)
2349393c00Smartynas
2449393c00Smartynas /*
2549393c00Smartynas * These macros add and remove an explicit integer bit in front of the
2649393c00Smartynas * fractional mantissa, if the architecture doesn't have such a bit by
2749393c00Smartynas * default already.
2849393c00Smartynas */
2949393c00Smartynas #ifdef LDBL_IMPLICIT_NBIT
3049393c00Smartynas #define LDBL_NBIT 0
3149393c00Smartynas #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE))
3249393c00Smartynas #define HFRAC_BITS EXT_FRACHBITS
3349393c00Smartynas #else
3449393c00Smartynas #define LDBL_NBIT 0x80000000
3549393c00Smartynas #define SET_NBIT(hx) (hx)
3649393c00Smartynas #define HFRAC_BITS (EXT_FRACHBITS - 1)
3749393c00Smartynas #endif
3849393c00Smartynas
3949393c00Smartynas #define MANL_SHIFT (EXT_FRACLBITS - 1)
4049393c00Smartynas
4149393c00Smartynas static const long double Zero[] = {0.0L, -0.0L};
4249393c00Smartynas
4349393c00Smartynas /*
4449393c00Smartynas * Return the IEEE remainder and set *quo to the last n bits of the
4549393c00Smartynas * quotient, rounded to the nearest integer. We choose n=31 because
4649393c00Smartynas * we wind up computing all the integer bits of the quotient anyway as
4749393c00Smartynas * a side-effect of computing the remainder by the shift and subtract
4849393c00Smartynas * method. In practice, this is far more bits than are needed to use
4949393c00Smartynas * remquo in reduction algorithms.
5049393c00Smartynas *
5149393c00Smartynas * Assumptions:
5249393c00Smartynas * - The low part of the mantissa fits in a manl_t exactly.
5349393c00Smartynas * - The high part of the mantissa fits in an int64_t with enough room
5449393c00Smartynas * for an explicit integer bit in front of the fractional bits.
5549393c00Smartynas */
5649393c00Smartynas long double
remquol(long double x,long double y,int * quo)5749393c00Smartynas remquol(long double x, long double y, int *quo)
5849393c00Smartynas {
5949393c00Smartynas int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
6049393c00Smartynas uint32_t hy;
6149393c00Smartynas uint32_t lx,ly,lz;
6249393c00Smartynas uint32_t esx, esy;
6349393c00Smartynas int ix,iy,n,q,sx,sxy;
6449393c00Smartynas
6549393c00Smartynas GET_LDOUBLE_WORDS(esx,hx,lx,x);
6649393c00Smartynas GET_LDOUBLE_WORDS(esy,hy,ly,y);
6749393c00Smartynas sx = esx & 0x8000;
6849393c00Smartynas sxy = sx ^ (esy & 0x8000);
6949393c00Smartynas esx &= 0x7fff; /* |x| */
7049393c00Smartynas esy &= 0x7fff; /* |y| */
7149393c00Smartynas SET_LDOUBLE_EXP(x,esx);
7249393c00Smartynas SET_LDOUBLE_EXP(y,esy);
7349393c00Smartynas
7449393c00Smartynas /* purge off exception values */
7549393c00Smartynas if((esy|hy|ly)==0 || /* y=0 */
7649393c00Smartynas (esx == BIAS + LDBL_MAX_EXP) || /* or x not finite */
7749393c00Smartynas (esy == BIAS + LDBL_MAX_EXP &&
7849393c00Smartynas ((hy&~LDBL_NBIT)|ly)!=0)) /* or y is NaN */
7949393c00Smartynas return (x*y)/(x*y);
8049393c00Smartynas if(esx<=esy) {
8149393c00Smartynas if((esx<esy) ||
8249393c00Smartynas (hx<=hy &&
8349393c00Smartynas (hx<hy ||
8449393c00Smartynas lx<ly))) {
8549393c00Smartynas q = 0;
8649393c00Smartynas goto fixup; /* |x|<|y| return x or x-y */
8749393c00Smartynas }
8849393c00Smartynas if(hx==hy && lx==ly) {
8949393c00Smartynas *quo = 1;
9049393c00Smartynas return Zero[sx!=0]; /* |x|=|y| return x*0*/
9149393c00Smartynas }
9249393c00Smartynas }
9349393c00Smartynas
9449393c00Smartynas /* determine ix = ilogb(x) */
9549393c00Smartynas if(esx == 0) { /* subnormal x */
9649393c00Smartynas x *= 0x1.0p512;
9749393c00Smartynas GET_LDOUBLE_WORDS(esx,hx,lx,x);
9849393c00Smartynas ix = esx - (BIAS + 512);
9949393c00Smartynas } else {
10049393c00Smartynas ix = esx - BIAS;
10149393c00Smartynas }
10249393c00Smartynas
10349393c00Smartynas /* determine iy = ilogb(y) */
10449393c00Smartynas if(esy == 0) { /* subnormal y */
10549393c00Smartynas y *= 0x1.0p512;
10649393c00Smartynas GET_LDOUBLE_WORDS(esy,hy,ly,y);
10749393c00Smartynas iy = esy - (BIAS + 512);
10849393c00Smartynas } else {
10949393c00Smartynas iy = esy - BIAS;
11049393c00Smartynas }
11149393c00Smartynas
11249393c00Smartynas /* set up {hx,lx}, {hy,ly} and align y to x */
11349393c00Smartynas hx = SET_NBIT(hx);
11449393c00Smartynas lx = SET_NBIT(lx);
11549393c00Smartynas
11649393c00Smartynas /* fix point fmod */
11749393c00Smartynas n = ix - iy;
11849393c00Smartynas q = 0;
11949393c00Smartynas
12049393c00Smartynas while(n--) {
12149393c00Smartynas hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
12249393c00Smartynas if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
12349393c00Smartynas else {hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz; q++;}
12449393c00Smartynas q <<= 1;
12549393c00Smartynas }
12649393c00Smartynas hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
12749393c00Smartynas if(hz>=0) {hx=hz;lx=lz;q++;}
12849393c00Smartynas
12949393c00Smartynas /* convert back to floating value and restore the sign */
13049393c00Smartynas if((hx|lx)==0) { /* return sign(x)*0 */
13149393c00Smartynas *quo = (sxy ? -q : q);
13249393c00Smartynas return Zero[sx!=0];
13349393c00Smartynas }
13449393c00Smartynas while(hx<(1ULL<<HFRAC_BITS)) { /* normalize x */
13549393c00Smartynas hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
13649393c00Smartynas iy -= 1;
13749393c00Smartynas }
13849393c00Smartynas if (iy < LDBL_MIN_EXP) {
13949393c00Smartynas esx = (iy + BIAS + 512) & 0x7fff;
14049393c00Smartynas SET_LDOUBLE_WORDS(x,esx,hx,lx);
14149393c00Smartynas x *= 0x1p-512;
14249393c00Smartynas GET_LDOUBLE_WORDS(esx,hx,lx,x);
14349393c00Smartynas } else {
14449393c00Smartynas esx = (iy + BIAS) & 0x7fff;
14549393c00Smartynas }
14649393c00Smartynas SET_LDOUBLE_WORDS(x,esx,hx,lx);
14749393c00Smartynas fixup:
14849393c00Smartynas y = fabsl(y);
14949393c00Smartynas if (y < LDBL_MIN * 2) {
15049393c00Smartynas if (x+x>y || (x+x==y && (q & 1))) {
15149393c00Smartynas q++;
15249393c00Smartynas x-=y;
15349393c00Smartynas }
15449393c00Smartynas } else if (x>0.5*y || (x==0.5*y && (q & 1))) {
15549393c00Smartynas q++;
15649393c00Smartynas x-=y;
15749393c00Smartynas }
15849393c00Smartynas
15949393c00Smartynas GET_LDOUBLE_EXP(esx,x);
16049393c00Smartynas esx ^= sx;
16149393c00Smartynas SET_LDOUBLE_EXP(x,esx);
16249393c00Smartynas
16349393c00Smartynas q &= 0x7fffffff;
16449393c00Smartynas *quo = (sxy ? -q : q);
16549393c00Smartynas return x;
16649393c00Smartynas }
167*2f2c0062Sguenther DEF_STD(remquol);
168