s_remquol.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "cdefs-compat.h"
  13. //__FBSDID("$FreeBSD: src/lib/msun/src/s_remquol.c,v 1.2 2008/07/31 20:09:47 das Exp $");
  14. #include <float.h>
  15. #include <openlibm_math.h>
  16. #include <stdint.h>
  17. #include "fpmath.h"
  18. #include "math_private.h"
  19. #define BIAS (LDBL_MAX_EXP - 1)
  20. #if LDBL_MANL_SIZE > 32
  21. typedef u_int64_t manl_t;
  22. #else
  23. typedef u_int32_t manl_t;
  24. #endif
  25. #if LDBL_MANH_SIZE > 32
  26. typedef u_int64_t manh_t;
  27. #else
  28. typedef u_int32_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.0L, -0.0L};
  44. /*
  45. * Return the IEEE remainder and set *quo to the last n bits of the
  46. * quotient, rounded to the nearest integer. We choose n=31 because
  47. * we wind up computing all the integer bits of the quotient anyway as
  48. * a side-effect of computing the remainder by the shift and subtract
  49. * method. In practice, this is far more bits than are needed to use
  50. * remquo in reduction algorithms.
  51. *
  52. * Assumptions:
  53. * - The low part of the mantissa fits in a manl_t exactly.
  54. * - The high part of the mantissa fits in an int64_t with enough room
  55. * for an explicit integer bit in front of the fractional bits.
  56. */
  57. OLM_DLLEXPORT long double
  58. remquol(long double x, long double y, int *quo)
  59. {
  60. union IEEEl2bits ux, uy;
  61. int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
  62. manh_t hy;
  63. manl_t lx,ly,lz;
  64. int ix,iy,n,q,sx,sxy;
  65. ux.e = x;
  66. uy.e = y;
  67. sx = ux.bits.sign;
  68. sxy = sx ^ uy.bits.sign;
  69. ux.bits.sign = 0; /* |x| */
  70. uy.bits.sign = 0; /* |y| */
  71. x = ux.e;
  72. /* purge off exception values */
  73. if((uy.bits.exp|uy.bits.manh|uy.bits.manl)==0 || /* y=0 */
  74. (ux.bits.exp == BIAS + LDBL_MAX_EXP) || /* or x not finite */
  75. (uy.bits.exp == BIAS + LDBL_MAX_EXP &&
  76. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* or y is NaN */
  77. return (x*y)/(x*y);
  78. if(ux.bits.exp<=uy.bits.exp) {
  79. if((ux.bits.exp<uy.bits.exp) ||
  80. (ux.bits.manh<=uy.bits.manh &&
  81. (ux.bits.manh<uy.bits.manh ||
  82. ux.bits.manl<uy.bits.manl))) {
  83. q = 0;
  84. goto fixup; /* |x|<|y| return x or x-y */
  85. }
  86. if(ux.bits.manh==uy.bits.manh && ux.bits.manl==uy.bits.manl) {
  87. *quo = 1;
  88. return Zero[sx]; /* |x|=|y| return x*0*/
  89. }
  90. }
  91. /* determine ix = ilogb(x) */
  92. if(ux.bits.exp == 0) { /* subnormal x */
  93. ux.e *= 0x1.0p512;
  94. ix = ux.bits.exp - (BIAS + 512);
  95. } else {
  96. ix = ux.bits.exp - BIAS;
  97. }
  98. /* determine iy = ilogb(y) */
  99. if(uy.bits.exp == 0) { /* subnormal y */
  100. uy.e *= 0x1.0p512;
  101. iy = uy.bits.exp - (BIAS + 512);
  102. } else {
  103. iy = uy.bits.exp - BIAS;
  104. }
  105. /* set up {hx,lx}, {hy,ly} and align y to x */
  106. hx = SET_NBIT(ux.bits.manh);
  107. hy = SET_NBIT(uy.bits.manh);
  108. lx = ux.bits.manl;
  109. ly = uy.bits.manl;
  110. /* fix point fmod */
  111. n = ix - iy;
  112. q = 0;
  113. while(n--) {
  114. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  115. if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
  116. else {hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz; q++;}
  117. q <<= 1;
  118. }
  119. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  120. if(hz>=0) {hx=hz;lx=lz;q++;}
  121. /* convert back to floating value and restore the sign */
  122. if((hx|lx)==0) { /* return sign(x)*0 */
  123. *quo = (sxy ? -q : q);
  124. return Zero[sx];
  125. }
  126. while(hx<(1ULL<<HFRAC_BITS)) { /* normalize x */
  127. hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
  128. iy -= 1;
  129. }
  130. ux.bits.manh = hx; /* The integer bit is truncated here if needed. */
  131. ux.bits.manl = lx;
  132. if (iy < LDBL_MIN_EXP) {
  133. ux.bits.exp = iy + (BIAS + 512);
  134. ux.e *= 0x1p-512;
  135. } else {
  136. ux.bits.exp = iy + BIAS;
  137. }
  138. ux.bits.sign = 0;
  139. x = ux.e;
  140. fixup:
  141. y = fabsl(y);
  142. if (y < LDBL_MIN * 2) {
  143. if (x+x>y || (x+x==y && (q & 1))) {
  144. q++;
  145. x-=y;
  146. }
  147. } else if (x>0.5*y || (x==0.5*y && (q & 1))) {
  148. q++;
  149. x-=y;
  150. }
  151. ux.e = x;
  152. ux.bits.sign ^= sx;
  153. x = ux.e;
  154. q &= 0x7fffffff;
  155. *quo = (sxy ? -q : q);
  156. return x;
  157. }