ruby-changes:67268
From: Nobuyoshi <ko1@a...>
Date: Fri, 27 Aug 2021 15:41:00 +0900 (JST)
Subject: [ruby-changes:67268] 04be8e84db (master): Use C99-defined macros to classify a floating-point number
https://git.ruby-lang.org/ruby.git/commit/?id=04be8e84db From 04be8e84db1cf4f8b2a7bc7679eda4336da75d43 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Fri, 27 Aug 2021 10:52:02 +0900 Subject: Use C99-defined macros to classify a floating-point number --- LEGAL | 3 --- bignum.c | 2 +- common.mk | 1 - configure.ac | 6 ++--- include/ruby/missing.h | 36 +++----------------------- include/ruby/win32.h | 10 -------- missing/erf.c | 15 ----------- missing/finite.c | 9 ------- missing/isinf.c | 69 -------------------------------------------------- missing/isnan.c | 32 ----------------------- missing/tgamma.c | 15 ----------- numeric.c | 10 +------- random.c | 2 +- rational.c | 4 +-- sprintf.c | 2 +- 15 files changed, 11 insertions(+), 205 deletions(-) delete mode 100644 missing/finite.c delete mode 100644 missing/isinf.c delete mode 100644 missing/isnan.c diff --git a/LEGAL b/LEGAL index ecd1e16..a4a6027 100644 --- a/LEGAL +++ b/LEGAL @@ -532,10 +532,7 @@ mentioned below. https://github.com/ruby/ruby/blob/trunk/LEGAL#L532 [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 c954fe6..939b33c 100644 --- a/bignum.c +++ b/bignum.c @@ -5382,7 +5382,7 @@ rb_integer_float_eq(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/bignum.c#L5382 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 7605f5f..74e8954 100644 --- a/common.mk +++ b/common.mk @@ -963,7 +963,6 @@ crypt.$(OBJEXT): {$(VPATH)}crypt.c {$(VPATH)}crypt.h {$(VPATH)}missing/des_table https://github.com/ruby/ruby/blob/trunk/common.mk#L963 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 341d535..9009674 100644 --- a/configure.ac +++ b/configure.ac @@ -799,7 +799,7 @@ AS_IF([test "$GCC" = yes], [ https://github.com/ruby/ruby/blob/trunk/configure.ac#L799 ]) ], [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) https://github.com/ruby/ruby/blob/trunk/configure.ac#L1906 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 7d55124..a4a2bbb 100644 --- a/include/ruby/missing.h +++ b/include/ruby/missing.h @@ -92,10 +92,6 @@ RUBY_EXTERN int eaccess(const char*, int); https://github.com/ruby/ruby/blob/trunk/include/ruby/missing.h#L92 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; https://github.com/ruby/ruby/blob/trunk/include/ruby/missing.h#L148 # 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 4978655..c8ae599 100644 --- a/include/ruby/win32.h +++ b/include/ruby/win32.h @@ -343,14 +343,6 @@ rb_infinity_float(void) https://github.com/ruby/ruby/blob/trunk/include/ruby/win32.h#L343 #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) https://github.com/ruby/ruby/blob/trunk/include/ruby/win32.h#L351 { 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 d72c4ea..c2c9d5f 100644 --- a/missing/erf.c +++ b/missing/erf.c @@ -7,21 +7,6 @@ reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten https://github.com/ruby/ruby/blob/trunk/missing/erf.c#L7 #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 ab76863..0000000 --- a/missing/finite.c +++ /dev/null @@ -1,9 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/missing/erf.c#L0 -/* 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 ba24b77..0000000 --- a/missing/isinf.c +++ /dev/null @@ -1,69 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/missing/erf.c#L0 -/* 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 ed10bf5..0000000 --- a/missing/isnan.c +++ /dev/null @@ -1,32 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/missing/erf.c#L0 -/* 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 c8638ea..82d614d 100644 --- a/missing/tgamma.c +++ b/missing/tgamma.c @@ -14,21 +14,6 @@ reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten https://github.com/ruby/ruby/blob/trunk/missing/tgamma.c#L14 #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 f04cf61..755051d 100644 --- a/numeric.c +++ b/numeric.c @@ -1699,15 +1699,7 @@ rb_flo_is_finite_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1699 { 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 5663f87..232fe3e 100644 --- a/random.c +++ b/random.c @@ -1348,7 +1348,7 @@ static inline double https://github.com/ruby/ruby/blob/trunk/random.c#L1348 float_value(VALUE v) { double x = RFLOAT_VALUE(v); - (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/