test-math-issignaling.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Test for the C++ implementation of issignaling.
  2. Copyright (C) 2017-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. /* There is no signaling_NaN for _Float128 in std::numeric_limits.
  20. Include ieee754_float128.h and use the bitfields in the union
  21. ieee854_float128.ieee_nan to build a signaling NaN. */
  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 (issignaling (T{0}), 0);
  45. if (limits::has_infinity)
  46. {
  47. CHECK (issignaling (limits::infinity ()), 0);
  48. CHECK (issignaling (-limits::infinity ()), 0);
  49. }
  50. if (limits::has_quiet_NaN)
  51. CHECK (issignaling (limits::quiet_NaN ()), 0);
  52. if (limits::has_signaling_NaN)
  53. CHECK (issignaling (limits::signaling_NaN ()), 1);
  54. }
  55. #if __HAVE_DISTINCT_FLOAT128
  56. static void
  57. check_float128 ()
  58. {
  59. ieee854_float128 q;
  60. q.d = 0;
  61. CHECK (issignaling (q.d), 0);
  62. /* Infinity. */
  63. q.ieee.negative = 0;
  64. q.ieee.exponent = 0x7FFF;
  65. q.ieee.mantissa0 = 0x0000;
  66. q.ieee.mantissa1 = 0x00000000;
  67. q.ieee.mantissa2 = 0x00000000;
  68. q.ieee.mantissa3 = 0x00000000;
  69. CHECK (issignaling (q.d), 0);
  70. /* Quiet NaN. */
  71. q.ieee_nan.quiet_nan = 1;
  72. q.ieee_nan.mantissa0 = 0x0000;
  73. CHECK (issignaling (q.d), 0);
  74. /* Still a quiet NaN. */
  75. q.ieee_nan.quiet_nan = 1;
  76. q.ieee_nan.mantissa0 = 0x4000;
  77. CHECK (issignaling (q.d), 0);
  78. /* Signaling NaN. */
  79. q.ieee_nan.quiet_nan = 0;
  80. q.ieee_nan.mantissa0 = 0x4000;
  81. CHECK (issignaling (q.d), 1);
  82. }
  83. #endif
  84. static int
  85. do_test (void)
  86. {
  87. check_type<float> ();
  88. check_type<double> ();
  89. check_type<long double> ();
  90. #if __HAVE_DISTINCT_FLOAT128
  91. check_float128 ();
  92. #endif
  93. return errors;
  94. }
  95. #include <support/test-driver.c>