s_modf.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* @(#)s_modf.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. /*
  13. * modf(double x, double *iptr)
  14. * return fraction part of x, and return x's integral part in *iptr.
  15. * Method:
  16. * Bit twiddling.
  17. *
  18. * Exception:
  19. * No exception.
  20. */
  21. #include "openlibm.h"
  22. #include "math_private.h"
  23. static const double one = 1.0;
  24. double
  25. modf(double x, double *iptr)
  26. {
  27. int32_t i0,i1,j0;
  28. u_int32_t i;
  29. EXTRACT_WORDS(i0,i1,x);
  30. j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
  31. if(j0<20) { /* integer part in high x */
  32. if(j0<0) { /* |x|<1 */
  33. INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
  34. return x;
  35. } else {
  36. i = (0x000fffff)>>j0;
  37. if(((i0&i)|i1)==0) { /* x is integral */
  38. u_int32_t high;
  39. *iptr = x;
  40. GET_HIGH_WORD(high,x);
  41. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  42. return x;
  43. } else {
  44. INSERT_WORDS(*iptr,i0&(~i),0);
  45. return x - *iptr;
  46. }
  47. }
  48. } else if (j0>51) { /* no fraction part */
  49. u_int32_t high;
  50. if (j0 == 0x400) { /* inf/NaN */
  51. *iptr = x;
  52. return 0.0 / x;
  53. }
  54. *iptr = x*one;
  55. GET_HIGH_WORD(high,x);
  56. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  57. return x;
  58. } else { /* fraction part in low x */
  59. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  60. if((i1&i)==0) { /* x is integral */
  61. u_int32_t high;
  62. *iptr = x;
  63. GET_HIGH_WORD(high,x);
  64. INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
  65. return x;
  66. } else {
  67. INSERT_WORDS(*iptr,i0,i1&(~i));
  68. return x - *iptr;
  69. }
  70. }
  71. }