Slim numerical data compression  1.0
bitstream.h
Go to the documentation of this file.
1 // -*- mode: c++; -*-
2 
5 
6 // Copyright (C) 2008, 2009 Joseph Fowler
7 //
8 // This file is part of slim, a compression package for science data.
9 //
10 // Slim is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Slim is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with slim. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef SLIM_BITSTREAM_H
24 #define SLIM_BITSTREAM_H
25 
26 #include <stdint.h>
27 #include <cstdio>
28 
29 #include "bit_constants.h"
30 
31 using namespace std;
32 
33 class bitstream {
34 public:
35  bitstream();
36  bitstream(FILE *file, int buffersize=DEFAULT_IOBUFFER_SIZE);
37  bitstream(const char *filename,
38  int buffersize=DEFAULT_IOBUFFER_SIZE);
39  bitstream(const bitstream &b);
40  bitstream & operator=(const bitstream &b);
41  virtual ~bitstream();
42 
43  virtual void close();
44  bool is_open() const;
45  virtual void setupstream();
46  virtual void windup()=0;
47  virtual int get_bytes_used();
48  int get_bitptr();
49  virtual void print() const = 0;
50 
51 protected:
52  static const int Bits_per_word = 8*sizeof(Word_t);
53 
54  size_t bufsize;
55  size_t buf_used;
56  FILE *fp;
57  Byte_t *buffer_base;
58  Byte_t *beyondbuffer;
59  union {
60  Byte_t *Bptr;
61  Word_t *Dptr;
62  } buffptr;
63  int bitptr;
64 
67 public:
68  enum {DEFAULT_IOBUFFER_SIZE=1024*1024};
69  enum {MAX_BITSTREAM_BUFSIZE=16*1024*1024};
70 };
71 
72 
73 
74 class obitstream : public bitstream {
75 private:
76  // No private attributes.
77 
78 public:
79  obitstream(FILE *file, int buffersize=DEFAULT_IOBUFFER_SIZE);
80  obitstream(const char *filename,
81  int buffersize=DEFAULT_IOBUFFER_SIZE);
82  ~obitstream();
83 
84  void writebits(uint32_t data, int nbits);
85  void writestring(const char *str, bool write_trailing_null=false);
86  template <typename T> void writeword(const T data);
87  void write_unary(unsigned int value);
88  virtual void print() const;
89  virtual void close();
90  void windup();
91  void flush(bool flush_trailing_bits);
92 };
93 
94 
95 
96 
97 class ibitstream : public bitstream {
98 private:
99  // No private attributes (unless debugging).
100 #ifdef DEBUG_READBITS
101  int c1,c2, c3, c4, c5, c6;
102  void print_debug();
103 #endif
104 
105 public:
106  ibitstream(FILE *file, int buffersize=DEFAULT_IOBUFFER_SIZE);
107  ibitstream(const char *filename,
108  int buffersize=DEFAULT_IOBUFFER_SIZE);
109  ~ibitstream();
110 
111  void setupstream();
112  void windup();
113  virtual void print() const;
114  virtual int get_bytes_used();
115  Word_t readbits(int nbits);
116  int32_t readbits_int(int nbits);
117  Word_t read_unary();
118  int readstring(char *s, int count=-1);
119  //int get_bits_used() { return bitptr + Bits_per_word*buf_used;}
120 
121 private:
122  void next_word();
123  int fill();
124 
125  Word_t partial_word;
126  int partial_word_bitptr;
127 };
128 
129 
130 
132 // Inline functions
134 
141 static inline unsigned int bit_size(int32_t i) {
142  if (i < 0)
143  i = (-i)-1; // Convert negative int to non-negatives of same size.
144 
145  if (i>lowestNset32bits[15]) {
146  if (i>lowestNset32bits[23]) {
147  if (i>lowestNset32bits[27]) {
148  if (i>lowestNset32bits[29]) {
149  if (i>lowestNset32bits[30]) {
150  return 32;
151  } else {
152  return 31;
153  }
154  } else {
155  if (i>lowestNset32bits[28]) {
156  return 30;
157  } else {
158  return 29;
159  }
160  }
161  } else {
162  if (i>lowestNset32bits[25]) {
163  if (i>lowestNset32bits[26]) {
164  return 28;
165  } else {
166  return 27;
167  }
168  } else {
169  if (i>lowestNset32bits[24]) {
170  return 26;
171  } else {
172  return 25;
173  }
174  }
175  }
176  } else {
177  if (i>lowestNset32bits[19]) {
178  if (i>lowestNset32bits[21]) {
179  if (i>lowestNset32bits[22]) {
180  return 24;
181  } else {
182  return 23;
183  }
184  } else {
185  if (i>lowestNset32bits[20]) {
186  return 22;
187  } else {
188  return 21;
189  }
190  }
191  } else {
192  if (i>lowestNset32bits[17]) {
193  if (i>lowestNset32bits[18]) {
194  return 20;
195  } else {
196  return 19;
197  }
198  } else {
199  if (i>lowestNset32bits[16]) {
200  return 18;
201  } else {
202  return 17;
203  }
204  }
205  }
206  }
207  } else {
208  if (i>lowestNset32bits[7]) {
209  if (i>lowestNset32bits[11]) {
210  if (i>lowestNset32bits[13]) {
211  if (i>lowestNset32bits[14]) {
212  return 16;
213  } else {
214  return 15;
215  }
216  } else {
217  if (i>lowestNset32bits[12]) {
218  return 14;
219  } else {
220  return 13;
221  }
222  }
223  } else {
224  if (i>lowestNset32bits[9]) {
225  if (i>lowestNset32bits[10]) {
226  return 12;
227  } else {
228  return 11;
229  }
230  } else {
231  if (i>lowestNset32bits[8]) {
232  return 10;
233  } else {
234  return 9;
235  }
236  }
237  }
238  } else {
239  if (i>lowestNset32bits[3]) {
240  if (i>lowestNset32bits[5]) {
241  if (i>lowestNset32bits[6]) {
242  return 8;
243  } else {
244  return 7;
245  }
246  } else {
247  if (i>lowestNset32bits[4]) {
248  return 6;
249  } else {
250  return 5;
251  }
252  }
253  } else {
254  if (i>lowestNset32bits[1]) {
255  if (i>lowestNset32bits[2]) {
256  return 4;
257  } else {
258  return 3;
259  }
260  } else {
261  if (i>lowestNset32bits[0]) {
262  return 2;
263  } else {
264  return 1;
265  }
266  }
267  }
268  }
269  }
270 }
271 
272 
273 
278 static inline unsigned int bit_size(unsigned int u) {
279  for (int bs=1; bs<=32; bs++)
280  if (u == (u&lowestNset32bits[bs]))
281  return bs;
282  throw "Bit size (unsigned int) fails!";
283 }
284 
285 
286 #endif // #ifdef SLIM_BITSTREAM_H
size_t buf_used
Definition: bitstream.h:55
int bitptr
Pointer to the current bits.
Definition: bitstream.h:63
FILE * fp
The I/O stream.
Definition: bitstream.h:56
Word_t * Dptr
Pointer to the current word (as Word_t *).
Definition: bitstream.h:61
Input bit stream.
Definition: bitstream.h:97
static unsigned int bit_size(int32_t i)
Find size (on [0,32]) of the smallest # that can hold the integer i.
Definition: bitstream.h:141
Output bit stream.
Definition: bitstream.h:74
size_t bufsize
Size of I/O buffer (bytes)
Definition: bitstream.h:54
Byte_t * buffer_base
Pointer to the buffer.
Definition: bitstream.h:57
Byte_t * Bptr
Pointer to the current word (as Byte_t *).
Definition: bitstream.h:60
Byte_t * beyondbuffer
Pointer just beyond buffer (convenience).
Definition: bitstream.h:58
Bit stream base class.
Definition: bitstream.h:33