[前][次][番号順一覧][スレッド一覧]

ruby-changes:45393

From: shyouhei <ko1@a...>
Date: Mon, 30 Jan 2017 19:12:24 +0900 (JST)
Subject: [ruby-changes:45393] shyouhei:r57465 (trunk): make FIXNUM_MAX visible from Ruby

shyouhei	2017-01-30 19:12:18 +0900 (Mon, 30 Jan 2017)

  New Revision: 57465

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57465

  Log:
    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.

  Added files:
    trunk/template/limits.c.tmpl
  Modified files:
    trunk/bootstraptest/test_insns.rb
    trunk/ext/rbconfig/sizeof/depend
    trunk/ext/rbconfig/sizeof/extconf.rb
    trunk/template/sizes.c.tmpl
    trunk/test/-ext-/num2int/test_num2int.rb
Index: test/-ext-/num2int/test_num2int.rb
===================================================================
--- test/-ext-/num2int/test_num2int.rb	(revision 57464)
+++ test/-ext-/num2int/test_num2int.rb	(revision 57465)
@@ -2,33 +2,29 @@ https://github.com/ruby/ruby/blob/trunk/test/-ext-/num2int/test_num2int.rb#L2
 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
-
-  INT_MIN = -2147483648
-  INT_MAX = 2147483647
-  UINT_MAX = 4294967295
-
-  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
-
-  LLONG_MAX = 9223372036854775807
-  LLONG_MIN = -9223372036854775808
-  ULLONG_MAX = 18446744073709551615
+  l = RbConfig::Limits
 
-  FIXNUM_MAX = LONG_MAX/2
-  FIXNUM_MIN = LONG_MIN/2
+  SHRT_MIN = l["SHRT_MIN"]
+  SHRT_MAX = l["SHRT_MAX"]
+  USHRT_MAX = l["USHRT_MAX"]
+
+  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 = l["LLONG_MAX"]
+  LLONG_MIN = l["LLONG_MIN"]
+  ULLONG_MAX = l["ULLONG_MAX"]
+
+  FIXNUM_MAX = l["FIXNUM_MAX"]
+  FIXNUM_MIN = l["FIXNUM_MIN"]
 
   def fix2big(n)
     10000000000000000000000000000.coerce(n)[0]
Index: template/sizes.c.tmpl
===================================================================
--- template/sizes.c.tmpl	(revision 57464)
+++ template/sizes.c.tmpl	(revision 57465)
@@ -28,6 +28,7 @@ conditions = { https://github.com/ruby/ruby/blob/trunk/template/sizes.c.tmpl#L28
 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) https://github.com/ruby/ruby/blob/trunk/template/sizes.c.tmpl#L49
 % end
 
 #undef DEFINE
+      Init_limits();
 }
Index: template/limits.c.tmpl
===================================================================
--- template/limits.c.tmpl	(revision 0)
+++ template/limits.c.tmpl	(revision 57465)
@@ -0,0 +1,93 @@ https://github.com/ruby/ruby/blob/trunk/template/limits.c.tmpl#L1
+%#  -*- 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 <limits.h>
+#include "ruby/ruby.h"
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
+#ifdef HAVE_INTTYPES_H
+# include <stdint.h>
+#endif
+#ifdef HAVE_FLOAT_H
+# include <float.h>
+#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
+}
Index: bootstraptest/test_insns.rb
===================================================================
--- bootstraptest/test_insns.rb	(revision 57464)
+++ bootstraptest/test_insns.rb	(revision 57465)
@@ -10,11 +10,8 @@ begin https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_insns.rb#L10
 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 = [ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_insns.rb#L269
   [ '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 = [ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_insns.rb#L278
   [ '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 = [ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_insns.rb#L361
   [ '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' }, ],
Index: ext/rbconfig/sizeof/extconf.rb
===================================================================
--- ext/rbconfig/sizeof/extconf.rb	(revision 57464)
+++ ext/rbconfig/sizeof/extconf.rb	(revision 57465)
@@ -1,5 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/ext/rbconfig/sizeof/extconf.rb#L1
 # frozen_string_literal: false
-$srcs = %w[sizes.c]
+$srcs = %w[sizes.c limits.c]
 $distcleanfiles.concat($srcs)
 
 have_type('int_least8_t')
Index: ext/rbconfig/sizeof/depend
===================================================================
--- ext/rbconfig/sizeof/depend	(revision 57464)
+++ ext/rbconfig/sizeof/depend	(revision 57465)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ext/rbconfig/sizeof/depend#L1
+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 \

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]