s_modfl.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * modfl(long double x, long 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_math.h>
  22. #include "math_private.h"
  23. static const long double one = 1.0;
  24. long double
  25. modfl(long double x, long double *iptr)
  26. {
  27. int64_t i0,i1,jj0;
  28. u_int64_t i;
  29. GET_LDOUBLE_WORDS64(i0,i1,x);
  30. jj0 = ((i0>>48)&0x7fff)-0x3fff; /* exponent of x */
  31. if(jj0<48) { /* integer part in high x */
  32. if(jj0<0) { /* |x|<1 */
  33. /* *iptr = +-0 */
  34. SET_LDOUBLE_WORDS64(*iptr,i0&0x8000000000000000ULL,0);
  35. return x;
  36. } else {
  37. i = (0x0000ffffffffffffLL)>>jj0;
  38. if(((i0&i)|i1)==0) { /* x is integral */
  39. *iptr = x;
  40. /* return +-0 */
  41. SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0);
  42. return x;
  43. } else {
  44. SET_LDOUBLE_WORDS64(*iptr,i0&(~i),0);
  45. return x - *iptr;
  46. }
  47. }
  48. } else if (jj0>111) { /* no fraction part */
  49. *iptr = x*one;
  50. /* We must handle NaNs separately. */
  51. if (jj0 == 0x4000 && ((i0 & 0x0000ffffffffffffLL) | i1))
  52. return x*one;
  53. /* return +-0 */
  54. SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0);
  55. return x;
  56. } else { /* fraction part in low x */
  57. i = -1ULL>>(jj0-48);
  58. if((i1&i)==0) { /* x is integral */
  59. *iptr = x;
  60. /* return +-0 */
  61. SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0);
  62. return x;
  63. } else {
  64. SET_LDOUBLE_WORDS64(*iptr,i0,i1&(~i));
  65. return x - *iptr;
  66. }
  67. }
  68. }