e_fmodl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* @(#)e_fmod.c 1.3 95/01/18 */
  2. /*-
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. #include <sys/types.h>
  13. //#include <machine/ieee.h>
  14. #include <float.h>
  15. #include <openlibm_math.h>
  16. #include <stdint.h>
  17. #include "math_private.h"
  18. #define BIAS (LDBL_MAX_EXP - 1)
  19. /*
  20. * These macros add and remove an explicit integer bit in front of the
  21. * fractional mantissa, if the architecture doesn't have such a bit by
  22. * default already.
  23. */
  24. #ifdef LDBL_IMPLICIT_NBIT
  25. #define LDBL_NBIT 0
  26. #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE))
  27. #define HFRAC_BITS EXT_FRACHBITS
  28. #else
  29. #define LDBL_NBIT 0x80000000
  30. #define SET_NBIT(hx) (hx)
  31. #define HFRAC_BITS (EXT_FRACHBITS - 1)
  32. #endif
  33. #define MANL_SHIFT (EXT_FRACLBITS - 1)
  34. static const long double one = 1.0, Zero[] = {0.0, -0.0,};
  35. /*
  36. * fmodl(x,y)
  37. * Return x mod y in exact arithmetic
  38. * Method: shift and subtract
  39. *
  40. * Assumptions:
  41. * - The low part of the mantissa fits in a manl_t exactly.
  42. * - The high part of the mantissa fits in an int64_t with enough room
  43. * for an explicit integer bit in front of the fractional bits.
  44. */
  45. long double
  46. fmodl(long double x, long double y)
  47. {
  48. union {
  49. long double e;
  50. struct ieee_ext bits;
  51. } ux, uy;
  52. int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
  53. uint32_t hy;
  54. uint32_t lx,ly,lz;
  55. int ix,iy,n,sx;
  56. ux.e = x;
  57. uy.e = y;
  58. sx = ux.bits.ext_sign;
  59. /* purge off exception values */
  60. if((uy.bits.ext_exp|uy.bits.ext_frach|uy.bits.ext_fracl)==0 || /* y=0 */
  61. (ux.bits.ext_exp == BIAS + LDBL_MAX_EXP) || /* or x not finite */
  62. (uy.bits.ext_exp == BIAS + LDBL_MAX_EXP &&
  63. ((uy.bits.ext_frach&~LDBL_NBIT)|uy.bits.ext_fracl)!=0)) /* or y is NaN */
  64. return (x*y)/(x*y);
  65. if(ux.bits.ext_exp<=uy.bits.ext_exp) {
  66. if((ux.bits.ext_exp<uy.bits.ext_exp) ||
  67. (ux.bits.ext_frach<=uy.bits.ext_frach &&
  68. (ux.bits.ext_frach<uy.bits.ext_frach ||
  69. ux.bits.ext_fracl<uy.bits.ext_fracl))) {
  70. return x; /* |x|<|y| return x or x-y */
  71. }
  72. if(ux.bits.ext_frach==uy.bits.ext_frach &&
  73. ux.bits.ext_fracl==uy.bits.ext_fracl) {
  74. return Zero[sx]; /* |x|=|y| return x*0*/
  75. }
  76. }
  77. /* determine ix = ilogb(x) */
  78. if(ux.bits.ext_exp == 0) { /* subnormal x */
  79. ux.e *= 0x1.0p512;
  80. ix = ux.bits.ext_exp - (BIAS + 512);
  81. } else {
  82. ix = ux.bits.ext_exp - BIAS;
  83. }
  84. /* determine iy = ilogb(y) */
  85. if(uy.bits.ext_exp == 0) { /* subnormal y */
  86. uy.e *= 0x1.0p512;
  87. iy = uy.bits.ext_exp - (BIAS + 512);
  88. } else {
  89. iy = uy.bits.ext_exp - BIAS;
  90. }
  91. /* set up {hx,lx}, {hy,ly} and align y to x */
  92. hx = SET_NBIT(ux.bits.ext_frach);
  93. hy = SET_NBIT(uy.bits.ext_frach);
  94. lx = ux.bits.ext_fracl;
  95. ly = uy.bits.ext_fracl;
  96. /* fix point fmod */
  97. n = ix - iy;
  98. while(n--) {
  99. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  100. if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
  101. else {
  102. if ((hz|lz)==0) /* return sign(x)*0 */
  103. return Zero[sx];
  104. hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz;
  105. }
  106. }
  107. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  108. if(hz>=0) {hx=hz;lx=lz;}
  109. /* convert back to floating value and restore the sign */
  110. if((hx|lx)==0) /* return sign(x)*0 */
  111. return Zero[sx];
  112. while(hx<(1ULL<<HFRAC_BITS)) { /* normalize x */
  113. hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
  114. iy -= 1;
  115. }
  116. ux.bits.ext_frach = hx; /* The mantissa is truncated here if needed. */
  117. ux.bits.ext_fracl = lx;
  118. if (iy < LDBL_MIN_EXP) {
  119. ux.bits.ext_exp = iy + (BIAS + 512);
  120. ux.e *= 0x1p-512;
  121. } else {
  122. ux.bits.ext_exp = iy + BIAS;
  123. }
  124. x = ux.e * one; /* create necessary signal */
  125. return x; /* exact output */
  126. }