e_fmod.c 3.5 KB

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