ruby-changes:42088
From: akr <ko1@a...>
Date: Fri, 18 Mar 2016 00:27:10 +0900 (JST)
Subject: [ruby-changes:42088] akr:r54162 (trunk): * enum.c (ary_inject_op): Implement the specialized code for sum of
akr 2016-03-18 00:27:05 +0900 (Fri, 18 Mar 2016) New Revision: 54162 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54162 Log: * enum.c (ary_inject_op): Implement the specialized code for sum of float numbers. Modified files: trunk/ChangeLog trunk/enum.c trunk/test/ruby/test_enum.rb Index: enum.c =================================================================== --- enum.c (revision 54161) +++ enum.c (revision 54162) @@ -632,8 +632,9 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/enum.c#L632 ary_inject_op(VALUE ary, VALUE init, VALUE op) { ID id; - VALUE v; - long i; + VALUE v, e; + long i, n; + double f; if (RARRAY_LEN(ary) == 0) return init == Qundef ? Qnil : init; @@ -641,6 +642,8 @@ ary_inject_op(VALUE ary, VALUE init, VAL https://github.com/ruby/ruby/blob/trunk/enum.c#L642 if (init == Qundef) { v = RARRAY_AREF(ary, 0); i = 1; + if (RARRAY_LEN(ary) == 1) + return v; } else { v = init; @@ -649,43 +652,54 @@ ary_inject_op(VALUE ary, VALUE init, VAL https://github.com/ruby/ruby/blob/trunk/enum.c#L652 id = SYM2ID(op); if (id == idPLUS) { - if (FIXNUM_P(v) && - rb_method_basic_definition_p(rb_cFixnum, idPLUS)) { - long n = FIX2LONG(v); - while (i < RARRAY_LEN(ary)) { - VALUE e = RARRAY_AREF(ary, i); - if (!FIXNUM_P(e)) break; - n += FIX2LONG(e); /* should not overflow long type */ - i++; - if (!FIXABLE(n)) break; - } - v = LONG2NUM(n); - } - if (i < RARRAY_LEN(ary) && (FIXNUM_P(v) || RB_TYPE_P(v, T_BIGNUM)) && - rb_method_basic_definition_p(rb_cFixnum, idPLUS) && - rb_method_basic_definition_p(rb_cBignum, idPLUS)) { - long n = 0; - while (i < RARRAY_LEN(ary)) { - VALUE e = RARRAY_AREF(ary, i); + if ((FIXNUM_P(v) || RB_TYPE_P(v, T_BIGNUM)) && + rb_method_basic_definition_p(rb_cFixnum, idPLUS) && + rb_method_basic_definition_p(rb_cBignum, idPLUS)) { + n = 0; + while (1) { + e = RARRAY_AREF(ary, i); if (FIXNUM_P(e)) { n += FIX2LONG(e); /* should not overflow long type */ - i++; if (!FIXABLE(n)) { v = rb_big_plus(LONG2NUM(n), v); n = 0; } } - else if (RB_TYPE_P(e, T_BIGNUM)) { + else if (RB_TYPE_P(e, T_BIGNUM)) v = rb_big_plus(e, v); - i++; - } - else { + else break; - } + i++; + if (RARRAY_LEN(ary) <= i) + return n == 0 ? v : rb_fix_plus(LONG2FIX(n), v); } if (n != 0) { v = rb_fix_plus(LONG2FIX(n), v); } + if (RB_FLOAT_TYPE_P(e) && + rb_method_basic_definition_p(rb_cFloat, idPLUS)) { + f = NUM2DBL(v); + goto sum_float; + } + } + else if (RB_FLOAT_TYPE_P(v) && + rb_method_basic_definition_p(rb_cFloat, idPLUS)) { + f = RFLOAT_VALUE(v); + sum_float: + while (1) { + e = RARRAY_AREF(ary, i); + if (RB_FLOAT_TYPE_P(e)) + f += RFLOAT_VALUE(e); + else if (FIXNUM_P(e)) + f += FIX2LONG(e); + else if (RB_TYPE_P(e, T_BIGNUM)) + f += rb_big2dbl(e); + else + break; + i++; + if (RARRAY_LEN(ary) <= i) + return DBL2NUM(f); + } } } for (; i<RARRAY_LEN(ary); i++) { Index: ChangeLog =================================================================== --- ChangeLog (revision 54161) +++ ChangeLog (revision 54162) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Mar 18 00:25:56 2016 Tanaka Akira <akr@f...> + + * enum.c (ary_inject_op): Implement the specialized code for sum of + float numbers. + Fri Mar 18 00:15:05 2016 Yusuke Endoh <mame@r...> * numeric.c (num_step): use rb_equal for zero check. rb_num_coerce_cmp Index: test/ruby/test_enum.rb =================================================================== --- test/ruby/test_enum.rb (revision 54161) +++ test/ruby/test_enum.rb (revision 54162) @@ -197,11 +197,16 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L197 assert_equal(105, [5, 7].inject(3, :*)) end + def assert_float_equal(e, v, msg=nil) + assert_equal(Float, v.class, msg) + assert_equal(e, v, msg) + end + def test_inject_array_plus assert_equal(3, [3].inject(:+)) assert_equal(8, [3, 5].inject(:+)) assert_equal(15, [3, 5, 7].inject(:+)) - assert_equal(15.0, [3, 5, 7.0].inject(:+)) + assert_float_equal(15.0, [3, 5, 7.0].inject(:+)) assert_equal(2*FIXNUM_MAX, Array.new(2, FIXNUM_MAX).inject(:+)) assert_equal(2*(FIXNUM_MAX+1), Array.new(2, FIXNUM_MAX+1).inject(:+)) assert_equal(10*FIXNUM_MAX, Array.new(10, FIXNUM_MAX).inject(:+)) @@ -209,6 +214,9 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L214 assert_equal(FIXNUM_MAX*10, ([FIXNUM_MAX+1, -1]*10).inject(:+)) assert_equal(2*FIXNUM_MIN, Array.new(2, FIXNUM_MIN).inject(:+)) assert_equal((FIXNUM_MAX+1).to_f, [FIXNUM_MAX, 1, 0.0].inject(:+)) + assert_float_equal(10.0, [3.0, 5].inject(2.0, :+)) + assert_float_equal((FIXNUM_MAX+1).to_f, [0.0, FIXNUM_MAX+1].inject(:+)) + assert_equal(2.0+3.0i, [2.0, 3.0i].inject(:+)) end def test_inject_array_plus_redefined -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/