e_fmod.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /*
  12. * __ieee754_fmod(x,y)
  13. * Return x mod y in exact arithmetic
  14. * Method: shift and subtract
  15. */
  16. #include "math.h"
  17. #include "math_private.h"
  18. static const double one = 1.0, Zero[] = {0.0, -0.0,};
  19. double attribute_hidden __ieee754_fmod(double x, double y)
  20. {
  21. int32_t n,hx,hy,hz,ix,iy,sx,i;
  22. u_int32_t lx,ly,lz;
  23. EXTRACT_WORDS(hx,lx,x);
  24. EXTRACT_WORDS(hy,ly,y);
  25. sx = hx&0x80000000; /* sign of x */
  26. hx ^=sx; /* |x| */
  27. hy &= 0x7fffffff; /* |y| */
  28. /* purge off exception values */
  29. if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */
  30. ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */
  31. return (x*y)/(x*y);
  32. if(hx<=hy) {
  33. if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */
  34. if(lx==ly)
  35. return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
  36. }
  37. /* determine ix = ilogb(x) */
  38. if(hx<0x00100000) { /* subnormal x */
  39. if(hx==0) {
  40. for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
  41. } else {
  42. for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
  43. }
  44. } else ix = (hx>>20)-1023;
  45. /* determine iy = ilogb(y) */
  46. if(hy<0x00100000) { /* subnormal y */
  47. if(hy==0) {
  48. for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
  49. } else {
  50. for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
  51. }
  52. } else iy = (hy>>20)-1023;
  53. /* set up {hx,lx}, {hy,ly} and align y to x */
  54. if(ix >= -1022)
  55. hx = 0x00100000|(0x000fffff&hx);
  56. else { /* subnormal x, shift x to normal */
  57. n = -1022-ix;
  58. if(n<=31) {
  59. hx = (hx<<n)|(lx>>(32-n));
  60. lx <<= n;
  61. } else {
  62. hx = lx<<(n-32);
  63. lx = 0;
  64. }
  65. }
  66. if(iy >= -1022)
  67. hy = 0x00100000|(0x000fffff&hy);
  68. else { /* subnormal y, shift y to normal */
  69. n = -1022-iy;
  70. if(n<=31) {
  71. hy = (hy<<n)|(ly>>(32-n));
  72. ly <<= n;
  73. } else {
  74. hy = ly<<(n-32);
  75. ly = 0;
  76. }
  77. }
  78. /* fix point fmod */
  79. n = ix - iy;
  80. while(n--) {
  81. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  82. if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
  83. else {
  84. if((hz|lz)==0) /* return sign(x)*0 */
  85. return Zero[(u_int32_t)sx>>31];
  86. hx = hz+hz+(lz>>31); lx = lz+lz;
  87. }
  88. }
  89. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  90. if(hz>=0) {hx=hz;lx=lz;}
  91. /* convert back to floating value and restore the sign */
  92. if((hx|lx)==0) /* return sign(x)*0 */
  93. return Zero[(u_int32_t)sx>>31];
  94. while(hx<0x00100000) { /* normalize x */
  95. hx = hx+hx+(lx>>31); lx = lx+lx;
  96. iy -= 1;
  97. }
  98. if(iy>= -1022) { /* normalize output */
  99. hx = ((hx-0x00100000)|((iy+1023)<<20));
  100. INSERT_WORDS(x,hx|sx,lx);
  101. } else { /* subnormal output */
  102. n = -1022 - iy;
  103. if(n<=20) {
  104. lx = (lx>>n)|((u_int32_t)hx<<(32-n));
  105. hx >>= n;
  106. } else if (n<=31) {
  107. lx = (hx<<(32-n))|(lx>>n); hx = sx;
  108. } else {
  109. lx = hx>>(n-32); hx = sx;
  110. }
  111. INSERT_WORDS(x,hx|sx,lx);
  112. x *= one; /* create necessary signal */
  113. }
  114. return x; /* exact output */
  115. }