From 8ad74296447f628fef2110516f5c332f8c0a31fe Mon Sep 17 00:00:00 2001 From: shyouhei Date: Mon, 30 Jan 2017 10:12:18 +0000 Subject: make FIXNUM_MAX visible from Ruby Because our tests now have several places where FIXNUM_MAX is needed, we decided to provide it along with several other constants. * template/limits.c.tmpl: new file, defining RbConfig::Limits * ext/rbconfig/sizeof/depend (limits.c): rule to generate limits.c * test/-ext-/num2int/test_num2int.rb: use RbConfig::Limits * bootstraptest/test_insns.rb: ditto. * .gitignore: ignore new generated file. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57465 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- bootstraptest/test_insns.rb | 13 ++---- ext/rbconfig/sizeof/depend | 4 ++ ext/rbconfig/sizeof/extconf.rb | 2 +- template/limits.c.tmpl | 93 ++++++++++++++++++++++++++++++++++++++ template/sizes.c.tmpl | 2 + test/-ext-/num2int/test_num2int.rb | 38 +++++++--------- 6 files changed, 122 insertions(+), 30 deletions(-) create mode 100644 template/limits.c.tmpl diff --git a/bootstraptest/test_insns.rb b/bootstraptest/test_insns.rb index bafa5e85a1..2001e2c0ef 100644 --- a/bootstraptest/test_insns.rb +++ b/bootstraptest/test_insns.rb @@ -10,11 +10,8 @@ begin rescue LoadError # OK, just skip else - bits = 8 * RbConfig::SIZEOF['long'] - $LONG_MAX = (1 << (bits - 1)) - 1 - $LONG_MIN = -1 * $LONG_MAX - 1 - $FIXNUM_MAX = $LONG_MAX >> 1 - $FIXNUM_MIN = $LONG_MIN >> 1 + $FIXNUM_MAX = RbConfig::Limits["FIXNUM_MAX"] + $FIXNUM_MIN = RbConfig::Limits["FIXNUM_MIN"] end fsl = { frozen_string_literal: true } # used later @@ -272,7 +269,7 @@ tests = [ [ 'opt_case_dispatch', %q{ case 1.0 when 1.1 then false else true end }, ], [ 'opt_plus', %q{ 1 + 1 == 2 }, ], - if defined? $LONG_MAX then + if defined? $FIXNUM_MAX then [ 'opt_plus', %Q{ #{ $FIXNUM_MAX } + 1 == #{ $FIXNUM_MAX + 1 } }, ] end, [ 'opt_plus', %q{ 1.0 + 1.0 == 2.0 }, ], @@ -281,7 +278,7 @@ tests = [ [ 'opt_plus', %q{ ( ['t'] + ['r', ['u', ['e'], ], ] ).join }, ], [ 'opt_plus', %q{ Time.at(1) + 1 == Time.at(2) }, ], [ 'opt_minus', %q{ 1 - 1 == 0 }, ], - if defined? $LONG_MAX then + if defined? $FIXNUM_MIN then [ 'opt_minus', %Q{ #{ $FIXNUM_MIN } - 1 == #{ $FIXNUM_MIN - 1 } }, ] end, [ 'opt_minus', %q{ 1.0 - 1.0 == 0.0 }, ], @@ -364,7 +361,7 @@ tests = [ [ 'opt_empty_p', %q{ Queue.new.empty? }, ], [ 'opt_succ', %q{ 1.succ == 2 }, ], - if defined? $LONG_MAX then + if defined? $FIXNUM_MAX then [ 'opt_succ',%Q{ #{ $FIXNUM_MAX }.succ == #{ $FIXNUM_MAX + 1 } }, ] end, [ 'opt_succ', %q{ '1'.succ == '2' }, ], diff --git a/ext/rbconfig/sizeof/depend b/ext/rbconfig/sizeof/depend index 589837bee7..5dbe49298f 100644 --- a/ext/rbconfig/sizeof/depend +++ b/ext/rbconfig/sizeof/depend @@ -1,3 +1,7 @@ +limits.c: $(top_srcdir)/tool/generic_erb.rb $(top_srcdir)/template/limits.c.tmpl + $(Q) $(RUBY) $(top_srcdir)/tool/generic_erb.rb --output=$@ \ + $(top_srcdir)/template/limits.c.tmpl \ + sizes.c: $(top_srcdir)/tool/generic_erb.rb \ $(top_srcdir)/template/sizes.c.tmpl \ $(top_srcdir)/configure.in \ diff --git a/ext/rbconfig/sizeof/extconf.rb b/ext/rbconfig/sizeof/extconf.rb index 8cb16ca866..f7cd58f2d9 100644 --- a/ext/rbconfig/sizeof/extconf.rb +++ b/ext/rbconfig/sizeof/extconf.rb @@ -1,5 +1,5 @@ # frozen_string_literal: false -$srcs = %w[sizes.c] +$srcs = %w[sizes.c limits.c] $distcleanfiles.concat($srcs) have_type('int_least8_t') diff --git a/template/limits.c.tmpl b/template/limits.c.tmpl new file mode 100644 index 0000000000..fc95e0b307 --- /dev/null +++ b/template/limits.c.tmpl @@ -0,0 +1,93 @@ +%# -*- c -*- +% limits = %w[ +% FIXNUM +% CHAR SCHAR UCHAR WCHAR +% SHRT USHRT +% INT UINT +% LONG ULONG +% LLONG ULLONG +% INT8 UINT8 INT_LEAST8 UINT_LEAST8 INT_FAST8 UINT_FAST8 +% INT16 UINT16 INT_LEAST16 UINT_LEAST16 INT_FAST16 UINT_FAST16 +% INT32 UINT32 INT_LEAST32 UINT_LEAST32 INT_FAST32 UINT_FAST32 +% INT64 UINT64 INT_LEAST64 UINT_LEAST64 INT_FAST64 UINT_FAST64 +% INT128 UINT128 +% INTMAX UINTMAX +% INTPTR UINTPTR +% SSZIE SIZE +% PTRDIFF +% ] +% +% verbatim_integers = %w[ +% FLT_RADIX +% FLT_ROUNDS +% FLT_EVAL_METHOD +% FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG +% FLT_DIG DBL_DIG LDBL_DIG +% FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP +% FLT_MIN_10_EXP DBL_MIN_10_EXP LDBL_MIN_10_EXP +% FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP +% FLT_MAX_10_EXP DBL_MAX_10_EXP LDBL_MAX_10_EXP +% FLT_DECIMAL_DIG DBL_DECIMAL_DIG LDBL_DECIMAL_DIG DECIMAL_DIG +% FLT_HAS_SUBNORM DBL_HAS_SUBNORM LDBL_HAS_SUBNORM +% ] +% +% # Beware; Ruby cannot handle LDBL_MAX. +% verbatim_doubles = %w[ +% FLT_MAX DBL_MAX +% FLT_EPSILON DBL_EPSILON +% FLT_MIN DBL_MIN +% FLT_TRUE_MIN DBL_TRUE_MIN +% ] +% +#include +#include "ruby/ruby.h" +#ifdef HAVE_STDINT_H +# include +#endif +#ifdef HAVE_INTTYPES_H +# include +#endif +#ifdef HAVE_FLOAT_H +# include +#endif + +void +Init_limits(void) +{ + VALUE h = rb_hash_new(); + rb_define_const(rb_define_module("RbConfig"), "Limits", h); + +#ifdef HAVE_LONG_LONG +#define MAX2NUM(name) ULL2NUM(name ## _MAX) +#define MIN2NUM(name) LL2NUM(name ## _MIN) +#else +#define MAX2NUM(name) ULONG2NUM(name ## _MAX) +#define MIN2NUM(name) LONG2NUM(name ## _MIN) +#endif +#define DEFINE(k, v) rb_hash_aset(h, rb_str_new_cstr(#k), v) + +% limits.each do |type| +#ifdef <%= type %>_MAX + DEFINE(<%= type %>_MAX, MAX2NUM(<%= type %>)); +#endif +#ifdef <%= type %>_MIN + DEFINE(<%= type %>_MIN, MIN2NUM(<%= type %>)); +#endif +% end + +% verbatim_integers.each do |name| +#ifdef <%= name %> + DEFINE(<%= name %>, LONG2NUM(<%= name %>)); +#endif +% end + +% verbatim_doubles.each do |name| +#ifdef <%= name %> + DEFINE(<%= name %>, DBL2NUM(<%= name %>)); +#endif +% end + +#undef DEFINE +#undef MIN2NUM +#undef MAX2NUM +} diff --git a/template/sizes.c.tmpl b/template/sizes.c.tmpl index 2d23cacace..75c1ff22c9 100644 --- a/template/sizes.c.tmpl +++ b/template/sizes.c.tmpl @@ -28,6 +28,7 @@ conditions = { void Init_sizeof(void) { + extern void Init_limits(); VALUE s = rb_hash_new(); rb_define_const(rb_define_module("RbConfig"), "SIZEOF", s); @@ -48,4 +49,5 @@ Init_sizeof(void) % end #undef DEFINE + Init_limits(); } diff --git a/test/-ext-/num2int/test_num2int.rb b/test/-ext-/num2int/test_num2int.rb index 8eedc50db1..0b12209134 100644 --- a/test/-ext-/num2int/test_num2int.rb +++ b/test/-ext-/num2int/test_num2int.rb @@ -2,33 +2,29 @@ require 'test/unit' require '-test-/num2int' require '-test-/integer' +require 'rbconfig/sizeof' class TestNum2int < Test::Unit::TestCase - SHRT_MIN = -32768 - SHRT_MAX = 32767 - USHRT_MAX = 65535 + l = RbConfig::Limits - INT_MIN = -2147483648 - INT_MAX = 2147483647 - UINT_MAX = 4294967295 + SHRT_MIN = l["SHRT_MIN"] + SHRT_MAX = l["SHRT_MAX"] + USHRT_MAX = l["USHRT_MAX"] - case [0].pack('L!').size - when 4 - LONG_MAX = 2147483647 - LONG_MIN = -2147483648 - ULONG_MAX = 4294967295 - when 8 - LONG_MAX = 9223372036854775807 - LONG_MIN = -9223372036854775808 - ULONG_MAX = 18446744073709551615 - end + INT_MIN = l["INT_MIN"] + INT_MAX = l["INT_MAX"] + UINT_MAX = l["UINT_MAX"] + + LONG_MAX = l["LONG_MAX"] + LONG_MIN = l["LONG_MIN"] + ULONG_MAX = l["ULONG_MAX"] - LLONG_MAX = 9223372036854775807 - LLONG_MIN = -9223372036854775808 - ULLONG_MAX = 18446744073709551615 + LLONG_MAX = l["LLONG_MAX"] + LLONG_MIN = l["LLONG_MIN"] + ULLONG_MAX = l["ULLONG_MAX"] - FIXNUM_MAX = LONG_MAX/2 - FIXNUM_MIN = LONG_MIN/2 + FIXNUM_MAX = l["FIXNUM_MAX"] + FIXNUM_MIN = l["FIXNUM_MIN"] def fix2big(n) 10000000000000000000000000000.coerce(n)[0] -- cgit v1.2.3