Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define L_pb 8 // proba bit-precision for uABS
- #define L_IO 16 // 16bit I/O
- const uint32_t base_x = 1ul << L_pb;
- const uint32_t K = 1ul << L_pb;
- size_t uABSEncode(const uint8_t* in, size_t in_size,
- uint8_t* const buf_start, uint8_t* const buf_end,
- uint32_t p0) {
- p0 = (p0 < 1) ? 1 : (p0 >= K) ? K - 1 : p0; // avoid corner cases
- const uint16_t q0 = K - p0;
- const uint32_t threshold0 = p0 << L_IO, threshold1 = q0 << L_IO;
- uint16_t* buf = (uint16_t*)buf_end;
- uint32_t x = base_x;
- int i;
- for (i = in_size - 1; i >= 0; --i) {
- while (x >= (!in[i] ? threshold0 : threshold1)) {
- *--buf = x;
- x >>= L_IO;
- }
- const uint64_t xK = (uint64_t)x * K;
- x = !in[i] ? xK / p0 : (xK + K - 1) / q0;
- }
- for (i = 0; i < 2; ++i, x >>= L_IO) *--buf = x;
- return buf_end - (uint8_t*)buf;
- }
- void uABSDecode(const uint16_t* ptr, uint8_t* out, size_t in_size,
- uint16_t p0) {
- p0 = (p0 < 1) ? 1 : (p0 >= K) ? K - 1 : p0; // avoid corner cases
- const uint16_t q0 = K - p0;
- uint32_t x = 0;
- int i;
- for (i = 0; i < in_size; ++i) {
- while (x < base_x) {
- x = (x << L_IO) | *ptr++; // decode forward
- }
- uint64_t xp = (uint64_t)x * q0;
- out[i] = ((xp & (K - 1)) + q0) >> L_pb; // also: out[i] = ((xp & (K - 1)) >= p0);
- xp >>= L_pb;
- x = out[i] ? xp : x - xp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement