modfq.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* s_modfl.c -- long double version of s_modf.c.
  2. * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
  3. */
  4. /*
  5. * ====================================================
  6. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  7. *
  8. * Developed at SunPro, a Sun Microsystems, Inc. business.
  9. * Permission to use, copy, modify, and distribute this
  10. * software is freely granted, provided that this notice
  11. * is preserved.
  12. * ====================================================
  13. */
  14. #if defined(LIBM_SCCS) && !defined(lint)
  15. static char rcsid[] = "$NetBSD: $";
  16. #endif
  17. /*
  18. * modfq(long double x, long double *iptr)
  19. * return fraction part of x, and return x's integral part in *iptr.
  20. * Method:
  21. * Bit twiddling.
  22. *
  23. * Exception:
  24. * No exception.
  25. */
  26. #include "quadmath-imp.h"
  27. static const __float128 one = 1.0;
  28. __float128 modfq(__float128 x, __float128 *iptr)
  29. {
  30. int64_t i0,i1,j0;
  31. uint64_t i;
  32. GET_FLT128_WORDS64(i0,i1,x);
  33. j0 = ((i0>>48)&0x7fff)-0x3fff; /* exponent of x */
  34. if(j0<48) { /* integer part in high x */
  35. if(j0<0) { /* |x|<1 */
  36. /* *iptr = +-0 */
  37. SET_FLT128_WORDS64(*iptr,i0&0x8000000000000000ULL,0);
  38. return x;
  39. } else {
  40. i = (0x0000ffffffffffffLL)>>j0;
  41. if(((i0&i)|i1)==0) { /* x is integral */
  42. *iptr = x;
  43. /* return +-0 */
  44. SET_FLT128_WORDS64(x,i0&0x8000000000000000ULL,0);
  45. return x;
  46. } else {
  47. SET_FLT128_WORDS64(*iptr,i0&(~i),0);
  48. return x - *iptr;
  49. }
  50. }
  51. } else if (j0>111) { /* no fraction part */
  52. *iptr = x*one;
  53. /* We must handle NaNs separately. */
  54. if (j0 == 0x4000 && ((i0 & 0x0000ffffffffffffLL) | i1))
  55. return x*one;
  56. /* return +-0 */
  57. SET_FLT128_WORDS64(x,i0&0x8000000000000000ULL,0);
  58. return x;
  59. } else { /* fraction part in low x */
  60. i = -1ULL>>(j0-48);
  61. if((i1&i)==0) { /* x is integral */
  62. *iptr = x;
  63. /* return +-0 */
  64. SET_FLT128_WORDS64(x,i0&0x8000000000000000ULL,0);
  65. return x;
  66. } else {
  67. SET_FLT128_WORDS64(*iptr,i0,i1&(~i));
  68. return x - *iptr;
  69. }
  70. }
  71. }