| ... |
... |
@@ -519,79 +519,3 @@ INLINE_HEADER StgInt64 PK_Int64(W_ p_src[]) |
|
519
|
519
|
}
|
|
520
|
520
|
|
|
521
|
521
|
#endif /* SIZEOF_HSWORD == 4 */ |
|
522
|
|
-
|
|
523
|
|
-/* -----------------------------------------------------------------------------
|
|
524
|
|
- Integer multiply with overflow
|
|
525
|
|
- -------------------------------------------------------------------------- */
|
|
526
|
|
-
|
|
527
|
|
-/* Multiply with overflow checking.
|
|
528
|
|
- *
|
|
529
|
|
- * This is tricky - the usual sign rules for add/subtract don't apply.
|
|
530
|
|
- *
|
|
531
|
|
- * On 32-bit machines we use gcc's 'long long' types, finding
|
|
532
|
|
- * overflow with some careful bit-twiddling.
|
|
533
|
|
- *
|
|
534
|
|
- * On 64-bit machines where gcc's 'long long' type is also 64-bits,
|
|
535
|
|
- * we use a crude approximation, testing whether either operand is
|
|
536
|
|
- * larger than 32-bits; if neither is, then we go ahead with the
|
|
537
|
|
- * multiplication.
|
|
538
|
|
- *
|
|
539
|
|
- * Return non-zero if there is any possibility that the signed multiply
|
|
540
|
|
- * of a and b might overflow. Return zero only if you are absolutely sure
|
|
541
|
|
- * that it won't overflow. If in doubt, return non-zero.
|
|
542
|
|
- */
|
|
543
|
|
-
|
|
544
|
|
-#if SIZEOF_VOID_P == 4
|
|
545
|
|
-
|
|
546
|
|
-#if defined(WORDS_BIGENDIAN)
|
|
547
|
|
-#define RTS_CARRY_IDX__ 0
|
|
548
|
|
-#define RTS_REM_IDX__ 1
|
|
549
|
|
-#else
|
|
550
|
|
-#define RTS_CARRY_IDX__ 1
|
|
551
|
|
-#define RTS_REM_IDX__ 0
|
|
552
|
|
-#endif
|
|
553
|
|
-
|
|
554
|
|
-typedef union {
|
|
555
|
|
- StgInt64 l;
|
|
556
|
|
- StgInt32 i[2];
|
|
557
|
|
-} long_long_u ;
|
|
558
|
|
-
|
|
559
|
|
-#define mulIntMayOflo(a,b) \
|
|
560
|
|
-({ \
|
|
561
|
|
- StgInt32 r, c; \
|
|
562
|
|
- long_long_u z; \
|
|
563
|
|
- z.l = (StgInt64)a * (StgInt64)b; \
|
|
564
|
|
- r = z.i[RTS_REM_IDX__]; \
|
|
565
|
|
- c = z.i[RTS_CARRY_IDX__]; \
|
|
566
|
|
- if (c == 0 || c == -1) { \
|
|
567
|
|
- c = ((StgWord)((a^b) ^ r)) \
|
|
568
|
|
- >> (BITS_IN (I_) - 1); \
|
|
569
|
|
- } \
|
|
570
|
|
- c; \
|
|
571
|
|
-})
|
|
572
|
|
-
|
|
573
|
|
-/* Careful: the carry calculation above is extremely delicate. Make sure
|
|
574
|
|
- * you test it thoroughly after changing it.
|
|
575
|
|
- */
|
|
576
|
|
-
|
|
577
|
|
-#else
|
|
578
|
|
-
|
|
579
|
|
-/* Approximate version when we don't have long arithmetic (on 64-bit archs) */
|
|
580
|
|
-
|
|
581
|
|
-/* If we have n-bit words then we have n-1 bits after accounting for the
|
|
582
|
|
- * sign bit, so we can fit the result of multiplying 2 (n-1)/2-bit numbers */
|
|
583
|
|
-#define HALF_POS_INT (((I_)1) << ((BITS_IN (I_) - 1) / 2))
|
|
584
|
|
-#define HALF_NEG_INT (-HALF_POS_INT)
|
|
585
|
|
-
|
|
586
|
|
-#define mulIntMayOflo(a,b) \
|
|
587
|
|
-({ \
|
|
588
|
|
- I_ c; \
|
|
589
|
|
- if ((I_)a <= HALF_NEG_INT || a >= HALF_POS_INT \
|
|
590
|
|
- || (I_)b <= HALF_NEG_INT || b >= HALF_POS_INT) {\
|
|
591
|
|
- c = 1; \
|
|
592
|
|
- } else { \
|
|
593
|
|
- c = 0; \
|
|
594
|
|
- } \
|
|
595
|
|
- c; \
|
|
596
|
|
-})
|
|
597
|
|
-#endif |