[#8815] Segfault in libc strlen, via rb_str_new2 — "Sean E. Russell" <ser@...>

Howdy,

12 messages 2006/09/09
[#8817] Re: Segfault in libc strlen, via rb_str_new2 — Eric Hodel <drbrain@...7.net> 2006/09/09

On Sep 8, 2006, at 10:10 PM, Sean E. Russell wrote:

Re: bignums

From: nobu@...
Date: 2006-09-10 03:05:22 UTC
List: ruby-core #8822
Hi,

At Sat, 9 Sep 2006 19:10:04 +0900,
Ondrej Bilka wrote in [ruby-core:08819]:
> I thought that assembler has something like MULC which like ADC handles
> carry but didnt find anything.
> I written another patch. 
> For 32 bits I use long long multiplication which is faster than checking by division.
> For 64bits I have heuristic that if both are <2**31 is not necessary
> perform check. But I am not sure if it will cause slowdown or speedup.

You should use SIGNED_VALUE.


Index: numeric.c
===================================================================
RCS file: /cvs/ruby/src/ruby/numeric.c,v
retrieving revision 1.143
diff -p -U 2 -r1.143 numeric.c
--- numeric.c	4 Sep 2006 20:10:45 -0000	1.143
+++ numeric.c	10 Sep 2006 03:02:24 -0000
@@ -1974,6 +1974,11 @@ fix_mul(VALUE x, VALUE y)
         volatile
 #endif
-	long a, b, c;
+	SIGNED_VALUE a, b;
+#if SIZEOF_VALUE * 2 <= SIZEOF_LONG_LONG
+	LONG_LONG d;
+#else
+	SIGNED_VALUE c;
 	VALUE r;
+#endif
 
 	a = FIX2LONG(x);
@@ -1981,4 +1986,15 @@ fix_mul(VALUE x, VALUE y)
 
 	b = FIX2LONG(y);
+
+#if SIZEOF_VALUE * 2 <= SIZEOF_LONG_LONG
+	d = (LONG_LONG)a * b;
+	if (FIXABLE(d)) return LONG2FIX(d);
+	return rb_ll2inum(d);
+#else
+#	define SQRT_LONG_MAX (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;
 	r = LONG2FIX(c);
@@ -1988,4 +2004,5 @@ fix_mul(VALUE x, VALUE y)
 	}
 	return r;
+#endif
     }
     switch (TYPE(y)) {


-- 
Nobu Nakada

In This Thread