e_fmod.c 3.5 KB

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