/* 128-bit blocks and associated operations * * $ gcc -Wall -o blk128 blk128.c */ #include #include #include typedef struct __blk128_t { char mx; /* The 65th bit */ uint64_t hi; uint64_t lo; } blk128_t; void blk128_add(blk128_t *blk, uint64_t x) { blk->lo += x; if (blk->lo < x) { blk->hi++; if (!blk->hi) blk->mx = 1; } } #define blk128_init() (blk128_t*)malloc(sizeof(blk128_t)) #define u64(x) ((uint64_t)x) #define blk128_print(x) \ do {\ printf("c: %d, hi: %lu, lo: %lu\n", x.mx, x.hi, x.lo);\ } while(0); /* Compose two 64 bit numbers as a 128-bit number */ void blk128_compose(blk128_t *rop, uint64_t x, uint64_t y) { /* Compute x * (B + 1) + y * * Equivalent to x*B + x + y */ rop->hi = x; rop->lo = y; blk128_add(rop, x); } int main(int argc, char *argv[]) { blk128_t t = {0, 0, 0}; blk128_add(&t, 5); blk128_add(&t, u64(~0)); blk128_print(t); /* Max it out! */ t.hi = u64(~0); blk128_print(t); blk128_add(&t, u64(~0)); blk128_print(t); blk128_t rop = {0, 0, 0}; blk128_compose(&rop, 5, 7); blk128_print(rop); return 0; }