diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2021-08-27 10:52:02 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2021-08-27 12:41:30 +0900 |
commit | 04be8e84db1cf4f8b2a7bc7679eda4336da75d43 (patch) | |
tree | c7914614f4a5cf91109f7893b87ff9e5d143ad0e | |
parent | 37114673623c4d2b1d9f2c2ddfffba070b2d96e6 (diff) | |
download | ruby-04be8e84db1cf4f8b2a7bc7679eda4336da75d43.tar.gz |
Use C99-defined macros to classify a floating-point number
-rw-r--r-- | LEGAL | 3 | ||||
-rw-r--r-- | bignum.c | 2 | ||||
-rw-r--r-- | common.mk | 1 | ||||
-rw-r--r-- | configure.ac | 6 | ||||
-rw-r--r-- | include/ruby/missing.h | 36 | ||||
-rw-r--r-- | include/ruby/win32.h | 10 | ||||
-rw-r--r-- | missing/erf.c | 15 | ||||
-rw-r--r-- | missing/finite.c | 9 | ||||
-rw-r--r-- | missing/isinf.c | 69 | ||||
-rw-r--r-- | missing/isnan.c | 32 | ||||
-rw-r--r-- | missing/tgamma.c | 15 | ||||
-rw-r--r-- | numeric.c | 10 | ||||
-rw-r--r-- | random.c | 2 | ||||
-rw-r--r-- | rational.c | 4 | ||||
-rw-r--r-- | sprintf.c | 2 |
15 files changed, 11 insertions, 205 deletions
@@ -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] @@ -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) @@ -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> @@ -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 @@ -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); @@ -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; |