1 /* Test for correct rounding of results of strtod and related
3 Copyright (C) 2012-2016 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 /* Defining _LIBC_TEST ensures long double math functions are
21 declared in the headers. */
30 #include <math-tests.h>
47 struct exactness exact
;
48 struct test_results rd
, rn
, rz
, ru
;
51 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
52 # define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
53 ld53exact, ld53d, ld53n, ld53z, ld53u, \
54 ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
55 ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
56 ld106exact, ld106d, ld106n, ld106z, ld106u, \
57 ld113exact, ld113d, ld113n, ld113z, ld113u) \
60 { fexact, dexact, ld53exact }, \
66 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16381
67 /* This is for the Intel extended float format. */
68 # define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
69 ld53exact, ld53d, ld53n, ld53z, ld53u, \
70 ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
71 ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
72 ld106exact, ld106d, ld106n, ld106z, ld106u, \
73 ld113exact, ld113d, ld113n, ld113z, ld113u) \
76 { fexact, dexact, ld64iexact }, \
82 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16382
83 /* This is for the Motorola extended float format. */
84 # define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
85 ld53exact, ld53d, ld53n, ld53z, ld53u, \
86 ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
87 ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
88 ld106exact, ld106d, ld106n, ld106z, ld106u, \
89 ld113exact, ld113d, ld113n, ld113z, ld113u) \
92 { fexact, dexact, ld64mexact }, \
98 #elif LDBL_MANT_DIG == 106 && LDBL_MAX_EXP == 1024
99 # define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
100 ld53exact, ld53d, ld53n, ld53z, ld53u, \
101 ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
102 ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
103 ld106exact, ld106d, ld106n, ld106z, ld106u, \
104 ld113exact, ld113d, ld113n, ld113z, ld113u) \
107 { fexact, dexact, ld106exact }, \
108 { fd, dd, ld106d }, \
109 { fn, dn, ld106n }, \
110 { fz, dz, ld106z }, \
113 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
114 # define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
115 ld53exact, ld53d, ld53n, ld53z, ld53u, \
116 ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
117 ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
118 ld106exact, ld106d, ld106n, ld106z, ld106u, \
119 ld113exact, ld113d, ld113n, ld113z, ld113u) \
122 { fexact, dexact, ld113exact }, \
123 { fd, dd, ld113d }, \
124 { fn, dn, ld113n }, \
125 { fz, dz, ld113z }, \
129 # error "unknown long double format"
132 /* Include the generated test data. */
133 #include "tst-strtod-round-data.h"
136 test_in_one_mode (const char *s
, const struct test_results
*expected
,
137 const struct exactness
*exact
, const char *mode_name
,
138 bool float_round_ok
, bool double_round_ok
,
139 bool long_double_round_ok
)
142 float f
= strtof (s
, NULL
);
143 double d
= strtod (s
, NULL
);
144 long double ld
= strtold (s
, NULL
);
146 || copysignf (1.0f
, f
) != copysignf (1.0f
, expected
->f
))
148 printf ("strtof (%s) returned %a not %a (%s)\n", s
, f
,
149 expected
->f
, mode_name
);
150 if (float_round_ok
|| exact
->f
)
153 printf ("ignoring this inexact result\n");
156 || copysign (1.0, d
) != copysign (1.0, expected
->d
))
158 printf ("strtod (%s) returned %a not %a (%s)\n", s
, d
,
159 expected
->d
, mode_name
);
160 if (double_round_ok
|| exact
->d
)
163 printf ("ignoring this inexact result\n");
165 if (ld
!= expected
->ld
166 || copysignl (1.0L, ld
) != copysignl (1.0L, expected
->ld
))
168 printf ("strtold (%s) returned %La not %La (%s)\n", s
, ld
,
169 expected
->ld
, mode_name
);
170 if ((long_double_round_ok
&& LDBL_MANT_DIG
!= 106) || exact
->ld
)
173 printf ("ignoring this inexact result\n");
181 int save_round_mode
__attribute__ ((unused
)) = fegetround ();
183 for (size_t i
= 0; i
< sizeof (tests
) / sizeof (tests
[0]); i
++)
185 result
|= test_in_one_mode (tests
[i
].s
, &tests
[i
].rn
, &tests
[i
].exact
,
186 "default rounding mode",
189 if (!fesetround (FE_DOWNWARD
))
191 result
|= test_in_one_mode (tests
[i
].s
, &tests
[i
].rd
,
192 &tests
[i
].exact
, "FE_DOWNWARD",
193 ROUNDING_TESTS (float, FE_DOWNWARD
),
194 ROUNDING_TESTS (double, FE_DOWNWARD
),
195 ROUNDING_TESTS (long double,
197 fesetround (save_round_mode
);
201 if (!fesetround (FE_TOWARDZERO
))
203 result
|= test_in_one_mode (tests
[i
].s
, &tests
[i
].rz
,
204 &tests
[i
].exact
, "FE_TOWARDZERO",
205 ROUNDING_TESTS (float, FE_TOWARDZERO
),
206 ROUNDING_TESTS (double, FE_TOWARDZERO
),
207 ROUNDING_TESTS (long double,
209 fesetround (save_round_mode
);
213 if (!fesetround (FE_UPWARD
))
215 result
|= test_in_one_mode (tests
[i
].s
, &tests
[i
].ru
,
216 &tests
[i
].exact
, "FE_UPWARD",
217 ROUNDING_TESTS (float, FE_UPWARD
),
218 ROUNDING_TESTS (double, FE_UPWARD
),
219 ROUNDING_TESTS (long double, FE_UPWARD
));
220 fesetround (save_round_mode
);
227 #define TEST_FUNCTION do_test ()
228 #include "../test-skeleton.c"