[4/4] put_bits: cache 64 bits when HAVE_FAST_64BIT

Message ID 20170226175813.20428-5-stebbins@jetheaddev.com
State New
Headers show

Commit Message

John Stebbins Feb. 26, 2017, 5:58 p.m.
---
 libavcodec/put_bits.h | 58 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 37 insertions(+), 21 deletions(-)

Patch

diff --git a/libavcodec/put_bits.h b/libavcodec/put_bits.h
index 30b1dd2..19d352c 100644
--- a/libavcodec/put_bits.h
+++ b/libavcodec/put_bits.h
@@ -32,8 +32,24 @@ 
 
 #include "libavutil/intreadwrite.h"
 
+#if defined(HAVE_FAST_64BIT)
+#define BIT_BUF_SZ      64
+#define BIT_BUF_SHIFT   6
+#define BIT_BUF_BYTE_SZ 8
+#define BIT_BUF_TYPE    uint64_t
+#define BIT_WL          AV_WL64
+#define BIT_WB          AV_WB64
+#else
+#define BIT_BUF_SZ      32
+#define BIT_BUF_SHIFT   5
+#define BIT_BUF_BYTE_SZ 4
+#define BIT_BUF_TYPE    uint32_t
+#define BIT_WL          AV_WL32
+#define BIT_WB          AV_WB32
+#endif
+
 typedef struct PutBitContext {
-    uint32_t bit_buf;
+    BIT_BUF_TYPE bit_buf;
     int bit_left;
     uint8_t *buf, *buf_ptr, *buf_end;
     int size_in_bits;
@@ -57,7 +73,7 @@  static inline void init_put_bits(PutBitContext *s, uint8_t *buffer,
     s->buf          = buffer;
     s->buf_end      = s->buf + buffer_size;
     s->buf_ptr      = s->buf;
-    s->bit_left     = 32;
+    s->bit_left     = BIT_BUF_SZ;
     s->bit_buf      = 0;
 }
 
@@ -66,7 +82,7 @@  static inline void init_put_bits(PutBitContext *s, uint8_t *buffer,
  */
 static inline int put_bits_count(PutBitContext *s)
 {
-    return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
+    return (s->buf_ptr - s->buf) * 8 + BIT_BUF_SZ - s->bit_left;
 }
 
 /**
@@ -74,7 +90,7 @@  static inline int put_bits_count(PutBitContext *s)
  */
 static inline int put_bits_left(PutBitContext* s)
 {
-    return (s->buf_end - s->buf_ptr) * 8 - 32 + s->bit_left;
+    return (s->buf_end - s->buf_ptr) * 8 - BIT_BUF_SZ + s->bit_left;
 }
 
 /**
@@ -83,10 +99,10 @@  static inline int put_bits_left(PutBitContext* s)
 static inline void flush_put_bits(PutBitContext *s)
 {
 #ifndef BITSTREAM_WRITER_LE
-    if (s->bit_left < 32)
+    if (s->bit_left < BIT_BUF_SZ)
         s->bit_buf <<= s->bit_left;
 #endif
-    while (s->bit_left < 32) {
+    while (s->bit_left < BIT_BUF_SZ) {
         /* XXX: should test end of buffer */
 #ifdef BITSTREAM_WRITER_LE
         if (s->buf_ptr < s->buf_end)
@@ -95,13 +111,13 @@  static inline void flush_put_bits(PutBitContext *s)
         s->bit_buf  >>= 8;
 #else
         if (s->buf_ptr < s->buf_end)
-            *s->buf_ptr = s->bit_buf >> 24;
+            *s->buf_ptr = s->bit_buf >> (BIT_BUF_SZ - 8);
         s->buf_ptr++;
         s->bit_buf  <<= 8;
 #endif
         s->bit_left  += 8;
     }
-    s->bit_left = 32;
+    s->bit_left = BIT_BUF_SZ;
     s->bit_buf  = 0;
 }
 
@@ -137,23 +153,23 @@  void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
  */
 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
 {
-    unsigned int bit_buf;
+    BIT_BUF_TYPE bit_buf;
     int bit_left;
 
-    assert(n <= 31 && value < (1U << n));
+    assert(n <= BIT_BUF_SZ - 1 && value < (1U << n));
 
     bit_buf  = s->bit_buf;
     bit_left = s->bit_left;
 
     /* XXX: optimize */
 #ifdef BITSTREAM_WRITER_LE
-    bit_buf |= value << (32 - bit_left);
+    bit_buf |= (BIT_BUF_TYPE)value << (BIT_BUF_SZ - bit_left);
     if (n >= bit_left) {
         if (s->buf_ptr < s->buf_end)
-            AV_WL32(s->buf_ptr, bit_buf);
-        s->buf_ptr += 4;
-        bit_buf     = (bit_left == 32) ? 0 : value >> bit_left;
-        bit_left   += 32;
+            BIT_WL(s->buf_ptr, bit_buf);
+        s->buf_ptr += BIT_BUF_BYTE_SZ;
+        bit_buf     = (bit_left == BIT_BUF_SZ) ? 0 : value >> bit_left;
+        bit_left   += BIT_BUF_SZ;
     }
     bit_left -= n;
 #else
@@ -164,9 +180,9 @@  static inline void put_bits(PutBitContext *s, int n, unsigned int value)
         bit_buf   <<= bit_left;
         bit_buf    |= value >> (n - bit_left);
         if (s->buf_ptr < s->buf_end)
-            AV_WB32(s->buf_ptr, bit_buf);
-        s->buf_ptr += 4;
-        bit_left   += 32 - n;
+            BIT_WB(s->buf_ptr, bit_buf);
+        s->buf_ptr += BIT_BUF_BYTE_SZ;
+        bit_left   += BIT_BUF_SZ - n;
         bit_buf     = value;
     }
 #endif
@@ -214,7 +230,7 @@  static inline uint8_t *put_bits_ptr(PutBitContext *s)
 static inline void skip_put_bytes(PutBitContext *s, int n)
 {
     assert((put_bits_count(s) & 7) == 0);
-    assert(s->bit_left == 32);
+    assert(s->bit_left == BIT_BUF_SZ);
     s->buf_ptr += n;
 }
 
@@ -226,8 +242,8 @@  static inline void skip_put_bytes(PutBitContext *s, int n)
 static inline void skip_put_bits(PutBitContext *s, int n)
 {
     s->bit_left -= n;
-    s->buf_ptr  -= 4 * (s->bit_left >> 5);
-    s->bit_left &= 31;
+    s->buf_ptr  -= BIT_BUF_BYTE_SZ * (s->bit_left >> BIT_BUF_SHIFT);
+    s->bit_left &= BIT_BUF_SZ - 1;
 }
 
 /**