105a0b428SJohn Marino /* @(#)e_fmod.c 1.3 95/01/18 */
205a0b428SJohn Marino /*-
305a0b428SJohn Marino * ====================================================
405a0b428SJohn Marino * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
505a0b428SJohn Marino *
605a0b428SJohn Marino * Developed at SunSoft, a Sun Microsystems, Inc. business.
705a0b428SJohn Marino * Permission to use, copy, modify, and distribute this
805a0b428SJohn Marino * software is freely granted, provided that this notice
905a0b428SJohn Marino * is preserved.
1005a0b428SJohn Marino * ====================================================
1105a0b428SJohn Marino */
1205a0b428SJohn Marino
1305a0b428SJohn Marino #include <sys/types.h>
1405a0b428SJohn Marino #include <machine/ieee.h>
1505a0b428SJohn Marino
1605a0b428SJohn Marino #include <float.h>
1705a0b428SJohn Marino #include <math.h>
1805a0b428SJohn Marino #include <stdint.h>
1905a0b428SJohn Marino
2005a0b428SJohn Marino #include "math_private.h"
2105a0b428SJohn Marino
2205a0b428SJohn Marino #define BIAS (LDBL_MAX_EXP - 1)
2305a0b428SJohn Marino
2405a0b428SJohn Marino /*
2505a0b428SJohn Marino * These macros add and remove an explicit integer bit in front of the
2605a0b428SJohn Marino * fractional mantissa, if the architecture doesn't have such a bit by
2705a0b428SJohn Marino * default already.
2805a0b428SJohn Marino */
2905a0b428SJohn Marino #ifdef LDBL_IMPLICIT_NBIT
3005a0b428SJohn Marino #define LDBL_NBIT 0
3105a0b428SJohn Marino #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE))
3205a0b428SJohn Marino #define HFRAC_BITS EXT_FRACHBITS
3305a0b428SJohn Marino #else
3405a0b428SJohn Marino #define LDBL_NBIT 0x80000000
3505a0b428SJohn Marino #define SET_NBIT(hx) (hx)
3605a0b428SJohn Marino #define HFRAC_BITS (EXT_FRACHBITS - 1)
3705a0b428SJohn Marino #endif
3805a0b428SJohn Marino
3905a0b428SJohn Marino #define MANL_SHIFT (EXT_FRACLBITS - 1)
4005a0b428SJohn Marino
4105a0b428SJohn Marino static const long double one = 1.0, Zero[] = {0.0, -0.0,};
4205a0b428SJohn Marino
4305a0b428SJohn Marino /*
4405a0b428SJohn Marino * fmodl(x,y)
4505a0b428SJohn Marino * Return x mod y in exact arithmetic
4605a0b428SJohn Marino * Method: shift and subtract
4705a0b428SJohn Marino *
4805a0b428SJohn Marino * Assumptions:
4905a0b428SJohn Marino * - The low part of the mantissa fits in a manl_t exactly.
5005a0b428SJohn Marino * - The high part of the mantissa fits in an int64_t with enough room
5105a0b428SJohn Marino * for an explicit integer bit in front of the fractional bits.
5205a0b428SJohn Marino */
5305a0b428SJohn Marino long double
fmodl(long double x,long double y)5405a0b428SJohn Marino fmodl(long double x, long double y)
5505a0b428SJohn Marino {
5605a0b428SJohn Marino union {
5705a0b428SJohn Marino long double e;
5805a0b428SJohn Marino struct ieee_ext bits;
5905a0b428SJohn Marino } ux, uy;
6005a0b428SJohn Marino int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
6105a0b428SJohn Marino uint32_t hy;
6205a0b428SJohn Marino uint32_t lx,ly,lz;
6305a0b428SJohn Marino int ix,iy,n,sx;
6405a0b428SJohn Marino
6505a0b428SJohn Marino ux.e = x;
6605a0b428SJohn Marino uy.e = y;
6705a0b428SJohn Marino sx = ux.bits.ext_sign;
6805a0b428SJohn Marino
6905a0b428SJohn Marino /* purge off exception values */
7005a0b428SJohn Marino if((uy.bits.ext_exp|uy.bits.ext_frach|uy.bits.ext_fracl)==0 || /* y=0 */
7105a0b428SJohn Marino (ux.bits.ext_exp == BIAS + LDBL_MAX_EXP) || /* or x not finite */
7205a0b428SJohn Marino (uy.bits.ext_exp == BIAS + LDBL_MAX_EXP &&
7305a0b428SJohn Marino ((uy.bits.ext_frach&~LDBL_NBIT)|uy.bits.ext_fracl)!=0)) /* or y is NaN */
7405a0b428SJohn Marino return (x*y)/(x*y);
7505a0b428SJohn Marino if(ux.bits.ext_exp<=uy.bits.ext_exp) {
7605a0b428SJohn Marino if((ux.bits.ext_exp<uy.bits.ext_exp) ||
7705a0b428SJohn Marino (ux.bits.ext_frach<=uy.bits.ext_frach &&
7805a0b428SJohn Marino (ux.bits.ext_frach<uy.bits.ext_frach ||
7905a0b428SJohn Marino ux.bits.ext_fracl<uy.bits.ext_fracl))) {
8005a0b428SJohn Marino return x; /* |x|<|y| return x or x-y */
8105a0b428SJohn Marino }
8205a0b428SJohn Marino if(ux.bits.ext_frach==uy.bits.ext_frach &&
8305a0b428SJohn Marino ux.bits.ext_fracl==uy.bits.ext_fracl) {
8405a0b428SJohn Marino return Zero[sx]; /* |x|=|y| return x*0*/
8505a0b428SJohn Marino }
8605a0b428SJohn Marino }
8705a0b428SJohn Marino
8805a0b428SJohn Marino /* determine ix = ilogb(x) */
8905a0b428SJohn Marino if(ux.bits.ext_exp == 0) { /* subnormal x */
9005a0b428SJohn Marino ux.e *= 0x1.0p512;
9105a0b428SJohn Marino ix = ux.bits.ext_exp - (BIAS + 512);
9205a0b428SJohn Marino } else {
9305a0b428SJohn Marino ix = ux.bits.ext_exp - BIAS;
9405a0b428SJohn Marino }
9505a0b428SJohn Marino
9605a0b428SJohn Marino /* determine iy = ilogb(y) */
9705a0b428SJohn Marino if(uy.bits.ext_exp == 0) { /* subnormal y */
9805a0b428SJohn Marino uy.e *= 0x1.0p512;
9905a0b428SJohn Marino iy = uy.bits.ext_exp - (BIAS + 512);
10005a0b428SJohn Marino } else {
10105a0b428SJohn Marino iy = uy.bits.ext_exp - BIAS;
10205a0b428SJohn Marino }
10305a0b428SJohn Marino
10405a0b428SJohn Marino /* set up {hx,lx}, {hy,ly} and align y to x */
10505a0b428SJohn Marino hx = SET_NBIT(ux.bits.ext_frach);
10605a0b428SJohn Marino hy = SET_NBIT(uy.bits.ext_frach);
10705a0b428SJohn Marino lx = ux.bits.ext_fracl;
10805a0b428SJohn Marino ly = uy.bits.ext_fracl;
10905a0b428SJohn Marino
11005a0b428SJohn Marino /* fix point fmod */
11105a0b428SJohn Marino n = ix - iy;
11205a0b428SJohn Marino
11305a0b428SJohn Marino while(n--) {
11405a0b428SJohn Marino hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
11505a0b428SJohn Marino if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
11605a0b428SJohn Marino else {
11705a0b428SJohn Marino if ((hz|lz)==0) /* return sign(x)*0 */
11805a0b428SJohn Marino return Zero[sx];
11905a0b428SJohn Marino hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz;
12005a0b428SJohn Marino }
12105a0b428SJohn Marino }
12205a0b428SJohn Marino hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
12305a0b428SJohn Marino if(hz>=0) {hx=hz;lx=lz;}
12405a0b428SJohn Marino
12505a0b428SJohn Marino /* convert back to floating value and restore the sign */
12605a0b428SJohn Marino if((hx|lx)==0) /* return sign(x)*0 */
12705a0b428SJohn Marino return Zero[sx];
128*74b7c7a8SJohn Marino while(hx<(int64_t)(1ULL<<HFRAC_BITS)) { /* normalize x */
12905a0b428SJohn Marino hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
13005a0b428SJohn Marino iy -= 1;
13105a0b428SJohn Marino }
13205a0b428SJohn Marino ux.bits.ext_frach = hx; /* The mantissa is truncated here if needed. */
13305a0b428SJohn Marino ux.bits.ext_fracl = lx;
13405a0b428SJohn Marino if (iy < LDBL_MIN_EXP) {
13505a0b428SJohn Marino ux.bits.ext_exp = iy + (BIAS + 512);
13605a0b428SJohn Marino ux.e *= 0x1p-512;
13705a0b428SJohn Marino } else {
13805a0b428SJohn Marino ux.bits.ext_exp = iy + BIAS;
13905a0b428SJohn Marino }
14005a0b428SJohn Marino x = ux.e * one; /* create necessary signal */
14105a0b428SJohn Marino return x; /* exact output */
14205a0b428SJohn Marino }
143