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

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.
This commit is contained in:
Yusuke Endoh 2020-05-12 02:12:06 +09:00
parent 95ac235537
commit 42abad2464
2 changed files with 6 additions and 1 deletions

View file

@ -1309,7 +1309,11 @@ VALUE
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);
}

View file

@ -305,6 +305,7 @@ class TestFloat < Test::Unit::TestCase
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