mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* internal.h (struct RBignum): Use size_t for len.
* include/ruby/intern.h (rb_big_new): Use size_t instead of long to specify the size of bignum. (rb_big_resize): Ditto. * bignum.c: Follow above changes. * rational.c: Follow above changes. * marshal.c: Follow above changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45636 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
8233f969a2
commit
95de2b0012
6 changed files with 51 additions and 26 deletions
14
ChangeLog
14
ChangeLog
|
@ -1,3 +1,17 @@
|
||||||
|
Sat Apr 19 10:07:24 2014 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* internal.h (struct RBignum): Use size_t for len.
|
||||||
|
|
||||||
|
* include/ruby/intern.h (rb_big_new): Use size_t instead of long to
|
||||||
|
specify the size of bignum.
|
||||||
|
(rb_big_resize): Ditto.
|
||||||
|
|
||||||
|
* bignum.c: Follow above changes.
|
||||||
|
|
||||||
|
* rational.c: Follow above changes.
|
||||||
|
|
||||||
|
* marshal.c: Follow above changes.
|
||||||
|
|
||||||
Sat Apr 19 00:32:07 2014 Tanaka Akira <akr@fsij.org>
|
Sat Apr 19 00:32:07 2014 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* numeric.c (rb_num2long): Returns a long.
|
* numeric.c (rb_num2long): Returns a long.
|
||||||
|
|
39
bignum.c
39
bignum.c
|
@ -148,7 +148,7 @@ static void bary_divmod(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BD
|
||||||
|
|
||||||
static VALUE bigmul0(VALUE x, VALUE y);
|
static VALUE bigmul0(VALUE x, VALUE y);
|
||||||
static void bary_mul_toom3(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn);
|
static void bary_mul_toom3(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn);
|
||||||
static VALUE bignew_1(VALUE klass, long len, int sign);
|
static VALUE bignew_1(VALUE klass, size_t len, int sign);
|
||||||
static inline VALUE bigtrunc(VALUE x);
|
static inline VALUE bigtrunc(VALUE x);
|
||||||
|
|
||||||
static VALUE bigsq(VALUE x);
|
static VALUE bigsq(VALUE x);
|
||||||
|
@ -2879,7 +2879,7 @@ dump_bignum(VALUE x)
|
||||||
for (i = BIGNUM_LEN(x); i--; ) {
|
for (i = BIGNUM_LEN(x); i--; ) {
|
||||||
printf("_%0*"PRIxBDIGIT, SIZEOF_BDIGIT*2, BDIGITS(x)[i]);
|
printf("_%0*"PRIxBDIGIT, SIZEOF_BDIGIT*2, BDIGITS(x)[i]);
|
||||||
}
|
}
|
||||||
printf(", len=%lu", BIGNUM_LEN(x));
|
printf(", len=%"PRIuSIZE, BIGNUM_LEN(x));
|
||||||
puts("");
|
puts("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2935,7 +2935,7 @@ rb_cmpint(VALUE val, VALUE a, VALUE b)
|
||||||
(void)(RBIGNUM(b)->as.heap.len = (l)))
|
(void)(RBIGNUM(b)->as.heap.len = (l)))
|
||||||
|
|
||||||
static void
|
static void
|
||||||
rb_big_realloc(VALUE big, long len)
|
rb_big_realloc(VALUE big, size_t len)
|
||||||
{
|
{
|
||||||
BDIGIT *ds;
|
BDIGIT *ds;
|
||||||
if (RBASIC(big)->flags & BIGNUM_EMBED_FLAG) {
|
if (RBASIC(big)->flags & BIGNUM_EMBED_FLAG) {
|
||||||
|
@ -2970,14 +2970,14 @@ rb_big_realloc(VALUE big, long len)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
rb_big_resize(VALUE big, long len)
|
rb_big_resize(VALUE big, size_t len)
|
||||||
{
|
{
|
||||||
rb_big_realloc(big, len);
|
rb_big_realloc(big, len);
|
||||||
BIGNUM_SET_LEN(big, len);
|
BIGNUM_SET_LEN(big, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
bignew_1(VALUE klass, long len, int sign)
|
bignew_1(VALUE klass, size_t len, int sign)
|
||||||
{
|
{
|
||||||
NEWOBJ_OF(big, struct RBignum, klass, T_BIGNUM | (RGENGC_WB_PROTECTED_BIGNUM ? FL_WB_PROTECTED : 0));
|
NEWOBJ_OF(big, struct RBignum, klass, T_BIGNUM | (RGENGC_WB_PROTECTED_BIGNUM ? FL_WB_PROTECTED : 0));
|
||||||
BIGNUM_SET_SIGN(big, sign?1:0);
|
BIGNUM_SET_SIGN(big, sign?1:0);
|
||||||
|
@ -2995,7 +2995,7 @@ bignew_1(VALUE klass, long len, int sign)
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_big_new(long len, int sign)
|
rb_big_new(size_t len, int sign)
|
||||||
{
|
{
|
||||||
return bignew(len, sign != 0);
|
return bignew(len, sign != 0);
|
||||||
}
|
}
|
||||||
|
@ -3003,7 +3003,7 @@ rb_big_new(long len, int sign)
|
||||||
VALUE
|
VALUE
|
||||||
rb_big_clone(VALUE x)
|
rb_big_clone(VALUE x)
|
||||||
{
|
{
|
||||||
long len = BIGNUM_LEN(x);
|
size_t len = BIGNUM_LEN(x);
|
||||||
VALUE z = bignew_1(CLASS_OF(x), len, BIGNUM_SIGN(x));
|
VALUE z = bignew_1(CLASS_OF(x), len, BIGNUM_SIGN(x));
|
||||||
|
|
||||||
MEMCPY(BDIGITS(z), BDIGITS(x), BDIGIT, len);
|
MEMCPY(BDIGITS(z), BDIGITS(x), BDIGIT, len);
|
||||||
|
@ -3068,7 +3068,7 @@ twocomp2abs_bang(VALUE x, int hibits)
|
||||||
static inline VALUE
|
static inline VALUE
|
||||||
bigtrunc(VALUE x)
|
bigtrunc(VALUE x)
|
||||||
{
|
{
|
||||||
long len = BIGNUM_LEN(x);
|
size_t len = BIGNUM_LEN(x);
|
||||||
BDIGIT *ds = BDIGITS(x);
|
BDIGIT *ds = BDIGITS(x);
|
||||||
|
|
||||||
if (len == 0) return x;
|
if (len == 0) return x;
|
||||||
|
@ -4940,7 +4940,7 @@ rb_big_to_s(int argc, VALUE *argv, VALUE x)
|
||||||
static unsigned long
|
static unsigned long
|
||||||
big2ulong(VALUE x, const char *type)
|
big2ulong(VALUE x, const char *type)
|
||||||
{
|
{
|
||||||
long len = BIGNUM_LEN(x);
|
size_t len = BIGNUM_LEN(x);
|
||||||
unsigned long num;
|
unsigned long num;
|
||||||
BDIGIT *ds;
|
BDIGIT *ds;
|
||||||
|
|
||||||
|
@ -5002,7 +5002,7 @@ rb_big2long(VALUE x)
|
||||||
static unsigned LONG_LONG
|
static unsigned LONG_LONG
|
||||||
big2ull(VALUE x, const char *type)
|
big2ull(VALUE x, const char *type)
|
||||||
{
|
{
|
||||||
long len = BIGNUM_LEN(x);
|
size_t len = BIGNUM_LEN(x);
|
||||||
unsigned LONG_LONG num;
|
unsigned LONG_LONG num;
|
||||||
BDIGIT *ds = BDIGITS(x);
|
BDIGIT *ds = BDIGITS(x);
|
||||||
|
|
||||||
|
@ -5724,7 +5724,7 @@ static VALUE
|
||||||
bigadd(VALUE x, VALUE y, int sign)
|
bigadd(VALUE x, VALUE y, int sign)
|
||||||
{
|
{
|
||||||
VALUE z;
|
VALUE z;
|
||||||
long len;
|
size_t len;
|
||||||
|
|
||||||
sign = (sign == BIGNUM_SIGN(y));
|
sign = (sign == BIGNUM_SIGN(y));
|
||||||
if (BIGNUM_SIGN(x) != sign) {
|
if (BIGNUM_SIGN(x) != sign) {
|
||||||
|
@ -6738,24 +6738,29 @@ static VALUE
|
||||||
rb_big_aref(VALUE x, VALUE y)
|
rb_big_aref(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
BDIGIT *xds;
|
BDIGIT *xds;
|
||||||
unsigned long shift;
|
size_t shift;
|
||||||
long i, s1, s2;
|
size_t i, s1, s2;
|
||||||
|
long l;
|
||||||
BDIGIT bit;
|
BDIGIT bit;
|
||||||
|
|
||||||
if (RB_BIGNUM_TYPE_P(y)) {
|
if (RB_BIGNUM_TYPE_P(y)) {
|
||||||
if (!BIGNUM_SIGN(y))
|
if (!BIGNUM_SIGN(y))
|
||||||
return INT2FIX(0);
|
return INT2FIX(0);
|
||||||
bigtrunc(y);
|
bigtrunc(y);
|
||||||
if (BIGSIZE(y) > sizeof(long)) {
|
if (BIGSIZE(y) > sizeof(size_t)) {
|
||||||
out_of_range:
|
out_of_range:
|
||||||
return BIGNUM_SIGN(x) ? INT2FIX(0) : INT2FIX(1);
|
return BIGNUM_SIGN(x) ? INT2FIX(0) : INT2FIX(1);
|
||||||
}
|
}
|
||||||
|
#if SIZEOF_SIZE_T <= SIZEOF_LONG
|
||||||
shift = big2ulong(y, "long");
|
shift = big2ulong(y, "long");
|
||||||
|
#else
|
||||||
|
shift = big2ull(y, "long long");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
i = NUM2LONG(y);
|
l = NUM2LONG(y);
|
||||||
if (i < 0) return INT2FIX(0);
|
if (l < 0) return INT2FIX(0);
|
||||||
shift = i;
|
shift = (size_t)l;
|
||||||
}
|
}
|
||||||
s1 = shift/BITSPERDIG;
|
s1 = shift/BITSPERDIG;
|
||||||
s2 = shift%BITSPERDIG;
|
s2 = shift%BITSPERDIG;
|
||||||
|
|
|
@ -91,12 +91,12 @@ VALUE rb_ary_resize(VALUE ary, long len);
|
||||||
#define rb_ary_new3 rb_ary_new_from_args
|
#define rb_ary_new3 rb_ary_new_from_args
|
||||||
#define rb_ary_new4 rb_ary_new_from_values
|
#define rb_ary_new4 rb_ary_new_from_values
|
||||||
/* bignum.c */
|
/* bignum.c */
|
||||||
VALUE rb_big_new(long, int);
|
VALUE rb_big_new(size_t, int);
|
||||||
int rb_bigzero_p(VALUE x);
|
int rb_bigzero_p(VALUE x);
|
||||||
VALUE rb_big_clone(VALUE);
|
VALUE rb_big_clone(VALUE);
|
||||||
void rb_big_2comp(VALUE);
|
void rb_big_2comp(VALUE);
|
||||||
VALUE rb_big_norm(VALUE);
|
VALUE rb_big_norm(VALUE);
|
||||||
void rb_big_resize(VALUE big, long len);
|
void rb_big_resize(VALUE big, size_t len);
|
||||||
VALUE rb_cstr_to_inum(const char*, int, int);
|
VALUE rb_cstr_to_inum(const char*, int, int);
|
||||||
VALUE rb_str_to_inum(VALUE, int, int);
|
VALUE rb_str_to_inum(VALUE, int, int);
|
||||||
VALUE rb_cstr2inum(const char*, int);
|
VALUE rb_cstr2inum(const char*, int);
|
||||||
|
|
|
@ -372,7 +372,7 @@ struct RBignum {
|
||||||
struct RBasic basic;
|
struct RBasic basic;
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
long len;
|
size_t len;
|
||||||
BDIGIT *digits;
|
BDIGIT *digits;
|
||||||
} heap;
|
} heap;
|
||||||
BDIGIT ary[BIGNUM_EMBED_LEN_MAX];
|
BDIGIT ary[BIGNUM_EMBED_LEN_MAX];
|
||||||
|
|
14
marshal.c
14
marshal.c
|
@ -35,8 +35,8 @@
|
||||||
#if SIZEOF_SHORT == SIZEOF_BDIGIT
|
#if SIZEOF_SHORT == SIZEOF_BDIGIT
|
||||||
#define SHORTLEN(x) (x)
|
#define SHORTLEN(x) (x)
|
||||||
#else
|
#else
|
||||||
static long
|
static size_t
|
||||||
shortlen(long len, BDIGIT *ds)
|
shortlen(size_t len, BDIGIT *ds)
|
||||||
{
|
{
|
||||||
BDIGIT num;
|
BDIGIT num;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
@ -774,11 +774,17 @@ w_object(VALUE obj, struct dump_arg *arg, int limit)
|
||||||
w_byte(TYPE_BIGNUM, arg);
|
w_byte(TYPE_BIGNUM, arg);
|
||||||
{
|
{
|
||||||
char sign = BIGNUM_SIGN(obj) ? '+' : '-';
|
char sign = BIGNUM_SIGN(obj) ? '+' : '-';
|
||||||
long len = BIGNUM_LEN(obj);
|
size_t len = BIGNUM_LEN(obj);
|
||||||
|
size_t slen;
|
||||||
BDIGIT *d = BIGNUM_DIGITS(obj);
|
BDIGIT *d = BIGNUM_DIGITS(obj);
|
||||||
|
|
||||||
|
slen = SHORTLEN(len);
|
||||||
|
if (LONG_MAX < slen) {
|
||||||
|
rb_raise(rb_eTypeError, "too big Bignum can't be dumped");
|
||||||
|
}
|
||||||
|
|
||||||
w_byte(sign, arg);
|
w_byte(sign, arg);
|
||||||
w_long(SHORTLEN(len), arg); /* w_short? */
|
w_long((long)slen, arg);
|
||||||
while (len--) {
|
while (len--) {
|
||||||
#if SIZEOF_BDIGIT > SIZEOF_SHORT
|
#if SIZEOF_BDIGIT > SIZEOF_SHORT
|
||||||
BDIGIT num = *d;
|
BDIGIT num = *d;
|
||||||
|
|
|
@ -363,8 +363,8 @@ f_gcd(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
#ifdef USE_GMP
|
#ifdef USE_GMP
|
||||||
if (RB_TYPE_P(x, T_BIGNUM) && RB_TYPE_P(y, T_BIGNUM)) {
|
if (RB_TYPE_P(x, T_BIGNUM) && RB_TYPE_P(y, T_BIGNUM)) {
|
||||||
long xn = BIGNUM_LEN(x);
|
size_t xn = BIGNUM_LEN(x);
|
||||||
long yn = BIGNUM_LEN(y);
|
size_t yn = BIGNUM_LEN(y);
|
||||||
if (GMP_GCD_DIGITS <= xn || GMP_GCD_DIGITS <= yn)
|
if (GMP_GCD_DIGITS <= xn || GMP_GCD_DIGITS <= yn)
|
||||||
return rb_gcd_gmp(x, y);
|
return rb_gcd_gmp(x, y);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue