minor comments
[flac.git] / src / libFLAC / bitbuffer.c
index 46bcb32..f6b1b74 100644 (file)
@@ -1,5 +1,5 @@
 /* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include <stdlib.h> /* for malloc() */
 #include <string.h> /* for memcpy(), memset() */
 #include "private/bitbuffer.h"
  */
 
 /*
+ * Some optimization strategies are slower with older versions of MSVC
+ */
+#if defined _MSC_VER && _MSC_VER <= 1200
+#define FLAC__OLD_MSVC_FLAVOR
+#endif
+
+/*
  * This should be at least twice as large as the largest number of blurbs
  * required to represent any 'number' (in any encoding) you are going to
  * read.  With FLAC this is on the order of maybe a few hundred bits.
  */
 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FLAC__BITS_PER_BLURB; /* blurbs */
 
+#ifndef FLAC__OLD_MSVC_FLAVOR
+static const unsigned char byte_to_unary_table[] = {
+       8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
+       3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+#endif
+
 #if FLAC__BITS_PER_BLURB == 8
 #define FLAC__BITS_PER_BLURB_LOG2 3
 #define FLAC__BYTES_PER_BLURB 1
@@ -70,6 +102,9 @@ static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FL
 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80)
 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b))
 #define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc));
+#ifndef FLAC__OLD_MSVC_FLAVOR
+#define FLAC__ALIGNED_BLURB_UNARY(blurb) (byte_to_unary_table[blurb])
+#endif
 #elif FLAC__BITS_PER_BLURB == 32
 #define FLAC__BITS_PER_BLURB_LOG2 5
 #define FLAC__BYTES_PER_BLURB 4
@@ -77,6 +112,9 @@ static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FL
 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b))
 #define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb));
+#ifndef FLAC__OLD_MSVC_FLAVOR
+#define FLAC__ALIGNED_BLURB_UNARY(blurb) ((blurb) <= 0xff ? byte_to_unary_table[blurb] + 24 : ((blurb) <= 0xffff ? byte_to_unary_table[(blurb) >> 8] + 16 : ((blurb) <= 0xffffff ? byte_to_unary_table[(blurb) >> 16] + 8 : byte_to_unary_table[(blurb) >> 24])))
+#endif
 #else
 /* ERROR, only sizes of 8 and 32 are supported */
 #endif
@@ -92,6 +130,13 @@ static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FL
 #endif
 #define max(x,y) ((x)>(y)?(x):(y))
 
+/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
+#ifdef _MSC_VER
+#define FLAC__U64L(x) x
+#else
+#define FLAC__U64L(x) x##LLU
+#endif
+
 #ifndef FLaC__INLINE
 #define FLaC__INLINE
 #endif
