ruby-changes:44677
From: mrkn <ko1@a...>
Date: Sat, 12 Nov 2016 20:52:04 +0900 (JST)
Subject: [ruby-changes:44677] mrkn:r56750 (trunk): rational.c: optimize Float#to_r
mrkn 2016-11-12 20:52:00 +0900 (Sat, 12 Nov 2016) New Revision: 56750 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56750 Log: 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. Modified files: trunk/internal.h trunk/numeric.c trunk/rational.c Index: internal.h =================================================================== --- internal.h (revision 56749) +++ internal.h (revision 56750) @@ -1178,6 +1178,7 @@ VALUE rb_int_cmp(VALUE x, VALUE y); https://github.com/ruby/ruby/blob/trunk/internal.h#L1178 VALUE rb_int_equal(VALUE x, VALUE y); VALUE rb_int_divmod(VALUE x, VALUE y); VALUE rb_int_and(VALUE x, VALUE y); +VALUE rb_int_lshift(VALUE x, VALUE y); #if USE_FLONUM #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n))) Index: rational.c =================================================================== --- rational.c (revision 56749) +++ rational.c (revision 56750) @@ -2071,14 +2071,17 @@ float_to_r(VALUE self) https://github.com/ruby/ruby/blob/trunk/rational.c#L2071 long ln = FIX2LONG(n); if (ln == 0) - return f_to_r(f); + return rb_rational_new1(f); if (ln > 0) - return f_to_r(f_lshift(f, n)); + return rb_rational_new1(rb_int_lshift(f, n)); ln = -ln; - return rb_rational_new2(f, f_lshift(ONE, INT2FIX(ln))); + return rb_rational_new2(f, rb_int_lshift(ONE, INT2FIX(ln))); } #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 } Index: numeric.c =================================================================== --- numeric.c (revision 56749) +++ numeric.c (revision 56750) @@ -4390,7 +4390,7 @@ fix_lshift(long val, unsigned long width https://github.com/ruby/ruby/blob/trunk/numeric.c#L4390 return LONG2NUM(val); } -static VALUE +VALUE rb_int_lshift(VALUE x, VALUE y) { if (FIXNUM_P(x)) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/