vfprintf-internal: Get rid of alloca.
[glibc.git] / math / test-math-issignaling.cc
blobb8079b0be18c85df660357dd4e07bbbc18ec2c46
1 /* Test for the C++ implementation of issignaling.
2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #define _GNU_SOURCE 1
20 #include <math.h>
21 #include <stdio.h>
23 #include <limits>
25 /* There is no signaling_NaN for _Float128 in std::numeric_limits.
26 Include ieee754_float128.h and use the bitfields in the union
27 ieee854_float128.ieee_nan to build a signaling NaN. */
28 #if __HAVE_DISTINCT_FLOAT128
29 # include <ieee754_float128.h>
30 #endif
32 static bool errors;
34 static void
35 check (int actual, int expected, const char *actual_expr, int line)
37 if (actual != expected)
39 errors = true;
40 printf ("%s:%d: error: %s\n", __FILE__, line, actual_expr);
41 printf ("%s:%d: expected: %d\n", __FILE__, line, expected);
42 printf ("%s:%d: actual: %d\n", __FILE__, line, actual);
46 #define CHECK(actual, expected) \
47 check ((actual), (expected), #actual, __LINE__)
49 template <class T>
50 static void
51 check_type ()
53 typedef std::numeric_limits<T> limits;
54 CHECK (issignaling (T{0}), 0);
55 if (limits::has_infinity)
57 CHECK (issignaling (limits::infinity ()), 0);
58 CHECK (issignaling (-limits::infinity ()), 0);
60 if (limits::has_quiet_NaN)
61 CHECK (issignaling (limits::quiet_NaN ()), 0);
62 if (limits::has_signaling_NaN)
63 CHECK (issignaling (limits::signaling_NaN ()), 1);
66 #if __HAVE_DISTINCT_FLOAT128
67 static void
68 check_float128 ()
70 ieee854_float128 q;
72 q.d = 0;
73 CHECK (issignaling (q.d), 0);
75 /* Infinity. */
76 q.ieee.negative = 0;
77 q.ieee.exponent = 0x7FFF;
78 q.ieee.mantissa0 = 0x0000;
79 q.ieee.mantissa1 = 0x00000000;
80 q.ieee.mantissa2 = 0x00000000;
81 q.ieee.mantissa3 = 0x00000000;
82 CHECK (issignaling (q.d), 0);
84 /* Quiet NaN. */
85 q.ieee_nan.quiet_nan = 1;
86 q.ieee_nan.mantissa0 = 0x0000;
87 CHECK (issignaling (q.d), 0);
89 /* Still a quiet NaN. */
90 q.ieee_nan.quiet_nan = 1;
91 q.ieee_nan.mantissa0 = 0x4000;
92 CHECK (issignaling (q.d), 0);
94 /* Signaling NaN. */
95 q.ieee_nan.quiet_nan = 0;
96 q.ieee_nan.mantissa0 = 0x4000;
97 CHECK (issignaling (q.d), 1);
99 #endif
101 static int
102 do_test (void)
104 check_type<float> ();
105 check_type<double> ();
106 check_type<long double> ();
107 #if __HAVE_DISTINCT_FLOAT128
108 check_float128 ();
109 #endif
110 return errors;
113 #include <support/test-driver.c>