e_fmod.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/e_fmod.c,v 1.10 2008/02/22 02:30:34 das Exp $");
  14. /*
  15. * __ieee754_fmod(x,y)
  16. * Return x mod y in exact arithmetic
  17. * Method: shift and subtract
  18. */
  19. #include <openlibm.h>
  20. #include "math_private.h"
  21. static const double one = 1.0, Zero[] = {0.0, -0.0,};
  22. DLLEXPORT double
  23. __ieee754_fmod(double x, double y)
  24. {
  25. int32_t n,hx,hy,hz,ix,iy,sx,i;
  26. u_int32_t lx,ly,lz;
  27. EXTRACT_WORDS(hx,lx,x);
  28. EXTRACT_WORDS(hy,ly,y);
  29. sx = hx&0x80000000; /* sign of x */
  30. hx ^=sx; /* |x| */
  31. hy &= 0x7fffffff; /* |y| */
  32. /* purge off exception values */
  33. if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */
  34. ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */
  35. return (x*y)/(x*y);
  36. if(hx<=hy) {
  37. if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */
  38. if(lx==ly)
  39. return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
  40. }
  41. /* determine ix = ilogb(x) */
  42. if(hx<0x00100000) { /* subnormal x */
  43. if(hx==0) {
  44. for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
  45. } else {
  46. for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
  47. }
  48. } else ix = (hx>>20)-1023;
  49. /* determine iy = ilogb(y) */
  50. if(hy<0x00100000) { /* subnormal y */
  51. if(hy==0) {
  52. for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
  53. } else {
  54. for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
  55. }
  56. } else iy = (hy>>20)-1023;
  57. /* set up {hx,lx}, {hy,ly} and align y to x */
  58. if(ix >= -1022)
  59. hx = 0x00100000|(0x000fffff&hx);
  60. else { /* subnormal x, shift x to normal */
  61. n = -1022-ix;
  62. if(n<=31) {
  63. hx = (hx<<n)|(lx>>(32-n));
  64. lx <<= n;
  65. } else {
  66. hx = lx<<(n-32);
  67. lx = 0;
  68. }
  69. }
  70. if(iy >= -1022)
  71. hy = 0x00100000|(0x000fffff&hy);
  72. else { /* subnormal y, shift y to normal */
  73. n = -1022-iy;
  74. if(n<=31) {
  75. hy = (hy<<n)|(ly>>(32-n));
  76. ly <<= n;
  77. } else {
  78. hy = ly<<(n-32);
  79. ly = 0;
  80. }
  81. }
  82. /* fix point fmod */
  83. n = ix - iy;
  84. while(n--) {
  85. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  86. if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
  87. else {
  88. if((hz|lz)==0) /* return sign(x)*0 */
  89. return Zero[(u_int32_t)sx>>31];
  90. hx = hz+hz+(lz>>31); lx = lz+lz;
  91. }
  92. }
  93. hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  94. if(hz>=0) {hx=hz;lx=lz;}
  95. /* convert back to floating value and restore the sign */
  96. if((hx|lx)==0) /* return sign(x)*0 */
  97. return Zero[(u_int32_t)sx>>31];
  98. while(hx<0x00100000) { /* normalize x */
  99. hx = hx+hx+(lx>>31); lx = lx+lx;
  100. iy -= 1;
  101. }
  102. if(iy>= -1022) { /* normalize output */
  103. hx = ((hx-0x00100000)|((iy+1023)<<20));
  104. INSERT_WORDS(x,hx|sx,lx);
  105. } else { /* subnormal output */
  106. n = -1022 - iy;
  107. if(n<=20) {
  108. lx = (lx>>n)|((u_int32_t)hx<<(32-n));
  109. hx >>= n;
  110. } else if (n<=31) {
  111. lx = (hx<<(32-n))|(lx>>n); hx = sx;
  112. } else {
  113. lx = hx>>(n-32); hx = sx;
  114. }
  115. INSERT_WORDS(x,hx|sx,lx);
  116. x *= one; /* create necessary signal */
  117. }
  118. return x; /* exact output */
  119. }