getusershell: Work around musl bugs.
[gnulib.git] / tests / test-nan-2.c
blobf0951180c719436b632dd8cb0fe9b9029595909d
1 /* Tests of quiet not-a-number.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
19 #include <config.h>
21 /* Specification. */
22 #include "nan.h"
24 #include <stdio.h>
26 #include "fpe-trapping.h"
28 #if HAVE_FPE_TRAPPING
30 # include <fenv.h>
32 # include "macros.h"
34 float volatile resultf;
35 double volatile resultd;
36 long double volatile resultl;
38 int
39 main ()
41 /* Fetch the NaN values before we start watching out for FE_INVALID
42 exceptions, because the division 0.0 / 0.0 itself also raises an
43 FE_INVALID exception.
44 The use of 'volatile' prevents the compiler from doing constant-folding
45 optimizations on these values. An alternative, for GCC only, would be
46 the command-line option '-fsignaling-nans'. */
47 float volatile nanf = NaNf ();
48 double volatile nand = NaNd ();
49 long double volatile nanl = NaNl ();
51 /* Check that the values are really quiet. */
53 /* Clear FE_INVALID exceptions from past operations. */
54 feclearexcept (FE_INVALID);
56 /* An FE_INVALID exception shall trigger a SIGFPE signal, which by default
57 terminates the program. */
58 if (sigfpe_on_invalid () < 0)
60 fputs ("Skipping test: trapping floating-point exceptions are not supported on this machine.\n", stderr);
61 return 77;
64 resultf = nanf + 42.0f;
65 resultd = nand + 42.0;
66 resultl = nanl + 42.0L;
68 return test_exit_status;
71 #else
73 /* No HAVE_FPE_TRAPPING available.
74 We could use the various alternative approaches from
75 libgfortran/config/fpu-*.h, but that's not worth it. */
77 int
78 main ()
80 fputs ("Skipping test: feenableexcept not available\n", stderr);
81 return 77;
84 #endif