summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-08-27 10:52:02 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-08-27 12:41:30 +0900
commit04be8e84db1cf4f8b2a7bc7679eda4336da75d43 (patch)
treec7914614f4a5cf91109f7893b87ff9e5d143ad0e
parent37114673623c4d2b1d9f2c2ddfffba070b2d96e6 (diff)
downloadruby-04be8e84db1cf4f8b2a7bc7679eda4336da75d43.tar.gz
Use C99-defined macros to classify a floating-point number
-rw-r--r--LEGAL3
-rw-r--r--bignum.c2
-rw-r--r--common.mk1
-rw-r--r--configure.ac6
-rw-r--r--include/ruby/missing.h36
-rw-r--r--include/ruby/win32.h10
-rw-r--r--missing/erf.c15
-rw-r--r--missing/finite.c9
-rw-r--r--missing/isinf.c69
-rw-r--r--missing/isnan.c32
-rw-r--r--missing/tgamma.c15
-rw-r--r--numeric.c10
-rw-r--r--random.c2
-rw-r--r--rational.c4
-rw-r--r--sprintf.c2
15 files changed, 11 insertions, 205 deletions
diff --git a/LEGAL b/LEGAL
index ecd1e16050..a4a6027108 100644
--- a/LEGAL
+++ b/LEGAL
@@ -532,10 +532,7 @@ mentioned below.
[missing/acosh.c]
[missing/alloca.c]
[missing/erf.c]
-[missing/finite.c]
[missing/hypot.c]
-[missing/isinf.c]
-[missing/isnan.c]
[missing/lgamma_r.c]
[missing/memcmp.c]
[missing/memmove.c]
diff --git a/bignum.c b/bignum.c
index c954fe6755..939b33ca45 100644
--- a/bignum.c
+++ b/bignum.c
@@ -5382,7 +5382,7 @@ rb_integer_float_eq(VALUE x, VALUE y)
double yd = RFLOAT_VALUE(y);
double yi, yf;
- if (isnan(yd) || isinf(yd))
+ if (!isfinite(yd))
return Qfalse;
yf = modf(yd, &yi);
if (yf != 0)
diff --git a/common.mk b/common.mk
index 7605f5f6e5..74e8954db4 100644
--- a/common.mk
+++ b/common.mk
@@ -963,7 +963,6 @@ crypt.$(OBJEXT): {$(VPATH)}crypt.c {$(VPATH)}crypt.h {$(VPATH)}missing/des_table
dup2.$(OBJEXT): {$(VPATH)}dup2.c
erf.$(OBJEXT): {$(VPATH)}erf.c
explicit_bzero.$(OBJEXT): {$(VPATH)}explicit_bzero.c
-finite.$(OBJEXT): {$(VPATH)}finite.c
flock.$(OBJEXT): {$(VPATH)}flock.c
memcmp.$(OBJEXT): {$(VPATH)}memcmp.c
memmove.$(OBJEXT): {$(VPATH)}memmove.c
diff --git a/configure.ac b/configure.ac
index 341d535235..900967492d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -799,7 +799,7 @@ AS_IF([test "$GCC" = yes], [
])
],
[cygwin*|msys*|darwin*|netbsd*], [
- # need lgamma_r(), finite()
+ # need lgamma_r()
])
# ANSI (no XCFLAGS because this is C only)
@@ -1906,9 +1906,7 @@ AC_REPLACE_FUNCS(strlcpy)
AC_REPLACE_FUNCS(strstr)
AC_REPLACE_FUNCS(tgamma)
-RUBY_REPLACE_FUNC([finite], [@%:@include <math.h>])
-RUBY_REPLACE_FUNC([isinf], [@%:@include <math.h>])
-RUBY_REPLACE_FUNC([isnan], [@%:@include <math.h>])
+AC_DEFINE(HAVE_ISFINITE) # C99; backward compatibility
# for missing/setproctitle.c
AS_CASE(["$target_os"],
diff --git a/include/ruby/missing.h b/include/ruby/missing.h
index 7d551248ae..a4a2bbb8ee 100644
--- a/include/ruby/missing.h
+++ b/include/ruby/missing.h
@@ -92,10 +92,6 @@ RUBY_EXTERN int eaccess(const char*, int);
RUBY_EXTERN double round(double); /* numeric.c */
#endif
-#ifndef HAVE_FINITE
-RUBY_EXTERN int finite(double);
-#endif
-
#ifndef HAVE_FLOCK
RUBY_EXTERN int flock(int, int);
#endif
@@ -152,35 +148,9 @@ RUBY_EXTERN const union bytesequence4_or_float rb_nan;
# define HUGE_VAL ((double)INFINITY)
#endif
-#if defined(isinf)
-# /* Take that. */
-#elif defined(HAVE_ISINF)
-# /* Take that. */
-#elif defined(HAVE_FINITE) && defined(HAVE_ISNAN)
-# define isinf(x) (!finite(x) && !isnan(x))
-#elif defined(__cplusplus) && __cplusplus >= 201103L
-# // <cmath> must include constexpr bool isinf(double);
-#else
-RUBY_EXTERN int isinf(double);
-#endif
-
-#if defined(isnan)
-# /* Take that. */
-#elif defined(HAVE_ISNAN)
-# /* Take that. */
-#elif defined(__cplusplus) && __cplusplus >= 201103L
-# // <cmath> must include constexpr bool isnan(double);
-#else
-RUBY_EXTERN int isnan(double);
-#endif
-
-#if defined(isfinite)
-# /* Take that. */
-#elif defined(HAVE_ISFINITE)
-# /* Take that. */
-#else
-# define HAVE_ISFINITE 1
-# define isfinite(x) finite(x)
+#ifndef HAVE_FINITE
+# define HAVE_FINITE 1
+# define finite(x) isfinite(x)
#endif
#ifndef HAVE_NAN
diff --git a/include/ruby/win32.h b/include/ruby/win32.h
index 4978655e3c..c8ae599f2f 100644
--- a/include/ruby/win32.h
+++ b/include/ruby/win32.h
@@ -343,14 +343,6 @@ rb_infinity_float(void)
#endif
#if !defined __MINGW32__ || defined __NO_ISOCEXT
-#ifndef isnan
-#define isnan(x) _isnan(x)
-#endif
-static inline int
-finite(double x)
-{
- return _finite(x);
-}
#ifndef copysign
#define copysign(a, b) _copysign(a, b)
#endif
@@ -359,8 +351,6 @@ scalb(double a, long b)
{
return _scalb(a, b);
}
-#else
-__declspec(dllimport) extern int finite(double);
#endif
#if !defined S_IFIFO && defined _S_IFIFO
diff --git a/missing/erf.c b/missing/erf.c
index d72c4eaf4e..c2c9d5f7e9 100644
--- a/missing/erf.c
+++ b/missing/erf.c
@@ -7,21 +7,6 @@ reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten
#include <stdio.h>
#include <math.h>
-#ifdef _WIN32
-# include <float.h>
-# if !defined __MINGW32__ || defined __NO_ISOCEXT
-# ifndef isnan
-# define isnan(x) _isnan(x)
-# endif
-# ifndef isinf
-# define isinf(x) (!_finite(x) && !_isnan(x))
-# endif
-# ifndef finite
-# define finite(x) _finite(x)
-# endif
-# endif
-#endif
-
static double q_gamma(double, double, double);
/* Incomplete gamma function
diff --git a/missing/finite.c b/missing/finite.c
deleted file mode 100644
index ab7686317b..0000000000
--- a/missing/finite.c
+++ /dev/null
@@ -1,9 +0,0 @@
-/* public domain rewrite of finite(3) */
-
-#include "ruby/missing.h"
-
-int
-finite(double n)
-{
- return !isnan(n) && !isinf(n);
-}
diff --git a/missing/isinf.c b/missing/isinf.c
deleted file mode 100644
index ba24b7768b..0000000000
--- a/missing/isinf.c
+++ /dev/null
@@ -1,69 +0,0 @@
-/* public domain rewrite of isinf(3) */
-
-#ifdef __osf__
-
-#define _IEEE 1
-#include <nan.h>
-
-int
-isinf(double n)
-{
- if (IsNANorINF(n) && IsINF(n)) {
- return 1;
- }
- else {
- return 0;
- }
-}
-
-#else
-
-#include "ruby/internal/config.h"
-
-#if defined(HAVE_FINITE) && defined(HAVE_ISNAN)
-
-#include <math.h>
-#ifdef HAVE_IEEEFP_H
-#include <ieeefp.h>
-#endif
-
-/*
- * isinf may be provided only as a macro.
- * ex. HP-UX, Solaris 10
- * http://www.gnu.org/software/automake/manual/autoconf/Function-Portability.html
- */
-#ifndef isinf
-int
-isinf(double n)
-{
- return (!finite(n) && !isnan(n));
-}
-#endif
-
-#else
-
-#ifdef HAVE_STRING_H
-# include <string.h>
-#else
-# include <strings.h>
-#endif
-
-static double zero(void) { return 0.0; }
-static double one (void) { return 1.0; }
-static double inf (void) { return one() / zero(); }
-
-int
-isinf(double n)
-{
- static double pinf = 0.0;
- static double ninf = 0.0;
-
- if (pinf == 0.0) {
- pinf = inf();
- ninf = -pinf;
- }
- return memcmp(&n, &pinf, sizeof n) == 0
- || memcmp(&n, &ninf, sizeof n) == 0;
-}
-#endif
-#endif
diff --git a/missing/isnan.c b/missing/isnan.c
deleted file mode 100644
index ed10bf5cd6..0000000000
--- a/missing/isnan.c
+++ /dev/null
@@ -1,32 +0,0 @@
-/* public domain rewrite of isnan(3) */
-
-#include "ruby/missing.h"
-
-/*
- * isnan() may be a macro, a function or both.
- * (The C99 standard defines that isnan() is a macro, though.)
- * http://www.gnu.org/software/automake/manual/autoconf/Function-Portability.html
- *
- * macro only: uClibc
- * both: GNU libc
- *
- * This file is compile if no isnan() function is available.
- * (autoconf AC_REPLACE_FUNCS detects only the function.)
- * The macro is detected by following #ifndef.
- */
-
-#ifndef isnan
-static int double_ne(double n1, double n2);
-
-int
-isnan(double n)
-{
- return double_ne(n, n);
-}
-
-static int
-double_ne(double n1, double n2)
-{
- return n1 != n2;
-}
-#endif
diff --git a/missing/tgamma.c b/missing/tgamma.c
index c8638eab2b..82d614d755 100644
--- a/missing/tgamma.c
+++ b/missing/tgamma.c
@@ -14,21 +14,6 @@ reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten
#include <math.h>
#include <errno.h>
-#ifdef _WIN32
-# include <float.h>
-# if !defined __MINGW32__ || defined __NO_ISOCEXT
-# ifndef isnan
-# define isnan(x) _isnan(x)
-# endif
-# ifndef isinf
-# define isinf(x) (!_finite(x) && !_isnan(x))
-# endif
-# ifndef finite
-# define finite(x) _finite(x)
-# endif
-# endif
-#endif
-
#ifndef HAVE_LGAMMA_R
#include <errno.h>
diff --git a/numeric.c b/numeric.c
index f04cf61b0d..755051de2a 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1699,15 +1699,7 @@ rb_flo_is_finite_p(VALUE num)
{
double value = RFLOAT_VALUE(num);
-#ifdef HAVE_ISFINITE
- if (!isfinite(value))
- return Qfalse;
-#else
- if (isinf(value) || isnan(value))
- return Qfalse;
-#endif
-
- return Qtrue;
+ return RBOOL(isfinite(value));
}
static VALUE
diff --git a/random.c b/random.c
index 5663f87be2..232fe3e6f5 100644
--- a/random.c
+++ b/random.c
@@ -1348,7 +1348,7 @@ static inline double
float_value(VALUE v)
{
double x = RFLOAT_VALUE(v);
- if (isinf(x) || isnan(x)) {
+ if (!isfinite(x)) {
domain_error();
}
return x;
diff --git a/rational.c b/rational.c
index 88693d94c4..b79809dc0e 100644
--- a/rational.c
+++ b/rational.c
@@ -2096,7 +2096,7 @@ rb_float_numerator(VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE r;
- if (isinf(d) || isnan(d))
+ if (!isfinite(d))
return self;
r = float_to_r(self);
return nurat_numerator(r);
@@ -2116,7 +2116,7 @@ rb_float_denominator(VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE r;
- if (isinf(d) || isnan(d))
+ if (!isfinite(d))
return INT2FIX(1);
r = float_to_r(self);
return nurat_denominator(r);
diff --git a/sprintf.c b/sprintf.c
index 5237b8d2c8..f4aa7f6d1d 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -875,7 +875,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
double fval;
fval = RFLOAT_VALUE(rb_Float(val));
- if (isnan(fval) || isinf(fval)) {
+ if (!isfinite(fval)) {
const char *expr;
int need;
int elen;