bitcoder.h
Go to the documentation of this file.
1 #ifndef IZ_BITCODER_H
2 #define IZ_BITCODER_H 1
3 
4 #include "intmacros.h"
5 
6 namespace IZ {
7 
8 /* 64 bit unsigned datatype. On x86-32, the 64 bit datatypes are slow, so
9  * use MMX if available.
10  */
11 #if defined(__LP64__) || defined(__L64__)
12 typedef unsigned long U64;
13 typedef unsigned int U32;
14 #elif defined(__LLP64__) || defined(__LL64__) || defined(_WIN64)
15 typedef unsigned long long U64;
16 typedef unsigned int U32;
17 #elif defined(__MMX__)
18 #include <mmintrin.h>
19 typedef __m64 U64;
20 typedef unsigned int U32;
21 #define USE_MMX
22 #else
23 typedef unsigned long long U64;
24 typedef unsigned int U32;
25 #endif
26 
27 template<typename Code = U32>
29 {
30 protected:
43  typedef U64 Cache;
44 
45  enum Constants {
46  CodeBits = sizeof(Code) * CHAR_BIT,
47  CacheBits = sizeof(Cache) * CHAR_BIT
48  };
49 
50 protected:
54  unsigned int len;
55 
59  Cache bitcache;
60 };
61 
62 template<typename Code = U32>
63 class BitDecoder : public BitCoderBase<Code>
64 {
68 
69 public:
70  Code fetchCode() {
71  return /*__builtin_bswap32*/(*p++);
72  }
73 
74  void begin(const unsigned char *ptr) {
75  p = (const Code *) ptr;
76 #if defined(USE_MMX)
77  bitcache = _mm_cvtsi32_si64(fetchCode());
78 #else
79  bitcache = fetchCode();
80 #endif
81  len = CodeBits;
82  }
83 
84  void fillCache() {
85  if (len < CodeBits) {
86 #if defined(USE_MMX)
87  bitcache = _mm_slli_si64(bitcache, CodeBits);
88  bitcache = _mm_or_si64(bitcache, _mm_cvtsi32_si64(fetchCode()));
89 #else
90  bitcache <<= CodeBits;
91  bitcache += fetchCode();
92 #endif
93  len += CodeBits;
94  }
95  }
96 
97  unsigned int peekBits(unsigned int count) const {
98 #if defined(USE_MMX)
99  return _mm_cvtsi64_si32(_mm_srli_si64(bitcache, len - count)) & bitMask(count);
100 #else
101  return (bitcache >> (len - count)) & bitMask(count);
102 #endif
103  }
104 
105  void skipBits(unsigned int count) {
106  len -= count;
107  }
108 
109  unsigned int cachedLength() const {
110  return len;
111  }
112 
113  unsigned int readBits(unsigned int count) {
114  len -= count;
115 #if defined(USE_MMX)
116  return _mm_cvtsi64_si32(_mm_srli_si64(bitcache, len)) & bitMask(count);
117 #else
118  return (bitcache >> len) & bitMask(count);
119 #endif
120  }
121 
122  void align() {
123  len = 0;
124  }
125 
126  const unsigned char *end() {
127 #if defined(USE_MMX)
128  _mm_empty();
129 #endif
130  return (const unsigned char *) (p - (len >= CodeBits));
131  }
132 
133 private:
134  const Code *p;
135 };
136 
137 template<typename Code = U32>
138 class BitEncoder : public BitCoderBase<Code>
139 {
143 
144 public:
145  void storeCode(Code code) {
146  *p++ = /*__builtin_bswap32*/(code);
147  }
148 
149  void begin(unsigned char *ptr) {
150  p = (Code *) ptr;
151  len = 0;
152 #if defined(USE_MMX)
153  bitcache = _mm_cvtsi32_si64(0);
154 #else
155  bitcache = 0; // silence compiler
156 #endif
157  }
158 
159  void flushCache() {
160  if (len >= CodeBits) {
161  len -= CodeBits;
162 #if defined(USE_MMX)
163  storeCode(_mm_cvtsi64_si32(_mm_srli_si64(bitcache, len)));
164 #else
165  storeCode(bitcache >> len);
166 #endif
167  }
168  }
169 
170  void writeBits(unsigned int bits, unsigned int count) {
171  len += count;
172 #if defined(USE_MMX)
173  bitcache = _mm_slli_si64(bitcache, count);
174  bitcache = _mm_or_si64(bitcache, _mm_cvtsi32_si64(bits));
175 #else
176  bitcache = (bitcache << count) + bits;
177 #endif
178  }
179 
180  void align() {
181  if (len > 0) {
182 #if defined(USE_MMX)
183  storeCode(_mm_cvtsi64_si32(_mm_slli_si64(bitcache, CodeBits - len)));
184  _mm_empty();
185 #else
186  storeCode(bitcache << (CodeBits - len));
187 #endif
188  len = 0;
189  }
190  }
191 
192  unsigned char *end() {
193  align();
194  return (unsigned char *) p;
195  }
196 
197 private:
198  Code *p;
199 };
200 
201 } // namespace IZ
202 
203 #endif
unsigned long long U64
Definition: bitcoder.h:23
void skipBits(unsigned int count)
Definition: bitcoder.h:105
const unsigned char * end()
Definition: bitcoder.h:126
unsigned int len
Definition: bitcoder.h:54
void align()
Definition: bitcoder.h:180
void begin(const unsigned char *ptr)
Definition: bitcoder.h:74
Definition: bitcoder.h:6
unsigned int U32
Definition: bitcoder.h:24
void writeBits(unsigned int bits, unsigned int count)
Definition: bitcoder.h:170
void storeCode(Code code)
Definition: bitcoder.h:145
const Code * p
Definition: bitcoder.h:134
void begin(unsigned char *ptr)
Definition: bitcoder.h:149
Cache bitcache
Definition: bitcoder.h:59
void align()
Definition: bitcoder.h:122
unsigned int readBits(unsigned int count)
Definition: bitcoder.h:113
unsigned int peekBits(unsigned int count) const
Definition: bitcoder.h:97
static unsigned int bitMask(unsigned int bitCount)
Definition: intmacros.h:11
void fillCache()
Definition: bitcoder.h:84
void flushCache()
Definition: bitcoder.h:159
unsigned char * end()
Definition: bitcoder.h:192
unsigned int cachedLength() const
Definition: bitcoder.h:109
Code fetchCode()
Definition: bitcoder.h:70


imagezero
Author(s):
autogenerated on Mon Jun 10 2019 13:37:21