summaryrefslogtreecommitdiffhomepage
path: root/src/numeric.c
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2017-05-26 02:19:07 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2017-05-26 02:19:07 +0900
commit06c4933f904efa9669d413a57e158cb079c52fee (patch)
tree34156e37dc806e9f9a9fbed14e2c406b8fc6c0e3 /src/numeric.c
parent9e40586d439a8e83ca3cd9608b5407fbf02e2d6d (diff)
downloadmruby-06c4933f904efa9669d413a57e158cb079c52fee.tar.gz
mruby-06c4933f904efa9669d413a57e158cb079c52fee.zip
Try our own `ipow()` if both base and exp are fixnums; fix #3652
Diffstat (limited to 'src/numeric.c')
-rw-r--r--src/numeric.c33
1 files changed, 27 insertions, 6 deletions
diff --git a/src/numeric.c b/src/numeric.c
index 3c8729a0c..a4b501ae3 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -52,14 +52,35 @@ static mrb_value
num_pow(mrb_state *mrb, mrb_value x)
{
mrb_value y;
- mrb_float d, yv;
+ mrb_float d;
mrb_get_args(mrb, "o", &y);
- yv = mrb_to_flo(mrb, y);
- d = pow(mrb_to_flo(mrb, x), yv);
- if (mrb_fixnum_p(x) && mrb_fixnum_p(y) && FIXABLE(d) && yv > 0 &&
- (d < 0 || (d > 0 && (mrb_int)d > 0)))
- return mrb_fixnum_value((mrb_int)d);
+ if (mrb_fixnum_p(x) && mrb_fixnum_p(y)) {
+ /* try ipow() */
+ mrb_int base = mrb_fixnum(x);
+ mrb_int exp = mrb_fixnum(y);
+ mrb_int result = 1;
+ mrb_bool ok = TRUE;
+
+ for (;;) {
+ if (exp & 1) {
+ if (mrb_int_mul_overflow(result, base, &result)) {
+ ok = FALSE;
+ break;
+ }
+ }
+ exp >>= 1;
+ if (exp == 0) break;
+ if (mrb_int_mul_overflow(base, base, &base)) {
+ ok = FALSE;
+ break;
+ }
+ }
+ if (ok) {
+ return mrb_fixnum_value(result);
+ }
+ }
+ d = pow(mrb_to_flo(mrb, x), mrb_to_flo(mrb, y));
return mrb_float_value(mrb, d);
}