31 lines
691 B
C

#include <limits.h>
#include <assert.h>
#include <stdlib.h>
int signed_high_prod(int x, int y) {
long long p = (long long) x * y;
return p >> 32;
}
unsigned unsigned_high_prod(unsigned x, unsigned y) {
int w = sizeof(int) << 3;
return signed_high_prod(x, y) + (((int) y >> (w - 1)) & x) + (((int) x >> (w - 1)) & y);
}
unsigned randu() {
if (rand() & 1) {
return rand();
} else {
return ~rand();
}
}
int main() {
assert(RAND_MAX == INT_MAX);
const int test_cases = 1e8;
for (int i = 0; i < test_cases; i++) {
unsigned x = randu(), y = randu();
assert((unsigned long long) x * y == ((unsigned long long) unsigned_high_prod(x, y) << 32) + x * y);
}
}