floor.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Round towards negative infinity.
  2. Copyright (C) 2007, 2010-2017 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
  14. #if ! defined USE_LONG_DOUBLE
  15. # include <config.h>
  16. #endif
  17. /* Specification. */
  18. #include <math.h>
  19. #include <float.h>
  20. #ifdef USE_LONG_DOUBLE
  21. # define FUNC floorl
  22. # define DOUBLE long double
  23. # define MANT_DIG LDBL_MANT_DIG
  24. # define L_(literal) literal##L
  25. #elif ! defined USE_FLOAT
  26. # define FUNC floor
  27. # define DOUBLE double
  28. # define MANT_DIG DBL_MANT_DIG
  29. # define L_(literal) literal
  30. #else /* defined USE_FLOAT */
  31. # define FUNC floorf
  32. # define DOUBLE float
  33. # define MANT_DIG FLT_MANT_DIG
  34. # define L_(literal) literal##f
  35. #endif
  36. /* MSVC with option -fp:strict refuses to compile constant initializers that
  37. contain floating-point operations. Pacify this compiler. */
  38. #ifdef _MSC_VER
  39. # pragma fenv_access (off)
  40. #endif
  41. /* 2^(MANT_DIG-1). */
  42. static const DOUBLE TWO_MANT_DIG =
  43. /* Assume MANT_DIG <= 5 * 31.
  44. Use the identity
  45. n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
  46. (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
  47. * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
  48. * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
  49. * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
  50. * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
  51. DOUBLE
  52. FUNC (DOUBLE x)
  53. {
  54. /* The use of 'volatile' guarantees that excess precision bits are dropped
  55. at each addition step and before the following comparison at the caller's
  56. site. It is necessary on x86 systems where double-floats are not IEEE
  57. compliant by default, to avoid that the results become platform and compiler
  58. option dependent. 'volatile' is a portable alternative to gcc's
  59. -ffloat-store option. */
  60. volatile DOUBLE y = x;
  61. volatile DOUBLE z = y;
  62. if (z > L_(0.0))
  63. {
  64. /* For 0 < x < 1, return +0.0 even if the current rounding mode is
  65. FE_DOWNWARD. */
  66. if (z < L_(1.0))
  67. z = L_(0.0);
  68. /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
  69. else if (z < TWO_MANT_DIG)
  70. {
  71. /* Round to the next integer (nearest or up or down, doesn't matter). */
  72. z += TWO_MANT_DIG;
  73. z -= TWO_MANT_DIG;
  74. /* Enforce rounding down. */
  75. if (z > y)
  76. z -= L_(1.0);
  77. }
  78. }
  79. else if (z < L_(0.0))
  80. {
  81. /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
  82. if (z > - TWO_MANT_DIG)
  83. {
  84. /* Round to the next integer (nearest or up or down, doesn't matter). */
  85. z -= TWO_MANT_DIG;
  86. z += TWO_MANT_DIG;
  87. /* Enforce rounding down. */
  88. if (z > y)
  89. z -= L_(1.0);
  90. }
  91. }
  92. return z;
  93. }