s_remquof.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_remquof.c,v 1.1 2005/03/25 04:40:44 das Exp $");
  14. #include <openlibm_math.h>
  15. #include "math_private.h"
  16. static const float Zero[] = {0.0, -0.0,};
  17. /*
  18. * Return the IEEE remainder and set *quo to the last n bits of the
  19. * quotient, rounded to the nearest integer. We choose n=31 because
  20. * we wind up computing all the integer bits of the quotient anyway as
  21. * a side-effect of computing the remainder by the shift and subtract
  22. * method. In practice, this is far more bits than are needed to use
  23. * remquo in reduction algorithms.
  24. */
  25. DLLEXPORT float
  26. remquof(float x, float y, int *quo)
  27. {
  28. int32_t n,hx,hy,hz,ix,iy,sx,i;
  29. u_int32_t q,sxy;
  30. GET_FLOAT_WORD(hx,x);
  31. GET_FLOAT_WORD(hy,y);
  32. sxy = (hx ^ hy) & 0x80000000;
  33. sx = hx&0x80000000; /* sign of x */
  34. hx ^=sx; /* |x| */
  35. hy &= 0x7fffffff; /* |y| */
  36. /* purge off exception values */
  37. if(hy==0||hx>=0x7f800000||hy>0x7f800000) /* y=0,NaN;or x not finite */
  38. return (x*y)/(x*y);
  39. if(hx<hy) {
  40. q = 0;
  41. goto fixup; /* |x|<|y| return x or x-y */
  42. } else if(hx==hy) {
  43. *quo = 1;
  44. return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
  45. }
  46. /* determine ix = ilogb(x) */
  47. if(hx<0x00800000) { /* subnormal x */
  48. for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
  49. } else ix = (hx>>23)-127;
  50. /* determine iy = ilogb(y) */
  51. if(hy<0x00800000) { /* subnormal y */
  52. for (iy = -126,i=(hy<<8); i>0; i<<=1) iy -=1;
  53. } else iy = (hy>>23)-127;
  54. /* set up {hx,lx}, {hy,ly} and align y to x */
  55. if(ix >= -126)
  56. hx = 0x00800000|(0x007fffff&hx);
  57. else { /* subnormal x, shift x to normal */
  58. n = -126-ix;
  59. hx <<= n;
  60. }
  61. if(iy >= -126)
  62. hy = 0x00800000|(0x007fffff&hy);
  63. else { /* subnormal y, shift y to normal */
  64. n = -126-iy;
  65. hy <<= n;
  66. }
  67. /* fix point fmod */
  68. n = ix - iy;
  69. q = 0;
  70. while(n--) {
  71. hz=hx-hy;
  72. if(hz<0) hx = hx << 1;
  73. else {hx = hz << 1; q++;}
  74. q <<= 1;
  75. }
  76. hz=hx-hy;
  77. if(hz>=0) {hx=hz;q++;}
  78. /* convert back to floating value and restore the sign */
  79. if(hx==0) { /* return sign(x)*0 */
  80. *quo = (sxy ? -q : q);
  81. return Zero[(u_int32_t)sx>>31];
  82. }
  83. while(hx<0x00800000) { /* normalize x */
  84. hx <<= 1;
  85. iy -= 1;
  86. }
  87. if(iy>= -126) { /* normalize output */
  88. hx = ((hx-0x00800000)|((iy+127)<<23));
  89. } else { /* subnormal output */
  90. n = -126 - iy;
  91. hx >>= n;
  92. }
  93. fixup:
  94. SET_FLOAT_WORD(x,hx);
  95. y = fabsf(y);
  96. if (y < 0x1p-125f) {
  97. if (x+x>y || (x+x==y && (q & 1))) {
  98. q++;
  99. x-=y;
  100. }
  101. } else if (x>0.5f*y || (x==0.5f*y && (q & 1))) {
  102. q++;
  103. x-=y;
  104. }
  105. GET_FLOAT_WORD(hx,x);
  106. SET_FLOAT_WORD(x,hx^sx);
  107. q &= 0x7fffffff;
  108. *quo = (sxy ? -q : q);
  109. return x;
  110. }