fromfp.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Round to integer type. Common helper functions.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fenv.h>
  17. #include <float.h>
  18. #include <math.h>
  19. #include <math-barriers.h>
  20. #include <stdbool.h>
  21. #include <stdint.h>
  22. /* The including file should have defined UNSIGNED to 0 (signed return
  23. type) or 1 (unsigned return type), INEXACT to 0 (no inexact
  24. exceptions) or 1 (raise inexact exceptions) and RET_TYPE to the
  25. return type (intmax_t or uintmax_t). */
  26. /* Return the maximum unbiased exponent for an argument (negative if
  27. NEGATIVE is set) that might be in range for a call to a fromfp
  28. function with width WIDTH (greater than 0, and not exceeding that
  29. of intmax_t). The truncated argument may still be out of range in
  30. the case of negative arguments, and if not out of range it may
  31. become out of range as a result of rounding. */
  32. static int
  33. fromfp_max_exponent (bool negative, int width)
  34. {
  35. if (UNSIGNED)
  36. return negative ? -1 : width - 1;
  37. else
  38. return negative ? width - 1 : width - 2;
  39. }
  40. /* Return the result of rounding an integer value X (passed as the
  41. absolute value; NEGATIVE is true if the value is negative), where
  42. HALF_BIT is true if the bit with value 0.5 is set and MORE_BITS is
  43. true if any lower bits are set, in the rounding direction
  44. ROUND. */
  45. static uintmax_t
  46. fromfp_round (bool negative, uintmax_t x, bool half_bit, bool more_bits,
  47. int round)
  48. {
  49. switch (round)
  50. {
  51. case FP_INT_UPWARD:
  52. return x + (!negative && (half_bit || more_bits));
  53. case FP_INT_DOWNWARD:
  54. return x + (negative && (half_bit || more_bits));
  55. case FP_INT_TOWARDZERO:
  56. default:
  57. /* Unknown rounding directions are defined to mean unspecified
  58. rounding; treat this as truncation. */
  59. return x;
  60. case FP_INT_TONEARESTFROMZERO:
  61. return x + half_bit;
  62. case FP_INT_TONEAREST:
  63. return x + (half_bit && ((x & 1) || more_bits));
  64. }
  65. }
  66. /* Integer rounding, of a value whose exponent EXPONENT did not exceed
  67. the maximum exponent MAX_EXPONENT and so did not necessarily
  68. overflow, has produced X (possibly wrapping to 0); the sign is
  69. negative if NEGATIVE is true. Return whether this overflowed the
  70. allowed width. */
  71. static bool
  72. fromfp_overflowed (bool negative, uintmax_t x, int exponent, int max_exponent)
  73. {
  74. if (UNSIGNED)
  75. {
  76. if (negative)
  77. return x != 0;
  78. else if (max_exponent == INTMAX_WIDTH - 1)
  79. return exponent == INTMAX_WIDTH - 1 && x == 0;
  80. else
  81. return x == (1ULL << (max_exponent + 1));
  82. }
  83. else
  84. {
  85. if (negative)
  86. return exponent == max_exponent && x != (1ULL << max_exponent);
  87. else
  88. return x == (1ULL << (max_exponent + 1));
  89. }
  90. }
  91. /* Handle a domain error for a call to a fromfp function with an
  92. argument which is negative if NEGATIVE is set, and specified width
  93. (not exceeding that of intmax_t) WIDTH. The return value is
  94. unspecified (with it being unclear if the result needs to fit
  95. within WIDTH bits in this case); we choose to saturate to the given
  96. number of bits (treating NaNs like any other value). */
  97. static RET_TYPE
  98. fromfp_domain_error (bool negative, unsigned int width)
  99. {
  100. feraiseexcept (FE_INVALID);
  101. __set_errno (EDOM);
  102. /* The return value is unspecified; we choose to saturate to the
  103. given number of bits (treating NaNs like any other value). */
  104. if (UNSIGNED)
  105. {
  106. if (negative)
  107. return 0;
  108. else if (width == INTMAX_WIDTH)
  109. return -1;
  110. else
  111. return (1ULL << width) - 1;
  112. }
  113. else
  114. {
  115. if (width == 0)
  116. return 0;
  117. else if (negative)
  118. return -(1ULL << (width - 1));
  119. else
  120. return (1ULL << (width - 1)) - 1;
  121. }
  122. }
  123. /* Given X, the absolute value of a floating-point number (negative if
  124. NEGATIVE is set) truncated towards zero, where HALF_BIT is true if
  125. the bit with value 0.5 is set and MORE_BITS is true if any lower
  126. bits are set, round it in the rounding direction ROUND, handle
  127. errors and exceptions and return the appropriate return value for a
  128. fromfp function. X originally had floating-point exponent
  129. EXPONENT, which does not exceed MAX_EXPONENT, the return value from
  130. fromfp_max_exponent with width WIDTH. */
  131. static RET_TYPE
  132. fromfp_round_and_return (bool negative, uintmax_t x, bool half_bit,
  133. bool more_bits, int round, int exponent,
  134. int max_exponent, unsigned int width)
  135. {
  136. uintmax_t uret = fromfp_round (negative, x, half_bit, more_bits, round);
  137. if (fromfp_overflowed (negative, uret, exponent, max_exponent))
  138. return fromfp_domain_error (negative, width);
  139. if (INEXACT && (half_bit || more_bits))
  140. {
  141. /* There is no need for this to use the specific floating-point
  142. type for which this header is included, and there is no need
  143. for this header to know that type at all, so just use float
  144. here. */
  145. float force_inexact = 1.0f + FLT_MIN;
  146. math_force_eval (force_inexact);
  147. }
  148. if (UNSIGNED)
  149. /* A negative argument not rounding to zero will already have
  150. produced a domain error. */
  151. return uret;
  152. else
  153. return negative ? -uret : uret;
  154. }