void show_signed_high_prod(int x, int y) { printf("%d %d\n\n", x, y); unsigned long long ullx = (unsigned long long)(unsigned)x; unsigned long long ully = (unsigned long long)(unsigned)y; unsigned long long ullxy = ullx*ully; unsigned ullxy1 = ullxy>>32; show_bytes(&ullx, 8); show_bytes(&ully, 8); show_bytes(&ullxy, 8); show_bytes(&ullxy1, 4); assert(ullxy1 == unsigned_high_prod(x, y)); printf("\n\n"); unsigned ullxy2 = unsigned_high_prod(x, y); show_bytes(&ullxy2, 4); signed long long sllx = (signed long long)x; signed long long slly = (signed long long)y; signed long long sllxy = sllx*slly; signed long sllxy1 = sllxy >>32; show_bytes(&sllx, 8); show_bytes(&slly, 8); show_bytes(&sllxy, 8); show_bytes(&sllxy1, 4); assert(sllxy1 == check_signed_high_prod(x, y)); printf("check_signed_high_prod = 0x%08X\n", check_signed_high_prod(x, y)); printf("\n"); assert(signed_high_prod(x,y) == check_signed_high_prod(x, y)); }
unsigned int unsigned_high_prod(unsigned x, unsigned y) { /* this solution is based on equation(2.18) on pp.90*/ int w = sizeof(int) << 3; int msb_x = x >> (w-1); int msb_y = y >> (w-1); return signed_high_prod(x, y) + msb_y*x + msb_x*y; }
unsigned unsigned_high_prod(unsigned x, unsigned y) { unsigned p = (unsigned) signed_high_prod((int) x, (int) y); /* Solution omitted */ return p; }