gallium/util: s/uint/enum tgsi_semantic/ in simple shader code
[mesa.git] / src / gallium / auxiliary / vl / vl_vlc.h
index 451fd5edb789b6f66a9ed3770cdb64d2e6b2716c..dd7b0918ae2f497f6fa921db6d39e3642b6cf658 100644 (file)
@@ -45,7 +45,6 @@ struct vl_vlc
    const uint8_t *data;
    const uint8_t *end;
 
-   unsigned          num_inputs;
    const void *const *inputs;
    const unsigned    *sizes;
    unsigned          bytes_left;
@@ -66,7 +65,7 @@ struct vl_vlc_compressed
 /**
  * initalize and decompress a lookup table
  */
-static INLINE void
+static inline void
 vl_vlc_init_table(struct vl_vlc_entry *dst, unsigned dst_size, const struct vl_vlc_compressed *src, unsigned src_size)
 {
    unsigned i, bits = util_logbase2(dst_size);
@@ -80,7 +79,7 @@ vl_vlc_init_table(struct vl_vlc_entry *dst, unsigned dst_size, const struct vl_v
    }
 
    for(; src_size > 0; --src_size, ++src) {
-      for(i=0; i<(1 << (bits - src->entry.length)); ++i)
+      for(i = 0; i < (1u << (bits - src->entry.length)); ++i)
          dst[src->bitcode >> (16 - bits) | i] = src->entry;
    }
 }
