trunc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Round towards zero.
  2. Copyright (C) 2007, 2010-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This file 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 <https://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. #undef MIN
  21. #ifdef USE_LONG_DOUBLE
  22. # define FUNC truncl
  23. # define DOUBLE long double
  24. # define MANT_DIG LDBL_MANT_DIG
  25. # define MIN LDBL_MIN
  26. # define L_(literal) literal##L
  27. #elif ! defined USE_FLOAT
  28. # define FUNC trunc
  29. # define DOUBLE double
  30. # define MANT_DIG DBL_MANT_DIG
  31. # define MIN DBL_MIN
  32. # define L_(literal) literal
  33. #else /* defined USE_FLOAT */
  34. # define FUNC truncf
  35. # define DOUBLE float
  36. # define MANT_DIG FLT_MANT_DIG
  37. # define MIN FLT_MIN
  38. # define L_(literal) literal##f
  39. #endif
  40. /* -0.0. See minus-zero.h. */
  41. #if defined __hpux || defined __sgi || defined __ICC
  42. # define MINUS_ZERO (-MIN * MIN)
  43. #else
  44. # define MINUS_ZERO L_(-0.0)
  45. #endif
  46. /* MSVC with option -fp:strict refuses to compile constant initializers that
  47. contain floating-point operations. Pacify this compiler. */
  48. #if defined _MSC_VER && !defined __clang__
  49. # pragma fenv_access (off)
  50. #endif
  51. /* 2^(MANT_DIG-1). */
  52. static const DOUBLE TWO_MANT_DIG =
  53. /* Assume MANT_DIG <= 5 * 31.
  54. Use the identity
  55. n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */
  56. (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
  57. * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
  58. * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
  59. * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
  60. * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
  61. DOUBLE
  62. FUNC (DOUBLE x)
  63. {
  64. /* The use of 'volatile' guarantees that excess precision bits are dropped
  65. at each addition step and before the following comparison at the caller's
  66. site. It is necessary on x86 systems where double-floats are not IEEE
  67. compliant by default, to avoid that the results become platform and compiler
  68. option dependent. 'volatile' is a portable alternative to gcc's
  69. -ffloat-store option. */
  70. volatile DOUBLE y = x;
  71. volatile DOUBLE z = y;
  72. if (z > L_(0.0))
  73. {
  74. /* For 0 < x < 1, return +0.0 even if the current rounding mode is
  75. FE_DOWNWARD. */
  76. if (z < L_(1.0))
  77. z = L_(0.0);
  78. /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */
  79. else if (z < TWO_MANT_DIG)
  80. {
  81. /* Round to the next integer (nearest or up or down, doesn't matter). */
  82. z += TWO_MANT_DIG;
  83. z -= TWO_MANT_DIG;
  84. /* Enforce rounding down. */
  85. if (z > y)
  86. z -= L_(1.0);
  87. }
  88. }
  89. else if (z < L_(0.0))
  90. {
  91. /* For -1 < x < 0, return -0.0 regardless of the current rounding
  92. mode. */
  93. if (z > L_(-1.0))
  94. z = MINUS_ZERO;
  95. /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */
  96. else if (z > - TWO_MANT_DIG)
  97. {
  98. /* Round to the next integer (nearest or up or down, doesn't matter). */
  99. z -= TWO_MANT_DIG;
  100. z += TWO_MANT_DIG;
  101. /* Enforce rounding up. */
  102. if (z < y)
  103. z += L_(1.0);
  104. }
  105. }
  106. return z;
  107. }