]> git.ipfire.org Git - thirdparty/glibc.git/blob - math/test-math-issignaling.cc
Use '%z' instead of '%Z' on printf functions
[thirdparty/glibc.git] / math / test-math-issignaling.cc
1 /* Test for the C++ implementation of issignaling.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #define _GNU_SOURCE 1
20 #include <math.h>
21 #include <stdio.h>
22
23 #include <limits>
24
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
31
32 static bool errors;
33
34 static void
35 check (int actual, int expected, const char *actual_expr, int line)
36 {
37 if (actual != expected)
38 {
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);
43 }
44 }
45
46 #define CHECK(actual, expected) \
47 check ((actual), (expected), #actual, __LINE__)
48
49 template <class T>
50 static void
51 check_type ()
52 {
53 typedef std::numeric_limits<T> limits;
54 CHECK (issignaling (T{0}), 0);
55 if (limits::has_infinity)
56 {
57 CHECK (issignaling (limits::infinity ()), 0);
58 CHECK (issignaling (-limits::infinity ()), 0);
59 }
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);
64 }
65
66 #if __HAVE_DISTINCT_FLOAT128
67 static void
68 check_float128 ()
69 {
70 ieee854_float128 q;
71
72 q.d = 0;
73 CHECK (issignaling (q.d), 0);
74
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);
83
84 /* Quiet NaN. */
85 q.ieee_nan.quiet_nan = 1;
86 q.ieee_nan.mantissa0 = 0x0000;
87 CHECK (issignaling (q.d), 0);
88
89 /* Still a quiet NaN. */
90 q.ieee_nan.quiet_nan = 1;
91 q.ieee_nan.mantissa0 = 0x4000;
92 CHECK (issignaling (q.d), 0);
93
94 /* Signaling NaN. */
95 q.ieee_nan.quiet_nan = 0;
96 q.ieee_nan.mantissa0 = 0x4000;
97 CHECK (issignaling (q.d), 1);
98 }
99 #endif
100
101 static int
102 do_test (void)
103 {
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;
111 }
112
113 #include <support/test-driver.c>