| 1 | extern "C" { |
|---|
| 2 | #include <assert.h> |
|---|
| 3 | #include <limits.h> |
|---|
| 4 | #include <stdio.h> |
|---|
| 5 | #include <stdlib.h> |
|---|
| 6 | #include <string.h> |
|---|
| 7 | } |
|---|
| 8 | #include <algorithm> |
|---|
| 9 | #include "range_coder.hpp" |
|---|
| 10 | |
|---|
| 11 | #include "table.c" |
|---|
| 12 | |
|---|
| 13 | class writer_t { |
|---|
| 14 | char **p, *max; |
|---|
| 15 | public: |
|---|
| 16 | struct overrun_t { |
|---|
| 17 | }; |
|---|
| 18 | writer_t(char **_p, char *_max) : p(_p), max(_max) {} |
|---|
| 19 | writer_t &operator=(char c) { |
|---|
| 20 | if (*p == max) { |
|---|
| 21 | throw overrun_t(); |
|---|
| 22 | } |
|---|
| 23 | *(*p)++ = c; |
|---|
| 24 | return *this; |
|---|
| 25 | } |
|---|
| 26 | writer_t &operator*() { return *this; } |
|---|
| 27 | writer_t &operator++() { return *this; } |
|---|
| 28 | writer_t &operator++(int) { return *this; } |
|---|
| 29 | }; |
|---|
| 30 | |
|---|
| 31 | #define FREQ_BASE SHRT_MIN |
|---|
| 32 | #define LOOP_CNT 1024 |
|---|
| 33 | |
|---|
| 34 | int main(int argc, char **argv) |
|---|
| 35 | { |
|---|
| 36 | char buf[1024 * 1024], cbuf[1024 * 1024], rbuf[1024 * 1024]; |
|---|
| 37 | size_t buflen, cbuflen; |
|---|
| 38 | unsigned long long start; |
|---|
| 39 | int i; |
|---|
| 40 | |
|---|
| 41 | /* read */ |
|---|
| 42 | buflen = fread(buf, 1, sizeof(buf) - 1, stdin); |
|---|
| 43 | /* compress */ |
|---|
| 44 | start = rdtsc(); |
|---|
| 45 | for (i = 0; i < LOOP_CNT; i++) { |
|---|
| 46 | char *cbufpt = cbuf; |
|---|
| 47 | rc_encoder_t<writer_t> enc(writer_t(&cbufpt, cbuf + sizeof(cbuf))); |
|---|
| 48 | for (const char *p = buf, *e = buf + buflen; p != e; p++) { |
|---|
| 49 | unsigned ch = (unsigned char)*p; |
|---|
| 50 | #ifdef USE_ORDERED_TABLE |
|---|
| 51 | ch = to_ordered[ch]; |
|---|
| 52 | #endif |
|---|
| 53 | assert(freq[ch] != freq[ch + 1]); |
|---|
| 54 | enc.encode(freq[ch] - FREQ_BASE, freq[ch + 1] - FREQ_BASE, MAX_FREQ); |
|---|
| 55 | } |
|---|
| 56 | enc.final(); |
|---|
| 57 | cbuflen = cbufpt - cbuf; |
|---|
| 58 | } |
|---|
| 59 | printf("compression: %lu Mticks\n", (long)((rdtsc() - start) / 1024 / 1024)); |
|---|
| 60 | /* decompress */ |
|---|
| 61 | start = rdtsc(); |
|---|
| 62 | for (i = 0; i < LOOP_CNT; i++) { |
|---|
| 63 | rc_decoder_t<const char*, rc_decoder_search_t<short, 256, FREQ_BASE> > |
|---|
| 64 | dec(cbuf, cbuf + cbuflen); |
|---|
| 65 | for (char *p = rbuf, *e = rbuf + buflen; p != e; p++) { |
|---|
| 66 | unsigned ch = dec.decode(MAX_FREQ, freq); |
|---|
| 67 | #ifdef USE_ORDERED_TABLE |
|---|
| 68 | ch = from_ordered[ch]; |
|---|
| 69 | #endif |
|---|
| 70 | *p = ch; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | printf("decompression: %lu Mticks\n", |
|---|
| 74 | (long)((rdtsc() - start) / 1024 / 1024)); |
|---|
| 75 | /* check result */ |
|---|
| 76 | if (memcmp(buf, rbuf, buflen) != 0) { |
|---|
| 77 | fprintf(stderr, "original data and decompressed data does not match.\n"); |
|---|
| 78 | exit(99); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return 0; |
|---|
| 82 | } |
|---|