ceil.c 3.3 KB

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