modfl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_modfl.c */
  2. /*-
  3. * Copyright (c) 2007 David Schultz <[email protected]>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. * Derived from s_modf.c, which has the following Copyright:
  28. * ====================================================
  29. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  30. *
  31. * Developed at SunPro, a Sun Microsystems, Inc. business.
  32. * Permission to use, copy, modify, and distribute this
  33. * software is freely granted, provided that this notice
  34. * is preserved.
  35. * ====================================================
  36. */
  37. #include "libm.h"
  38. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  39. long double modfl(long double x, long double *iptr)
  40. {
  41. return modf(x, (double *)iptr);
  42. }
  43. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  44. #if LDBL_MANL_SIZE > 32
  45. #define MASK ((uint64_t)-1)
  46. #else
  47. #define MASK ((uint32_t)-1)
  48. #endif
  49. /* Return the last n bits of a word, representing the fractional part. */
  50. #define GETFRAC(bits, n) ((bits) & ~(MASK << (n)))
  51. /* The number of fraction bits in manh, not counting the integer bit */
  52. #define HIBITS (LDBL_MANT_DIG - LDBL_MANL_SIZE)
  53. static const long double zero[] = { 0.0, -0.0 };
  54. long double modfl(long double x, long double *iptr)
  55. {
  56. union IEEEl2bits ux;
  57. int e;
  58. ux.e = x;
  59. e = ux.bits.exp - LDBL_MAX_EXP + 1;
  60. if (e < HIBITS) { /* Integer part is in manh. */
  61. if (e < 0) { /* |x|<1 */
  62. *iptr = zero[ux.bits.sign];
  63. return x;
  64. }
  65. if ((GETFRAC(ux.bits.manh, HIBITS - 1 - e)|ux.bits.manl) == 0) {
  66. /* x is an integer. */
  67. *iptr = x;
  68. return zero[ux.bits.sign];
  69. }
  70. /* Clear all but the top e+1 bits. */
  71. ux.bits.manh >>= HIBITS - 1 - e;
  72. ux.bits.manh <<= HIBITS - 1 - e;
  73. ux.bits.manl = 0;
  74. *iptr = ux.e;
  75. return x - ux.e;
  76. } else if (e >= LDBL_MANT_DIG - 1) { /* x has no fraction part. */
  77. *iptr = x;
  78. if (e == LDBL_MAX_EXP && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)) /* nan */
  79. return x;
  80. return zero[ux.bits.sign];
  81. } else { /* Fraction part is in manl. */
  82. if (GETFRAC(ux.bits.manl, LDBL_MANT_DIG - 1 - e) == 0) {
  83. /* x is integral. */
  84. *iptr = x;
  85. return zero[ux.bits.sign];
  86. }
  87. /* Clear all but the top e+1 bits. */
  88. ux.bits.manl >>= LDBL_MANT_DIG - 1 - e;
  89. ux.bits.manl <<= LDBL_MANT_DIG - 1 - e;
  90. *iptr = ux.e;
  91. return x - ux.e;
  92. }
  93. }
  94. #endif