summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
author卜部昌平 <shyouhei@ruby-lang.org>2020-06-16 10:39:07 +0900
committer卜部昌平 <shyouhei@ruby-lang.org>2020-06-29 11:05:41 +0900
commit03354feb6a01b048039cc586dd7135ee71446821 (patch)
tree7ec75a38cdf7740f6df6d13ecf672928037e0dda /numeric.c
parent250189f54f2cf690195573ee82082c42b21ccac6 (diff)
fix_pow: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a branch is definitely a bad idea. Better refactor.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3247
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c57
1 files changed, 26 insertions, 31 deletions
diff --git a/numeric.c b/numeric.c
index 52d7a74344..f0c45fa6ce 100644
--- a/numeric.c
+++ b/numeric.c
@@ -4009,6 +4009,25 @@ rb_int_positive_pow(long x, unsigned long y)
}
static VALUE
+fix_pow_inverted(VALUE x, VALUE minusb)
+{
+ if (x == INT2FIX(0)) {
+ rb_num_zerodiv();
+ }
+ else {
+ VALUE y = rb_int_pow(x, minusb);
+
+ if (RB_FLOAT_TYPE_P(y)) {
+ double d = pow((double)FIX2LONG(x), RFLOAT_VALUE(y));
+ return DBL2NUM(1.0 / d);
+ }
+ else {
+ return rb_rational_raw(INT2FIX(1), y);
+ }
+ }
+}
+
+static VALUE
fix_pow(VALUE x, VALUE y)
{
long a = FIX2LONG(x);
@@ -4017,18 +4036,8 @@ fix_pow(VALUE x, VALUE y)
long b = FIX2LONG(y);
if (a == 1) return INT2FIX(1);
- if (a == -1) {
- if (b % 2 == 0)
- return INT2FIX(1);
- else
- return INT2FIX(-1);
- }
- if (b < 0) {
- if (a == 0) rb_num_zerodiv();
- y = rb_int_pow(x, LONG2NUM(-b));
- goto inverted;
- }
-
+ if (a == -1) return INT2FIX(b % 2 ? -1 : 1);
+ if (b < 0) return fix_pow_inverted(x, fix_uminus(y));
if (b == 0) return INT2FIX(1);
if (b == 1) return x;
if (a == 0) return INT2FIX(0);
@@ -4036,20 +4045,8 @@ fix_pow(VALUE x, VALUE y)
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
if (a == 1) return INT2FIX(1);
- if (a == -1) {
- if (int_even_p(y)) return INT2FIX(1);
- else return INT2FIX(-1);
- }
- if (BIGNUM_NEGATIVE_P(y)) {
- if (a == 0) rb_num_zerodiv();
- y = rb_int_pow(x, rb_big_uminus(y));
- inverted:
- if (RB_FLOAT_TYPE_P(y)) {
- double d = pow((double)a, RFLOAT_VALUE(y));
- return DBL2NUM(1.0 / d);
- }
- return rb_rational_raw(INT2FIX(1), y);
- }
+ if (a == -1) return INT2FIX(int_even_p(y) ? 1 : -1);
+ if (BIGNUM_NEGATIVE_P(y)) return fix_pow_inverted(x, rb_big_uminus(y));
if (a == 0) return INT2FIX(0);
x = rb_int2big(FIX2LONG(x));
return rb_big_pow(x, y);
@@ -4061,11 +4058,9 @@ fix_pow(VALUE x, VALUE y)
return DBL2NUM(dy < 0 ? HUGE_VAL : 0.0);
}
if (a == 1) return DBL2NUM(1.0);
- {
- if (a < 0 && dy != round(dy))
- return rb_dbl_complex_new_polar_pi(pow(-(double)a, dy), dy);
- return DBL2NUM(pow((double)a, dy));
- }
+ if (a < 0 && dy != round(dy))
+ return rb_dbl_complex_new_polar_pi(pow(-(double)a, dy), dy);
+ return DBL2NUM(pow((double)a, dy));
}
else {
return rb_num_coerce_bin(x, y, idPow);