]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /********************************************************************** |
2 | Copyright(c) 2011-2016 Intel Corporation All rights reserved. | |
3 | ||
4 | Redistribution and use in source and binary forms, with or without | |
5 | modification, are permitted provided that the following conditions | |
6 | are met: | |
7 | * Redistributions of source code must retain the above copyright | |
8 | notice, this list of conditions and the following disclaimer. | |
9 | * Redistributions in binary form must reproduce the above copyright | |
10 | notice, this list of conditions and the following disclaimer in | |
11 | the documentation and/or other materials provided with the | |
12 | distribution. | |
13 | * Neither the name of Intel Corporation nor the names of its | |
14 | contributors may be used to endorse or promote products derived | |
15 | from this software without specific prior written permission. | |
16 | ||
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | **********************************************************************/ | |
29 | #ifndef BITBUF2_H | |
30 | #define BITBUF2_H | |
31 | ||
32 | #include "igzip_lib.h" | |
33 | ||
224ce89b WB |
34 | /* bit buffer types |
35 | * BITBUF8: (e) Always write 8 bytes of data | |
36 | * BITBUFB: (b) Always write data | |
37 | */ | |
38 | #if !(defined(USE_BITBUFB) || defined(USE_BITBUF8) || defined(USE_BITBUF_ELSE)) | |
39 | # define USE_BITBUFB | |
40 | #endif | |
41 | ||
42 | #if defined (__unix__) || (__APPLE__) || (__MINGW32__) | |
7c673cae FG |
43 | #define _mm_stream_si64x(dst, src) *((uint64_t*)dst) = src |
44 | #else | |
45 | #include <intrin.h> | |
46 | #endif | |
47 | ||
48 | #ifdef _WIN64 | |
49 | #pragma warning(disable: 4996) | |
50 | #endif | |
51 | ||
52 | #ifdef _MSC_VER | |
53 | #define inline __inline | |
54 | #endif | |
55 | ||
56 | ||
57 | /* MAX_BITBUF_BIT WRITE is the maximum number of bits than can be safely written | |
58 | * by consecutive calls of write_bits. Note this assumes the bitbuf is in a | |
59 | * state that is possible at the exit of write_bits */ | |
60 | #ifdef USE_BITBUF8 /*Write bits safe */ | |
61 | # define MAX_BITBUF_BIT_WRITE 63 | |
62 | #elif defined(USE_BITBUFB) /* Write bits always */ | |
63 | # define MAX_BITBUF_BIT_WRITE 56 | |
64 | #else /* USE_BITBUF_ELSE */ | |
65 | # define MAX_BITBUF_BIT_WRITE 56 | |
66 | #endif | |
67 | ||
68 | ||
69 | static | |
70 | inline void construct(struct BitBuf2 *me) | |
71 | { | |
72 | me->m_bits = 0; | |
73 | me->m_bit_count = 0; | |
74 | me->m_out_buf = me->m_out_start = me->m_out_end = NULL; | |
75 | } | |
76 | ||
77 | static inline void init(struct BitBuf2 *me) | |
78 | { | |
79 | me->m_bits = 0; | |
80 | me->m_bit_count = 0; | |
81 | } | |
82 | ||
83 | static inline void set_buf(struct BitBuf2 *me, unsigned char *buf, unsigned int len) | |
84 | { | |
85 | unsigned int slop = 8; | |
86 | me->m_out_buf = me->m_out_start = buf; | |
87 | me->m_out_end = buf + len - slop; | |
88 | } | |
89 | ||
90 | static inline int is_full(struct BitBuf2 *me) | |
91 | { | |
92 | return (me->m_out_buf > me->m_out_end); | |
93 | } | |
94 | ||
95 | static inline uint8_t * buffer_ptr(struct BitBuf2 *me) | |
96 | { | |
97 | return me->m_out_buf; | |
98 | } | |
99 | ||
100 | static inline uint32_t buffer_used(struct BitBuf2 *me) | |
101 | { | |
102 | return (uint32_t)(me->m_out_buf - me->m_out_start); | |
103 | } | |
104 | ||
224ce89b WB |
105 | static inline uint32_t buffer_bits_used(struct BitBuf2 *me) |
106 | { | |
107 | return (8 * (uint32_t)(me->m_out_buf - me->m_out_start) + me->m_bit_count); | |
108 | } | |
109 | ||
110 | static inline void flush_bits(struct BitBuf2 *me) | |
111 | { | |
112 | uint32_t bits; | |
113 | _mm_stream_si64x((int64_t *) me->m_out_buf, me->m_bits); | |
114 | bits = me->m_bit_count & ~7; | |
115 | me->m_bit_count -= bits; | |
116 | me->m_out_buf += bits/8; | |
117 | me->m_bits >>= bits; | |
118 | ||
119 | } | |
120 | ||
7c673cae FG |
121 | static inline void check_space(struct BitBuf2 *me, uint32_t num_bits) |
122 | { | |
123 | /* Checks if bitbuf has num_bits extra space and flushes the bytes in | |
124 | * the bitbuf if it doesn't. */ | |
224ce89b WB |
125 | if (63 - me->m_bit_count < num_bits) |
126 | flush_bits(me); | |
7c673cae FG |
127 | } |
128 | ||
129 | static inline void write_bits_unsafe(struct BitBuf2 *me, uint64_t code, uint32_t count) | |
130 | { | |
131 | me->m_bits |= code << me->m_bit_count; | |
132 | me->m_bit_count += count; | |
133 | } | |
134 | ||
135 | static inline void write_bits(struct BitBuf2 *me, uint64_t code, uint32_t count) | |
136 | { | |
137 | #ifdef USE_BITBUF8 /*Write bits safe */ | |
138 | me->m_bits |= code << me->m_bit_count; | |
139 | me->m_bit_count += count; | |
140 | if (me->m_bit_count >= 64) { | |
141 | _mm_stream_si64x((int64_t *) me->m_out_buf, me->m_bits); | |
142 | me->m_out_buf += 8; | |
143 | me->m_bit_count -= 64; | |
144 | me->m_bits = code >> (count - me->m_bit_count); | |
145 | } | |
146 | #elif defined(USE_BITBUFB) /* Write bits always */ | |
147 | /* Assumes there is space to fit code into m_bits. */ | |
7c673cae FG |
148 | me->m_bits |= code << me->m_bit_count; |
149 | me->m_bit_count += count; | |
224ce89b WB |
150 | if (me->m_bit_count >= 8) |
151 | flush_bits(me); | |
7c673cae FG |
152 | #else /* USE_BITBUF_ELSE */ |
153 | check_space(me, count); | |
154 | write_bits_unsafe(me, code, count); | |
155 | #endif | |
156 | ||
157 | } | |
158 | ||
159 | /* Can write up to 8 bytes to output buffer */ | |
160 | static inline void flush(struct BitBuf2 *me) | |
161 | { | |
162 | uint32_t bytes; | |
163 | if (me->m_bit_count) { | |
164 | _mm_stream_si64x((int64_t *) me->m_out_buf, me->m_bits); | |
165 | bytes = (me->m_bit_count + 7) / 8; | |
166 | me->m_out_buf += bytes; | |
167 | } | |
168 | me->m_bits = 0; | |
169 | me->m_bit_count = 0; | |
170 | } | |
171 | ||
172 | #endif //BITBUF2_H |