2012-08-27 16:01:27 +00:00
|
|
|
/* Test for correct rounding of results of strtod and related
|
|
|
|
functions.
|
2016-01-04 16:05:18 +00:00
|
|
|
Copyright (C) 2012-2016 Free Software Foundation, Inc.
|
2012-08-27 16:01:27 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
2014-09-16 19:37:04 +00:00
|
|
|
/* Defining _LIBC_TEST ensures long double math functions are
|
|
|
|
declared in the headers. */
|
|
|
|
#define _LIBC_TEST 1
|
2012-09-12 23:36:19 +00:00
|
|
|
#include <fenv.h>
|
2012-08-27 16:01:27 +00:00
|
|
|
#include <float.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-06-14 17:58:41 +00:00
|
|
|
#include <math-tests.h>
|
2012-08-27 16:01:27 +00:00
|
|
|
|
2013-06-13 15:41:58 +00:00
|
|
|
struct exactness
|
|
|
|
{
|
|
|
|
bool f;
|
|
|
|
bool d;
|
|
|
|
bool ld;
|
|
|
|
};
|
|
|
|
|
2012-09-12 23:36:19 +00:00
|
|
|
struct test_results {
|
2012-08-27 16:01:27 +00:00
|
|
|
float f;
|
|
|
|
double d;
|
|
|
|
long double ld;
|
|
|
|
};
|
|
|
|
|
2012-09-12 23:36:19 +00:00
|
|
|
struct test {
|
|
|
|
const char *s;
|
2013-06-13 15:41:58 +00:00
|
|
|
struct exactness exact;
|
2012-09-12 23:36:19 +00:00
|
|
|
struct test_results rd, rn, rz, ru;
|
|
|
|
};
|
|
|
|
|
2012-08-27 16:01:27 +00:00
|
|
|
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
2013-06-13 15:41:58 +00:00
|
|
|
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
|
|
|
|
ld53exact, ld53d, ld53n, ld53z, ld53u, \
|
|
|
|
ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
|
|
|
|
ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
|
|
|
|
ld106exact, ld106d, ld106n, ld106z, ld106u, \
|
|
|
|
ld113exact, ld113d, ld113n, ld113z, ld113u) \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ \
|
|
|
|
s, \
|
2013-06-13 15:41:58 +00:00
|
|
|
{ fexact, dexact, ld53exact }, \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ fd, dd, ld53d }, \
|
|
|
|
{ fn, dn, ld53n }, \
|
|
|
|
{ fz, dz, ld53z }, \
|
|
|
|
{ fu, du, ld53u } \
|
|
|
|
}
|
2012-09-05 11:45:10 +00:00
|
|
|
#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16381
|
2012-09-05 10:37:16 +00:00
|
|
|
/* This is for the Intel extended float format. */
|
2013-06-13 15:41:58 +00:00
|
|
|
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
|
|
|
|
ld53exact, ld53d, ld53n, ld53z, ld53u, \
|
|
|
|
ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
|
|
|
|
ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
|
|
|
|
ld106exact, ld106d, ld106n, ld106z, ld106u, \
|
|
|
|
ld113exact, ld113d, ld113n, ld113z, ld113u) \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ \
|
|
|
|
s, \
|
2013-06-13 15:41:58 +00:00
|
|
|
{ fexact, dexact, ld64iexact }, \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ fd, dd, ld64id }, \
|
|
|
|
{ fn, dn, ld64in }, \
|
|
|
|
{ fz, dz, ld64iz }, \
|
|
|
|
{ fu, du, ld64iu } \
|
|
|
|
}
|
2012-09-05 11:45:10 +00:00
|
|
|
#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16382
|
2012-09-05 10:37:16 +00:00
|
|
|
/* This is for the Motorola extended float format. */
|
2013-06-13 15:41:58 +00:00
|
|
|
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
|
|
|
|
ld53exact, ld53d, ld53n, ld53z, ld53u, \
|
|
|
|
ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
|
|
|
|
ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
|
|
|
|
ld106exact, ld106d, ld106n, ld106z, ld106u, \
|
|
|
|
ld113exact, ld113d, ld113n, ld113z, ld113u) \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ \
|
|
|
|
s, \
|
2013-06-13 15:41:58 +00:00
|
|
|
{ fexact, dexact, ld64mexact }, \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ fd, dd, ld64md }, \
|
|
|
|
{ fn, dn, ld64mn }, \
|
|
|
|
{ fz, dz, ld64mz }, \
|
|
|
|
{ fu, du, ld64mu } \
|
|
|
|
}
|
2012-08-27 16:01:27 +00:00
|
|
|
#elif LDBL_MANT_DIG == 106 && LDBL_MAX_EXP == 1024
|
2013-06-13 15:41:58 +00:00
|
|
|
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
|
|
|
|
ld53exact, ld53d, ld53n, ld53z, ld53u, \
|
|
|
|
ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
|
|
|
|
ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
|
|
|
|
ld106exact, ld106d, ld106n, ld106z, ld106u, \
|
|
|
|
ld113exact, ld113d, ld113n, ld113z, ld113u) \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ \
|
|
|
|
s, \
|
2013-06-13 15:41:58 +00:00
|
|
|
{ fexact, dexact, ld106exact }, \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ fd, dd, ld106d }, \
|
|
|
|
{ fn, dn, ld106n }, \
|
|
|
|
{ fz, dz, ld106z }, \
|
|
|
|
{ fu, du, ld106u } \
|
|
|
|
}
|
2012-08-27 16:01:27 +00:00
|
|
|
#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
|
2013-06-13 15:41:58 +00:00
|
|
|
# define TEST(s, fexact, fd, fn, fz, fu, dexact, dd, dn, dz, du, \
|
|
|
|
ld53exact, ld53d, ld53n, ld53z, ld53u, \
|
|
|
|
ld64iexact, ld64id, ld64in, ld64iz, ld64iu, \
|
|
|
|
ld64mexact, ld64md, ld64mn, ld64mz, ld64mu, \
|
|
|
|
ld106exact, ld106d, ld106n, ld106z, ld106u, \
|
|
|
|
ld113exact, ld113d, ld113n, ld113z, ld113u) \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ \
|
|
|
|
s, \
|
2013-06-13 15:41:58 +00:00
|
|
|
{ fexact, dexact, ld113exact }, \
|
2012-09-12 23:36:19 +00:00
|
|
|
{ fd, dd, ld113d }, \
|
|
|
|
{ fn, dn, ld113n }, \
|
|
|
|
{ fz, dz, ld113z }, \
|
|
|
|
{ fu, du, ld113u } \
|
|
|
|
}
|
2012-08-27 16:01:27 +00:00
|
|
|
#else
|
|
|
|
# error "unknown long double format"
|
|
|
|
#endif
|
|
|
|
|
2016-05-09 16:49:11 +00:00
|
|
|
/* Include the generated test data. */
|
|
|
|
#include "tst-strtod-round-data.h"
|
2012-08-27 16:01:27 +00:00
|
|
|
|
2012-09-12 23:36:19 +00:00
|
|
|
static int
|
|
|
|
test_in_one_mode (const char *s, const struct test_results *expected,
|
2013-06-14 17:58:41 +00:00
|
|
|
const struct exactness *exact, const char *mode_name,
|
|
|
|
bool float_round_ok, bool double_round_ok,
|
|
|
|
bool long_double_round_ok)
|
2012-09-12 23:36:19 +00:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
float f = strtof (s, NULL);
|
|
|
|
double d = strtod (s, NULL);
|
|
|
|
long double ld = strtold (s, NULL);
|
|
|
|
if (f != expected->f
|
|
|
|
|| copysignf (1.0f, f) != copysignf (1.0f, expected->f))
|
|
|
|
{
|
|
|
|
printf ("strtof (%s) returned %a not %a (%s)\n", s, f,
|
|
|
|
expected->f, mode_name);
|
2013-06-14 17:58:41 +00:00
|
|
|
if (float_round_ok || exact->f)
|
|
|
|
result = 1;
|
|
|
|
else
|
|
|
|
printf ("ignoring this inexact result\n");
|
2012-09-12 23:36:19 +00:00
|
|
|
}
|
|
|
|
if (d != expected->d
|
|
|
|
|| copysign (1.0, d) != copysign (1.0, expected->d))
|
|
|
|
{
|
|
|
|
printf ("strtod (%s) returned %a not %a (%s)\n", s, d,
|
|
|
|
expected->d, mode_name);
|
2013-06-14 17:58:41 +00:00
|
|
|
if (double_round_ok || exact->d)
|
|
|
|
result = 1;
|
|
|
|
else
|
|
|
|
printf ("ignoring this inexact result\n");
|
2012-09-12 23:36:19 +00:00
|
|
|
}
|
|
|
|
if (ld != expected->ld
|
|
|
|
|| copysignl (1.0L, ld) != copysignl (1.0L, expected->ld))
|
|
|
|
{
|
|
|
|
printf ("strtold (%s) returned %La not %La (%s)\n", s, ld,
|
|
|
|
expected->ld, mode_name);
|
2013-06-14 17:58:41 +00:00
|
|
|
if ((long_double_round_ok && LDBL_MANT_DIG != 106) || exact->ld)
|
2012-09-12 23:36:19 +00:00
|
|
|
result = 1;
|
|
|
|
else
|
2013-06-14 17:58:41 +00:00
|
|
|
printf ("ignoring this inexact result\n");
|
2012-09-12 23:36:19 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-27 16:01:27 +00:00
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
2014-12-30 19:11:13 +00:00
|
|
|
int save_round_mode __attribute__ ((unused)) = fegetround ();
|
2012-08-27 16:01:27 +00:00
|
|
|
int result = 0;
|
|
|
|
for (size_t i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
|
|
|
|
{
|
2013-06-13 15:41:58 +00:00
|
|
|
result |= test_in_one_mode (tests[i].s, &tests[i].rn, &tests[i].exact,
|
2013-06-14 17:58:41 +00:00
|
|
|
"default rounding mode",
|
|
|
|
true, true, true);
|
2012-09-12 23:36:19 +00:00
|
|
|
#ifdef FE_DOWNWARD
|
|
|
|
if (!fesetround (FE_DOWNWARD))
|
2012-08-27 16:01:27 +00:00
|
|
|
{
|
2013-06-13 15:41:58 +00:00
|
|
|
result |= test_in_one_mode (tests[i].s, &tests[i].rd,
|
2013-06-14 17:58:41 +00:00
|
|
|
&tests[i].exact, "FE_DOWNWARD",
|
|
|
|
ROUNDING_TESTS (float, FE_DOWNWARD),
|
|
|
|
ROUNDING_TESTS (double, FE_DOWNWARD),
|
|
|
|
ROUNDING_TESTS (long double,
|
|
|
|
FE_DOWNWARD));
|
2012-09-12 23:36:19 +00:00
|
|
|
fesetround (save_round_mode);
|
2012-08-27 16:01:27 +00:00
|
|
|
}
|
2012-09-12 23:36:19 +00:00
|
|
|
#endif
|
|
|
|
#ifdef FE_TOWARDZERO
|
|
|
|
if (!fesetround (FE_TOWARDZERO))
|
2012-08-27 16:01:27 +00:00
|
|
|
{
|
2013-06-13 15:41:58 +00:00
|
|
|
result |= test_in_one_mode (tests[i].s, &tests[i].rz,
|
2013-06-14 17:58:41 +00:00
|
|
|
&tests[i].exact, "FE_TOWARDZERO",
|
|
|
|
ROUNDING_TESTS (float, FE_TOWARDZERO),
|
|
|
|
ROUNDING_TESTS (double, FE_TOWARDZERO),
|
|
|
|
ROUNDING_TESTS (long double,
|
|
|
|
FE_TOWARDZERO));
|
2012-09-12 23:36:19 +00:00
|
|
|
fesetround (save_round_mode);
|
2012-08-27 16:01:27 +00:00
|
|
|
}
|
2012-09-12 23:36:19 +00:00
|
|
|
#endif
|
|
|
|
#ifdef FE_UPWARD
|
|
|
|
if (!fesetround (FE_UPWARD))
|
2012-08-27 16:01:27 +00:00
|
|
|
{
|
2013-06-13 15:41:58 +00:00
|
|
|
result |= test_in_one_mode (tests[i].s, &tests[i].ru,
|
2013-06-14 17:58:41 +00:00
|
|
|
&tests[i].exact, "FE_UPWARD",
|
|
|
|
ROUNDING_TESTS (float, FE_UPWARD),
|
|
|
|
ROUNDING_TESTS (double, FE_UPWARD),
|
|
|
|
ROUNDING_TESTS (long double, FE_UPWARD));
|
2012-09-12 23:36:19 +00:00
|
|
|
fesetround (save_round_mode);
|
2012-08-27 16:01:27 +00:00
|
|
|
}
|
2012-09-12 23:36:19 +00:00
|
|
|
#endif
|
2012-08-27 16:01:27 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|