summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenta Murata <mrkn@mrkn.jp>2021-01-04 12:10:09 +0900
committerKenta Murata <mrkn@mrkn.jp>2021-01-04 13:29:10 +0900
commit71f0dd339b14d314d47db6c1ed9356fdb2eb4b73 (patch)
treefd2ba1a307ebb683df9a6ab23abeba9f7608d558
parent69ed64949b0c02d4b195809fa104ff23dd100093 (diff)
[ruby/bigdecimal] Move some definitions to missing.h
https://github.com/ruby/bigdecimal/commit/c2b22cc8b3 https://github.com/ruby/bigdecimal/commit/8cbca8481d https://github.com/ruby/bigdecimal/commit/f05aecf673
-rw-r--r--ext/bigdecimal/bigdecimal.c52
-rw-r--r--ext/bigdecimal/bigdecimal.gemspec1
-rw-r--r--ext/bigdecimal/bigdecimal.h118
-rw-r--r--ext/bigdecimal/bits.h4
-rw-r--r--ext/bigdecimal/extconf.rb4
-rw-r--r--ext/bigdecimal/missing.h232
6 files changed, 244 insertions, 167 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 5a69242..b4f7114 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -39,10 +39,6 @@
#define SIGNED_VALUE_MIN INTPTR_MIN
#define MUL_OVERFLOW_SIGNED_VALUE_P(a, b) MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, SIGNED_VALUE_MIN, SIGNED_VALUE_MAX)
-#define numberof(array) ((int)(sizeof(array) / sizeof((array)[0])))
-#define roomof(x, y) (((x) + (y) - 1) / (y))
-#define type_roomof(x, y) roomof(sizeof(x), sizeof(y))
-
VALUE rb_cBigDecimal;
VALUE rb_mBigMath;
@@ -106,54 +102,6 @@ static ID id_half;
# define RB_OBJ_STRING(obj) StringValueCStr(obj)
#endif
-#ifndef HAVE_RB_RATIONAL_NUM
-static inline VALUE
-rb_rational_num(VALUE rat)
-{
-#ifdef HAVE_TYPE_STRUCT_RRATIONAL
- return RRATIONAL(rat)->num;
-#else
- return rb_funcall(rat, rb_intern("numerator"), 0);
-#endif
-}
-#endif
-
-#ifndef HAVE_RB_RATIONAL_DEN
-static inline VALUE
-rb_rational_den(VALUE rat)
-{
-#ifdef HAVE_TYPE_STRUCT_RRATIONAL
- return RRATIONAL(rat)->den;
-#else
- return rb_funcall(rat, rb_intern("denominator"), 0);
-#endif
-}
-#endif
-
-#ifndef HAVE_RB_COMPLEX_REAL
-static inline VALUE
-rb_complex_real(VALUE cmp)
-{
-#ifdef HAVE_TYPE_STRUCT_RCOMPLEX
- return RCOMPLEX(cmp)->real;
-#else
- return rb_funcall(cmp, rb_intern("real"), 0);
-#endif
-}
-#endif
-
-#ifndef HAVE_RB_COMPLEX_IMAG
-static inline VALUE
-rb_complex_imag(VALUE cmp)
-{
-#ifdef HAVE_TYPE_STRUCT_RCOMPLEX
- return RCOMPLEX(cmp)->imag;
-#else
- return rb_funcall(cmp, rb_intern("imag"), 0);
-#endif
-}
-#endif
-
#define BIGDECIMAL_POSITIVE_P(bd) ((bd)->sign > 0)
#define BIGDECIMAL_NEGATIVE_P(bd) ((bd)->sign < 0)
diff --git a/ext/bigdecimal/bigdecimal.gemspec b/ext/bigdecimal/bigdecimal.gemspec
index caf449b..8a2f5b6 100644
--- a/ext/bigdecimal/bigdecimal.gemspec
+++ b/ext/bigdecimal/bigdecimal.gemspec
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
ext/bigdecimal/bigdecimal.h
ext/bigdecimal/bits.h
ext/bigdecimal/feature.h
+ ext/bigdecimal/missing.h
ext/bigdecimal/static_assert.h
lib/bigdecimal.rb
lib/bigdecimal/jacobian.rb
diff --git a/ext/bigdecimal/bigdecimal.h b/ext/bigdecimal/bigdecimal.h
index 900ebd4..12b9ee6 100644
--- a/ext/bigdecimal/bigdecimal.h
+++ b/ext/bigdecimal/bigdecimal.h
@@ -10,34 +10,11 @@
#define RUBY_BIG_DECIMAL_H 1
#define RUBY_NO_OLD_COMPATIBILITY
-
#include "ruby/ruby.h"
-#include <float.h>
-
-#if defined(__bool_true_false_are_defined)
-# /* Take that. */
-
-#elif defined(HAVE_STDBOOL_H)
-# include <stdbool.h>
-
-#else
-typedef unsigned char _Bool;
-# define bool _Bool
-# define true ((_Bool)+1)
-# define false ((_Bool)-1)
-# define __bool_true_false_are_defined
-#endif
-
-#ifndef RB_UNUSED_VAR
-# ifdef __GNUC__
-# define RB_UNUSED_VAR(x) x __attribute__ ((unused))
-# else
-# define RB_UNUSED_VAR(x) x
-# endif
-#endif
+#include "missing.h"
-#ifndef UNREACHABLE
-# define UNREACHABLE /* unreachable */
+#ifdef HAVE_FLOAT_H
+# include <float.h>
#endif
#undef BDIGIT
@@ -88,95 +65,6 @@ extern "C" {
#endif
#endif
-#ifndef HAVE_LABS
-static inline long
-labs(long const x)
-{
- if (x < 0) return -x;
- return x;
-}
-#endif
-
-#ifndef HAVE_LLABS
-static inline LONG_LONG
-llabs(LONG_LONG const x)
-{
- if (x < 0) return -x;
- return x;
-}
-#endif
-
-#ifndef HAVE_FINITE
-static int
-finite(double)
-{
- return !isnan(n) && !isinf(n);
-}
-#endif
-
-#ifndef isfinite
-# ifndef HAVE_ISFINITE
-# define HAVE_ISFINITE 1
-# define isfinite(x) finite(x)
-# endif
-#endif
-
-#ifndef FIX_CONST_VALUE_PTR
-# if defined(__fcc__) || defined(__fcc_version) || \
- defined(__FCC__) || defined(__FCC_VERSION)
-/* workaround for old version of Fujitsu C Compiler (fcc) */
-# define FIX_CONST_VALUE_PTR(x) ((const VALUE *)(x))
-# else
-# define FIX_CONST_VALUE_PTR(x) (x)
-# endif
-#endif
-
-#ifndef HAVE_RB_ARRAY_CONST_PTR
-static inline const VALUE *
-rb_array_const_ptr(VALUE a)
-{
- return FIX_CONST_VALUE_PTR((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ?
- RARRAY(a)->as.ary : RARRAY(a)->as.heap.ptr);
-}
-#endif
-
-#ifndef RARRAY_CONST_PTR
-# define RARRAY_CONST_PTR(a) rb_array_const_ptr(a)
-#endif
-
-#ifndef RARRAY_AREF
-# define RARRAY_AREF(a, i) (RARRAY_CONST_PTR(a)[i])
-#endif
-
-#ifndef HAVE_RB_SYM2STR
-static inline VALUE
-rb_sym2str(VALUE sym)
-{
- return rb_id2str(SYM2ID(sym));
-}
-#endif
-
-#ifndef ST2FIX
-# undef RB_ST2FIX
-# define RB_ST2FIX(h) LONG2FIX((long)(h))
-# define ST2FIX(h) RB_ST2FIX(h)
-#endif
-
-#ifdef vabs
-# undef vabs
-#endif
-#if SIZEOF_VALUE <= SIZEOF_INT
-# define vabs abs
-#elif SIZEOF_VALUE <= SIZEOF_LONG
-# define vabs labs
-#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
-# define vabs llabs
-#endif
-
-#if !defined(HAVE_RB_CATEGORY_WARN) || !defined(HAVE_CONST_RB_WARN_CATEGORY_DEPRECATED)
-# define rb_category_warn(category, ...) rb_warn(__VA_ARGS__)
-#endif
-
extern VALUE rb_cBigDecimal;
#if 0 || SIZEOF_BDIGITS >= 16
diff --git a/ext/bigdecimal/bits.h b/ext/bigdecimal/bits.h
index f39e83b..3835fe3 100644
--- a/ext/bigdecimal/bits.h
+++ b/ext/bigdecimal/bits.h
@@ -15,6 +15,10 @@
# pragma intrinsic(__lzcnt64)
#endif
+#define numberof(array) ((int)(sizeof(array) / sizeof((array)[0])))
+#define roomof(x, y) (((x) + (y) - 1) / (y))
+#define type_roomof(x, y) roomof(sizeof(x), sizeof(y))
+
#define MUL_OVERFLOW_SIGNED_INTEGER_P(a, b, min, max) ( \
(a) == 0 ? 0 : \
(a) == -1 ? (b) < -(max) : \
diff --git a/ext/bigdecimal/extconf.rb b/ext/bigdecimal/extconf.rb
index 4ad5172..5055e10 100644
--- a/ext/bigdecimal/extconf.rb
+++ b/ext/bigdecimal/extconf.rb
@@ -45,7 +45,11 @@ check_bigdecimal_version(gemspec_path)
have_builtin_func("__builtin_clz", "__builtin_clz(0)")
have_builtin_func("__builtin_clzl", "__builtin_clzl(0)")
+have_header("float.h")
+have_header("math.h")
have_header("stdbool.h")
+have_header("stdlib.h")
+
if have_func("_lzcnt_u64", "x86intrin.h") # check availability
$defs << "-DHAVE_X86INTRIN_H"
end
diff --git a/ext/bigdecimal/missing.h b/ext/bigdecimal/missing.h
new file mode 100644
index 0000000..aa056c3
--- /dev/null
+++ b/ext/bigdecimal/missing.h
@@ -0,0 +1,232 @@
+#ifndef MISSING_H
+#define MISSING_H 1
+
+#if defined(__cplusplus)
+extern "C" {
+#if 0
+} /* satisfy cc-mode */
+#endif
+#endif
+
+#ifdef HAVE_STDLIB_H
+# include <stdlib.h>
+#endif
+
+#ifdef HAVE_MATH_H
+# include <math.h>
+#endif
+
+#ifndef RB_UNUSED_VAR
+# if defined(_MSC_VER) && _MSC_VER >= 1911
+# define RB_UNUSED_VAR(x) x [[maybe_unused]]
+
+# elif defined(__has_cpp_attribute) && __has_cpp_attribute(maybe_unused)
+# define RB_UNUSED_VAR(x) x [[maybe_unused]]
+
+# elif defined(__has_c_attribute) && __has_c_attribute(maybe_unused)
+# define RB_UNUSED_VAR(x) x [[maybe_unused]]
+
+# elif defined(__GNUC__)
+# define RB_UNUSED_VAR(x) x __attribute__ ((unused))
+
+# else
+# define RB_UNUSED_VAR(x) x
+# endif
+#endif /* RB_UNUSED_VAR */
+
+#if defined(_MSC_VER) && _MSC_VER >= 1310
+# define HAVE___ASSUME
+
+#elif defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1300
+# define HAVE___ASSUME
+#endif
+
+#ifndef UNREACHABLE
+# if __has_builtin(__builtin_unreachable)
+# define UNREACHABLE __builtin_unreachable()
+
+# elif HAVE___ASSUME
+# define UNREACHABLE __assume(0)
+
+# else
+# define UNREACHABLE /* unreachable */
+# endif
+#endif /* UNREACHABLE */
+
+/* bool */
+
+#if defined(__bool_true_false_are_defined)
+# /* Take that. */
+
+#elif defined(HAVE_STDBOOL_H)
+# include <stdbool.h>
+
+#else
+typedef unsigned char _Bool;
+# define bool _Bool
+# define true ((_Bool)+1)
+# define false ((_Bool)-1)
+# define __bool_true_false_are_defined
+#endif
+
+/* abs */
+
+#ifndef HAVE_LABS
+static inline long
+labs(long const x)
+{
+ if (x < 0) return -x;
+ return x;
+}
+#endif
+
+#ifndef HAVE_LLABS
+static inline LONG_LONG
+llabs(LONG_LONG const x)
+{
+ if (x < 0) return -x;
+ return x;
+}
+#endif
+
+#ifdef vabs
+# undef vabs
+#endif
+#if SIZEOF_VALUE <= SIZEOF_INT
+# define vabs abs
+#elif SIZEOF_VALUE <= SIZEOF_LONG
+# define vabs labs
+#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
+# define vabs llabs
+#endif
+
+/* finite */
+
+#ifndef HAVE_FINITE
+static int
+finite(double)
+{
+ return !isnan(n) && !isinf(n);
+}
+#endif
+
+#ifndef isfinite
+# ifndef HAVE_ISFINITE
+# define HAVE_ISFINITE 1
+# define isfinite(x) finite(x)
+# endif
+#endif
+
+/* rational */
+
+#ifndef HAVE_RB_RATIONAL_NUM
+static inline VALUE
+rb_rational_num(VALUE rat)
+{
+#ifdef HAVE_TYPE_STRUCT_RRATIONAL
+ return RRATIONAL(rat)->num;
+#else
+ return rb_funcall(rat, rb_intern("numerator"), 0);
+#endif
+}
+#endif
+
+#ifndef HAVE_RB_RATIONAL_DEN
+static inline VALUE
+rb_rational_den(VALUE rat)
+{
+#ifdef HAVE_TYPE_STRUCT_RRATIONAL
+ return RRATIONAL(rat)->den;
+#else
+ return rb_funcall(rat, rb_intern("denominator"), 0);
+#endif
+}
+#endif
+
+/* complex */
+
+#ifndef HAVE_RB_COMPLEX_REAL
+static inline VALUE
+rb_complex_real(VALUE cmp)
+{
+#ifdef HAVE_TYPE_STRUCT_RCOMPLEX
+ return RCOMPLEX(cmp)->real;
+#else
+ return rb_funcall(cmp, rb_intern("real"), 0);
+#endif
+}
+#endif
+
+#ifndef HAVE_RB_COMPLEX_IMAG
+static inline VALUE
+rb_complex_imag(VALUE cmp)
+{
+# ifdef HAVE_TYPE_STRUCT_RCOMPLEX
+ return RCOMPLEX(cmp)->imag;
+# else
+ return rb_funcall(cmp, rb_intern("imag"), 0);
+# endif
+}
+#endif
+
+/* array */
+
+#ifndef FIX_CONST_VALUE_PTR
+# if defined(__fcc__) || defined(__fcc_version) || \
+ defined(__FCC__) || defined(__FCC_VERSION)
+/* workaround for old version of Fujitsu C Compiler (fcc) */
+# define FIX_CONST_VALUE_PTR(x) ((const VALUE *)(x))
+# else
+# define FIX_CONST_VALUE_PTR(x) (x)
+# endif
+#endif
+
+#ifndef HAVE_RB_ARRAY_CONST_PTR
+static inline const VALUE *
+rb_array_const_ptr(VALUE a)
+{
+ return FIX_CONST_VALUE_PTR((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ?
+ RARRAY(a)->as.ary : RARRAY(a)->as.heap.ptr);
+}
+#endif
+
+#ifndef RARRAY_CONST_PTR
+# define RARRAY_CONST_PTR(a) rb_array_const_ptr(a)
+#endif
+
+#ifndef RARRAY_AREF
+# define RARRAY_AREF(a, i) (RARRAY_CONST_PTR(a)[i])
+#endif
+
+/* symbol */
+
+#ifndef HAVE_RB_SYM2STR
+static inline VALUE
+rb_sym2str(VALUE sym)
+{
+ return rb_id2str(SYM2ID(sym));
+}
+#endif
+
+/* st */
+
+#ifndef ST2FIX
+# undef RB_ST2FIX
+# define RB_ST2FIX(h) LONG2FIX((long)(h))
+# define ST2FIX(h) RB_ST2FIX(h)
+#endif
+
+/* warning */
+
+#if !defined(HAVE_RB_CATEGORY_WARN) || !defined(HAVE_CONST_RB_WARN_CATEGORY_DEPRECATED)
+# define rb_category_warn(category, ...) rb_warn(__VA_ARGS__)
+#endif
+
+#if defined(__cplusplus)
+#if 0
+{ /* satisfy cc-mode */
+#endif
+} /* extern "C" { */
+#endif
+
+#endif /* MISSING_H */