mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
numeric.c: int_round_zero_p
* bignum.c (rb_big_size): add wrapper function of BIGSIZE and rename the method funtion with _m suffix. * numeric.c (int_round_zero_p): extracted from rb_int_round. optimize for Bignum, and convert VALUE returned by Numeric#size to long. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54557 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
9d25813745
commit
1ea1d2e4a7
4 changed files with 40 additions and 8 deletions
|
@ -1,3 +1,12 @@
|
||||||
|
Wed Apr 13 14:11:59 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* bignum.c (rb_big_size): add wrapper function of BIGSIZE and
|
||||||
|
rename the method funtion with _m suffix.
|
||||||
|
|
||||||
|
* numeric.c (int_round_zero_p): extracted from rb_int_round.
|
||||||
|
optimize for Bignum, and convert VALUE returned by Numeric#size
|
||||||
|
to long.
|
||||||
|
|
||||||
Wed Apr 13 12:00:08 2016 Koichi Sasada <ko1@atdot.net>
|
Wed Apr 13 12:00:08 2016 Koichi Sasada <ko1@atdot.net>
|
||||||
|
|
||||||
* test/ruby/test_basicinstructions.rb: add a test to check access
|
* test/ruby/test_basicinstructions.rb: add a test to check access
|
||||||
|
|
12
bignum.c
12
bignum.c
|
@ -6906,6 +6906,12 @@ rb_big_abs(VALUE x)
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t
|
||||||
|
rb_big_size(VALUE big)
|
||||||
|
{
|
||||||
|
return BIGSIZE(big);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* big.size -> integer
|
* big.size -> integer
|
||||||
|
@ -6919,9 +6925,9 @@ rb_big_abs(VALUE x)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_big_size(VALUE big)
|
rb_big_size_m(VALUE big)
|
||||||
{
|
{
|
||||||
return SIZET2NUM(BIGSIZE(big));
|
return SIZET2NUM(rb_big_size(big));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -7087,7 +7093,7 @@ Init_Bignum(void)
|
||||||
rb_define_method(rb_cBignum, "===", rb_big_eq, 1);
|
rb_define_method(rb_cBignum, "===", rb_big_eq, 1);
|
||||||
rb_define_method(rb_cBignum, "abs", rb_big_abs, 0);
|
rb_define_method(rb_cBignum, "abs", rb_big_abs, 0);
|
||||||
rb_define_method(rb_cBignum, "magnitude", rb_big_abs, 0);
|
rb_define_method(rb_cBignum, "magnitude", rb_big_abs, 0);
|
||||||
rb_define_method(rb_cBignum, "size", rb_big_size, 0);
|
rb_define_method(rb_cBignum, "size", rb_big_size_m, 0);
|
||||||
rb_define_method(rb_cBignum, "bit_length", rb_big_bit_length, 0);
|
rb_define_method(rb_cBignum, "bit_length", rb_big_bit_length, 0);
|
||||||
|
|
||||||
#ifdef USE_GMP
|
#ifdef USE_GMP
|
||||||
|
|
|
@ -780,6 +780,7 @@ VALUE rb_big_uminus(VALUE x);
|
||||||
VALUE rb_big_hash(VALUE);
|
VALUE rb_big_hash(VALUE);
|
||||||
VALUE rb_big_odd_p(VALUE);
|
VALUE rb_big_odd_p(VALUE);
|
||||||
VALUE rb_big_even_p(VALUE);
|
VALUE rb_big_even_p(VALUE);
|
||||||
|
size_t rb_big_size(VALUE);
|
||||||
VALUE rb_integer_float_cmp(VALUE x, VALUE y);
|
VALUE rb_integer_float_cmp(VALUE x, VALUE y);
|
||||||
VALUE rb_integer_float_eq(VALUE x, VALUE y);
|
VALUE rb_integer_float_eq(VALUE x, VALUE y);
|
||||||
VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base);
|
VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base);
|
||||||
|
|
26
numeric.c
26
numeric.c
|
@ -109,6 +109,7 @@ static VALUE fix_lshift(long, unsigned long);
|
||||||
static VALUE fix_rshift(long, unsigned long);
|
static VALUE fix_rshift(long, unsigned long);
|
||||||
static VALUE int_pow(long x, unsigned long y);
|
static VALUE int_pow(long x, unsigned long y);
|
||||||
static VALUE int_cmp(VALUE x, VALUE y);
|
static VALUE int_cmp(VALUE x, VALUE y);
|
||||||
|
static int int_round_zero_p(VALUE num, int ndigits);
|
||||||
static VALUE flo_truncate(VALUE num);
|
static VALUE flo_truncate(VALUE num);
|
||||||
static int float_invariant_round(double number, int ndigits, VALUE *num);
|
static int float_invariant_round(double number, int ndigits, VALUE *num);
|
||||||
|
|
||||||
|
@ -1766,6 +1767,24 @@ flo_ceil(VALUE num)
|
||||||
return dbl2ival(f);
|
return dbl2ival(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
int_round_zero_p(VALUE num, int ndigits)
|
||||||
|
{
|
||||||
|
long bytes;
|
||||||
|
/* If 10**N / 2 > num, then return 0 */
|
||||||
|
/* We have log_256(10) > 0.415241 and log_256(1/2) = -0.125, so */
|
||||||
|
if (FIXNUM_P(num)) {
|
||||||
|
bytes = sizeof(long);
|
||||||
|
}
|
||||||
|
else if (RB_TYPE_P(num, T_BIGNUM)) {
|
||||||
|
bytes = rb_big_size(num);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
bytes = NUM2LONG(rb_funcall(num, idSize, 0));
|
||||||
|
}
|
||||||
|
return (-0.415241 * ndigits - 0.125 > bytes);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Assumes num is an Integer, ndigits <= 0
|
* Assumes num is an Integer, ndigits <= 0
|
||||||
*/
|
*/
|
||||||
|
@ -1773,11 +1792,8 @@ VALUE
|
||||||
rb_int_round(VALUE num, int ndigits)
|
rb_int_round(VALUE num, int ndigits)
|
||||||
{
|
{
|
||||||
VALUE n, f, h, r;
|
VALUE n, f, h, r;
|
||||||
long bytes;
|
|
||||||
/* If 10**N / 2 > num, then return 0 */
|
if (int_round_zero_p(num, ndigits)) {
|
||||||
/* We have log_256(10) > 0.415241 and log_256(1/2) = -0.125, so */
|
|
||||||
bytes = FIXNUM_P(num) ? sizeof(long) : rb_funcall(num, idSize, 0);
|
|
||||||
if (-0.415241 * ndigits - 0.125 > bytes ) {
|
|
||||||
return INT2FIX(0);
|
return INT2FIX(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue