e_remainder.c 1.9 KB

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