s_modf.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. u_int32_t high;
  37. *iptr = x;
  38. GET_HIGH_WORD(high,x);
  39. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  40. return x;
  41. } else {
  42. INSERT_WORDS(*iptr,i0&(~i),0);
  43. return x - *iptr;
  44. }
  45. }
  46. } else if (j0>51) { /* no fraction part */
  47. u_int32_t high;
  48. *iptr = x*one;
  49. GET_HIGH_WORD(high,x);
  50. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  51. return x;
  52. } else { /* fraction part in low x */
  53. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  54. if((i1&i)==0) { /* x is integral */
  55. u_int32_t high;
  56. *iptr = x;
  57. GET_HIGH_WORD(high,x);
  58. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  59. return x;
  60. } else {
  61. INSERT_WORDS(*iptr,i0,i1&(~i));
  62. return x - *iptr;
  63. }
  64. }
  65. }
  66. libm_hidden_def(modf)