mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* numeric.c (int_pow): overflow detection using FIT_SQRT_LONG().
[ruby-dev:31215] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12778 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2a5c48a54d
commit
b13b3e624b
2 changed files with 11 additions and 6 deletions
|
@ -1,3 +1,8 @@
|
|||
Sat Jul 14 02:27:43 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* numeric.c (int_pow): overflow detection using FIT_SQRT_LONG().
|
||||
[ruby-dev:31215]
|
||||
|
||||
Sat Jul 14 02:05:53 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||
|
||||
* insns.def (opt_div): LONG2FIX() may not work for corner cases,
|
||||
|
|
12
numeric.c
12
numeric.c
|
@ -2046,6 +2046,10 @@ fix_minus(VALUE x, VALUE y)
|
|||
* result.
|
||||
*/
|
||||
|
||||
#define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_VALUE*CHAR_BIT-1)/2))
|
||||
/*tests if N*N would overflow*/
|
||||
#define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
|
||||
|
||||
static VALUE
|
||||
fix_mul(VALUE x, VALUE y)
|
||||
{
|
||||
|
@ -2070,9 +2074,6 @@ fix_mul(VALUE x, VALUE y)
|
|||
if (FIXABLE(d)) return LONG2FIX(d);
|
||||
return rb_ll2inum(d);
|
||||
#else
|
||||
# define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_VALUE*CHAR_BIT-1)/2))
|
||||
/*tests if N*N would overflow*/
|
||||
# define FIT_SQRT_LONG(n) (((n)<SQRT_LONG_MAX)&&((n)>=-SQRT_LONG_MAX))
|
||||
if (FIT_SQRT_LONG(a) && FIT_SQRT_LONG(b))
|
||||
return LONG2FIX(a*b);
|
||||
c = a * b;
|
||||
|
@ -2288,15 +2289,14 @@ int_pow(long x, unsigned long y)
|
|||
y &= ~1;
|
||||
do {
|
||||
while (y % 2 == 0) {
|
||||
long x2 = x * x;
|
||||
if (x2 < x || !POSFIXABLE(x2)) {
|
||||
if (!FIT_SQRT_LONG(x)) {
|
||||
VALUE v;
|
||||
bignum:
|
||||
v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
|
||||
if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
|
||||
return v;
|
||||
}
|
||||
x = x2;
|
||||
x = x * x;
|
||||
y >>= 1;
|
||||
}
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue