fmodl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_fmodl.c */
  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 "libm.h"
  13. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  14. long double fmodl(long double x, long double y)
  15. {
  16. return fmod(x, y);
  17. }
  18. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  19. #define BIAS (LDBL_MAX_EXP - 1)
  20. #if LDBL_MANL_SIZE > 32
  21. typedef uint64_t manl_t;
  22. #else
  23. typedef uint32_t manl_t;
  24. #endif
  25. #if LDBL_MANH_SIZE > 32
  26. typedef uint64_t manh_t;
  27. #else
  28. typedef uint32_t manh_t;
  29. #endif
  30. /*
  31. * These macros add and remove an explicit integer bit in front of the
  32. * fractional mantissa, if the architecture doesn't have such a bit by
  33. * default already.
  34. */
  35. #ifdef LDBL_IMPLICIT_NBIT
  36. #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE))
  37. #define HFRAC_BITS LDBL_MANH_SIZE
  38. #else
  39. #define SET_NBIT(hx) (hx)
  40. #define HFRAC_BITS (LDBL_MANH_SIZE - 1)
  41. #endif
  42. #define MANL_SHIFT (LDBL_MANL_SIZE - 1)
  43. static const long double Zero[] = {0.0, -0.0,};
  44. /*
  45. * fmodl(x,y)
  46. * Return x mod y in exact arithmetic
  47. * Method: shift and subtract
  48. *
  49. * Assumptions:
  50. * - The low part of the mantissa fits in a manl_t exactly.
  51. * - The high part of the mantissa fits in an int64_t with enough room
  52. * for an explicit integer bit in front of the fractional bits.
  53. */
  54. long double fmodl(long double x, long double y)
  55. {
  56. union IEEEl2bits ux, uy;
  57. int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
  58. manh_t hy;
  59. manl_t lx,ly,lz;
  60. int ix,iy,n,sx;
  61. ux.e = x;
  62. uy.e = y;
  63. sx = ux.bits.sign;
  64. /* purge off exception values */
  65. if ((uy.bits.exp|uy.bits.manh|uy.bits.manl) == 0 || /* y=0 */
  66. ux.bits.exp == BIAS + LDBL_MAX_EXP || /* or x not finite */
  67. (uy.bits.exp == BIAS + LDBL_MAX_EXP &&
  68. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0)) /* or y is NaN */
  69. return (x*y)/(x*y);
  70. if (ux.bits.exp <= uy.bits.exp) {
  71. if (ux.bits.exp < uy.bits.exp ||
  72. (ux.bits.manh<=uy.bits.manh &&
  73. (ux.bits.manh<uy.bits.manh ||
  74. ux.bits.manl<uy.bits.manl))) /* |x|<|y| return x or x-y */
  75. return x;
  76. if (ux.bits.manh==uy.bits.manh && ux.bits.manl==uy.bits.manl)
  77. return Zero[sx]; /* |x| = |y| return x*0 */
  78. }
  79. /* determine ix = ilogb(x) */
  80. if (ux.bits.exp == 0) { /* subnormal x */
  81. ux.e *= 0x1.0p512;
  82. ix = ux.bits.exp - (BIAS + 512);
  83. } else {
  84. ix = ux.bits.exp - BIAS;
  85. }
  86. /* determine iy = ilogb(y) */
  87. if (uy.bits.exp == 0) { /* subnormal y */
  88. uy.e *= 0x1.0p512;
  89. iy = uy.bits.exp - (BIAS + 512);
  90. } else {
  91. iy = uy.bits.exp - BIAS;
  92. }
  93. /* set up {hx,lx}, {hy,ly} and align y to x */
  94. hx = SET_NBIT(ux.bits.manh);
  95. hy = SET_NBIT(uy.bits.manh);
  96. lx = ux.bits.manl;
  97. ly = uy.bits.manl;
  98. /* fix point fmod */
  99. n = ix - iy;
  100. while (n--) {
  101. hz = hx-hy;
  102. lz = lx-ly;
  103. if (lx < ly)
  104. hz -= 1;
  105. if (hz < 0) {
  106. hx = hx+hx+(lx>>MANL_SHIFT);
  107. lx = lx+lx;
  108. } else {
  109. if ((hz|lz)==0) /* return sign(x)*0 */
  110. return Zero[sx];
  111. hx = hz+hz+(lz>>MANL_SHIFT);
  112. lx = lz+lz;
  113. }
  114. }
  115. hz = hx-hy;
  116. lz = lx-ly;
  117. if (lx < ly)
  118. hz -= 1;
  119. if (hz >= 0) {
  120. hx = hz;
  121. lx = lz;
  122. }
  123. /* convert back to floating value and restore the sign */
  124. if ((hx|lx) == 0) /* return sign(x)*0 */
  125. return Zero[sx];
  126. while (hx < (1ULL<<HFRAC_BITS)) { /* normalize x */
  127. hx = hx+hx+(lx>>MANL_SHIFT);
  128. lx = lx+lx;
  129. iy -= 1;
  130. }
  131. ux.bits.manh = hx; /* The mantissa is truncated here if needed. */
  132. ux.bits.manl = lx;
  133. if (iy < LDBL_MIN_EXP) {
  134. ux.bits.exp = iy + (BIAS + 512);
  135. ux.e *= 0x1p-512;
  136. } else {
  137. ux.bits.exp = iy + BIAS;
  138. }
  139. return ux.e; /* exact output */
  140. }
  141. #endif