Get isfinite, isinf, and isnan right in jerry-math (#4497)

- `finite` is not C99 but a BSD fp classification function, so it
  is removed.
- The standard specifies that `isnan` is a macro (just like
  `isfinite` and `isinf`), so the `isnan` function implementation
  is removed.
- `isfinite` incorrectly classified NAN as finite, which is fixed.
- `isinf` returned 1 for negative infinity. This is not a bug
  according to the standard, but is not aligned with recent glibc,
  which returns -1. This is changed to simplify testing.
- Added test cases for `isfinite` and `isinf` to the unit test of
  jerry-math.
- Added a new pass to unittests to ensure that jerry-math is
  tested.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
Akos Kiss 2021-01-18 15:07:25 +01:00 committed by GitHub
parent c4676a21fe
commit ef8a6a9f39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 111 deletions

View File

@ -43,10 +43,8 @@ set(SOURCE_MATH
exp.c
expm1.c
fabs.c
finite.c
floor.c
fmod.c
isnan.c
log.c
log10.c
log1p.c

View File

@ -1,41 +0,0 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is based on work under the following copyright and permission
* notice:
*
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunSoft, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
*
* @(#)s_finite.c 1.3 95/01/18
*/
#include "jerry-math-internal.h"
/* finite(x) returns 1 is x is finite, else 0;
* no branching!
*/
int
finite (double x)
{
int hx;
hx = __HI (x);
return (unsigned) ((hx & 0x7fffffff) - 0x7ff00000) >> 31;
} /* finite */

View File

@ -27,8 +27,8 @@ extern "C"
#define HUGE_VAL INFINITY
#define isnan(x) ((x) != (x))
#define isinf(x) (((x) == INFINITY) || ((x) == -INFINITY))
#define isfinite(x) (!(isinf(x)) && (x != NAN))
#define isinf(x) ((x) == INFINITY ? 1 : (x) == -INFINITY ? -1 : 0)
#define isfinite(x) (!isinf(x) && !isnan(x))
/* Exponential and Logarithmic constants. */
#define M_E 2.7182818284590452353602874713526625

View File

@ -1,44 +0,0 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is based on work under the following copyright and permission
* notice:
*
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunSoft, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
*
* @(#)s_isnan.c 1.3 95/01/18
*/
#include "jerry-math-internal.h"
/* isnan(x) returns 1 is x is nan, else 0;
* no branching!
*/
int
isnan (double x)
{
int hx, lx;
hx = (__HI (x) & 0x7fffffff);
lx = __LO (x);
hx |= (unsigned) (lx | (-lx)) >> 31;
hx = 0x7ff00000 - hx;
return ((unsigned) (hx)) >> 31;
} /* isnan */

View File

@ -112,9 +112,6 @@ double fabs (double x);
double floor (double x);
double fmod (double x, double y);
int isnan (double x);
int finite (double x);
double nextafter (double x, double y);
/*

View File

@ -396,6 +396,36 @@ check_double ("fmod (6.5, 2.3)", fmod (6.5, 2.3), 1.90000000000000035527E+00);
check_double ("fmod (6.5, -2.3)", fmod (6.5, -2.3), 1.90000000000000035527E+00);
check_double ("fmod (-6.5, 2.3)", fmod (-6.5, 2.3), -1.90000000000000035527E+00);
check_double ("fmod (-6.5, -2.3)", fmod (-6.5, -2.3), -1.90000000000000035527E+00);
check_int ("isfinite (0.0)", isfinite (0.0), 1);
check_int ("isfinite (-0.0)", isfinite (-0.0), 1);
check_int ("isfinite (1.0)", isfinite (1.0), 1);
check_int ("isfinite (-1.0)", isfinite (-1.0), 1);
check_int ("isfinite (INFINITY)", isfinite (INFINITY), 0);
check_int ("isfinite (-INFINITY)", isfinite (-INFINITY), 0);
check_int ("isfinite (NAN)", isfinite (NAN), 0);
check_int ("isfinite (3.14)", isfinite (3.14), 1);
check_int ("isfinite (-3.14)", isfinite (-3.14), 1);
check_int ("isfinite (0.7)", isfinite (0.7), 1);
check_int ("isfinite (-0.7)", isfinite (-0.7), 1);
check_int ("isfinite (3.72e-09)", isfinite (3.72e-09), 1);
check_int ("isfinite (-3.72e-09)", isfinite (-3.72e-09), 1);
check_int ("isfinite (7.37e+19)", isfinite (7.37e+19), 1);
check_int ("isfinite (-7.37e+19)", isfinite (-7.37e+19), 1);
check_int ("isinf (0.0)", isinf (0.0), 0);
check_int ("isinf (-0.0)", isinf (-0.0), 0);
check_int ("isinf (1.0)", isinf (1.0), 0);
check_int ("isinf (-1.0)", isinf (-1.0), 0);
check_int ("isinf (INFINITY)", isinf (INFINITY), 1);
check_int ("isinf (-INFINITY)", isinf (-INFINITY), -1);
check_int ("isinf (NAN)", isinf (NAN), 0);
check_int ("isinf (3.14)", isinf (3.14), 0);
check_int ("isinf (-3.14)", isinf (-3.14), 0);
check_int ("isinf (0.7)", isinf (0.7), 0);
check_int ("isinf (-0.7)", isinf (-0.7), 0);
check_int ("isinf (3.72e-09)", isinf (3.72e-09), 0);
check_int ("isinf (-3.72e-09)", isinf (-3.72e-09), 0);
check_int ("isinf (7.37e+19)", isinf (7.37e+19), 0);
check_int ("isinf (-7.37e+19)", isinf (-7.37e+19), 0);
check_int ("isnan (0.0)", isnan (0.0), 0);
check_int ("isnan (-0.0)", isnan (-0.0), 0);
check_int ("isnan (1.0)", isnan (1.0), 0);

View File

@ -72,6 +72,9 @@ JERRY_UNITTESTS_OPTIONS = [
skip=skip_if((sys.platform == 'win32'), 'FEATURE_INIT_FINI build flag isn\'t supported on Windows,' +
' because Microsoft Visual C/C++ Compiler doesn\'t support' +
' library constructors and destructors.')),
Options('unittests-es5.1-debug-math',
OPTIONS_COMMON + OPTIONS_UNITTESTS + OPTIONS_PROFILE_ES51 + OPTIONS_DEBUG
+ ['--jerry-math=on']),
]
# Test options for jerry-tests

View File

@ -442,25 +442,6 @@ main (int argc, char **args)
GEN_DBL_TEST (fabs (7.37e+19));
GEN_DBL_TEST (fabs (-7.37e+19));
/* finite tests */
/* SKIPPED: not publicly declared in jerry-math
GEN_INT_TEST (finite (0.0));
GEN_INT_TEST (finite (-0.0));
GEN_INT_TEST (finite (1.0));
GEN_INT_TEST (finite (-1.0));
GEN_INT_TEST (finite (INFINITY));
GEN_INT_TEST (finite (-INFINITY));
GEN_INT_TEST (finite (NAN));
GEN_INT_TEST (finite (3.14));
GEN_INT_TEST (finite (-3.14));
GEN_INT_TEST (finite (0.7));
GEN_INT_TEST (finite (-0.7));
GEN_INT_TEST (finite (3.72e-09));
GEN_INT_TEST (finite (-3.72e-09));
GEN_INT_TEST (finite (7.37e+19));
GEN_INT_TEST (finite (-7.37e+19));
*/
/* floor tests */
GEN_DBL_TEST (floor (0.0));
GEN_DBL_TEST (floor (-0.0));
@ -533,6 +514,40 @@ main (int argc, char **args)
GEN_DBL_TEST (fmod (-6.5, 2.3));
GEN_DBL_TEST (fmod (-6.5, -2.3));
/* isfinite tests */
GEN_INT_TEST (isfinite (0.0));
GEN_INT_TEST (isfinite (-0.0));
GEN_INT_TEST (isfinite (1.0));
GEN_INT_TEST (isfinite (-1.0));
GEN_INT_TEST (isfinite (INFINITY));
GEN_INT_TEST (isfinite (-INFINITY));
GEN_INT_TEST (isfinite (NAN));
GEN_INT_TEST (isfinite (3.14));
GEN_INT_TEST (isfinite (-3.14));
GEN_INT_TEST (isfinite (0.7));
GEN_INT_TEST (isfinite (-0.7));
GEN_INT_TEST (isfinite (3.72e-09));
GEN_INT_TEST (isfinite (-3.72e-09));
GEN_INT_TEST (isfinite (7.37e+19));
GEN_INT_TEST (isfinite (-7.37e+19));
/* isinf tests */
GEN_INT_TEST (isinf (0.0));
GEN_INT_TEST (isinf (-0.0));
GEN_INT_TEST (isinf (1.0));
GEN_INT_TEST (isinf (-1.0));
GEN_INT_TEST (isinf (INFINITY));
GEN_INT_TEST (isinf (-INFINITY));
GEN_INT_TEST (isinf (NAN));
GEN_INT_TEST (isinf (3.14));
GEN_INT_TEST (isinf (-3.14));
GEN_INT_TEST (isinf (0.7));
GEN_INT_TEST (isinf (-0.7));
GEN_INT_TEST (isinf (3.72e-09));
GEN_INT_TEST (isinf (-3.72e-09));
GEN_INT_TEST (isinf (7.37e+19));
GEN_INT_TEST (isinf (-7.37e+19));
/* isnan tests */
GEN_INT_TEST (isnan (0.0));
GEN_INT_TEST (isnan (-0.0));