floorl.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_floorl.c */
  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. * floorl(x)
  14. * Return x rounded toward -inf to integral value
  15. * Method:
  16. * Bit twiddling.
  17. * Exception:
  18. * Inexact flag raised if x not equal to floorl(x).
  19. */
  20. #include "libm.h"
  21. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  22. long double floorl(long double x)
  23. {
  24. return floor(x);
  25. }
  26. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  27. #ifdef LDBL_IMPLICIT_NBIT
  28. #define MANH_SIZE (LDBL_MANH_SIZE + 1)
  29. #define INC_MANH(u, c) do { \
  30. uint64_t o = u.bits.manh; \
  31. u.bits.manh += (c); \
  32. if (u.bits.manh < o) \
  33. u.bits.exp++; \
  34. } while (0)
  35. #else
  36. #define MANH_SIZE LDBL_MANH_SIZE
  37. #define INC_MANH(u, c) do { \
  38. uint64_t o = u.bits.manh; \
  39. u.bits.manh += (c); \
  40. if (u.bits.manh < o) { \
  41. u.bits.exp++; \
  42. u.bits.manh |= 1llu << (LDBL_MANH_SIZE - 1); \
  43. } \
  44. } while (0)
  45. #endif
  46. static const long double huge = 1.0e300;
  47. long double floorl(long double x)
  48. {
  49. union IEEEl2bits u = { .e = x };
  50. int e = u.bits.exp - LDBL_MAX_EXP + 1;
  51. if (e < MANH_SIZE - 1) {
  52. if (e < 0) {
  53. /* raise inexact if x != 0 */
  54. if (huge + x > 0.0)
  55. if (u.bits.exp > 0 ||
  56. (u.bits.manh | u.bits.manl) != 0)
  57. u.e = u.bits.sign ? -1.0 : 0.0;
  58. } else {
  59. uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
  60. if (((u.bits.manh & m) | u.bits.manl) == 0)
  61. return x; /* x is integral */
  62. if (u.bits.sign) {
  63. #ifdef LDBL_IMPLICIT_NBIT
  64. if (e == 0)
  65. u.bits.exp++;
  66. else
  67. #endif
  68. INC_MANH(u, 1llu << (MANH_SIZE - e - 1));
  69. }
  70. /* raise inexact flag */
  71. if (huge + x > 0.0) {
  72. u.bits.manh &= ~m;
  73. u.bits.manl = 0;
  74. }
  75. }
  76. } else if (e < LDBL_MANT_DIG - 1) {
  77. uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
  78. if ((u.bits.manl & m) == 0)
  79. return x; /* x is integral */
  80. if (u.bits.sign) {
  81. if (e == MANH_SIZE - 1)
  82. INC_MANH(u, 1);
  83. else {
  84. uint64_t o = u.bits.manl;
  85. u.bits.manl += 1llu << (LDBL_MANT_DIG - e - 1);
  86. if (u.bits.manl < o) /* got a carry */
  87. INC_MANH(u, 1);
  88. }
  89. }
  90. /* raise inexact flag */
  91. if (huge + x > 0.0)
  92. u.bits.manl &= ~m;
  93. }
  94. return u.e;
  95. }
  96. #endif