summaryrefslogtreecommitdiff
path: root/bignum.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-18 08:17:50 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-18 08:17:50 +0000
commited4c5f38ba25e61a8c65abad4254317e253e36e8 (patch)
tree7b8786083cd7ded63c75b23f509b56641c3fd3e9 /bignum.c
parent2555f3f5bf0c2c343fdbec6c42570c781cbbe7c8 (diff)
* bignum.c (rb_big_pow): estimate result bit size more precisely.
[ruby-core:30735][Feature #3429] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35081 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'bignum.c')
-rw-r--r--bignum.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/bignum.c b/bignum.c
index b53719311c..fae29652b6 100644
--- a/bignum.c
+++ b/bignum.c
@@ -3095,10 +3095,11 @@ rb_big_pow(VALUE x, VALUE y)
else {
VALUE z = 0;
SIGNED_VALUE mask;
- const long BIGLEN_LIMIT = 1024*1024 / SIZEOF_BDIGITS;
+ const long xlen = RBIGNUM_LEN(x) - 1;
+ const long xbits = ffs(RBIGNUM_DIGITS(x)[xlen]) + SIZEOF_BDIGITS*BITSPERDIG*xlen;
+ const long BIGLEN_LIMIT = BITSPERDIG*1024*1024;
- if ((RBIGNUM_LEN(x) > BIGLEN_LIMIT) ||
- (RBIGNUM_LEN(x) > BIGLEN_LIMIT / yy)) {
+ if ((xbits > BIGLEN_LIMIT) || (xbits * yy > BIGLEN_LIMIT)) {
rb_warn("in a**b, b may be too big");
d = (double)yy;
break;