Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define L 48 // between 16 and 48
- // The I/O is still 16b-based.
- size_t bANSEncode(const uint8_t* in, size_t in_size,
- uint8_t* const buf_start, uint8_t* const buf_end,
- uint16_t p0) {
- const uint16_t q0 = 0x10000u - p0;
- const uint64_t threshold0 = (uint64_t)p0 << L;
- const uint64_t threshold1 = (uint64_t)q0 << L;
- uint16_t* buf = (uint16_t*)buf_end;
- uint64_t x = 1ull << L;
- int i;
- for (i = in_size - 1; i >= 0; --i) {
- if (x >= (in[i] ? threshold1 : threshold0)) {
- *--buf = x & 0xffff;
- x >>= 16;
- }
- if (in[i]) {
- x = ((x / q0) << 16) + (x % q0) + p0;
- } else {
- x = ((x / p0) << 16) + (x % p0) + 0;
- }
- }
- *--buf = (x >> 0) & 0xffff;
- *--buf = (x >> 16) & 0xffff;
- *--buf = (x >> 32) & 0xffff;
- *--buf = (x >> 48) & 0xffff;
- return buf_end - (uint8_t*)buf;
- }
- void bANSDecode(const uint16_t* ptr, uint8_t* out, size_t in_size, uint16_t p0) {
- int i;
- uint64_t x = 0;
- for (i = 0; i < 4; ++i) x = (x << 16) | *ptr++;
- for (i = 0; i < in_size; ++i) {
- if (x < (1ull << L)) {
- x = (x << 16) | *ptr++; // decode forward
- }
- const uint16_t xfrac = x & 0xffff;
- const uint64_t x_new = p0 * (x >> 16);
- out[i] = (xfrac >= p0);
- if (xfrac < p0) {
- x = x_new + xfrac;
- } else {
- x -= x_new + p0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement