floor.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Round towards negative infinity.
  2. Copyright (C) 2007, 2010-2011 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. #include <config.h>
  15. /* Specification. */
  16. #include <math.h>
  17. #include <float.h>
  18. #ifdef USE_LONG_DOUBLE
  19. # define FUNC floorl
  20. # define DOUBLE long double
  21. # define MANT_DIG LDBL_MANT_DIG
  22. # define L_(literal) literal##L
  23. #elif ! defined USE_FLOAT
  24. # define FUNC floor
  25. # define DOUBLE double
  26. # define MANT_DIG DBL_MANT_DIG
  27. # define L_(literal) literal
  28. #else /* defined USE_FLOAT */
  29. # define FUNC floorf
  30. # define DOUBLE float
  31. # define MANT_DIG FLT_MANT_DIG
  32. # define L_(literal) literal##f
  33. #endif
  34. /* 2^(MANT_DIG-1). */
  35. static const DOUBLE TWO_MANT_DIG =
  36. /* Assume MANT_DIG <= 5 * 31.
  37. Use the identity
  38. n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
  39. (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
  40. * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
  41. * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
  42. * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
  43. * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
  44. DOUBLE
  45. FUNC (DOUBLE x)
  46. {
  47. /* The use of 'volatile' guarantees that excess precision bits are dropped
  48. at each addition step and before the following comparison at the caller's
  49. site. It is necessary on x86 systems where double-floats are not IEEE
  50. compliant by default, to avoid that the results become platform and compiler
  51. option dependent. 'volatile' is a portable alternative to gcc's
  52. -ffloat-store option. */
  53. volatile DOUBLE y = x;
  54. volatile DOUBLE z = y;
  55. if (z > L_(0.0))
  56. {
  57. /* For 0 < x < 1, return +0.0 even if the current rounding mode is
  58. FE_DOWNWARD. */
  59. if (z < L_(1.0))
  60. z = L_(0.0);
  61. /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
  62. else if (z < TWO_MANT_DIG)
  63. {
  64. /* Round to the next integer (nearest or up or down, doesn't matter). */
  65. z += TWO_MANT_DIG;
  66. z -= TWO_MANT_DIG;
  67. /* Enforce rounding down. */
  68. if (z > y)
  69. z -= L_(1.0);
  70. }
  71. }
  72. else if (z < L_(0.0))
  73. {
  74. /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
  75. if (z > - TWO_MANT_DIG)
  76. {
  77. /* Round to the next integer (nearest or up or down, doesn't matter). */
  78. z -= TWO_MANT_DIG;
  79. z += TWO_MANT_DIG;
  80. /* Enforce rounding down. */
  81. if (z > y)
  82. z -= L_(1.0);
  83. }
  84. }
  85. return z;
  86. }