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

rational.c: optimize Float#to_r

* rational.c (float_to_r): optimize Float#to_r.

* numeric.c (rb_int_lshift): exported.

* internal.h (rb_int_lshift): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56750 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2016-11-12 11:52:00 +00:00
parent ffa2b0061f
commit f40607d15d
3 changed files with 9 additions and 5 deletions

View file

@ -1178,6 +1178,7 @@ VALUE rb_int_cmp(VALUE x, VALUE y);
VALUE rb_int_equal(VALUE x, VALUE y); VALUE rb_int_equal(VALUE x, VALUE y);
VALUE rb_int_divmod(VALUE x, VALUE y); VALUE rb_int_divmod(VALUE x, VALUE y);
VALUE rb_int_and(VALUE x, VALUE y); VALUE rb_int_and(VALUE x, VALUE y);
VALUE rb_int_lshift(VALUE x, VALUE y);
#if USE_FLONUM #if USE_FLONUM
#define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n))) #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n)))

View file

@ -4390,7 +4390,7 @@ fix_lshift(long val, unsigned long width)
return LONG2NUM(val); return LONG2NUM(val);
} }
static VALUE VALUE
rb_int_lshift(VALUE x, VALUE y) rb_int_lshift(VALUE x, VALUE y)
{ {
if (FIXNUM_P(x)) { if (FIXNUM_P(x)) {

View file

@ -2071,14 +2071,17 @@ float_to_r(VALUE self)
long ln = FIX2LONG(n); long ln = FIX2LONG(n);
if (ln == 0) if (ln == 0)
return f_to_r(f); return rb_rational_new1(f);
if (ln > 0) if (ln > 0)
return f_to_r(f_lshift(f, n)); return rb_rational_new1(rb_int_lshift(f, n));
ln = -ln; ln = -ln;
return rb_rational_new2(f, f_lshift(ONE, INT2FIX(ln))); return rb_rational_new2(f, rb_int_lshift(ONE, INT2FIX(ln)));
} }
#else #else
return f_to_r(f_mul(f, f_expt(INT2FIX(FLT_RADIX), n))); f = rb_int_mul(f, rb_int_pow(INT2FIX(FLT_RADIX), n));
if (RB_TYPE_P(f, T_RATIONAL))
return f;
return rb_rational_new1(f);
#endif #endif
} }