summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog15
-rw-r--r--bignum.c82
-rw-r--r--ext/-test-/bignum/big2str.c40
-rw-r--r--ext/-test-/bignum/depend1
-rw-r--r--internal.h2
-rw-r--r--test/-ext-/bignum/test_big2str.rb23
6 files changed, 135 insertions, 28 deletions
diff --git a/ChangeLog b/ChangeLog
index d1958746c0..519ef8a4ef 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+Sun Sep 1 20:32:40 2013 Tanaka Akira <akr@fsij.org>
+
+ * 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@fsij.org>
* bignum.c (big2str_2bdigits): Renamed from big2str_orig.
diff --git a/bignum.c b/bignum.c
index 91e931e36c..971678b3d3 100644
--- a/bignum.c
+++ b/bignum.c
@@ -4354,7 +4354,7 @@ big2str_karatsuba(struct big2str_struct *b2s, BDIGIT *xds, size_t xn, size_t wn,
}
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)
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)
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)
diff --git a/ext/-test-/bignum/big2str.c b/ext/-test-/bignum/big2str.c
new file mode 100644
index 0000000000..2c5df6a656
--- /dev/null
+++ b/ext/-test-/bignum/big2str.c
@@ -0,0 +1,40 @@
+#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);
+}
diff --git a/ext/-test-/bignum/depend b/ext/-test-/bignum/depend
index 947441064c..cac4d52a12 100644
--- a/ext/-test-/bignum/depend
+++ b/ext/-test-/bignum/depend
@@ -2,3 +2,4 @@ $(OBJS): $(HDRS) $(ruby_headers)
intpack.o: intpack.c $(top_srcdir)/internal.h
mul.o: mul.c $(top_srcdir)/internal.h
+big2str.o: big2str.c $(top_srcdir)/internal.h
diff --git a/internal.h b/internal.h
index ec5456c13a..db3fa0b670 100644
--- a/internal.h
+++ b/internal.h
@@ -648,6 +648,8 @@ VALUE rb_big_mul_toom3(VALUE x, VALUE y);
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__
diff --git a/test/-ext-/bignum/test_big2str.rb b/test/-ext-/bignum/test_big2str.rb
new file mode 100644
index 0000000000..3e45232d6c
--- /dev/null
+++ b/test/-ext-/bignum/test_big2str.rb
@@ -0,0 +1,23 @@
+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