#define BITBUF2_H
#include "igzip_lib.h"
-
-/* bit buffer types
- * BITBUF8: (e) Always write 8 bytes of data
- * BITBUFB: (b) Always write data
- */
-#if !(defined(USE_BITBUFB) || defined(USE_BITBUF8) || defined(USE_BITBUF_ELSE))
-# define USE_BITBUFB
-#endif
-
-#if defined (__unix__) || (__APPLE__) || (__MINGW32__)
-#define _mm_stream_si64x(dst, src) *((uint64_t*)dst) = src
-#else
-#include <intrin.h>
-#endif
-
-#ifdef _WIN64
-#pragma warning(disable: 4996)
-#endif
+#include "unaligned.h"
#ifdef _MSC_VER
#define inline __inline
/* MAX_BITBUF_BIT WRITE is the maximum number of bits than can be safely written
* by consecutive calls of write_bits. Note this assumes the bitbuf is in a
* state that is possible at the exit of write_bits */
-#ifdef USE_BITBUF8 /*Write bits safe */
-# define MAX_BITBUF_BIT_WRITE 63
-#elif defined(USE_BITBUFB) /* Write bits always */
-# define MAX_BITBUF_BIT_WRITE 56
-#else /* USE_BITBUF_ELSE */
-# define MAX_BITBUF_BIT_WRITE 56
-#endif
-
-
-static
- inline void construct(struct BitBuf2 *me)
-{
- me->m_bits = 0;
- me->m_bit_count = 0;
- me->m_out_buf = me->m_out_start = me->m_out_end = NULL;
-}
+#define MAX_BITBUF_BIT_WRITE 56
static inline void init(struct BitBuf2 *me)
{
static inline void flush_bits(struct BitBuf2 *me)
{
uint32_t bits;
- _mm_stream_si64x((int64_t *) me->m_out_buf, me->m_bits);
+ store_u64(me->m_out_buf, me->m_bits);
bits = me->m_bit_count & ~7;
me->m_bit_count -= bits;
me->m_out_buf += bits/8;
}
+/* Can write up to 8 bytes to output buffer */
+static inline void flush(struct BitBuf2 *me)
+{
+ uint32_t bytes;
+ if (me->m_bit_count) {
+ store_u64(me->m_out_buf, me->m_bits);
+ bytes = (me->m_bit_count + 7) / 8;
+ me->m_out_buf += bytes;
+ }
+ me->m_bits = 0;
+ me->m_bit_count = 0;
+}
+
static inline void check_space(struct BitBuf2 *me, uint32_t num_bits)
{
/* Checks if bitbuf has num_bits extra space and flushes the bytes in
}
static inline void write_bits(struct BitBuf2 *me, uint64_t code, uint32_t count)
-{
-#ifdef USE_BITBUF8 /*Write bits safe */
+{ /* Assumes there is space to fit code into m_bits. */
me->m_bits |= code << me->m_bit_count;
me->m_bit_count += count;
- if (me->m_bit_count >= 64) {
- _mm_stream_si64x((int64_t *) me->m_out_buf, me->m_bits);
- me->m_out_buf += 8;
- me->m_bit_count -= 64;
- me->m_bits = code >> (count - me->m_bit_count);
- }
-#elif defined(USE_BITBUFB) /* Write bits always */
- /* Assumes there is space to fit code into m_bits. */
- me->m_bits |= code << me->m_bit_count;
- me->m_bit_count += count;
- if (me->m_bit_count >= 8)
- flush_bits(me);
-#else /* USE_BITBUF_ELSE */
- check_space(me, count);
- write_bits_unsafe(me, code, count);
-#endif
-
+ flush_bits(me);
}
-/* Can write up to 8 bytes to output buffer */
-static inline void flush(struct BitBuf2 *me)
-{
- uint32_t bytes;
- if (me->m_bit_count) {
- _mm_stream_si64x((int64_t *) me->m_out_buf, me->m_bits);
- bytes = (me->m_bit_count + 7) / 8;
- me->m_out_buf += bytes;
- }
- me->m_bits = 0;
- me->m_bit_count = 0;
+static inline void write_bits_flush(struct BitBuf2 *me, uint64_t code, uint32_t count)
+{ /* Assumes there is space to fit code into m_bits. */
+ me->m_bits |= code << me->m_bit_count;
+ me->m_bit_count += count;
+ flush(me);
}
#endif //BITBUF2_H