@@ -88,20 +87,24 @@ vl_vlc_init_table(struct vl_vlc_entry *dst, unsigned dst_size, const struct vl_v
 /**
  * switch over to next input buffer
  */
-static INLINE void
+static inline void
 vl_vlc_next_input(struct vl_vlc *vlc)
 {
    unsigned len = vlc->sizes[0];
 
    assert(vlc);
-   assert(vlc->num_inputs);
+   assert(vlc->bytes_left);
 
-   vlc->bytes_left -= len;
+   if (len < vlc->bytes_left)
+      vlc->bytes_left -= len;
+   else {
+      len = vlc->bytes_left;
+      vlc->bytes_left = 0;
+   }
 
    vlc->data = vlc->inputs[0];
    vlc->end = vlc->data + len;
 
-   --vlc->num_inputs;
    ++vlc->inputs;
    ++vlc->sizes;
 }
@@ -109,7 +112,7 @@ vl_vlc_next_input(struct vl_vlc *vlc)
 /**
  * align the data pointer to the next dword
  */
-static INLINE void
+static inline void
 vl_vlc_align_data_ptr(struct vl_vlc *vlc)
 {
    /* align the data pointer */
@@ -123,7 +126,7 @@ vl_vlc_align_data_ptr(struct vl_vlc *vlc)
 /**
  * fill the bit buffer, so that at least 32 bits are valid
  */
-static INLINE void
+static inline void
 vl_vlc_fillbits(struct vl_vlc *vlc)
 {
    assert(vlc);
@@ -135,10 +138,11 @@ vl_vlc_fillbits(struct vl_vlc *vlc)
       /* if this input is depleted */
       if (bytes_left == 0) {
 
-         if (vlc->num_inputs)
+         if (vlc->bytes_left) {
             /* go on to next input */
             vl_vlc_next_input(vlc);
-         else
+            vl_vlc_align_data_ptr(vlc);
+         } else
             /* or give up since we don't have anymore inputs */
             return;
 
@@ -171,7 +175,7 @@ vl_vlc_fillbits(struct vl_vlc *vlc)
 /**
  * initialize vlc structure and start reading from first input buffer
  */
-static INLINE void
+static inline void
 vl_vlc_init(struct vl_vlc *vlc, unsigned num_inputs,
             const void *const *inputs, const unsigned *sizes)
 {
@@ -182,7 +186,6 @@ vl_vlc_init(struct vl_vlc *vlc, unsigned num_inputs,
 
    vlc->buffer = 0;
    vlc->invalid_bits = 32;
-   vlc->num_inputs = num_inputs;
    vlc->inputs = inputs;
    vlc->sizes = sizes;
    vlc->bytes_left = 0;
@@ -190,16 +193,17 @@ vl_vlc_init(struct vl_vlc *vlc, unsigned num_inputs,
    for (i = 0; i < num_inputs; ++i)
       vlc->bytes_left += sizes[i];
 
-   vl_vlc_next_input(vlc);
-   vl_vlc_align_data_ptr(vlc);
-   vl_vlc_fillbits(vlc);
-   vl_vlc_fillbits(vlc);
+   if (vlc->bytes_left) {
+      vl_vlc_next_input(vlc);
+      vl_vlc_align_data_ptr(vlc);
+      vl_vlc_fillbits(vlc);
+   }
 }
 
 /**
  * number of bits still valid in bit buffer
  */
-static INLINE unsigned
+static inline unsigned
 vl_vlc_valid_bits(struct vl_vlc *vlc)
 {
    return 32 - vlc->invalid_bits;
@@ -208,7 +212,7 @@ vl_vlc_valid_bits(struct vl_vlc *vlc)
 /**
  * number of bits left over all inbut buffers
  */
-static INLINE unsigned
+static inline unsigned
 vl_vlc_bits_left(struct vl_vlc *vlc)
 {
    signed bytes_left = vlc->end - vlc->data;
@@ -219,7 +223,7 @@ vl_vlc_bits_left(struct vl_vlc *vlc)
 /**
  * get num_bits from bit buffer without removing them
  */
-static INLINE unsigned
+static inline unsigned
 vl_vlc_peekbits(struct vl_vlc *vlc, unsigned num_bits)
 {
    assert(vl_vlc_valid_bits(vlc) >= num_bits || vlc->data >= vlc->end);
@@ -229,7 +233,7 @@ vl_vlc_peekbits(struct vl_vlc *vlc, unsigned num_bits)
 /**
  * remove num_bits from bit buffer
  */
-static INLINE void
+static inline void
 vl_vlc_eatbits(struct vl_vlc *vlc, unsigned num_bits)
 {
    assert(vl_vlc_valid_bits(vlc) >= num_bits);
@@ -241,7 +245,7 @@ vl_vlc_eatbits(struct vl_vlc *vlc, unsigned num_bits)
 /**
  * get num_bits from bit buffer with removing them
  */
-static INLINE unsigned
+static inline unsigned
 vl_vlc_get_uimsbf(struct vl_vlc *vlc, unsigned num_bits)
 {
    unsigned value;
@@ -257,7 +261,7 @@ vl_vlc_get_uimsbf(struct vl_vlc *vlc, unsigned num_bits)
 /**
  * treat num_bits as signed value and remove them from bit buffer
  */
-static INLINE signed
+static inline signed
 vl_vlc_get_simsbf(struct vl_vlc *vlc, unsigned num_bits)
 {
    signed value;
@@ -273,7 +277,7 @@ vl_vlc_get_simsbf(struct vl_vlc *vlc, unsigned num_bits)
 /**
  * lookup a value and length in a decompressed table
  */
-static INLINE int8_t
+static inline int8_t
 vl_vlc_get_vlclbf(struct vl_vlc *vlc, const struct vl_vlc_entry *tbl, unsigned num_bits)
 {
    tbl += vl_vlc_peekbits(vlc, num_bits);
@@ -284,12 +288,12 @@ vl_vlc_get_vlclbf(struct vl_vlc *vlc, const struct vl_vlc_entry *tbl, unsigned n
 /**
  * fast forward search for a specific byte value
  */
-static INLINE boolean
+static inline boolean
 vl_vlc_search_byte(struct vl_vlc *vlc, unsigned num_bits, uint8_t value)
 {
    /* make sure we are on a byte boundary */
    assert((vl_vlc_valid_bits(vlc) % 8) == 0);
-   assert(num_bits == ~0 || (num_bits % 8) == 0);
+   assert(num_bits == ~0u || (num_bits % 8) == 0);
 
    /* deplete the bit buffer */
    while (vl_vlc_valid_bits(vlc) > 0) {
@@ -301,7 +305,7 @@ vl_vlc_search_byte(struct vl_vlc *vlc, unsigned num_bits, uint8_t value)
 
       vl_vlc_eatbits(vlc, 8);
 
-      if (num_bits != ~0) {
+      if (num_bits != ~0u) {
          num_bits -= 8;
          if (num_bits == 0)
             return FALSE;
@@ -313,7 +317,7 @@ vl_vlc_search_byte(struct vl_vlc *vlc, unsigned num_bits, uint8_t value)
 
       /* if this input is depleted */
       if (vlc->data == vlc->end) {
-         if (vlc->num_inputs)
+         if (vlc->bytes_left)
             /* go on to next input */
             vl_vlc_next_input(vlc);
          else
@@ -328,7 +332,7 @@ vl_vlc_search_byte(struct vl_vlc *vlc, unsigned num_bits, uint8_t value)
       }
 
       ++vlc->data;
-      if (num_bits != ~0) {
+      if (num_bits != ~0u) {
          num_bits -= 8;
          if (num_bits == 0) {
             vl_vlc_align_data_ptr(vlc);
@@ -338,4 +342,41 @@ vl_vlc_search_byte(struct vl_vlc *vlc, unsigned num_bits, uint8_t value)
    }
 }
 
+/**
+ * remove num_bits bits starting at pos from the bitbuffer
+ */
+static inline void
+vl_vlc_removebits(struct vl_vlc *vlc, unsigned pos, unsigned num_bits)
+{
+   uint64_t lo = (vlc->buffer & (~0UL >> (pos + num_bits))) << num_bits;
+   uint64_t hi = (vlc->buffer & (~0UL << (64 - pos)));
+   vlc->buffer = lo | hi;
+   vlc->invalid_bits += num_bits;
+}
+
+/**
+ * limit the number of bits left for fetching
+ */
+static inline void
+vl_vlc_limit(struct vl_vlc *vlc, unsigned bits_left)
+{
+   assert(bits_left <= vl_vlc_bits_left(vlc));
+
+   vl_vlc_fillbits(vlc);
+   if (bits_left < vl_vlc_valid_bits(vlc)) {
+      vlc->invalid_bits = 32 - bits_left;
+      vlc->buffer &= ~0L << (vlc->invalid_bits + 32);
+      vlc->end = vlc->data;
+      vlc->bytes_left = 0;
+   } else {
+      assert((bits_left - vl_vlc_valid_bits(vlc)) % 8 == 0);
+      vlc->bytes_left = (bits_left - vl_vlc_valid_bits(vlc)) / 8;
+      if (vlc->bytes_left < (vlc->end - vlc->data)) {
+         vlc->end = vlc->data + vlc->bytes_left;
+         vlc->bytes_left = 0;
+      } else
+         vlc->bytes_left -= vlc->end - vlc->data;
+   }
+}
+
 #endif /* vl_vlc_h */