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

ruby-changes:30679

From: akr <ko1@a...>
Date: Sun, 1 Sep 2013 20:36:05 +0900 (JST)
Subject: [ruby-changes:30679] akr:r42758 (trunk): * bignum.c (big2str_base_poweroftwo): Renamed from

akr	2013-09-01 20:35:57 +0900 (Sun, 01 Sep 2013)

  New Revision: 42758

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42758

  Log:
    * bignum.c (big2str_base_poweroftwo): Renamed from
      big2str_base_powerof2.
      (rb_big2str_poweroftwo): New function for test.
      (big2str_generic): Extracted from rb_big2str1.
      (rb_big2str_generic): New function for test.
    
    * internal.h (rb_big2str_poweroftwo): Declared.
      (rb_big2str_generic): Ditto.
    
    * ext/-test-/bignum/big2str.c: New file.
    
    * test/-ext-/bignum/test_big2str.rb: New file.

  Added files:
    trunk/ext/-test-/bignum/big2str.c
    trunk/test/-ext-/bignum/test_big2str.rb
  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/ext/-test-/bignum/depend
    trunk/internal.h
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42757)
+++ ChangeLog	(revision 42758)
@@ -1,3 +1,18 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Sep  1 20:32:40 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (big2str_base_poweroftwo): Renamed from
+	  big2str_base_powerof2.
+	  (rb_big2str_poweroftwo): New function for test.
+	  (big2str_generic): Extracted from rb_big2str1.
+	  (rb_big2str_generic): New function for test.
+
+	* internal.h (rb_big2str_poweroftwo): Declared.
+	  (rb_big2str_generic): Ditto.
+
+	* ext/-test-/bignum/big2str.c: New file.
+
+	* test/-ext-/bignum/test_big2str.rb: New file.
+
 Sun Sep  1 15:21:21 2013  Tanaka Akira  <akr@f...>
 
 	* bignum.c (big2str_2bdigits): Renamed from big2str_orig.
Index: ext/-test-/bignum/depend
===================================================================
--- ext/-test-/bignum/depend	(revision 42757)
+++ ext/-test-/bignum/depend	(revision 42758)
@@ -2,3 +2,4 @@ $(OBJS): $(HDRS) $(ruby_headers) https://github.com/ruby/ruby/blob/trunk/ext/-test-/bignum/depend#L2
 
 intpack.o: intpack.c $(top_srcdir)/internal.h
 mul.o: mul.c $(top_srcdir)/internal.h
+big2str.o: big2str.c $(top_srcdir)/internal.h
Index: ext/-test-/bignum/big2str.c
===================================================================
--- ext/-test-/bignum/big2str.c	(revision 0)
+++ ext/-test-/bignum/big2str.c	(revision 42758)
@@ -0,0 +1,40 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/bignum/big2str.c#L1
+#include "ruby.h"
+#include "internal.h"
+
+static VALUE
+big(VALUE x)
+{
+    if (FIXNUM_P(x))
+        return rb_int2big(FIX2LONG(x));
+    if (RB_TYPE_P(x, T_BIGNUM))
+        return x;
+    rb_raise(rb_eTypeError, "can't convert %s to Bignum",
+            rb_obj_classname(x));
+}
+
+static VALUE
+big2str_generic(VALUE x, VALUE vbase)
+{
+    int base = NUM2INT(vbase);
+    if (base < 2 || 36 < base)
+        rb_raise(rb_eArgError, "invalid radix %d", base);
+    return rb_big2str_generic(big(x), NUM2INT(vbase));
+}
+
+#define POW2_P(x) (((x)&((x)-1))==0)
+
+static VALUE
+big2str_poweroftwo(VALUE x, VALUE vbase)
+{
+    int base = NUM2INT(vbase);
+    if (base < 2 || 36 < base || !POW2_P(base))
+        rb_raise(rb_eArgError, "invalid radix %d", base);
+    return rb_big2str_poweroftwo(big(x), NUM2INT(vbase));
+}
+
+void
+Init_big2str(VALUE klass)
+{
+    rb_define_method(rb_cInteger, "big2str_generic", big2str_generic, 1);
+    rb_define_method(rb_cInteger, "big2str_poweroftwo", big2str_poweroftwo, 1);
+}
Index: internal.h
===================================================================
--- internal.h	(revision 42757)
+++ internal.h	(revision 42758)
@@ -648,6 +648,8 @@ VALUE rb_big_mul_toom3(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/internal.h#L648
 VALUE rb_big_mul_gmp(VALUE x, VALUE y);
 #endif
 VALUE rb_big_sq_fast(VALUE x);
+VALUE rb_big2str_poweroftwo(VALUE x, int base);
+VALUE rb_big2str_generic(VALUE x, int base);
 
 /* file.c */
 #ifdef __APPLE__
Index: bignum.c
===================================================================
--- bignum.c	(revision 42757)
+++ bignum.c	(revision 42758)
@@ -4354,7 +4354,7 @@ big2str_karatsuba(struct big2str_struct https://github.com/ruby/ruby/blob/trunk/bignum.c#L4354
 }
 
 static VALUE
-big2str_base_powerof2(VALUE x, int base)
+big2str_base_poweroftwo(VALUE x, int base)
 {
     int word_numbits = ffs(base) - 1;
     size_t numwords;
@@ -4384,38 +4384,18 @@ big2str_base_powerof2(VALUE x, int base) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4384
     return result;
 }
 
+VALUE
+rb_big2str_poweroftwo(VALUE x, int base)
+{
+    return big2str_base_poweroftwo(x, base);
+}
+
 static VALUE
-rb_big2str1(VALUE x, int base)
+big2str_generic(VALUE x, int base, BDIGIT *xds, size_t xn)
 {
     struct big2str_struct b2s_data;
     int power_level;
     VALUE power;
-    BDIGIT *xds;
-    size_t xn;
-
-    if (FIXNUM_P(x)) {
-	return rb_fix2str(x, base);
-    }
-
-    xds = BDIGITS(x);
-    xn = RBIGNUM_LEN(x);
-    BARY_TRUNC(xds, xn);
-
-    if (xn == 0) {
-	return rb_usascii_str_new2("0");
-    }
-
-    if (base < 2 || 36 < base)
-	rb_raise(rb_eArgError, "invalid radix %d", base);
-
-    if (xn >= LONG_MAX/BITSPERDIG) {
-        rb_raise(rb_eRangeError, "bignum too big to convert into `string'");
-    }
-
-    if (POW2_P(base)) {
-        /* base == 2 || base == 4 || base == 8 || base == 16 || base == 32 */
-        return big2str_base_powerof2(x, base);
-    }
 
     power_level = 0;
     power = power_cache_get_power(base, power_level, NULL);
@@ -4469,6 +4449,52 @@ rb_big2str1(VALUE x, int base) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4449
     return b2s_data.result;
 }
 
+VALUE
+rb_big2str_generic(VALUE x, int base)
+{
+    BDIGIT *xds;
+    size_t xn;
+
+    xds = BDIGITS(x);
+    xn = RBIGNUM_LEN(x);
+    BARY_TRUNC(xds, xn);
+
+    return big2str_generic(x, base, xds, xn);
+}
+
+static VALUE
+rb_big2str1(VALUE x, int base)
+{
+    BDIGIT *xds;
+    size_t xn;
+
+    if (FIXNUM_P(x)) {
+	return rb_fix2str(x, base);
+    }
+
+    xds = BDIGITS(x);
+    xn = RBIGNUM_LEN(x);
+    BARY_TRUNC(xds, xn);
+
+    if (xn == 0) {
+	return rb_usascii_str_new2("0");
+    }
+
+    if (base < 2 || 36 < base)
+	rb_raise(rb_eArgError, "invalid radix %d", base);
+
+    if (xn >= LONG_MAX/BITSPERDIG) {
+        rb_raise(rb_eRangeError, "bignum too big to convert into `string'");
+    }
+
+    if (POW2_P(base)) {
+        /* base == 2 || base == 4 || base == 8 || base == 16 || base == 32 */
+        return big2str_base_poweroftwo(x, base);
+    }
+
+    return big2str_generic(x, base, xds, xn);
+}
+
 /* deprecated */
 VALUE
 rb_big2str0(VALUE x, int base, int trim)
Index: test/-ext-/bignum/test_big2str.rb
===================================================================
--- test/-ext-/bignum/test_big2str.rb	(revision 0)
+++ test/-ext-/bignum/test_big2str.rb	(revision 42758)
@@ -0,0 +1,23 @@ https://github.com/ruby/ruby/blob/trunk/test/-ext-/bignum/test_big2str.rb#L1
+require 'test/unit'
+require "-test-/bignum"
+
+class TestBignum < Test::Unit::TestCase
+  class TestBig2str < Test::Unit::TestCase
+
+    SIZEOF_BDIGITS = Bignum::SIZEOF_BDIGITS
+    BITSPERDIG = Bignum::BITSPERDIG
+    BDIGMAX = (1 << BITSPERDIG) - 1
+
+    def test_big2str_generic
+      x = 10**1000
+      assert_equal("1" + "0" * 1000, x.big2str_generic(10))
+    end
+
+    def test_big2str_poweroftwo
+      e = BITSPERDIG*2
+      x = 0b10**e
+      assert_equal("1" + "0" * e, x.big2str_poweroftwo(2))
+    end
+
+  end
+end

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

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