blob: cb4114916ba6e0d26082d364498773670cc8f9b8 [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001/* Test nearbyint functions do not disable exception traps (bug 19228).
2 Copyright (C) 2015-2016 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 <http://www.gnu.org/licenses/>. */
18
19#include <fenv.h>
20#include <math.h>
21#include <stdio.h>
22
23#ifndef FE_INEXACT
24# define FE_INEXACT 0
25#endif
26
27#define TEST_FUNC(NAME, FLOAT, SUFFIX) \
28static int \
29NAME (void) \
30{ \
31 int result = 0; \
32 volatile FLOAT a, b __attribute__ ((unused)); \
33 a = 1.5; \
34 /* nearbyint must work when traps on "inexact" are enabled. */ \
35 b = nearbyint ## SUFFIX (a); \
36 /* And it must have left those traps enabled. */ \
37 if (fegetexcept () == FE_INEXACT) \
38 puts ("PASS: " #FLOAT); \
39 else \
40 { \
41 puts ("FAIL: " #FLOAT); \
42 result = 1; \
43 } \
44 return result; \
45}
46
47TEST_FUNC (float_test, float, f)
48TEST_FUNC (double_test, double, )
49#ifndef NO_LONG_DOUBLE
50TEST_FUNC (ldouble_test, long double, l)
51#endif
52
53static int
54do_test (void)
55{
56 if (feenableexcept (FE_INEXACT) == -1)
57 {
58 puts ("enabling FE_INEXACT traps failed, cannot test");
59 return 77;
60 }
61 int result = float_test ();
62 feenableexcept (FE_INEXACT);
63 result |= double_test ();
64#ifndef NO_LONG_DOUBLE
65 feenableexcept (FE_INEXACT);
66 result |= ldouble_test ();
67#endif
68 return result;
69}
70
71#define TEST_FUNCTION do_test ()
72#include "../test-skeleton.c"