glibc/stdlib/gen-tst-strtod-round.c

204 lines
5.4 KiB
C
Raw Normal View History

2012-08-27 16:01:27 +00:00
/* Generate table of tests in tst-strtod-round.c from
tst-strtod-round-data.
Copyright (C) 2012-2022 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
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-08-27 16:01:27 +00:00
/* Compile this program as:
gcc -std=gnu11 -O2 -Wall -Wextra gen-tst-strtod-round.c -lmpfr \
-o gen-tst-strtod-round
(use of current MPFR version recommended) and run it as:
gen-tst-strtod-round tst-strtod-round-data tst-strtod-round-data.h
The output file will be generated as tst-strtod-round-data.h
*/
2012-08-27 16:01:27 +00:00
#define _GNU_SOURCE
#include <assert.h>
2012-08-27 16:01:27 +00:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2012-08-27 16:01:27 +00:00
#include <mpfr.h>
/* Work around incorrect ternary value from mpfr_strtofr
<https://sympa.inria.fr/sympa/arc/mpfr/2012-08/msg00005.html>. */
#define WORKAROUND
static int
string_to_fp (mpfr_t f, const char *s, mpfr_rnd_t rnd)
{
Fix strtod overflow detection (bug 23279). As shown by bug 23279, strtod's round_and_return has an off-by-one error in its overflow detection, only counting an exponent greater than MAX_EXP as overflowing when an exponent of MAX_EXP also means overflow (recall the ISO C definition of DBL_MAX_EXP etc. is based on a floating-point model where 2^exp is multiplied by a value in the interval [0.5, 1), so 2^MAX_EXP is not representable). For decimal arguments to strtod, a separate overflow check in the main implementation covers the case where the integer part of the argument (truncated to the nearest integer towards zero) has more than MAX_EXP bits, meaning that this issue in round_and_return only affects cases (arguments with absolute value strictly between the maximum representable value and 2^MAX_EXP) where overflow depends on the rounding mode; in such cases, the returned value would still have been correct on overflow but without the overflow exception being raised or errno being set to ERANGE. For hex float arguments, however, other cases can arise, as shown in bug 23279, where a value with exponent already set to MAX_EXP is passed into round_and_return and a result can wrongly end up being NaN, or infinity instead of the largest finite value. This patch fixes the off-by-one error, adds testing of overflow exceptions to the tst-strtod-round framework, and adds tests of these issues. Tested for x86_64. Also ran the tst-strtod-round tests for powerpc to make sure the new tests didn't introduce any new failures for IBM long double. [BZ #23279] * stdlib/strtod_l.c (round_and_return): Handle an exponent of MAX_EXP as overflowing. * stdlib/gen-tst-strtod-round.c (string_to_fp): Clear MPFR overflow flag. (round_str): Output also whether result overflows in each rounding mode. * stdlib/tst-strtod-round-data: Add more tests. * stdlib/tst-strtod-round-data.h: Regenerated. * stdlib/tst-strtod-round-skeleton.c (_XNTRY): Update comment. (TEST): Handle extra arguments for overflow flags. (struct test_overflow): New type. [!FE_OVERFLOW] (FE_OVERFLOW): Define to 0. (GEN_ONE_TEST): Clear all exceptions. Test overflow flag. (test_in_one_mode): Take argument with overflow information. (do_test): Update calls to test_in_one_mode.
2018-06-13 16:06:14 +00:00
mpfr_clear_overflow ();
2012-08-27 16:01:27 +00:00
#ifdef WORKAROUND
mpfr_t f2;
mpfr_init2 (f2, 100000);
int r0 = mpfr_strtofr (f2, s, NULL, 0, rnd);
int r = mpfr_set (f, f2, rnd);
r |= mpfr_subnormalize (f, r, rnd);
2012-08-27 16:01:27 +00:00
mpfr_clear (f2);
return r0 | r;
#else
int r = mpfr_strtofr (f, s, NULL, 0, rnd);
r |= mpfr_subnormalize (f, r, rnd);
2012-08-27 16:01:27 +00:00
return r;
#endif
}
void
print_fp (FILE *fout, mpfr_t f, const char *suffix)
2012-08-27 16:01:27 +00:00
{
if (mpfr_inf_p (f))
mpfr_fprintf (fout, "\t%sINF%s", mpfr_signbit (f) ? "-" : "", suffix);
2012-08-27 16:01:27 +00:00
else
mpfr_fprintf (fout, "\t%Ra%s", f, suffix);
2012-08-27 16:01:27 +00:00
}
static void
round_str (FILE *fout, const char *s, int prec, int emin, int emax,
bool ibm_ld)
2012-08-27 16:01:27 +00:00
{
Fix strtod overflow detection (bug 23279). As shown by bug 23279, strtod's round_and_return has an off-by-one error in its overflow detection, only counting an exponent greater than MAX_EXP as overflowing when an exponent of MAX_EXP also means overflow (recall the ISO C definition of DBL_MAX_EXP etc. is based on a floating-point model where 2^exp is multiplied by a value in the interval [0.5, 1), so 2^MAX_EXP is not representable). For decimal arguments to strtod, a separate overflow check in the main implementation covers the case where the integer part of the argument (truncated to the nearest integer towards zero) has more than MAX_EXP bits, meaning that this issue in round_and_return only affects cases (arguments with absolute value strictly between the maximum representable value and 2^MAX_EXP) where overflow depends on the rounding mode; in such cases, the returned value would still have been correct on overflow but without the overflow exception being raised or errno being set to ERANGE. For hex float arguments, however, other cases can arise, as shown in bug 23279, where a value with exponent already set to MAX_EXP is passed into round_and_return and a result can wrongly end up being NaN, or infinity instead of the largest finite value. This patch fixes the off-by-one error, adds testing of overflow exceptions to the tst-strtod-round framework, and adds tests of these issues. Tested for x86_64. Also ran the tst-strtod-round tests for powerpc to make sure the new tests didn't introduce any new failures for IBM long double. [BZ #23279] * stdlib/strtod_l.c (round_and_return): Handle an exponent of MAX_EXP as overflowing. * stdlib/gen-tst-strtod-round.c (string_to_fp): Clear MPFR overflow flag. (round_str): Output also whether result overflows in each rounding mode. * stdlib/tst-strtod-round-data: Add more tests. * stdlib/tst-strtod-round-data.h: Regenerated. * stdlib/tst-strtod-round-skeleton.c (_XNTRY): Update comment. (TEST): Handle extra arguments for overflow flags. (struct test_overflow): New type. [!FE_OVERFLOW] (FE_OVERFLOW): Define to 0. (GEN_ONE_TEST): Clear all exceptions. Test overflow flag. (test_in_one_mode): Take argument with overflow information. (do_test): Update calls to test_in_one_mode.
2018-06-13 16:06:14 +00:00
mpfr_t max_value;
2012-08-27 16:01:27 +00:00
mpfr_t f;
mpfr_set_default_prec (prec);
mpfr_set_emin (emin);
mpfr_set_emax (emax);
mpfr_init (f);
int r = string_to_fp (f, s, MPFR_RNDD);
Fix strtod overflow detection (bug 23279). As shown by bug 23279, strtod's round_and_return has an off-by-one error in its overflow detection, only counting an exponent greater than MAX_EXP as overflowing when an exponent of MAX_EXP also means overflow (recall the ISO C definition of DBL_MAX_EXP etc. is based on a floating-point model where 2^exp is multiplied by a value in the interval [0.5, 1), so 2^MAX_EXP is not representable). For decimal arguments to strtod, a separate overflow check in the main implementation covers the case where the integer part of the argument (truncated to the nearest integer towards zero) has more than MAX_EXP bits, meaning that this issue in round_and_return only affects cases (arguments with absolute value strictly between the maximum representable value and 2^MAX_EXP) where overflow depends on the rounding mode; in such cases, the returned value would still have been correct on overflow but without the overflow exception being raised or errno being set to ERANGE. For hex float arguments, however, other cases can arise, as shown in bug 23279, where a value with exponent already set to MAX_EXP is passed into round_and_return and a result can wrongly end up being NaN, or infinity instead of the largest finite value. This patch fixes the off-by-one error, adds testing of overflow exceptions to the tst-strtod-round framework, and adds tests of these issues. Tested for x86_64. Also ran the tst-strtod-round tests for powerpc to make sure the new tests didn't introduce any new failures for IBM long double. [BZ #23279] * stdlib/strtod_l.c (round_and_return): Handle an exponent of MAX_EXP as overflowing. * stdlib/gen-tst-strtod-round.c (string_to_fp): Clear MPFR overflow flag. (round_str): Output also whether result overflows in each rounding mode. * stdlib/tst-strtod-round-data: Add more tests. * stdlib/tst-strtod-round-data.h: Regenerated. * stdlib/tst-strtod-round-skeleton.c (_XNTRY): Update comment. (TEST): Handle extra arguments for overflow flags. (struct test_overflow): New type. [!FE_OVERFLOW] (FE_OVERFLOW): Define to 0. (GEN_ONE_TEST): Clear all exceptions. Test overflow flag. (test_in_one_mode): Take argument with overflow information. (do_test): Update calls to test_in_one_mode.
2018-06-13 16:06:14 +00:00
bool overflow = mpfr_overflow_p () != 0;
if (ibm_ld)
{
assert (prec == 106 && emin == -1073 && emax == 1024);
/* The maximum value in IBM long double has discontiguous
mantissa bits. */
mpfr_init2 (max_value, 107);
mpfr_set_str (max_value, "0x1.fffffffffffff7ffffffffffffcp+1023", 0,
MPFR_RNDN);
if (mpfr_cmpabs (f, max_value) > 0)
Fix strtod overflow detection (bug 23279). As shown by bug 23279, strtod's round_and_return has an off-by-one error in its overflow detection, only counting an exponent greater than MAX_EXP as overflowing when an exponent of MAX_EXP also means overflow (recall the ISO C definition of DBL_MAX_EXP etc. is based on a floating-point model where 2^exp is multiplied by a value in the interval [0.5, 1), so 2^MAX_EXP is not representable). For decimal arguments to strtod, a separate overflow check in the main implementation covers the case where the integer part of the argument (truncated to the nearest integer towards zero) has more than MAX_EXP bits, meaning that this issue in round_and_return only affects cases (arguments with absolute value strictly between the maximum representable value and 2^MAX_EXP) where overflow depends on the rounding mode; in such cases, the returned value would still have been correct on overflow but without the overflow exception being raised or errno being set to ERANGE. For hex float arguments, however, other cases can arise, as shown in bug 23279, where a value with exponent already set to MAX_EXP is passed into round_and_return and a result can wrongly end up being NaN, or infinity instead of the largest finite value. This patch fixes the off-by-one error, adds testing of overflow exceptions to the tst-strtod-round framework, and adds tests of these issues. Tested for x86_64. Also ran the tst-strtod-round tests for powerpc to make sure the new tests didn't introduce any new failures for IBM long double. [BZ #23279] * stdlib/strtod_l.c (round_and_return): Handle an exponent of MAX_EXP as overflowing. * stdlib/gen-tst-strtod-round.c (string_to_fp): Clear MPFR overflow flag. (round_str): Output also whether result overflows in each rounding mode. * stdlib/tst-strtod-round-data: Add more tests. * stdlib/tst-strtod-round-data.h: Regenerated. * stdlib/tst-strtod-round-skeleton.c (_XNTRY): Update comment. (TEST): Handle extra arguments for overflow flags. (struct test_overflow): New type. [!FE_OVERFLOW] (FE_OVERFLOW): Define to 0. (GEN_ONE_TEST): Clear all exceptions. Test overflow flag. (test_in_one_mode): Take argument with overflow information. (do_test): Update calls to test_in_one_mode.
2018-06-13 16:06:14 +00:00
{
r = 1;
overflow = true;
}
}
mpfr_fprintf (fout, "\t%s,\n", r ? "false" : "true");
Fix strtod overflow detection (bug 23279). As shown by bug 23279, strtod's round_and_return has an off-by-one error in its overflow detection, only counting an exponent greater than MAX_EXP as overflowing when an exponent of MAX_EXP also means overflow (recall the ISO C definition of DBL_MAX_EXP etc. is based on a floating-point model where 2^exp is multiplied by a value in the interval [0.5, 1), so 2^MAX_EXP is not representable). For decimal arguments to strtod, a separate overflow check in the main implementation covers the case where the integer part of the argument (truncated to the nearest integer towards zero) has more than MAX_EXP bits, meaning that this issue in round_and_return only affects cases (arguments with absolute value strictly between the maximum representable value and 2^MAX_EXP) where overflow depends on the rounding mode; in such cases, the returned value would still have been correct on overflow but without the overflow exception being raised or errno being set to ERANGE. For hex float arguments, however, other cases can arise, as shown in bug 23279, where a value with exponent already set to MAX_EXP is passed into round_and_return and a result can wrongly end up being NaN, or infinity instead of the largest finite value. This patch fixes the off-by-one error, adds testing of overflow exceptions to the tst-strtod-round framework, and adds tests of these issues. Tested for x86_64. Also ran the tst-strtod-round tests for powerpc to make sure the new tests didn't introduce any new failures for IBM long double. [BZ #23279] * stdlib/strtod_l.c (round_and_return): Handle an exponent of MAX_EXP as overflowing. * stdlib/gen-tst-strtod-round.c (string_to_fp): Clear MPFR overflow flag. (round_str): Output also whether result overflows in each rounding mode. * stdlib/tst-strtod-round-data: Add more tests. * stdlib/tst-strtod-round-data.h: Regenerated. * stdlib/tst-strtod-round-skeleton.c (_XNTRY): Update comment. (TEST): Handle extra arguments for overflow flags. (struct test_overflow): New type. [!FE_OVERFLOW] (FE_OVERFLOW): Define to 0. (GEN_ONE_TEST): Clear all exceptions. Test overflow flag. (test_in_one_mode): Take argument with overflow information. (do_test): Update calls to test_in_one_mode.
2018-06-13 16:06:14 +00:00
print_fp (fout, f, overflow ? ", true,\n" : ", false,\n");
2012-08-27 16:01:27 +00:00
string_to_fp (f, s, MPFR_RNDN);
Fix strtod overflow detection (bug 23279). As shown by bug 23279, strtod's round_and_return has an off-by-one error in its overflow detection, only counting an exponent greater than MAX_EXP as overflowing when an exponent of MAX_EXP also means overflow (recall the ISO C definition of DBL_MAX_EXP etc. is based on a floating-point model where 2^exp is multiplied by a value in the interval [0.5, 1), so 2^MAX_EXP is not representable). For decimal arguments to strtod, a separate overflow check in the main implementation covers the case where the integer part of the argument (truncated to the nearest integer towards zero) has more than MAX_EXP bits, meaning that this issue in round_and_return only affects cases (arguments with absolute value strictly between the maximum representable value and 2^MAX_EXP) where overflow depends on the rounding mode; in such cases, the returned value would still have been correct on overflow but without the overflow exception being raised or errno being set to ERANGE. For hex float arguments, however, other cases can arise, as shown in bug 23279, where a value with exponent already set to MAX_EXP is passed into round_and_return and a result can wrongly end up being NaN, or infinity instead of the largest finite value. This patch fixes the off-by-one error, adds testing of overflow exceptions to the tst-strtod-round framework, and adds tests of these issues. Tested for x86_64. Also ran the tst-strtod-round tests for powerpc to make sure the new tests didn't introduce any new failures for IBM long double. [BZ #23279] * stdlib/strtod_l.c (round_and_return): Handle an exponent of MAX_EXP as overflowing. * stdlib/gen-tst-strtod-round.c (string_to_fp): Clear MPFR overflow flag. (round_str): Output also whether result overflows in each rounding mode. * stdlib/tst-strtod-round-data: Add more tests. * stdlib/tst-strtod-round-data.h: Regenerated. * stdlib/tst-strtod-round-skeleton.c (_XNTRY): Update comment. (TEST): Handle extra arguments for overflow flags. (struct test_overflow): New type. [!FE_OVERFLOW] (FE_OVERFLOW): Define to 0. (GEN_ONE_TEST): Clear all exceptions. Test overflow flag. (test_in_one_mode): Take argument with overflow information. (do_test): Update calls to test_in_one_mode.
2018-06-13 16:06:14 +00:00
overflow = (mpfr_overflow_p () != 0
|| (ibm_ld && mpfr_cmpabs (f, max_value) > 0));
print_fp (fout, f, overflow ? ", true,\n" : ", false,\n");
2012-08-27 16:01:27 +00:00
string_to_fp (f, s, MPFR_RNDZ);
Fix strtod overflow detection (bug 23279). As shown by bug 23279, strtod's round_and_return has an off-by-one error in its overflow detection, only counting an exponent greater than MAX_EXP as overflowing when an exponent of MAX_EXP also means overflow (recall the ISO C definition of DBL_MAX_EXP etc. is based on a floating-point model where 2^exp is multiplied by a value in the interval [0.5, 1), so 2^MAX_EXP is not representable). For decimal arguments to strtod, a separate overflow check in the main implementation covers the case where the integer part of the argument (truncated to the nearest integer towards zero) has more than MAX_EXP bits, meaning that this issue in round_and_return only affects cases (arguments with absolute value strictly between the maximum representable value and 2^MAX_EXP) where overflow depends on the rounding mode; in such cases, the returned value would still have been correct on overflow but without the overflow exception being raised or errno being set to ERANGE. For hex float arguments, however, other cases can arise, as shown in bug 23279, where a value with exponent already set to MAX_EXP is passed into round_and_return and a result can wrongly end up being NaN, or infinity instead of the largest finite value. This patch fixes the off-by-one error, adds testing of overflow exceptions to the tst-strtod-round framework, and adds tests of these issues. Tested for x86_64. Also ran the tst-strtod-round tests for powerpc to make sure the new tests didn't introduce any new failures for IBM long double. [BZ #23279] * stdlib/strtod_l.c (round_and_return): Handle an exponent of MAX_EXP as overflowing. * stdlib/gen-tst-strtod-round.c (string_to_fp): Clear MPFR overflow flag. (round_str): Output also whether result overflows in each rounding mode. * stdlib/tst-strtod-round-data: Add more tests. * stdlib/tst-strtod-round-data.h: Regenerated. * stdlib/tst-strtod-round-skeleton.c (_XNTRY): Update comment. (TEST): Handle extra arguments for overflow flags. (struct test_overflow): New type. [!FE_OVERFLOW] (FE_OVERFLOW): Define to 0. (GEN_ONE_TEST): Clear all exceptions. Test overflow flag. (test_in_one_mode): Take argument with overflow information. (do_test): Update calls to test_in_one_mode.
2018-06-13 16:06:14 +00:00
overflow = (mpfr_overflow_p () != 0
|| (ibm_ld && mpfr_cmpabs (f, max_value) > 0));
print_fp (fout, f, overflow ? ", true,\n" : ", false,\n");
2012-08-27 16:01:27 +00:00
string_to_fp (f, s, MPFR_RNDU);
Fix strtod overflow detection (bug 23279). As shown by bug 23279, strtod's round_and_return has an off-by-one error in its overflow detection, only counting an exponent greater than MAX_EXP as overflowing when an exponent of MAX_EXP also means overflow (recall the ISO C definition of DBL_MAX_EXP etc. is based on a floating-point model where 2^exp is multiplied by a value in the interval [0.5, 1), so 2^MAX_EXP is not representable). For decimal arguments to strtod, a separate overflow check in the main implementation covers the case where the integer part of the argument (truncated to the nearest integer towards zero) has more than MAX_EXP bits, meaning that this issue in round_and_return only affects cases (arguments with absolute value strictly between the maximum representable value and 2^MAX_EXP) where overflow depends on the rounding mode; in such cases, the returned value would still have been correct on overflow but without the overflow exception being raised or errno being set to ERANGE. For hex float arguments, however, other cases can arise, as shown in bug 23279, where a value with exponent already set to MAX_EXP is passed into round_and_return and a result can wrongly end up being NaN, or infinity instead of the largest finite value. This patch fixes the off-by-one error, adds testing of overflow exceptions to the tst-strtod-round framework, and adds tests of these issues. Tested for x86_64. Also ran the tst-strtod-round tests for powerpc to make sure the new tests didn't introduce any new failures for IBM long double. [BZ #23279] * stdlib/strtod_l.c (round_and_return): Handle an exponent of MAX_EXP as overflowing. * stdlib/gen-tst-strtod-round.c (string_to_fp): Clear MPFR overflow flag. (round_str): Output also whether result overflows in each rounding mode. * stdlib/tst-strtod-round-data: Add more tests. * stdlib/tst-strtod-round-data.h: Regenerated. * stdlib/tst-strtod-round-skeleton.c (_XNTRY): Update comment. (TEST): Handle extra arguments for overflow flags. (struct test_overflow): New type. [!FE_OVERFLOW] (FE_OVERFLOW): Define to 0. (GEN_ONE_TEST): Clear all exceptions. Test overflow flag. (test_in_one_mode): Take argument with overflow information. (do_test): Update calls to test_in_one_mode.
2018-06-13 16:06:14 +00:00
overflow = (mpfr_overflow_p () != 0
|| (ibm_ld && mpfr_cmpabs (f, max_value) > 0));
print_fp (fout, f, overflow ? ", true" : ", false");
2012-08-27 16:01:27 +00:00
mpfr_clear (f);
Fix strtod overflow detection (bug 23279). As shown by bug 23279, strtod's round_and_return has an off-by-one error in its overflow detection, only counting an exponent greater than MAX_EXP as overflowing when an exponent of MAX_EXP also means overflow (recall the ISO C definition of DBL_MAX_EXP etc. is based on a floating-point model where 2^exp is multiplied by a value in the interval [0.5, 1), so 2^MAX_EXP is not representable). For decimal arguments to strtod, a separate overflow check in the main implementation covers the case where the integer part of the argument (truncated to the nearest integer towards zero) has more than MAX_EXP bits, meaning that this issue in round_and_return only affects cases (arguments with absolute value strictly between the maximum representable value and 2^MAX_EXP) where overflow depends on the rounding mode; in such cases, the returned value would still have been correct on overflow but without the overflow exception being raised or errno being set to ERANGE. For hex float arguments, however, other cases can arise, as shown in bug 23279, where a value with exponent already set to MAX_EXP is passed into round_and_return and a result can wrongly end up being NaN, or infinity instead of the largest finite value. This patch fixes the off-by-one error, adds testing of overflow exceptions to the tst-strtod-round framework, and adds tests of these issues. Tested for x86_64. Also ran the tst-strtod-round tests for powerpc to make sure the new tests didn't introduce any new failures for IBM long double. [BZ #23279] * stdlib/strtod_l.c (round_and_return): Handle an exponent of MAX_EXP as overflowing. * stdlib/gen-tst-strtod-round.c (string_to_fp): Clear MPFR overflow flag. (round_str): Output also whether result overflows in each rounding mode. * stdlib/tst-strtod-round-data: Add more tests. * stdlib/tst-strtod-round-data.h: Regenerated. * stdlib/tst-strtod-round-skeleton.c (_XNTRY): Update comment. (TEST): Handle extra arguments for overflow flags. (struct test_overflow): New type. [!FE_OVERFLOW] (FE_OVERFLOW): Define to 0. (GEN_ONE_TEST): Clear all exceptions. Test overflow flag. (test_in_one_mode): Take argument with overflow information. (do_test): Update calls to test_in_one_mode.
2018-06-13 16:06:14 +00:00
if (ibm_ld)
mpfr_clear (max_value);
2012-08-27 16:01:27 +00:00
}
static void
round_for_all (FILE *fout, const char *s)
2012-08-27 16:01:27 +00:00
{
static const struct fmt {
int prec;
int emin;
int emax;
bool ibm_ld;
} formats[] = {
{ 24, -148, 128, false },
{ 53, -1073, 1024, false },
/* This is the Intel extended float format. */
{ 64, -16444, 16384, false },
/* This is the Motorola extended float format. */
{ 64, -16445, 16384, false },
{ 106, -1073, 1024, true },
{ 113, -16493, 16384, false },
2012-08-27 16:01:27 +00:00
};
mpfr_fprintf (fout, " TEST (\"");
2012-08-27 16:01:27 +00:00
const char *p;
for (p = s; *p; p++)
{
fputc (*p, fout);
2012-08-27 16:01:27 +00:00
if ((p - s) % 60 == 59 && p[1])
mpfr_fprintf (fout, "\"\n\t\"");
2012-08-27 16:01:27 +00:00
}
mpfr_fprintf (fout, "\",\n");
2012-08-27 16:01:27 +00:00
int i;
int n_formats = sizeof (formats) / sizeof (formats[0]);
for (i = 0; i < n_formats; i++)
2012-08-27 16:01:27 +00:00
{
round_str (fout, s, formats[i].prec, formats[i].emin,
formats[i].emax, formats[i].ibm_ld);
if (i < n_formats - 1)
mpfr_fprintf (fout, ",\n");
2012-08-27 16:01:27 +00:00
}
mpfr_fprintf (fout, "),\n");
2012-08-27 16:01:27 +00:00
}
int
main (int argc, char **argv)
2012-08-27 16:01:27 +00:00
{
char *p = NULL;
size_t len;
ssize_t nbytes;
FILE *fin, *fout;
char *fin_name, *fout_name;
if (argc < 3)
{
fprintf (stderr, "Usage: %s <input> <output>\n", basename (argv[0]));
return EXIT_FAILURE;
}
fin_name = argv[1];
fout_name = argv[2];
fin = fopen (fin_name, "r");
if (fin == NULL)
{
perror ("Could not open input for reading");
return EXIT_FAILURE;
}
fout = fopen (fout_name, "w");
if (fout == NULL)
{
perror ("Could not open output for writing");
return EXIT_FAILURE;
}
fprintf (fout, "/* This file was generated by %s from %s. */\n",
__FILE__, fin_name);
fputs ("static const struct test tests[] = {\n", fout);
while ((nbytes = getline (&p, &len, fin)) != -1)
2012-08-27 16:01:27 +00:00
{
if (p[nbytes - 1] == '\n')
p[nbytes - 1] = 0;
round_for_all (fout, p);
2012-08-27 16:01:27 +00:00
free (p);
p = NULL;
}
fputs ("};\n", fout);
return EXIT_SUCCESS;
2012-08-27 16:01:27 +00:00
}