23 lines
881 B
C
23 lines
881 B
C
#include <limits.h>
|
|
#include <assert.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);
|
|
}
|
|
|
|
int main() {
|
|
unsigned x = 0x71827364, y = 0xF0E1D2C3;
|
|
assert((unsigned long long) x * y == ((unsigned long long) unsigned_high_prod(x, y) << 32) + x * y);
|
|
x = 0x91827364, y = 0xF0E1D2C3;
|
|
assert((unsigned long long) x * y == ((unsigned long long) unsigned_high_prod(x, y) << 32) + x * y);
|
|
x = 0x71827364, y = 0x60E1D2C3;
|
|
assert((unsigned long long) x * y == ((unsigned long long) unsigned_high_prod(x, y) << 32) + x * y);
|
|
x = 0x91827364, y = 0x60E1D2C3;
|
|
assert((unsigned long long) x * y == ((unsigned long long) unsigned_high_prod(x, y) << 32) + x * y);
|
|
} |