s_modfl.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. int32_t i0,i1,jj0;
  28. u_int32_t i,se;
  29. GET_LDOUBLE_WORDS(se,i0,i1,x);
  30. jj0 = (se&0x7fff)-0x3fff; /* exponent of x */
  31. if(jj0<32) { /* integer part in high x */
  32. if(jj0<0) { /* |x|<1 */
  33. SET_LDOUBLE_WORDS(*iptr,se&0x8000,0,0); /* *iptr = +-0 */
  34. return x;
  35. } else {
  36. i = (0x7fffffff)>>jj0;
  37. if(((i0&i)|i1)==0) { /* x is integral */
  38. *iptr = x;
  39. SET_LDOUBLE_WORDS(x,se&0x8000,0,0); /* return +-0 */
  40. return x;
  41. } else {
  42. SET_LDOUBLE_WORDS(*iptr,se,i0&(~i),0);
  43. return x - *iptr;
  44. }
  45. }
  46. } else if (jj0>63) { /* no fraction part */
  47. *iptr = x*one;
  48. /* We must handle NaNs separately. */
  49. if (jj0 == 0x4000 && ((i0 & 0x7fffffff) | i1))
  50. return x*one;
  51. SET_LDOUBLE_WORDS(x,se&0x8000,0,0); /* return +-0 */
  52. return x;
  53. } else { /* fraction part in low x */
  54. i = ((u_int32_t)(0x7fffffff))>>(jj0-32);
  55. if((i1&i)==0) { /* x is integral */
  56. *iptr = x;
  57. SET_LDOUBLE_WORDS(x,se&0x8000,0,0); /* return +-0 */
  58. return x;
  59. } else {
  60. SET_LDOUBLE_WORDS(*iptr,se,i0,i1&(~i));
  61. return x - *iptr;
  62. }
  63. }
  64. }