@@ -200,11 +245,27 @@ static FLAC__bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, FLAC__bool (*
 
        /* first shift the unconsumed buffer data toward the front as much as possible */
        if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
-               unsigned l = 0, r = bb->consumed_blurbs, r_end = bb->blurbs + (bb->bits? 1:0);
+#if FLAC__BITS_PER_BLURB == 8
+               /*
+                * memset and memcpy are usually implemented in assembly language
+                * by the system libc, and they can be much faster
+                */
+               const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
+               const unsigned r = bb->consumed_blurbs, l = r_end - r;
+               memmove(&bb->buffer[0], &bb->buffer[r], l);
+               memset(&bb->buffer[l], 0, r);
+#elif FLAC__BITS_PER_BLURB == 32
+               /* still needs optimization */
+               const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
+               unsigned l = 0, r = bb->consumed_blurbs;
                for( ; r < r_end; l++, r++)
                        bb->buffer[l] = bb->buffer[r];
                for( ; l < r_end; l++)
                        bb->buffer[l] = 0;
+#else
+               FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
+#endif /* FLAC__BITS_PER_BLURB == 32 or 8 */
+
                bb->blurbs -= bb->consumed_blurbs;
                bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
                bb->consumed_blurbs = 0;
@@ -647,22 +708,22 @@ FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 va
 {
        static const FLAC__uint64 mask[] = {
                0,
-               0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
-               0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
-               0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
-               0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
-               0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
-               0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
-               0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
-               0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
-               0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
-               0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
-               0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
-               0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
-               0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
-               0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
-               0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
-               0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
+               FLAC__U64L(0x0000000000000001), FLAC__U64L(0x0000000000000003), FLAC__U64L(0x0000000000000007), FLAC__U64L(0x000000000000000F),
+               FLAC__U64L(0x000000000000001F), FLAC__U64L(0x000000000000003F), FLAC__U64L(0x000000000000007F), FLAC__U64L(0x00000000000000FF),
+               FLAC__U64L(0x00000000000001FF), FLAC__U64L(0x00000000000003FF), FLAC__U64L(0x00000000000007FF), FLAC__U64L(0x0000000000000FFF),
+               FLAC__U64L(0x0000000000001FFF), FLAC__U64L(0x0000000000003FFF), FLAC__U64L(0x0000000000007FFF), FLAC__U64L(0x000000000000FFFF),
+               FLAC__U64L(0x000000000001FFFF), FLAC__U64L(0x000000000003FFFF), FLAC__U64L(0x000000000007FFFF), FLAC__U64L(0x00000000000FFFFF),
+               FLAC__U64L(0x00000000001FFFFF), FLAC__U64L(0x00000000003FFFFF), FLAC__U64L(0x00000000007FFFFF), FLAC__U64L(0x0000000000FFFFFF),
+               FLAC__U64L(0x0000000001FFFFFF), FLAC__U64L(0x0000000003FFFFFF), FLAC__U64L(0x0000000007FFFFFF), FLAC__U64L(0x000000000FFFFFFF),
+               FLAC__U64L(0x000000001FFFFFFF), FLAC__U64L(0x000000003FFFFFFF), FLAC__U64L(0x000000007FFFFFFF), FLAC__U64L(0x00000000FFFFFFFF),
+               FLAC__U64L(0x00000001FFFFFFFF), FLAC__U64L(0x00000003FFFFFFFF), FLAC__U64L(0x00000007FFFFFFFF), FLAC__U64L(0x0000000FFFFFFFFF),
+               FLAC__U64L(0x0000001FFFFFFFFF), FLAC__U64L(0x0000003FFFFFFFFF), FLAC__U64L(0x0000007FFFFFFFFF), FLAC__U64L(0x000000FFFFFFFFFF),
+               FLAC__U64L(0x000001FFFFFFFFFF), FLAC__U64L(0x000003FFFFFFFFFF), FLAC__U64L(0x000007FFFFFFFFFF), FLAC__U64L(0x00000FFFFFFFFFFF),
+               FLAC__U64L(0x00001FFFFFFFFFFF), FLAC__U64L(0x00003FFFFFFFFFFF), FLAC__U64L(0x00007FFFFFFFFFFF), FLAC__U64L(0x0000FFFFFFFFFFFF),
+               FLAC__U64L(0x0001FFFFFFFFFFFF), FLAC__U64L(0x0003FFFFFFFFFFFF), FLAC__U64L(0x0007FFFFFFFFFFFF), FLAC__U64L(0x000FFFFFFFFFFFFF),
+               FLAC__U64L(0x001FFFFFFFFFFFFF), FLAC__U64L(0x003FFFFFFFFFFFFF), FLAC__U64L(0x007FFFFFFFFFFFFF), FLAC__U64L(0x00FFFFFFFFFFFFFF),
+               FLAC__U64L(0x01FFFFFFFFFFFFFF), FLAC__U64L(0x03FFFFFFFFFFFFFF), FLAC__U64L(0x07FFFFFFFFFFFFFF), FLAC__U64L(0x0FFFFFFFFFFFFFFF),
+               FLAC__U64L(0x1FFFFFFFFFFFFFFF), FLAC__U64L(0x3FFFFFFFFFFFFFFF), FLAC__U64L(0x7FFFFFFFFFFFFFFF), FLAC__U64L(0xFFFFFFFFFFFFFFFF)
        };
        unsigned n, k;
 
@@ -691,7 +752,7 @@ FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 va
                                k = bits - FLAC__BITS_PER_BLURB;
                                bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
                                /* we know k < 64 so no need to protect against the gcc bug mentioned above */
-                               val &= (~(0xffffffffffffffff << k));
+                               val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
                                bits -= FLAC__BITS_PER_BLURB;
                        }
                }
