2016-07-29 00:14:11 +08:00
|
|
|
/* Test for correct rounding of results of strtod and related
|
|
|
|
functions.
|
2023-01-07 05:08:04 +08:00
|
|
|
Copyright (C) 2012-2023 Free Software Foundation, Inc.
|
2016-07-29 00:14:11 +08: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 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2016-07-29 00:14:11 +08:00
|
|
|
|
|
|
|
/* Defining _LIBC_TEST ensures long double math functions are
|
|
|
|
declared in the headers. */
|
|
|
|
#define _LIBC_TEST 1
|
2017-11-25 05:42:55 +08:00
|
|
|
#define __STDC_WANT_IEC_60559_TYPES_EXT__
|
2016-07-29 00:14:11 +08:00
|
|
|
#include <fenv.h>
|
|
|
|
#include <float.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math-tests.h>
|
|
|
|
|
|
|
|
#include "tst-strtod.h"
|
|
|
|
|
|
|
|
/* Non-standard macros expected to be externally defined:
|
|
|
|
|
|
|
|
L_(str): Pastes the appropriate modifier to a string literal str.
|
|
|
|
|
|
|
|
FNPFX: Expands to the correct prefix for the strtod equivalent
|
|
|
|
of type CHAR. (e.g str or wcs).
|
|
|
|
|
|
|
|
CHAR: Expands to the string type being tested (e.g wchar_t or char).
|
|
|
|
|
|
|
|
STRM: Expands to a string literal suitable for printing CHAR* via
|
|
|
|
printf (e.g "%s" or "%ls"). */
|
|
|
|
|
|
|
|
#define _CONCAT(a, b) a ## b
|
|
|
|
#define CONCAT(a, b) _CONCAT (a, b)
|
|
|
|
|
|
|
|
#define STRTO(x) CONCAT (CONCAT (FNPFX, to), x)
|
|
|
|
|
|
|
|
#if LDBL_MANT_DIG == 106 && LDBL_MAX_EXP == 1024
|
|
|
|
/* This is a stupid hack for IBM long double. This test ignores
|
|
|
|
inexact values for long double due to the limitations of the
|
|
|
|
format. This ensures rounding tests are ignored. */
|
|
|
|
# undef ROUNDING_TESTS_long_double
|
|
|
|
# define ROUNDING_TESTS_long_double(x) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Generator to create an FTYPE member variabled named FSUF
|
|
|
|
used to populate struct member variables. */
|
2016-08-30 14:13:21 +08:00
|
|
|
#define FTYPE_MEMBER(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \
|
2016-07-29 00:14:11 +08:00
|
|
|
FTYPE FSUF;
|
|
|
|
|
|
|
|
/* Likewise, but each member is of type bool. */
|
2016-08-30 14:13:21 +08:00
|
|
|
#define BOOL_MEMBER(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \
|
2016-07-29 00:14:11 +08:00
|
|
|
bool FSUF;
|
|
|
|
|
|
|
|
#define STRUCT_FOREACH_FLOAT_FTYPE GEN_TEST_STRTOD_FOREACH (FTYPE_MEMBER)
|
|
|
|
#define STRUCT_FOREACH_FLOAT_BOOL GEN_TEST_STRTOD_FOREACH (BOOL_MEMBER)
|
|
|
|
|
|
|
|
/* Define the long double choose (CHOOSE_ld) macro
|
|
|
|
to select the appropriate generated long double
|
|
|
|
value from the generated test data. */
|
|
|
|
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
|
|
|
/* This is for the long double == double format. */
|
|
|
|
# define CHOOSE_ld(f,d,...) d
|
|
|
|
#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16381
|
|
|
|
/* This is for the Intel extended float format. */
|
|
|
|
# define CHOOSE_ld(f,d,ld64i,...) ld64i
|
|
|
|
#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16382
|
|
|
|
/* This is for the Motorola extended float format. */
|
|
|
|
# define CHOOSE_ld(f,d,ld64i,ld64m,...) ld64m
|
|
|
|
#elif LDBL_MANT_DIG == 106 && LDBL_MAX_EXP == 1024
|
|
|
|
/* This is for the IBM extended double format. */
|
|
|
|
# define CHOOSE_ld(f,d,ld64i,ld64m,ld106,...) ld106
|
|
|
|
#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
|
|
|
|
/* This is for the IEEE binary128 format. */
|
|
|
|
# define CHOOSE_ld(f,d,ld64i,ld64m,ld106,ld113,...) ld113
|
|
|
|
#else
|
|
|
|
# error "unknown long double format"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Add type specific choosing macros below. */
|
|
|
|
#define CHOOSE_f(f,...) f
|
2017-11-08 02:08:44 +08:00
|
|
|
#define CHOOSE_f32(f,...) f
|
2016-07-29 00:14:11 +08:00
|
|
|
#define CHOOSE_d(f,d,...) d
|
2017-11-08 02:08:44 +08:00
|
|
|
#define CHOOSE_f64(f,d,...) d
|
|
|
|
#define CHOOSE_f32x(f,d,...) d
|
2016-07-26 02:10:00 +08:00
|
|
|
#define CHOOSE_f128(f,d,ld64i,ld64m,ld106,ld113,...) ld113
|
2017-11-08 02:08:44 +08:00
|
|
|
/* long double is special, and handled above. _Float16 would require
|
|
|
|
updates to the generator to generate appropriate expectations, and
|
|
|
|
updates to the test inputs to cover difficult rounding cases for
|
|
|
|
_Float16. */
|
|
|
|
|
|
|
|
#if __HAVE_FLOAT64X
|
|
|
|
# if FLT64X_MANT_DIG == 113 && FLT64X_MAX_EXP == 16384
|
|
|
|
# define CHOOSE_f64x(f,d,ld64i,ld64m,ld106,ld113,...) ld113
|
|
|
|
# elif (FLT64X_MANT_DIG == 64 \
|
|
|
|
&& FLT64X_MAX_EXP == 16384 \
|
|
|
|
&& FLT64X_MIN_EXP == -16381)
|
|
|
|
# define CHOOSE_f64x(f,d,ld64i,...) ld64i
|
|
|
|
# else
|
|
|
|
# error "unknown _Float64x format"
|
|
|
|
# endif
|
|
|
|
#endif
|
2016-07-29 00:14:11 +08:00
|
|
|
|
|
|
|
/* Selector for expected result field of a given type. */
|
2016-08-30 14:13:21 +08:00
|
|
|
#define _ENTRY(FSUF, FTYPE, FTOSTR, LSUF, CSUF, ...) \
|
2016-07-29 00:14:11 +08:00
|
|
|
CONCAT (CHOOSE_ ## FSUF (__VA_ARGS__), LSUF),
|
|
|
|
#define ENTRY(...) \
|
|
|
|
GEN_TEST_STRTOD_FOREACH (_ENTRY, __VA_ARGS__)
|
|
|
|
|
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-14 00:06:14 +08:00
|
|
|
/* Selector for boolean exact tag of expected results and that for
|
|
|
|
overflow. */
|
2016-08-30 14:13:21 +08:00
|
|
|
#define _XNTRY(FSUF, FTYPE, FTOSTR, LSUF, CSUF, ...) \
|
2016-07-29 00:14:11 +08:00
|
|
|
CHOOSE_ ## FSUF (__VA_ARGS__),
|
|
|
|
#define XNTRY(...) \
|
|
|
|
GEN_TEST_STRTOD_FOREACH (_XNTRY, __VA_ARGS__)
|
|
|
|
|
|
|
|
/* This is hacky way around the seemingly unavoidable macro
|
|
|
|
expansion of the INFINITY or HUGE_VAL like macros in the
|
|
|
|
above. It is assumed the compiler will implicitly convert
|
|
|
|
the infinity correctly. */
|
|
|
|
#define INF INFINITY + 0.0
|
|
|
|
|
|
|
|
/* This macro is used in conjunction with the output from the
|
|
|
|
gen-tst-strtod-round utility to select the appropriately
|
|
|
|
rounded long double value for a given format. */
|
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-14 00:06:14 +08:00
|
|
|
#define TEST(s, \
|
|
|
|
fx, fd, fdo, fn, fno, fz, fzo, fu, fuo, \
|
|
|
|
dx, dd, ddo, dn, dno, dz, dzo, du, duo, \
|
|
|
|
ld64ix, ld64id, ld64ido, ld64in, ld64ino, \
|
|
|
|
ld64iz, ld64izo, ld64iu, ld64iuo, \
|
|
|
|
ld64mx, ld64md, ld64mdo, ld64mn, ld64mno, \
|
|
|
|
ld64mz, ld64mzo, ld64mu, ld64muo, \
|
|
|
|
ld106x, ld106d, ld106do, ld106n, ld106no, \
|
|
|
|
ld106z, ld106zo, ld106u, ld106uo, \
|
|
|
|
ld113x, ld113d, ld113do, ld113n, ld113no, \
|
|
|
|
ld113z, ld113zo, ld113u, ld113uo) \
|
|
|
|
{ \
|
|
|
|
L_ (s), \
|
|
|
|
{ XNTRY (fx, dx, ld64ix, ld64mx, ld106x, ld113x) }, \
|
|
|
|
{ \
|
|
|
|
{ ENTRY (fn, dn, ld64in, ld64mn, ld106n, ld113n) }, \
|
|
|
|
{ ENTRY (fd, dd, ld64id, ld64md, ld106d, ld113d) }, \
|
|
|
|
{ ENTRY (fz, dz, ld64iz, ld64mz, ld106z, ld113z) }, \
|
|
|
|
{ ENTRY (fu, du, ld64iu, ld64mu, ld106u, ld113u) } \
|
|
|
|
}, \
|
|
|
|
{ \
|
|
|
|
{ XNTRY (fno, dno, ld64ino, ld64mno, ld106no, ld113no) }, \
|
|
|
|
{ XNTRY (fdo, ddo, ld64ido, ld64mdo, ld106do, ld113do) }, \
|
|
|
|
{ XNTRY (fzo, dzo, ld64izo, ld64mzo, ld106zo, ld113zo) }, \
|
|
|
|
{ XNTRY (fuo, duo, ld64iuo, ld64muo, ld106uo, ld113uo) } \
|
|
|
|
} \
|
2016-07-29 00:14:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct test_exactness
|
|
|
|
{
|
|
|
|
STRUCT_FOREACH_FLOAT_BOOL
|
|
|
|
};
|
|
|
|
|
|
|
|
struct test_results
|
|
|
|
{
|
|
|
|
STRUCT_FOREACH_FLOAT_FTYPE
|
|
|
|
};
|
|
|
|
|
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-14 00:06:14 +08:00
|
|
|
struct test_overflow
|
|
|
|
{
|
|
|
|
STRUCT_FOREACH_FLOAT_BOOL
|
|
|
|
};
|
|
|
|
|
2016-07-29 00:14:11 +08:00
|
|
|
struct test {
|
|
|
|
const CHAR *s;
|
|
|
|
struct test_exactness exact;
|
|
|
|
struct test_results r[4];
|
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-14 00:06:14 +08:00
|
|
|
struct test_overflow o[4];
|
2016-07-29 00:14:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Include the generated test data. */
|
|
|
|
#include "tst-strtod-round-data.h"
|
|
|
|
|
|
|
|
#define STRX(x) #x
|
|
|
|
#define STR(x) STRX (x)
|
|
|
|
#define FNPFXS STR (FNPFX)
|
|
|
|
|
2016-10-28 08:40:23 +08:00
|
|
|
#ifndef FE_INEXACT
|
|
|
|
# define FE_INEXACT 0
|
|
|
|
#endif
|
|
|
|
|
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-14 00:06:14 +08:00
|
|
|
#ifndef FE_OVERFLOW
|
|
|
|
# define FE_OVERFLOW 0
|
|
|
|
#endif
|
|
|
|
|
2016-08-30 14:13:21 +08:00
|
|
|
#define GEN_ONE_TEST(FSUF, FTYPE, FTOSTR, LSUF, CSUF) \
|
2016-07-29 00:14:11 +08: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-14 00:06:14 +08:00
|
|
|
feclearexcept (FE_ALL_EXCEPT); \
|
2016-07-29 00:14:11 +08:00
|
|
|
FTYPE f = STRTO (FSUF) (s, NULL); \
|
|
|
|
if (f != expected->FSUF \
|
|
|
|
|| (copysign ## CSUF) (1.0 ## LSUF, f) \
|
|
|
|
!= (copysign ## CSUF) (1.0 ## LSUF, expected->FSUF)) \
|
|
|
|
{ \
|
2016-08-30 14:13:21 +08:00
|
|
|
char efstr[FSTRLENMAX]; \
|
|
|
|
char fstr[FSTRLENMAX]; \
|
|
|
|
FTOSTR (efstr, FSTRLENMAX, "%a", expected->FSUF); \
|
|
|
|
FTOSTR (fstr, FSTRLENMAX, "%a", f); \
|
|
|
|
printf (FNPFXS "to" #FSUF " (" STRM ") returned %s not " \
|
|
|
|
"%s (%s)\n", s, fstr, efstr, mode_name); \
|
2016-07-29 00:14:11 +08:00
|
|
|
if (ROUNDING_TESTS (FTYPE, rnd_mode) || exact->FSUF) \
|
|
|
|
result = 1; \
|
|
|
|
else \
|
|
|
|
printf ("ignoring this inexact result\n"); \
|
|
|
|
} \
|
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-14 00:06:14 +08:00
|
|
|
else \
|
2016-10-28 08:40:23 +08: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-14 00:06:14 +08:00
|
|
|
if (FE_INEXACT != 0) \
|
|
|
|
{ \
|
|
|
|
bool inexact_raised = fetestexcept (FE_INEXACT) != 0; \
|
|
|
|
if (inexact_raised != !exact->FSUF) \
|
|
|
|
{ \
|
|
|
|
printf (FNPFXS "to" #FSUF \
|
|
|
|
" (" STRM ") inexact %d " \
|
|
|
|
"not %d\n", s, inexact_raised, \
|
|
|
|
!exact->FSUF); \
|
|
|
|
if (EXCEPTION_TESTS (FTYPE)) \
|
|
|
|
result = 1; \
|
|
|
|
else \
|
|
|
|
printf ("ignoring this exception error\n"); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (FE_OVERFLOW != 0) \
|
2016-10-28 08:40:23 +08: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-14 00:06:14 +08:00
|
|
|
bool overflow_raised \
|
|
|
|
= fetestexcept (FE_OVERFLOW) != 0; \
|
|
|
|
if (overflow_raised != overflow->FSUF) \
|
|
|
|
{ \
|
|
|
|
printf (FNPFXS "to" #FSUF \
|
|
|
|
" (" STRM ") overflow %d " \
|
|
|
|
"not %d\n", s, overflow_raised, \
|
|
|
|
overflow->FSUF); \
|
|
|
|
if (EXCEPTION_TESTS (FTYPE)) \
|
|
|
|
result = 1; \
|
|
|
|
else \
|
|
|
|
printf ("ignoring this exception error\n"); \
|
|
|
|
} \
|
2016-10-28 08:40:23 +08:00
|
|
|
} \
|
|
|
|
} \
|
2016-07-29 00:14:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
test_in_one_mode (const CHAR *s, const struct test_results *expected,
|
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-14 00:06:14 +08:00
|
|
|
const struct test_exactness *exact,
|
|
|
|
const struct test_overflow *overflow,
|
|
|
|
const char *mode_name, int rnd_mode)
|
2016-07-29 00:14:11 +08:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
GEN_TEST_STRTOD_FOREACH (GEN_ONE_TEST)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct fetestmodes
|
|
|
|
{
|
|
|
|
const char *mode_name;
|
|
|
|
int rnd_mode;
|
|
|
|
int rnd_i; /* Corresponding index into r array of struct test. */
|
|
|
|
} modes[] = {
|
|
|
|
{ "default rounding mode", FE_TONEAREST, 0 },
|
|
|
|
#ifdef FE_DOWNWARD
|
|
|
|
{ "FE_DOWNWARD", FE_DOWNWARD, 1 },
|
|
|
|
#endif
|
|
|
|
#ifdef FE_TOWARDZERO
|
|
|
|
{ "FE_TOWARDZERO", FE_TOWARDZERO, 2 },
|
|
|
|
#endif
|
|
|
|
#ifdef FE_UPWARD
|
|
|
|
{ "FE_UPWARD", FE_UPWARD, 3 },
|
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
int save_round_mode __attribute__ ((unused)) = fegetround ();
|
|
|
|
int result = 0;
|
|
|
|
for (size_t i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
|
|
|
|
{
|
|
|
|
result |= test_in_one_mode (tests[i].s, &tests[i].r[modes[0].rnd_i],
|
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-14 00:06:14 +08:00
|
|
|
&tests[i].exact, &tests[i].o[modes[0].rnd_i],
|
|
|
|
modes[0].mode_name, modes[0].rnd_mode);
|
2016-07-29 00:14:11 +08:00
|
|
|
for (const struct fetestmodes *m = &modes[1]; m->mode_name != NULL; m++)
|
|
|
|
{
|
|
|
|
if (!fesetround (m->rnd_mode))
|
|
|
|
{
|
|
|
|
result |= test_in_one_mode (tests[i].s, &tests[i].r[m->rnd_i],
|
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-14 00:06:14 +08:00
|
|
|
&tests[i].exact,
|
|
|
|
&tests[i].o[m->rnd_i], m->mode_name,
|
2016-07-29 00:14:11 +08:00
|
|
|
m->rnd_mode);
|
|
|
|
fesetround (save_round_mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|