123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #include <sys/types.h>
- #include <float.h>
- #include <openlibm_math.h>
- #include <stdint.h>
- #include "math_private.h"
- #define BIAS (LDBL_MAX_EXP - 1)
- #ifdef LDBL_IMPLICIT_NBIT
- #define LDBL_NBIT 0
- #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE))
- #define HFRAC_BITS EXT_FRACHBITS
- #else
- #define LDBL_NBIT 0x80000000
- #define SET_NBIT(hx) (hx)
- #define HFRAC_BITS (EXT_FRACHBITS - 1)
- #endif
- #define MANL_SHIFT (EXT_FRACLBITS - 1)
- static const long double one = 1.0, Zero[] = {0.0, -0.0,};
- long double
- fmodl(long double x, long double y)
- {
- union {
- long double e;
- struct ieee_ext bits;
- } ux, uy;
- int64_t hx,hz;
- uint32_t hy;
- uint32_t lx,ly,lz;
- int ix,iy,n,sx;
- ux.e = x;
- uy.e = y;
- sx = ux.bits.ext_sign;
-
- if((uy.bits.ext_exp|uy.bits.ext_frach|uy.bits.ext_fracl)==0 ||
- (ux.bits.ext_exp == BIAS + LDBL_MAX_EXP) ||
- (uy.bits.ext_exp == BIAS + LDBL_MAX_EXP &&
- ((uy.bits.ext_frach&~LDBL_NBIT)|uy.bits.ext_fracl)!=0))
- return (x*y)/(x*y);
- if(ux.bits.ext_exp<=uy.bits.ext_exp) {
- if((ux.bits.ext_exp<uy.bits.ext_exp) ||
- (ux.bits.ext_frach<=uy.bits.ext_frach &&
- (ux.bits.ext_frach<uy.bits.ext_frach ||
- ux.bits.ext_fracl<uy.bits.ext_fracl))) {
- return x;
- }
- if(ux.bits.ext_frach==uy.bits.ext_frach &&
- ux.bits.ext_fracl==uy.bits.ext_fracl) {
- return Zero[sx];
- }
- }
-
- if(ux.bits.ext_exp == 0) {
- ux.e *= 0x1.0p512;
- ix = ux.bits.ext_exp - (BIAS + 512);
- } else {
- ix = ux.bits.ext_exp - BIAS;
- }
-
- if(uy.bits.ext_exp == 0) {
- uy.e *= 0x1.0p512;
- iy = uy.bits.ext_exp - (BIAS + 512);
- } else {
- iy = uy.bits.ext_exp - BIAS;
- }
-
- hx = SET_NBIT(ux.bits.ext_frach);
- hy = SET_NBIT(uy.bits.ext_frach);
- lx = ux.bits.ext_fracl;
- ly = uy.bits.ext_fracl;
-
- n = ix - iy;
- while(n--) {
- hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
- if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
- else {
- if ((hz|lz)==0)
- return Zero[sx];
- hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz;
- }
- }
- hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
- if(hz>=0) {hx=hz;lx=lz;}
-
- if((hx|lx)==0)
- return Zero[sx];
- while(hx<(1ULL<<HFRAC_BITS)) {
- hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
- iy -= 1;
- }
- ux.bits.ext_frach = hx;
- ux.bits.ext_fracl = lx;
- if (iy < LDBL_MIN_EXP) {
- ux.bits.ext_exp = iy + (BIAS + 512);
- ux.e *= 0x1p-512;
- } else {
- ux.bits.ext_exp = iy + BIAS;
- }
- x = ux.e * one;
- return x;
- }
|