intvcomp.hh 9.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Copyright (C) 2011 Laboratoire de Recherche et Developpement de
// l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

#ifndef SPOT_MISC_INTVCOMP_HH
# define SPOT_MISC_INTVCOMP_HH

#include <cstddef>
#include <vector>
#include <cassert>

namespace spot
{

  //  Compression scheme
  //  ------------------
  //
  // Assumptions:
  //  - small and positive values are more frequent than negative
  //    and large values.
  //  - 0 is the most frequent value
  //  - repeated values (esp. repeated 0s occur often).
  //
  //  00  encodes "value 0"
  //  010 encodes "value 1"
  //  011 encodes "a value in [2..5]" followed by 2 bits
  //  100 encodes "a value in [6..22]" followed by 4 bits
  //  101 encodes "repeat prev. value [1..8] times" followed by 3 bits count
  //  110 encodes "repeat prev. value [9..40] times" followed by 5 bits count
  //  111 encodes "an int value" followed by 32 bits
  //
  // If 101 or 110 occur at the start, the value to repeat is 0.

  template <class Self>
  class stream_compression_base
  {
    static const unsigned int max_bits = sizeof(unsigned int) * 8;

  public:
    stream_compression_base()
      : cur_(0), bits_left_(max_bits)
    {
    }

    void emit(unsigned int val)
    {
      if (val == 0)
	{
	  self().push_bits(0x0, 2, 0x3);
	}
      else if (val == 1)
	{
	  self().push_bits(0x2, 3, 0x7);
	}
      else if (val >= 2 && val <= 5)
	{
	  self().push_bits(0x3, 3, 0x7);
	  self().push_bits(val - 2, 2, 0x3);
	}
      else if (val >= 6 && val <= 22)
	{
	  self().push_bits(0x4, 3, 0x7);
	  self().push_bits(val - 6, 4, 0xf);
	}
      else
	{
	  assert(val > 22);
	  self().push_bits(0x7, 3, 0x7);
	  self().push_bits(val, 32, -1U);
	}
    }

    void run()
    {
      unsigned int last_val = 0;

      while (self().have_data())
	{
	  unsigned int val = self().next_data();
	  // Repeated value?  Try to find more.
	  if (val == last_val)
	    {
	      unsigned int count = 1;
	      while (count <= 41 && self().skip_if(val))
		++count;

	      if ((val == 0 && count < 3) || (val == 1 && count == 1))
		{
		  // it is more efficient to emit 0 once or twice directly
		  // (e.g., 00 00 vs. 011 11)
		  // for value 1, repetition is worthwhile for count > 1
		  // (e.g., 010 010 vs. 011 00)
		  while (count--)
		    emit(val);
		}
	      else if (count < 9)
		{
		  self().push_bits(0x5, 3, 0x7);
		  self().push_bits(count - 1, 3, 0x7);
		}
	      else
		{
		  self().push_bits(0x6, 3, 0x7);
		  self().push_bits(count - 9, 5, 0x1f);
		}
	    }
	  else
	    {
	      emit(val);
	      last_val = val;
	    }
	}
      flush();
    }

    // This version assumes there is at least n bits free in cur_.
    void
    push_bits_unchecked(unsigned int bits, unsigned int n, unsigned int mask)
    {
      cur_ <<= n;
      cur_ |= (bits & mask);
      if (bits_left_ -= n)
	return;

      self().push_data(cur_);
      cur_ = 0;
      bits_left_ = max_bits;
    }

    void
    push_bits(unsigned int bits, unsigned int n, unsigned int mask)
    {
      if (n <= bits_left_)
	{
	  push_bits_unchecked(bits, n, mask);
	  return;
	}

      // bits_left_ < n

      unsigned int right_bit_count = n - bits_left_;
      unsigned int left = bits >> right_bit_count;
      push_bits_unchecked(left, bits_left_, (1 << bits_left_) - 1);
      push_bits_unchecked(bits, right_bit_count, (1 << right_bit_count) - 1);
    }

    void flush()
    {
      if (bits_left_ == max_bits)
	return;
      cur_ <<= bits_left_;
      self().push_data(cur_);
    }

  protected:
    Self& self()
    {
      return static_cast<Self&>(*this);
    }

    const Self& self() const
    {
      return static_cast<const Self&>(*this);
    }

    unsigned int cur_;
    unsigned int bits_left_;
  };

  class int_array_compression:
    public stream_compression_base<int_array_compression>
  {
  public:
    int_array_compression(int* array, size_t n)
      : array_(array), n_(n), pos_(0), result_(new std::vector<unsigned int>)
    {
    }

    void push_data(unsigned int i)
    {
      result_->push_back(i);
    }

    const std::vector<unsigned int>*
    result() const
    {
      return result_;
    }

    bool have_data() const
    {
      return pos_ < n_;
    }

    unsigned int next_data()
    {
      return static_cast<unsigned int>(array_[pos_++]);
    }

    bool skip_if(unsigned int val)
    {
      if (!have_data())
	return false;

      if (static_cast<unsigned int>(array_[pos_]) != val)
	return false;

      ++pos_;
      return true;
    }

  protected:
    int* array_;
    size_t n_;
    size_t pos_;
    std::vector<unsigned int>* result_;
  };

