s_modf.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /*
  12. * modf(double x, double *iptr)
  13. * return fraction part of x, and return x's integral part in *iptr.
  14. * Method:
  15. * Bit twiddling.
  16. *
  17. * Exception:
  18. * No exception.
  19. */
  20. #include "math.h"
  21. #include "math_private.h"
  22. static const double one = 1.0;
  23. double modf(double x, double *iptr)
  24. {
  25. int32_t i0,i1,_j0;
  26. u_int32_t i;
  27. EXTRACT_WORDS(i0,i1,x);
  28. _j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
  29. if(_j0<20) { /* integer part in high x */
  30. if(_j0<0) { /* |x|<1 */
  31. INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
  32. return x;
  33. } else {
  34. i = (0x000fffff)>>_j0;
  35. if(((i0&i)|i1)==0) { /* x is integral */
  36. *iptr = x;
  37. INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */
  38. return x;
  39. } else {
  40. INSERT_WORDS(*iptr,i0&(~i),0);
  41. return x - *iptr;
  42. }
  43. }
  44. } else if (_j0>51) { /* no fraction part */
  45. *iptr = x*one;
  46. /* We must handle NaNs separately. */
  47. if (_j0 == 0x400 && ((i0 & 0xfffff) | i1))
  48. return x*one;
  49. INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */
  50. return x;
  51. } else { /* fraction part in low x */
  52. i = ((u_int32_t)(0xffffffff))>>(_j0-20);
  53. if((i1&i)==0) { /* x is integral */
  54. *iptr = x;
  55. INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */
  56. return x;
  57. } else {
  58. INSERT_WORDS(*iptr,i0,i1&(~i));
  59. return x - *iptr;
  60. }
  61. }
  62. }
  63. libm_hidden_def(modf)