2012-09-14 20:18:49 +00:00
|
|
|
/* Test for correct rounding of printf floating-point output.
|
2023-01-06 21:08:04 +00:00
|
|
|
Copyright (C) 2012-2023 Free Software Foundation, Inc.
|
2012-09-14 20:18:49 +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
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2012-09-14 20:18:49 +00:00
|
|
|
|
2017-11-02 11:14:51 +00:00
|
|
|
#include <array_length.h>
|
2012-09-14 20:18:49 +00:00
|
|
|
#include <fenv.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct dec_test {
|
|
|
|
double d;
|
|
|
|
const char *fmt;
|
|
|
|
const char *rd, *rn, *rz, *ru;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct dec_test dec_tests[] = {
|
|
|
|
{ 1.5, "%.0f", "1", "2", "1", "2" },
|
|
|
|
{ -1.5, "%.0f", "-2", "-2", "-1", "-1" },
|
|
|
|
{ 2.5, "%.0f", "2", "2", "2", "3" },
|
|
|
|
{ -2.5, "%.0f", "-3", "-2", "-2", "-2" },
|
|
|
|
{ 1.4999, "%.0f", "1", "1", "1", "2" },
|
|
|
|
{ -1.4999, "%.0f", "-2", "-1", "-1", "-1" },
|
|
|
|
{ 1.5001, "%.0f", "1", "2", "1", "2" },
|
|
|
|
{ -1.5001, "%.0f", "-2", "-2", "-1", "-1" },
|
|
|
|
{ 2.4999, "%.0f", "2", "2", "2", "3" },
|
|
|
|
{ -2.4999, "%.0f", "-3", "-2", "-2", "-2" },
|
|
|
|
{ 2.5001, "%.0f", "2", "3", "2", "3" },
|
|
|
|
{ -2.5001, "%.0f", "-3", "-3", "-2", "-2" },
|
|
|
|
{ 1.0 / 3.0, "%f", "0.333333", "0.333333", "0.333333", "0.333334" },
|
|
|
|
{ -1.0 / 3.0, "%f", "-0.333334", "-0.333333", "-0.333333", "-0.333333" },
|
|
|
|
{ 0.2500001, "%.2e", "2.50e-01", "2.50e-01", "2.50e-01", "2.51e-01" },
|
|
|
|
{ -0.2500001, "%.2e", "-2.51e-01", "-2.50e-01", "-2.50e-01", "-2.50e-01" },
|
|
|
|
{ 1000001.0, "%.1e", "1.0e+06", "1.0e+06", "1.0e+06", "1.1e+06" },
|
|
|
|
{ -1000001.0, "%.1e", "-1.1e+06", "-1.0e+06", "-1.0e+06", "-1.0e+06" },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
test_dec_in_one_mode (double d, const char *fmt, const char *expected,
|
|
|
|
const char *mode_name)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
int ret = snprintf (buf, sizeof buf, fmt, d);
|
|
|
|
if (ret <= 0 || ret >= (int) sizeof buf)
|
|
|
|
{
|
|
|
|
printf ("snprintf for %a returned %d\n", d, ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (strcmp (buf, expected) == 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf ("snprintf (\"%s\", %a) returned \"%s\" not \"%s\" (%s)\n",
|
|
|
|
fmt, d, buf, expected, mode_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 15:38:21 +00:00
|
|
|
struct hex_test
|
|
|
|
{
|
|
|
|
double d;
|
|
|
|
const char *fmt;
|
|
|
|
const char *rd[4], *rn[4], *rz[4], *ru[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct hex_test hex_tests[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
0x1.fffffp+4, "%.1a",
|
|
|
|
{ "0x1.fp+4", "0x3.fp+3", "0x7.fp+2", "0xf.fp+1" },
|
|
|
|
{ "0x2.0p+4", "0x4.0p+3", "0x8.0p+2", "0x1.0p+5" },
|
|
|
|
{ "0x1.fp+4", "0x3.fp+3", "0x7.fp+2", "0xf.fp+1" },
|
|
|
|
{ "0x2.0p+4", "0x4.0p+3", "0x8.0p+2", "0x1.0p+5" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
-0x1.fffffp+4, "%.1a",
|
|
|
|
{ "-0x2.0p+4", "-0x4.0p+3", "-0x8.0p+2", "-0x1.0p+5" },
|
|
|
|
{ "-0x2.0p+4", "-0x4.0p+3", "-0x8.0p+2", "-0x1.0p+5" },
|
|
|
|
{ "-0x1.fp+4", "-0x3.fp+3", "-0x7.fp+2", "-0xf.fp+1" },
|
|
|
|
{ "-0x1.fp+4", "-0x3.fp+3", "-0x7.fp+2", "-0xf.fp+1" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0x1.88p+4, "%.1a",
|
|
|
|
{ "0x1.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
|
|
|
|
{ "0x1.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
|
|
|
|
{ "0x1.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
|
|
|
|
{ "0x1.9p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
-0x1.88p+4, "%.1a",
|
|
|
|
{ "-0x1.9p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" },
|
|
|
|
{ "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" },
|
|
|
|
{ "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" },
|
|
|
|
{ "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0x1.78p+4, "%.1a",
|
|
|
|
{ "0x1.7p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
|
|
|
|
{ "0x1.8p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
|
|
|
|
{ "0x1.7p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
|
|
|
|
{ "0x1.8p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
-0x1.78p+4, "%.1a",
|
|
|
|
{ "-0x1.8p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
|
|
|
|
{ "-0x1.8p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
|
|
|
|
{ "-0x1.7p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
|
|
|
|
{ "-0x1.7p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
64.0 / 3.0, "%.1a",
|
|
|
|
{ "0x1.5p+4", "0x2.ap+3", "0x5.5p+2", "0xa.ap+1" },
|
|
|
|
{ "0x1.5p+4", "0x2.bp+3", "0x5.5p+2", "0xa.bp+1" },
|
|
|
|
{ "0x1.5p+4", "0x2.ap+3", "0x5.5p+2", "0xa.ap+1" },
|
|
|
|
{ "0x1.6p+4", "0x2.bp+3", "0x5.6p+2", "0xa.bp+1" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
-64.0 / 3.0, "%.1a",
|
|
|
|
{ "-0x1.6p+4", "-0x2.bp+3", "-0x5.6p+2", "-0xa.bp+1" },
|
|
|
|
{ "-0x1.5p+4", "-0x2.bp+3", "-0x5.5p+2", "-0xa.bp+1" },
|
|
|
|
{ "-0x1.5p+4", "-0x2.ap+3", "-0x5.5p+2", "-0xa.ap+1" },
|
|
|
|
{ "-0x1.5p+4", "-0x2.ap+3", "-0x5.5p+2", "-0xa.ap+1" }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2014-11-25 23:31:21 +00:00
|
|
|
test_hex_in_one_mode (double d, const char *fmt, const char *const expected[4],
|
2012-09-24 15:38:21 +00:00
|
|
|
const char *mode_name)
|
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
int ret = snprintf (buf, sizeof buf, fmt, d);
|
|
|
|
if (ret <= 0 || ret >= (int) sizeof buf)
|
|
|
|
{
|
|
|
|
printf ("snprintf for %a returned %d\n", d, ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (strcmp (buf, expected[0]) == 0
|
|
|
|
|| strcmp (buf, expected[1]) == 0
|
|
|
|
|| strcmp (buf, expected[2]) == 0
|
|
|
|
|| strcmp (buf, expected[3]) == 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf ("snprintf (\"%s\", %a) returned \"%s\" not "
|
|
|
|
"\"%s\" or \"%s\" or \"%s\" or \"%s\" (%s)\n",
|
|
|
|
fmt, d, buf, expected[0], expected[1], expected[2], expected[3],
|
|
|
|
mode_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 20:18:49 +00:00
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
2014-12-30 19:11:13 +00:00
|
|
|
int save_round_mode __attribute__ ((unused)) = fegetround ();
|
2012-09-14 20:18:49 +00:00
|
|
|
int result = 0;
|
|
|
|
|
2017-11-02 11:14:51 +00:00
|
|
|
for (size_t i = 0; i < array_length (dec_tests); i++)
|
2012-09-14 20:18:49 +00:00
|
|
|
{
|
|
|
|
result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
|
|
|
|
dec_tests[i].rn, "default rounding mode");
|
|
|
|
#ifdef FE_DOWNWARD
|
|
|
|
if (!fesetround (FE_DOWNWARD))
|
|
|
|
{
|
|
|
|
result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
|
|
|
|
dec_tests[i].rd, "FE_DOWNWARD");
|
|
|
|
fesetround (save_round_mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef FE_TOWARDZERO
|
|
|
|
if (!fesetround (FE_TOWARDZERO))
|
|
|
|
{
|
|
|
|
result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
|
|
|
|
dec_tests[i].rz, "FE_TOWARDZERO");
|
|
|
|
fesetround (save_round_mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef FE_UPWARD
|
|
|
|
if (!fesetround (FE_UPWARD))
|
|
|
|
{
|
|
|
|
result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
|
|
|
|
dec_tests[i].ru, "FE_UPWARD");
|
|
|
|
fesetround (save_round_mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2012-09-24 15:38:21 +00:00
|
|
|
|
2017-11-02 11:14:51 +00:00
|
|
|
for (size_t i = 0; i < array_length (hex_tests); i++)
|
2012-09-24 15:38:21 +00:00
|
|
|
{
|
|
|
|
result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
|
|
|
|
hex_tests[i].rn, "default rounding mode");
|
|
|
|
#ifdef FE_DOWNWARD
|
|
|
|
if (!fesetround (FE_DOWNWARD))
|
|
|
|
{
|
|
|
|
result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
|
|
|
|
hex_tests[i].rd, "FE_DOWNWARD");
|
|
|
|
fesetround (save_round_mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef FE_TOWARDZERO
|
|
|
|
if (!fesetround (FE_TOWARDZERO))
|
|
|
|
{
|
|
|
|
result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
|
|
|
|
hex_tests[i].rz, "FE_TOWARDZERO");
|
|
|
|
fesetround (save_round_mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef FE_UPWARD
|
|
|
|
if (!fesetround (FE_UPWARD))
|
|
|
|
{
|
|
|
|
result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
|
|
|
|
hex_tests[i].ru, "FE_UPWARD");
|
|
|
|
fesetround (save_round_mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-14 20:18:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|