  const std::vector<unsigned int>*
  int_array_compress(int* array, unsigned int n)
  {
    int_array_compression c(array, n);
    c.run();
    return c.result();
  }

  //////////////////////////////////////////////////////////////////////

  template<class Self>
  class stream_decompression_base
  {
    static const unsigned int max_bits = sizeof(unsigned int) * 8;

  public:
    void refill()
    {
    oncemore:
      unsigned int fill_size = max_bits - look_bits_;
      if (fill_size > buffer_bits_)
	fill_size = buffer_bits_;

      look_ <<= fill_size;
      look_ |= (buffer_ >> (buffer_bits_ - fill_size));
      buffer_bits_ -= fill_size;
      buffer_mask_ >>= fill_size;
      buffer_ &= buffer_mask_;
      look_bits_ += fill_size;

      if (buffer_bits_ == 0 && self().have_comp_data())
	{
	  buffer_ = self().next_comp_data();
	  buffer_bits_ = max_bits;
	  buffer_mask_ = -1U;
	  if (look_bits_ != max_bits)
	    goto oncemore;
	}

      // Do not fold these two cases, because we cannot write
      // "x<<max_bits" safely.  (Intel "shl" instruction considers
      // only the first 5 bits, in other words "shl 32" is similar to
      // "shl 0": it has no effect.)
      if (look_bits_ == max_bits)
	{
	  look_mask_ = -1U;
	}
      else
	{
	  look_mask_ = (1U << look_bits_) - 1U;
	}
    }

    // 010 00 00 010 00 101:011 010 00 101:010 010
    // 010 00010000001000010010000100000
    unsigned int look_n_bits(unsigned int n)
    {
      if (look_bits_ < n)
	refill();
      return look_ >> (look_bits_ - n);
    }

    void skip_n_bits(unsigned int n)
    {
      assert (n <= look_bits_);
      look_bits_ -= n;
      look_mask_ >>= n;
      look_ &= look_mask_;
    }

    unsigned int get_n_bits(unsigned int n)
    {
      if (look_bits_ < n)
	refill();
      look_bits_ -= n;
      unsigned int val = look_ >> look_bits_;
      look_mask_ >>= n;
      look_ &= look_mask_;
      return val;
    }

    void run()
    {
      if (!self().have_comp_data())
	return;

      look_ = self().next_comp_data();
      look_bits_ = max_bits;
      look_mask_ = -1U;
      if (self().have_comp_data())
	{
	  buffer_ = self().next_comp_data();
	  buffer_bits_ = max_bits;
	  buffer_mask_ = -1U;
	}
      else
	{
	  buffer_ = 0;
	  buffer_bits_ = 0;
	  buffer_mask_ = 0;
	}

      while (!self().complete())
	{
	  unsigned int token = look_n_bits(3);
	  switch (token)
	    {
	    case 0x0: // 00[0]
	    case 0x1: // 00[1]
	      skip_n_bits(2);
	      self().push_data(0);
	      break;
	    case 0x2: // 010
	      skip_n_bits(3);
	      self().push_data(1);
	      break;
	    case 0x3: // 011
	      skip_n_bits(3);
	      self().push_data(2 + get_n_bits(2));
	      break;
	    case 0x4: // 100
	      skip_n_bits(3);
	      self().push_data(6 + get_n_bits(4));
	      break;
	    case 0x5: // 101
	      skip_n_bits(3);
	      self().repeat(1 + get_n_bits(3));
	      break;
	    case 0x6: // 110
	      skip_n_bits(3);
	      self().repeat(9 + get_n_bits(5));
	      break;
	    case 0x7: // 111
	      skip_n_bits(3);
	      self().push_data(get_n_bits(32));
	      break;
	    default:
	      assert(0);
	    }
	}
    }


  protected:
    Self& self()
    {
      return static_cast<Self&>(*this);
    }

    const Self& self() const
    {
      return static_cast<const Self&>(*this);
    }

    unsigned int look_;
    unsigned int look_bits_;
    unsigned int look_mask_;
    unsigned int buffer_;
    unsigned int buffer_bits_;
    unsigned int buffer_mask_;
  };

  class int_array_decompression:
    public stream_decompression_base<int_array_decompression>
  {
  public:
    int_array_decompression(const std::vector<unsigned int>* array, int* res,
			    size_t size)
      : prev_(0), array_(array), n_(array->size()), pos_(0), result_(res),
	size_(size)
    {
    }

    bool complete() const
    {
      return size_ == 0;
    }

    void push_data(int i)
    {
      prev_ = i;
      *result_++ = i;
      --size_;
    }

    void repeat(unsigned int i)
    {
      size_ -= i;
      while (i--)
	*result_++ = prev_;
    }

    bool have_comp_data() const
    {
      return pos_ < n_;
    }

    unsigned int next_comp_data()
    {
      return (*array_)[pos_++];
    }

  protected:
    int prev_;
    const std::vector<unsigned int>* array_;
    size_t n_;
    size_t pos_;
    int* result_;
    size_t size_;
  };

  void
  int_array_decompress(const std::vector<unsigned int>* array, int* res,
		       size_t size)
  {
    int_array_decompression c(array, res, size);
    c.run();
  }


}

#endif // SPOT_MISC_INTVCOMP_HH