]>
git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/igzip/bitbuf2.h
84d521df91abe8bc8265bd79eaac779e9320fc86
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
32 #include "igzip_lib.h"
34 #if defined (__unix__) || (__APPLE__)
35 #define _mm_stream_si64x(dst, src) *((uint64_t*)dst) = src
41 #pragma warning(disable: 4996)
45 #define inline __inline
49 /* MAX_BITBUF_BIT WRITE is the maximum number of bits than can be safely written
50 * by consecutive calls of write_bits. Note this assumes the bitbuf is in a
51 * state that is possible at the exit of write_bits */
52 #ifdef USE_BITBUF8 /*Write bits safe */
53 # define MAX_BITBUF_BIT_WRITE 63
54 #elif defined(USE_BITBUFB) /* Write bits always */
55 # define MAX_BITBUF_BIT_WRITE 56
56 #else /* USE_BITBUF_ELSE */
57 # define MAX_BITBUF_BIT_WRITE 56
62 inline void construct(struct BitBuf2
*me
)
66 me
->m_out_buf
= me
->m_out_start
= me
->m_out_end
= NULL
;
69 static inline void init(struct BitBuf2
*me
)
75 static inline void set_buf(struct BitBuf2
*me
, unsigned char *buf
, unsigned int len
)
77 unsigned int slop
= 8;
78 me
->m_out_buf
= me
->m_out_start
= buf
;
79 me
->m_out_end
= buf
+ len
- slop
;
82 static inline int is_full(struct BitBuf2
*me
)
84 return (me
->m_out_buf
> me
->m_out_end
);
87 static inline uint8_t * buffer_ptr(struct BitBuf2
*me
)
92 static inline uint32_t buffer_used(struct BitBuf2
*me
)
94 return (uint32_t)(me
->m_out_buf
- me
->m_out_start
);
97 static inline void check_space(struct BitBuf2
*me
, uint32_t num_bits
)
99 /* Checks if bitbuf has num_bits extra space and flushes the bytes in
100 * the bitbuf if it doesn't. */
102 if (63 - me
->m_bit_count
< num_bits
) {
103 _mm_stream_si64x((int64_t *) me
->m_out_buf
, me
->m_bits
);
104 bytes
= me
->m_bit_count
/ 8;
105 me
->m_out_buf
+= bytes
;
107 me
->m_bit_count
-= bytes
;
108 me
->m_bits
>>= bytes
;
112 static inline void write_bits_unsafe(struct BitBuf2
*me
, uint64_t code
, uint32_t count
)
114 me
->m_bits
|= code
<< me
->m_bit_count
;
115 me
->m_bit_count
+= count
;
118 static inline void write_bits(struct BitBuf2
*me
, uint64_t code
, uint32_t count
)
120 #ifdef USE_BITBUF8 /*Write bits safe */
121 me
->m_bits
|= code
<< me
->m_bit_count
;
122 me
->m_bit_count
+= count
;
123 if (me
->m_bit_count
>= 64) {
124 _mm_stream_si64x((int64_t *) me
->m_out_buf
, me
->m_bits
);
126 me
->m_bit_count
-= 64;
127 me
->m_bits
= code
>> (count
- me
->m_bit_count
);
129 #elif defined(USE_BITBUFB) /* Write bits always */
130 /* Assumes there is space to fit code into m_bits. */
132 me
->m_bits
|= code
<< me
->m_bit_count
;
133 me
->m_bit_count
+= count
;
134 if (me
->m_bit_count
>= 8) {
135 _mm_stream_si64x((int64_t *) me
->m_out_buf
, me
->m_bits
);
136 bits
= me
->m_bit_count
& ~7;
137 me
->m_bit_count
-= bits
;
138 me
->m_out_buf
+= bits
/8;
141 #else /* USE_BITBUF_ELSE */
142 check_space(me
, count
);
143 write_bits_unsafe(me
, code
, count
);
148 /* Can write up to 8 bytes to output buffer */
149 static inline void flush(struct BitBuf2
*me
)
152 if (me
->m_bit_count
) {
153 _mm_stream_si64x((int64_t *) me
->m_out_buf
, me
->m_bits
);
154 bytes
= (me
->m_bit_count
+ 7) / 8;
155 me
->m_out_buf
+= bytes
;