e_remainder.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include "cdefs-compat.h"
  13. //__FBSDID("$FreeBSD: src/lib/msun/src/e_remainder.c,v 1.12 2008/03/30 20:47:42 das Exp $");
  14. /* __ieee754_remainder(x,p)
  15. * Return :
  16. * returns x REM p = x - [x/p]*p as if in infinite
  17. * precise arithmetic, where [x/p] is the (infinite bit)
  18. * integer nearest x/p (in half way case choose the even one).
  19. * Method :
  20. * Based on fmod() return x-[x/p]chopped*p exactlp.
  21. */
  22. #include <float.h>
  23. #include <openlibm_math.h>
  24. #include "math_private.h"
  25. static const double zero = 0.0;
  26. DLLEXPORT double
  27. __ieee754_remainder(double x, double p)
  28. {
  29. int32_t hx,hp;
  30. u_int32_t sx,lx,lp;
  31. double p_half;
  32. EXTRACT_WORDS(hx,lx,x);
  33. EXTRACT_WORDS(hp,lp,p);
  34. sx = hx&0x80000000;
  35. hp &= 0x7fffffff;
  36. hx &= 0x7fffffff;
  37. /* purge off exception values */
  38. if((hp|lp)==0) return (x*p)/(x*p); /* p = 0 */
  39. if((hx>=0x7ff00000)|| /* x not finite */
  40. ((hp>=0x7ff00000)&& /* p is NaN */
  41. (((hp-0x7ff00000)|lp)!=0)))
  42. return ((long double)x*p)/((long double)x*p);
  43. if (hp<=0x7fdfffff) x = __ieee754_fmod(x,p+p); /* now x < 2p */
  44. if (((hx-hp)|(lx-lp))==0) return zero*x;
  45. x = fabs(x);
  46. p = fabs(p);
  47. if (hp<0x00200000) {
  48. if(x+x>p) {
  49. x-=p;
  50. if(x+x>=p) x -= p;
  51. }
  52. } else {
  53. p_half = 0.5*p;
  54. if(x>p_half) {
  55. x-=p;
  56. if(x>=p_half) x -= p;
  57. }
  58. }
  59. GET_HIGH_WORD(hx,x);
  60. if ((hx&0x7fffffff)==0) hx = 0;
  61. SET_HIGH_WORD(x,hx^sx);
  62. return x;
  63. }
  64. #if LDBL_MANT_DIG == 53
  65. __weak_reference(remainder, remainderl);
  66. #endif