Advertisement
Guest User

faster uABS implementation

a guest
Nov 25th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #define L_pb   8      // proba bit-precision for uABS
  2. #define L_IO   16     // 16bit I/O
  3. const uint32_t base_x = 1ul << L_pb;
  4. const uint32_t K = 1ul << L_pb;
  5.  
  6. size_t uABSEncode(const uint8_t* in, size_t in_size,
  7.                          uint8_t* const buf_start, uint8_t* const buf_end,
  8.                          uint32_t p0) {
  9.   p0 = (p0 < 1) ? 1 : (p0 >= K) ? K - 1 : p0;   // avoid corner cases
  10.   const uint16_t q0 = K - p0;
  11.   const uint32_t threshold0 = p0 << L_IO, threshold1 = q0 << L_IO;  
  12.   uint16_t* buf = (uint16_t*)buf_end;
  13.   uint32_t x = base_x;
  14.   int i;
  15.   for (i = in_size - 1; i >= 0; --i) {
  16.     while (x >= (!in[i] ? threshold0 : threshold1)) {
  17.       *--buf = x;
  18.       x >>= L_IO;
  19.     }
  20.     const uint64_t xK = (uint64_t)x * K;
  21.     x = !in[i] ? xK / p0 : (xK + K - 1) / q0;      
  22.   }
  23.   for (i = 0; i < 2; ++i, x >>= L_IO) *--buf = x;
  24.   return buf_end - (uint8_t*)buf;
  25. }
  26.  
  27. void uABSDecode(const uint16_t* ptr, uint8_t* out, size_t in_size,
  28.                        uint16_t p0) {
  29.   p0 = (p0 < 1) ? 1 : (p0 >= K) ? K - 1 : p0;   // avoid corner cases
  30.   const uint16_t q0 = K - p0;
  31.   uint32_t x = 0;
  32.   int i;
  33.   for (i = 0; i < in_size; ++i) {
  34.     while (x < base_x) {
  35.       x = (x << L_IO) | *ptr++;   // decode forward
  36.     }
  37.     uint64_t xp = (uint64_t)x * q0;  
  38.     out[i] = ((xp & (K - 1)) + q0) >> L_pb;  // also: out[i] = ((xp & (K - 1)) >= p0);
  39.     xp >>= L_pb;
  40.     x = out[i] ? xp : x - xp;
  41.   }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement