ruby-changes:61199
From: Yusuke <ko1@a...>
Date: Tue, 12 May 2020 02:14:45 +0900 (JST)
Subject: [ruby-changes:61199] 42abad2464 (master): numeric.c: optimize `float ** 2` case by fastpath
https://git.ruby-lang.org/ruby.git/commit/?id=42abad2464 From 42abad2464a78e3ec8a42f85c7bc878233f3ce16 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh <mame@r...> Date: Tue, 12 May 2020 02:12:06 +0900 Subject: numeric.c: optimize `float ** 2` case by fastpath It would be a relatively frequent case. It is still slower than `float * float` because `*` has a dedicated VM instruction (opt_mult), though. diff --git a/numeric.c b/numeric.c index 03d8b86..04fd3a2 100644 --- a/numeric.c +++ b/numeric.c @@ -1309,7 +1309,11 @@ VALUE https://github.com/ruby/ruby/blob/trunk/numeric.c#L1309 rb_float_pow(VALUE x, VALUE y) { double dx, dy; - if (RB_TYPE_P(y, T_FIXNUM)) { + if (y == INT2FIX(2)) { + dx = RFLOAT_VALUE(x); + return DBL2NUM(dx * dx); + } + else if (RB_TYPE_P(y, T_FIXNUM)) { dx = RFLOAT_VALUE(x); dy = (double)FIX2LONG(y); } diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index 0daa3cc..fbf0d87 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -305,6 +305,7 @@ class TestFloat < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_float.rb#L305 assert_equal(1.0, 1.0 ** (2**32)) assert_equal(1.0, 1.0 ** 1.0) assert_raise(TypeError) { 1.0 ** nil } + assert_equal(9.0, 3.0 ** 2) end def test_eql -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/