1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42758 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-09-01 11:35:57 +00:00
parent cec97f40f6
commit 7dd1604603
6 changed files with 135 additions and 28 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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);
}

View file

@ -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

View file

@ -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__

View file

@ -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