s_remquo.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_remquo.c,v 1.2 2008/03/30 20:47:26 das Exp $");
  14. #include <float.h>
  15. #include <openlibm_math.h>
  16. #include "math_private.h"
  17. static const double Zero[] = {0.0, -0.0,};
  18. /*
  19. * Return the IEEE remainder and set *quo to the last n bits of the
  20. * quotient, rounded to the nearest integer. We choose n=31 because
  21. * we wind up computing all the integer bits of the quotient anyway as
  22. * a side-effect of computing the remainder by the shift and subtract
  23. * method. In practice, this is far more bits than are needed to use
  24. * remquo in reduction algorithms.
  25. */
  26. DLLEXPORT double
  27. remquo(double x, double y, int *quo)
  28. {
  29. int32_t n,hx,hy,hz,ix,iy,sx,i;
  30. u_int32_t lx,ly,lz,q,sxy;
  31. EXTRACT_WORDS(hx,lx,x);
  32. EXTRACT_WORDS(hy,ly,y);
  33. sxy = (hx ^ hy) & 0x80000000;
  34. sx = hx&0x80000000; /* sign of x */
  35. hx ^=sx; /* |x| */
  36. hy &= 0x7fffffff; /* |y| */
  37. /* purge off exception values */
  38. if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */
  39. ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */
  40. return (x*y)/(x*y);
  41. if(hx<=hy) {
  42. if((hx<hy)||(lx<ly)) {
  43. q = 0;
  44. goto fixup; /* |x|<|y| return x or x-y */
  45. }
  46. if(lx==ly) {
  47. *quo = 1;
  48. return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
  49. }
  50. }
  51. /* determine ix = ilogb(x) */
  52. if(hx<0x00100000) { /* subnormal x */
  53. if(hx==0) {
  54. for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
  55. } else {
  56. for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
  57. }
  58. } else ix = (hx>>20)-1023;
  59. /* determine iy = ilogb(y) */
  60. if(hy<0x00100000) { /* subnormal y */
  61. if(hy==0) {
  62. for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
  63. } else {
  64. for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
  65. }
  66. } else iy = (hy>>20)-1023;
  67. /* set up {hx,lx}, {hy,ly} and align y to x */
  68. if(ix >= -1022)
  69. hx = 0x00100000|(0x000fffff&hx);
  70. else { /* subnormal x, shift x to normal */
  71. n = -1022-ix;
  72. if(n<=31) {
  73. hx = (hx<<n)|(lx>>(32-n));
  74. lx <<= n;
  75. } else {
  76. hx = lx<<(n-32);
  77. lx = 0;
  78. }
  79. }
  80. if(iy >= -1022)
  81. hy = 0x00100000|(0x000fffff&hy);
  82. else { /* subnormal y, shift y to normal */
  83. n = -1022-iy;
  84. if(n<=31) {
  85. hy = (hy<<n)|(ly>>(32-n));
  86. ly <<= n;
  87. } else {
  88. hy = ly<<(n-32);
  89. ly = 0;
  90. }
  91. }
  92. /* fix point fmod */
  93. n = ix - iy;
  94. q = 0;
  95. while(n--) {
  96. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  97. if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
  98. else {hx = hz+hz+(lz>>31); lx = lz+lz; q++;}
  99. q <<= 1;
  100. }
  101. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  102. if(hz>=0) {hx=hz;lx=lz;q++;}
  103. /* convert back to floating value and restore the sign */
  104. if((hx|lx)==0) { /* return sign(x)*0 */
  105. *quo = (sxy ? -q : q);
  106. return Zero[(u_int32_t)sx>>31];
  107. }
  108. while(hx<0x00100000) { /* normalize x */
  109. hx = hx+hx+(lx>>31); lx = lx+lx;
  110. iy -= 1;
  111. }
  112. if(iy>= -1022) { /* normalize output */
  113. hx = ((hx-0x00100000)|((iy+1023)<<20));
  114. } else { /* subnormal output */
  115. n = -1022 - iy;
  116. if(n<=20) {
  117. lx = (lx>>n)|((u_int32_t)hx<<(32-n));
  118. hx >>= n;
  119. } else if (n<=31) {
  120. lx = (hx<<(32-n))|(lx>>n); hx = sx;
  121. } else {
  122. lx = hx>>(n-32); hx = sx;
  123. }
  124. }
  125. fixup:
  126. INSERT_WORDS(x,hx,lx);
  127. y = fabs(y);
  128. if (y < 0x1p-1021) {
  129. if (x+x>y || (x+x==y && (q & 1))) {
  130. q++;
  131. x-=y;
  132. }
  133. } else if (x>0.5*y || (x==0.5*y && (q & 1))) {
  134. q++;
  135. x-=y;
  136. }
  137. GET_HIGH_WORD(hx,x);
  138. SET_HIGH_WORD(x,hx^sx);
  139. q &= 0x7fffffff;
  140. *quo = (sxy ? -q : q);
  141. return x;
  142. }
  143. #if LDBL_MANT_DIG == 53
  144. __weak_reference(remquo, remquol);
  145. #endif