e_fmodf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* e_fmodf.c -- float version of e_fmod.c.
  2. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  3. */
  4. /*
  5. * ====================================================
  6. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  7. *
  8. * Developed at SunPro, a Sun Microsystems, Inc. business.
  9. * Permission to use, copy, modify, and distribute this
  10. * software is freely granted, provided that this notice
  11. * is preserved.
  12. * ====================================================
  13. */
  14. #include "cdefs-compat.h"
  15. //__FBSDID("$FreeBSD: src/lib/msun/src/e_fmodf.c,v 1.7 2008/02/22 02:30:34 das Exp $");
  16. /*
  17. * __ieee754_fmodf(x,y)
  18. * Return x mod y in exact arithmetic
  19. * Method: shift and subtract
  20. */
  21. #include <openlibm_math.h>
  22. #include "math_private.h"
  23. static const float one = 1.0, Zero[] = {0.0, -0.0,};
  24. DLLEXPORT float
  25. __ieee754_fmodf(float x, float y)
  26. {
  27. int32_t n,hx,hy,hz,ix,iy,sx,i;
  28. GET_FLOAT_WORD(hx,x);
  29. GET_FLOAT_WORD(hy,y);
  30. sx = hx&0x80000000; /* sign of x */
  31. hx ^=sx; /* |x| */
  32. hy &= 0x7fffffff; /* |y| */
  33. /* purge off exception values */
  34. if(hy==0||(hx>=0x7f800000)|| /* y=0,or x not finite */
  35. (hy>0x7f800000)) /* or y is NaN */
  36. return (x*y)/(x*y);
  37. if(hx<hy) return x; /* |x|<|y| return x */
  38. if(hx==hy)
  39. return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
  40. /* determine ix = ilogb(x) */
  41. if(hx<0x00800000) { /* subnormal x */
  42. for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
  43. } else ix = (hx>>23)-127;
  44. /* determine iy = ilogb(y) */
  45. if(hy<0x00800000) { /* subnormal y */
  46. for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
  47. } else iy = (hy>>23)-127;
  48. /* set up {hx,lx}, {hy,ly} and align y to x */
  49. if(ix >= -126)
  50. hx = 0x00800000|(0x007fffff&hx);
  51. else { /* subnormal x, shift x to normal */
  52. n = -126-ix;
  53. hx = hx<<n;
  54. }
  55. if(iy >= -126)
  56. hy = 0x00800000|(0x007fffff&hy);
  57. else { /* subnormal y, shift y to normal */
  58. n = -126-iy;
  59. hy = hy<<n;
  60. }
  61. /* fix point fmod */
  62. n = ix - iy;
  63. while(n--) {
  64. hz=hx-hy;
  65. if(hz<0){hx = hx+hx;}
  66. else {
  67. if(hz==0) /* return sign(x)*0 */
  68. return Zero[(u_int32_t)sx>>31];
  69. hx = hz+hz;
  70. }
  71. }
  72. hz=hx-hy;
  73. if(hz>=0) {hx=hz;}
  74. /* convert back to floating value and restore the sign */
  75. if(hx==0) /* return sign(x)*0 */
  76. return Zero[(u_int32_t)sx>>31];
  77. while(hx<0x00800000) { /* normalize x */
  78. hx = hx+hx;
  79. iy -= 1;
  80. }
  81. if(iy>= -126) { /* normalize output */
  82. hx = ((hx-0x00800000)|((iy+127)<<23));
  83. SET_FLOAT_WORD(x,hx|sx);
  84. } else { /* subnormal output */
  85. n = -126 - iy;
  86. hx >>= n;
  87. SET_FLOAT_WORD(x,hx|sx);
  88. x *= one; /* create necessary signal */
  89. }
  90. return x; /* exact output */
  91. }