s_floorl.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_math.h>
  25. #include <stdint.h>
  26. #include "fpmath.h"
  27. #include "math_private.h"
  28. #ifdef LDBL_IMPLICIT_NBIT
  29. #define MANH_SIZE (LDBL_MANH_SIZE + 1)
  30. #define INC_MANH(u, c) do { \
  31. uint64_t o = u.bits.manh; \
  32. u.bits.manh += (c); \
  33. if (u.bits.manh < o) \
  34. u.bits.exp++; \
  35. } while (0)
  36. #else
  37. #define MANH_SIZE LDBL_MANH_SIZE
  38. #define INC_MANH(u, c) do { \
  39. uint64_t o = u.bits.manh; \
  40. u.bits.manh += (c); \
  41. if (u.bits.manh < o) { \
  42. u.bits.exp++; \
  43. u.bits.manh |= 1llu << (LDBL_MANH_SIZE - 1); \
  44. } \
  45. } while (0)
  46. #endif
  47. static const long double huge = 1.0e300;
  48. DLLEXPORT long double
  49. floorl(long double x)
  50. {
  51. union IEEEl2bits u = { .e = x };
  52. int e = u.bits.exp - LDBL_MAX_EXP + 1;
  53. if (e < MANH_SIZE - 1) {
  54. if (e < 0) { /* raise inexact if x != 0 */
  55. if (huge + x > 0.0)
  56. if (u.bits.exp > 0 ||
  57. (u.bits.manh | u.bits.manl) != 0)
  58. u.e = u.bits.sign ? -1.0 : 0.0;
  59. } else {
  60. uint64_t m = ((1llu << MANH_SIZE) - 1) >> (e + 1);
  61. if (((u.bits.manh & m) | u.bits.manl) == 0)
  62. return (x); /* x is integral */
  63. if (u.bits.sign) {
  64. #ifdef LDBL_IMPLICIT_NBIT
  65. if (e == 0)
  66. u.bits.exp++;
  67. else
  68. #endif
  69. INC_MANH(u, 1llu << (MANH_SIZE - e - 1));
  70. }
  71. if (huge + x > 0.0) { /* raise inexact flag */
  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. if (huge + x > 0.0) /* raise inexact flag */
  91. u.bits.manl &= ~m;
  92. }
  93. return (u.e);
  94. }