test-math-iszero.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Test for the C++ implementation of iszero.
  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. #define _GNU_SOURCE 1
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include <limits>
  19. /* Support for _Float128 in std::numeric_limits is limited.
  20. Include ieee754_float128.h and use the bitfields in the union
  21. ieee854_float128.ieee_nan to build corner-case inputs. */
  22. #if __HAVE_DISTINCT_FLOAT128
  23. # include <ieee754_float128.h>
  24. #endif
  25. static bool errors;
  26. static void
  27. check (int actual, int expected, const char *actual_expr, int line)
  28. {
  29. if (actual != expected)
  30. {
  31. errors = true;
  32. printf ("%s:%d: error: %s\n", __FILE__, line, actual_expr);
  33. printf ("%s:%d: expected: %d\n", __FILE__, line, expected);
  34. printf ("%s:%d: actual: %d\n", __FILE__, line, actual);
  35. }
  36. }
  37. #define CHECK(actual, expected) \
  38. check ((actual), (expected), #actual, __LINE__)
  39. template <class T>
  40. static void
  41. check_type ()
  42. {
  43. typedef std::numeric_limits<T> limits;
  44. CHECK (iszero (T{}), 1);
  45. CHECK (iszero (T{0}), 1);
  46. CHECK (iszero (T{-0.0}), 1);
  47. CHECK (iszero (T{1}), 0);
  48. CHECK (iszero (T{-1}), 0);
  49. CHECK (iszero (limits::min ()), 0);
  50. CHECK (iszero (-limits::min ()), 0);
  51. CHECK (iszero (limits::max ()), 0);
  52. CHECK (iszero (-limits::max ()), 0);
  53. if (limits::has_infinity)
  54. {
  55. CHECK (iszero (limits::infinity ()), 0);
  56. CHECK (iszero (-limits::infinity ()), 0);
  57. }
  58. CHECK (iszero (limits::epsilon ()), 0);
  59. CHECK (iszero (-limits::epsilon ()), 0);
  60. if (limits::has_quiet_NaN)
  61. CHECK (iszero (limits::quiet_NaN ()), 0);
  62. if (limits::has_signaling_NaN)
  63. CHECK (iszero (limits::signaling_NaN ()), 0);
  64. if (limits::has_signaling_NaN)
  65. CHECK (iszero (limits::signaling_NaN ()), 0);
  66. CHECK (iszero (limits::denorm_min ()),
  67. std::numeric_limits<T>::has_denorm == std::denorm_absent);
  68. CHECK (iszero (-limits::denorm_min ()),
  69. std::numeric_limits<T>::has_denorm == std::denorm_absent);
  70. }
  71. #if __HAVE_DISTINCT_FLOAT128
  72. static void
  73. check_float128 ()
  74. {
  75. ieee854_float128 q;
  76. q.d = 0.0Q;
  77. CHECK (iszero (q.d), 1);
  78. q.d = -0.0Q;
  79. CHECK (iszero (q.d), 1);
  80. q.d = 1.0Q;
  81. CHECK (iszero (q.d), 0);
  82. q.d = -1.0Q;
  83. CHECK (iszero (q.d), 0);
  84. /* Normal min. */
  85. q.ieee.negative = 0;
  86. q.ieee.exponent = 0x0001;
  87. q.ieee.mantissa0 = 0x0000;
  88. q.ieee.mantissa1 = 0x00000000;
  89. q.ieee.mantissa2 = 0x00000000;
  90. q.ieee.mantissa3 = 0x00000000;
  91. CHECK (iszero (q.d), 0);
  92. q.ieee.negative = 1;
  93. CHECK (iszero (q.d), 0);
  94. /* Normal max. */
  95. q.ieee.negative = 0;
  96. q.ieee.exponent = 0x7FFE;
  97. q.ieee.mantissa0 = 0xFFFF;
  98. q.ieee.mantissa1 = 0xFFFFFFFF;
  99. q.ieee.mantissa2 = 0xFFFFFFFF;
  100. q.ieee.mantissa3 = 0xFFFFFFFF;
  101. CHECK (iszero (q.d), 0);
  102. q.ieee.negative = 1;
  103. CHECK (iszero (q.d), 0);
  104. /* Infinity. */
  105. q.ieee.negative = 0;
  106. q.ieee.exponent = 0x7FFF;
  107. q.ieee.mantissa0 = 0x0000;
  108. q.ieee.mantissa1 = 0x00000000;
  109. q.ieee.mantissa2 = 0x00000000;
  110. q.ieee.mantissa3 = 0x00000000;
  111. CHECK (iszero (q.d), 0);
  112. /* Quiet NaN. */
  113. q.ieee_nan.quiet_nan = 1;
  114. q.ieee_nan.mantissa0 = 0x0000;
  115. CHECK (iszero (q.d), 0);
  116. /* Signaling NaN. */
  117. q.ieee_nan.quiet_nan = 0;
  118. q.ieee_nan.mantissa0 = 0x4000;
  119. CHECK (iszero (q.d), 0);
  120. /* Denormal min. */
  121. q.ieee.negative = 0;
  122. q.ieee.exponent = 0x0000;
  123. q.ieee.mantissa0 = 0x0000;
  124. q.ieee.mantissa1 = 0x00000000;
  125. q.ieee.mantissa2 = 0x00000000;
  126. q.ieee.mantissa3 = 0x00000001;
  127. CHECK (iszero (q.d), 0);
  128. q.ieee.negative = 1;
  129. CHECK (iszero (q.d), 0);
  130. }
  131. #endif
  132. static int
  133. do_test (void)
  134. {
  135. check_type<float> ();
  136. check_type<double> ();
  137. check_type<long double> ();
  138. #if __HAVE_DISTINCT_FLOAT128
  139. check_float128 ();
  140. #endif
  141. return errors;
  142. }
  143. #define TEST_FUNCTION do_test ()
  144. #include "../test-skeleton.c"