Skip to content
TheChatArchiveThe Chat Archive
Mistralmistral-medium-2508· Mistral AIHugging Face Arena Expert 5K

Give me an algorithm for determining whether a 64-bit sized integer multiplication…

  1. User

    Give me an algorithm for determining whether a 64-bit sized integer multiplication resulted in underflow or overflow using only the two operands and their (potentially incorrect) product.

  2. Mistral Assistant

    To determine whether a 64-bit integer multiplication resulted in overflow or underflow using only the two operands (a, b) and their (potentially incorrect) product (p), you can use the following algorithm. This works for both signed and unsigned 64-bit integers, but the logic differs slightly between the two cases.

    Key Observations:

    1. Overflow/Underflow in Unsigned Multiplication:

      • For unsigned 64-bit integers, overflow occurs if the true product is ≥ (2^{64}).
      • The true product can be checked by comparing p / a with b (or p / b with a). If they don't match, overflow occurred.
    2. Overflow/Underflow in Signed Multiplication:

      • For signed 64-bit integers, overflow occurs if the true product is outside the range ([-2^{63}, 2^{63} - 1]).
      • The true product can be checked by comparing p / a with b (or p / b with a), but you must account for division truncation and signs.

    Algorithm (Pseudocode):

    Here’s a general approach that works for both signed and unsigned integers:

    For Unsigned 64-bit Integers:

    def has_unsigned_overflow(a: uint64, b: uint64, p: uint64) -> bool:
        if a == 0 or b == 0:
            return False  # Product is 0, no overflow
        # Check if p / a == b (or p / b == a)
        # If not, overflow occurred
        return (p // a != b) and (p // b != a)
    

    For Signed 64-bit Integers:

    def has_signed_overflow(a: int64, b: int64, p: int64) -> bool:
        if a == 0 or b == 0:
            return False  # Product is 0, no overflow
        # Check if p / a == b (or p / b == a)
        # Need to handle division truncation and signs
        if a > 0:
            if b > 0:
                return p < 0 or (p // a != b)
            else:
                return p > 0 or (p // a != b)
        else:  # a < 0
            if b > 0:
                return p > 0 or (p // a != b)
            else:  # b < 0
                return p < 0 or (p // a != b)
    

    Explanation:

    1. Unsigned Case:

      • If a or b is 0, the product is 0, so no overflow.
      • Otherwise, if p // a != b and p // b != a, then p is not the true product, so overflow occurred.
    2. Signed Case:

      • If a or b is 0, the product is 0, so no overflow.
      • Otherwise, check the signs of a and b:
        • If a and b are both positive, the product should be positive. If p is negative or p // a != b, overflow occurred.
        • If a is positive and b is negative, the product should be negative. If p is positive or p // a != b, overflow occurred.
        • Similarly for other sign combinations.

    Edge Cases:

    • If a or b is 0, no overflow can occur.
    • If a or b is -1 and the other is INT64_MIN, the true product is INT64_MAX + 1, which overflows. The algorithm correctly detects this because p (the incorrect product) will not satisfy p // a == b.

    Example:

    • Unsigned Overflow:

      • a = 18446744073709551615 (UINT64_MAX), b = 2, p = 18446744073709551614 (incorrect due to overflow).
      • p // a = 0 != b, so overflow is detected.
    • Signed Overflow:

      • a = -9223372036854775808 (INT64_MIN), b = -1, p = -9223372036854775808 (incorrect due to overflow).
      • p // a = 1 != b, so overflow is detected.

    This algorithm efficiently checks for overflow/underflow without needing additional bits or wider arithmetic.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.