s_floorl.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. *
  11. * From: @(#)s_floor.c 5.1 93/09/24
  12. */
  13. #include "cdefs-compat.h"
  14. //__FBSDID("$FreeBSD: src/lib/msun/src/s_floorl.c,v 1.8 2008/02/14 15:10:34 bde Exp $");
  15. /*
  16. * floorl(x)
  17. * Return x rounded toward -inf to integral value
  18. * Method:
  19. * Bit twiddling.
  20. * Exception:
  21. * Inexact flag raised if x not equal to floorl(x).
  22. */
  23. #include <float.h>
  24. #include <openlibm.h>
  25. #include <stdint.h>
  26. #include "fpmath.h"
  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
  48. floorl(long double x)
  49. {
  50. union IEEEl2bits u = { .e = x };
  51. int e = u.bits.exp - LDBL_MAX_EXP + 1;
  52. if (e < MANH_SIZE - 1) {
  53. if (e < 0) { /* 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. if (huge + x > 0.0) { /* raise inexact flag */
  71. u.bits.manh &= ~m;
  72. u.bits.manl = 0;
  73. }
  74. }
  75. } else if (e < LDBL_MANT_DIG - 1) {
  76. uint64_t m = (uint64_t)-1 >> (64 - LDBL_MANT_DIG + e + 1);
  77. if ((u.bits.manl & m) == 0)
  78. return (x); /* x is integral */
  79. if (u.bits.sign) {
  80. if (e == MANH_SIZE - 1)
  81. INC_MANH(u, 1);
  82. else {
  83. uint64_t o = u.bits.manl;
  84. u.bits.manl += 1llu << (LDBL_MANT_DIG - e - 1);
  85. if (u.bits.manl < o) /* got a carry */
  86. INC_MANH(u, 1);
  87. }
  88. }
  89. if (huge + x > 0.0) /* raise inexact flag */
  90. u.bits.manl &= ~m;
  91. }
  92. return (u.e);
  93. }