Skip to content

saturating_mul for uint64 returns 0xffffffff00000000 instead of UINT64_MAX on overflow #3951

Closed

Description

@aneshlya

Both the uniform and varying unsigned int64 overloads of saturating_mul give the wrong result when exactly one operand has a nonzero upper 32 bits and the product overflows. They return 0xffffffff00000000 when they should saturate to UINT64_MAX (0xffffffffffffffff).

Reproducer

#include <stdio.h>
export void test() {
    uniform uint64 a = 0x8000000000000000, b = 2;
    uniform uint64 r = saturating_mul(a, b);
    print("%\n", r);   // prints 18446744069414584320 (0xffffffff00000000)
                       // expected 18446744073709551615 (UINT64_MAX)
}

Other inputs that also return 0xffffffff00000000: saturating_mul(0x8000000000000001, 2), saturating_mul(0xffffffff00000000, 2), saturating_mul(0x100000000, 0x100000000) and the swapped-operand versions of each.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Standard Library

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions