* 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
@ -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>
|
Sat Jul 14 02:05:53 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* insns.def (opt_div): LONG2FIX() may not work for corner cases,
|
* 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.
|
* 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
|
static VALUE
|
||||||
fix_mul(VALUE x, VALUE y)
|
fix_mul(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
@ -2070,9 +2074,6 @@ fix_mul(VALUE x, VALUE y)
|
|||||||
if (FIXABLE(d)) return LONG2FIX(d);
|
if (FIXABLE(d)) return LONG2FIX(d);
|
||||||
return rb_ll2inum(d);
|
return rb_ll2inum(d);
|
||||||
#else
|
#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))
|
if (FIT_SQRT_LONG(a) && FIT_SQRT_LONG(b))
|
||||||
return LONG2FIX(a*b);
|
return LONG2FIX(a*b);
|
||||||
c = a * b;
|
c = a * b;
|
||||||
@ -2288,15 +2289,14 @@ int_pow(long x, unsigned long y)
|
|||||||
y &= ~1;
|
y &= ~1;
|
||||||
do {
|
do {
|
||||||
while (y % 2 == 0) {
|
while (y % 2 == 0) {
|
||||||
long x2 = x * x;
|
if (!FIT_SQRT_LONG(x)) {
|
||||||
if (x2 < x || !POSFIXABLE(x2)) {
|
|
||||||
VALUE v;
|
VALUE v;
|
||||||
bignum:
|
bignum:
|
||||||
v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
|
v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
|
||||||
if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
|
if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
x = x2;
|
x = x * x;
|
||||||
y >>= 1;
|
y >>= 1;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user