Fix mulIntMayOflo on 64-bit arches; fixes trac #867
authorIan Lynagh <igloo@earth.li>
Thu, 28 Sep 2006 00:48:06 +0000 (00:48 +0000)
committerIan Lynagh <igloo@earth.li>
Thu, 28 Sep 2006 00:48:06 +0000 (00:48 +0000)
We were assuming we could multiply 2 32-bit numbers without overflowing
a 64-bit number, but we can't as the top bit is the sign bit.

includes/Stg.h

index 8e3da0f..5cd3701 100644 (file)
@@ -442,7 +442,9 @@ typedef union {
 
 /* Approximate version when we don't have long arithmetic (on 64-bit archs) */
 
-#define HALF_POS_INT  (((I_)1) << (BITS_IN (I_) / 2))
+/* If we have n-bit words then we have n-1 bits after accounting for the
+ * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
+#define HALF_POS_INT  (((I_)1) << ((BITS_IN (I_) - 1) / 2))
 #define HALF_NEG_INT  (-HALF_POS_INT)
 
 #define mulIntMayOflo(a,b)                     \