@@ -701,7 +762,7 @@ FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 va
                        bb->buffer[bb->blurbs] <<= n;
                        bb->buffer[bb->blurbs] |= (val >> k);
                        /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
-                       val &= (~(0xffffffffffffffff << k));
+                       val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
                        bits -= n;
                        bb->bits += n;
                        if(bb->bits == FLAC__BITS_PER_BLURB) {
@@ -851,127 +912,6 @@ unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
 }
 #endif /* UNUSED */
 
-#ifdef FLAC__SYMMETRIC_RICE
-FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
-{
-       unsigned total_bits, interesting_bits, msbs;
-       FLAC__uint32 pattern;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter <= 31);
-
-       /* init pattern with the unary end bit and the sign bit */
-       if(val < 0) {
-               pattern = 3;
-               val = -val;
-       }
-       else
-               pattern = 2;
-
-       msbs = val >> parameter;
-       interesting_bits = 2 + parameter;
-       total_bits = interesting_bits + msbs;
-       pattern <<= parameter;
-       pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
-
-       if(total_bits <= 32) {
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                       return false;
-       }
-       else {
-               /* write the unary MSBs */
-               if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
-                       return false;
-               /* write the unary end bit, the sign bit, and binary LSBs */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
-                       return false;
-       }
-       return true;
-}
-
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
-{
-       unsigned total_bits, interesting_bits, msbs;
-       FLAC__uint32 pattern;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter <= 31);
-
-       *overflow = false;
-
-       /* init pattern with the unary end bit and the sign bit */
-       if(val < 0) {
-               pattern = 3;
-               val = -val;
-       }
-       else
-               pattern = 2;
-
-       msbs = val >> parameter;
-       interesting_bits = 2 + parameter;
-       total_bits = interesting_bits + msbs;
-       pattern <<= parameter;
-       pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
-
-       if(total_bits <= 32) {
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                       return false;
-       }
-       else if(total_bits > max_bits) {
-               *overflow = true;
-               return true;
-       }
-       else {
-               /* write the unary MSBs */
-               if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
-                       return false;
-               /* write the unary end bit, the sign bit, and binary LSBs */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
-                       return false;
-       }
-       return true;
-}
-#endif /* UNUSED */
-
-FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
-{
-       unsigned total_bits, val_bits;
-       FLAC__uint32 pattern;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter <= 31);
-
-       val_bits = FLAC__bitmath_silog2(val);
-       total_bits = 2 + parameter + 5 + val_bits;
-
-       if(total_bits <= 32) {
-               pattern = 3;
-               pattern <<= (parameter + 5);
-               pattern |= val_bits;
-               pattern <<= val_bits;
-               pattern |= (val & ((1 << val_bits) - 1));
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                       return false;
-       }
-       else {
-               /* write the '-0' escape code first */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
-                       return false;
-               /* write the length */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
-                       return false;
-               /* write the value */
-               if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
-                       return false;
-       }
-       return true;
-}
-#endif /* ifdef FLAC__SYMMETRIC_RICE */
-
 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
 {
        unsigned total_bits, interesting_bits, msbs, uval;
@@ -1241,7 +1181,7 @@ FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 v
        FLAC__ASSERT(0 != bb);
        FLAC__ASSERT(0 != bb->buffer);
 
-       FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
+       FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
 
        if(val < 0x80) {
                return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
@@ -2029,36 +1969,6 @@ FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb,
 }
 #endif
 
-#ifdef FLAC__SYMMETRIC_RICE
-FLAC__bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
-{
-       FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
-
-       FLAC__ASSERT(0 != bb);
-       FLAC__ASSERT(0 != bb->buffer);
-       FLAC__ASSERT(parameter <= 31);
-
-       /* read the unary MSBs and end bit */
-       if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
-               return false;
-
-       /* read the sign bit */
-       if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
-               return false;
-
-       /* read the binary LSBs */
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
-               return false;
-
-       /* compose the value */
-       *val = (msbs << parameter) | lsbs;
-       if(sign)
-               *val = -(*val);
-
-       return true;
-}
-#endif /* ifdef FLAC__SYMMETRIC_RICE */
-
 FLAC__bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
 {
        FLAC__uint32 lsbs = 0, msbs = 0;
@@ -2087,6 +1997,7 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsig
 }
 
 FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[], unsigned nvals, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
+#ifdef FLAC__OLD_MSVC_FLAVOR
 {
        const FLAC__blurb *buffer = bb->buffer;
 
@@ -2293,6 +2204,115 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[
 
        return true;
 }
+#else
+{
+       const FLAC__blurb *buffer = bb->buffer;
+
+       unsigned i, j, val_i = nvals;
+       unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
+       FLAC__blurb blurb, save_blurb;
+       unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
+
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
+       FLAC__ASSERT(parameter <= 31);
+
+       if(nvals == 0)
+               return true;
+
+       cbits = bb->consumed_bits;
+       i = bb->consumed_blurbs;
+       while(val_i != 0) {
+               for( ; i < bb->blurbs; i++) {
+                       blurb = (save_blurb = buffer[i]) << cbits;
+                       while(1) {
+                               if(state == 0) {
+                                       if(blurb) {
+                                               j = FLAC__ALIGNED_BLURB_UNARY(blurb);
+                                               msbs += j;
+                                               j++;
+                                               cbits += j;
+
+                                               uval = 0;
+                                               lsbs_left = parameter;
+                                               state++;
+                                               if(cbits == FLAC__BITS_PER_BLURB) {
+                                                       cbits = 0;
+                                                       CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
+                                                       break;
+                                               }
+                                               blurb <<= j;
+                                       }
+                                       else {
+                                               msbs += FLAC__BITS_PER_BLURB - cbits;
+                                               cbits = 0;
+                                               CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
+                                               break;
+                                       }
+                               }
+                               else {
+                                       const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
+                                       if(lsbs_left >= available_bits) {
+                                               uval <<= available_bits;
+                                               uval |= (blurb >> cbits);
+                                               cbits = 0;
+                                               CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
+
+                                               if(lsbs_left == available_bits) {
+                                                       /* compose the value */
+                                                       uval |= (msbs << parameter);
+                                                       *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
+                                                       --val_i;
+                                                       if(val_i == 0) {
+                                                               i++;
+                                                               goto break2;
+                                                       }
+                                                       ++vals;
+
+                                                       msbs = 0;
+                                                       state = 0;
+                                               }
+
+                                               lsbs_left -= available_bits;
+                                               break;
+                                       }
+                                       else {
+                                               cbits += lsbs_left;
+                                               uval <<= lsbs_left;
+                                               uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
+                                               blurb <<= lsbs_left;
+
+                                               /* compose the value */
+                                               uval |= (msbs << parameter);
+                                               *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
+                                               --val_i;
+                                               if(val_i == 0)
+                                                       goto break2;
+                                               ++vals;
+
+                                               msbs = 0;
+                                               state = 0;
+                                       }
+                               }
+                       }
+               }
+break2:
+               bb->consumed_blurbs = i;
+               bb->consumed_bits = cbits;
+               bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
+               if(val_i != 0) {
+                       if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
+                               return false;
+                       /* these must be zero because we can only get here if we got to the end of the buffer */
+                       FLAC__ASSERT(bb->consumed_blurbs == 0);
+                       FLAC__ASSERT(bb->consumed_bits == 0);
+                       i = 0;
+               }
+       }
+
+       return true;
+}
+#endif
 
 #if 0 /* UNUSED */
 FLAC__bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
@@ -2473,7 +2493,7 @@ FLAC__bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *v
                i = 6;
        }
        else {
-               *val = 0xffffffffffffffff;
+               *val = FLAC__U64L(0xffffffffffffffff);
                return true;
        }
        for( ; i; i--) {
@@ -2482,7 +2502,7 @@ FLAC__bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *v
                if(raw)
                        raw[(*rawlen)++] = (FLAC__byte)x;
                if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
-                       *val = 0xffffffffffffffff;
+                       *val = FLAC__U64L(0xffffffffffffffff);
                        return true;
                }
                v <<= 6;