e_remainder.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* @(#)e_remainder.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. /* __ieee754_remainder(x,p)
  13. * Return :
  14. * returns x REM p = x - [x/p]*p as if in infinite
  15. * precise arithmetic, where [x/p] is the (infinite bit)
  16. * integer nearest x/p (in half way case choose the even one).
  17. * Method :
  18. * Based on fmod() return x-[x/p]chopped*p exactlp.
  19. */
  20. #include "fdlibm.h"
  21. #ifndef _DOUBLE_IS_32BITS
  22. #ifdef __STDC__
  23. static const double zero = 0.0;
  24. #else
  25. static double zero = 0.0;
  26. #endif
  27. #ifdef __STDC__
  28. double __ieee754_remainder(double x, double p)
  29. #else
  30. double __ieee754_remainder(x,p)
  31. double x,p;
  32. #endif
  33. {
  34. int32_t hx,hp;
  35. uint32_t sx,lx,lp;
  36. double p_half;
  37. EXTRACT_WORDS(hx,lx,x);
  38. EXTRACT_WORDS(hp,lp,p);
  39. sx = hx&0x80000000;
  40. hp &= 0x7fffffff;
  41. hx &= 0x7fffffff;
  42. /* purge off exception values */
  43. if((hp|lp)==0) return (x*p)/(x*p); /* p = 0 */
  44. if((hx>=0x7ff00000)|| /* x not finite */
  45. ((hp>=0x7ff00000)&& /* p is NaN */
  46. (((hp-0x7ff00000)|lp)!=0)))
  47. return (x*p)/(x*p);
  48. if (hp<=0x7fdfffff) x = __ieee754_fmod(x,p+p); /* now x < 2p */
  49. if (((hx-hp)|(lx-lp))==0) return zero*x;
  50. x = fabs(x);
  51. p = fabs(p);
  52. if (hp<0x00200000) {
  53. if(x+x>p) {
  54. x-=p;
  55. if(x+x>=p) x -= p;
  56. }
  57. } else {
  58. p_half = 0.5*p;
  59. if(x>p_half) {
  60. x-=p;
  61. if(x>=p_half) x -= p;
  62. }
  63. }
  64. GET_HIGH_WORD(hx,x);
  65. SET_HIGH_WORD(x,hx ^ sx);
  66. return x;
  67. }
  68. #endif /* defined(_DOUBLE_IS_32BITS) */