@@ -227,21 +227,25 @@ class SimpleBitstreamCursor {
227
227
return R;
228
228
}
229
229
230
- Expected<uint32_t > ReadVBR (unsigned NumBits) {
230
+ Expected<uint32_t > ReadVBR (const unsigned NumBits) {
231
231
Expected<unsigned > MaybeRead = Read (NumBits);
232
232
if (!MaybeRead)
233
233
return MaybeRead;
234
234
uint32_t Piece = MaybeRead.get ();
235
235
236
- if ((Piece & (1U << (NumBits-1 ))) == 0 )
236
+ assert (NumBits <= 32 && NumBits >= 1 && " Invalid NumBits value" );
237
+ const uint32_t MaskBitOrder = (NumBits - 1 );
238
+ const uint32_t Mask = 1UL << MaskBitOrder;
239
+
240
+ if ((Piece & Mask) == 0 )
237
241
return Piece;
238
242
239
243
uint32_t Result = 0 ;
240
244
unsigned NextBit = 0 ;
241
245
while (true ) {
242
- Result |= (Piece & (( 1U << (NumBits- 1 ))- 1 )) << NextBit;
246
+ Result |= (Piece & (Mask - 1 )) << NextBit;
243
247
244
- if ((Piece & ( 1U << (NumBits- 1 )) ) == 0 )
248
+ if ((Piece & Mask ) == 0 )
245
249
return Result;
246
250
247
251
NextBit += NumBits-1 ;
@@ -258,21 +262,24 @@ class SimpleBitstreamCursor {
258
262
259
263
// Read a VBR that may have a value up to 64-bits in size. The chunk size of
260
264
// the VBR must still be <= 32 bits though.
261
- Expected<uint64_t > ReadVBR64 (unsigned NumBits) {
265
+ Expected<uint64_t > ReadVBR64 (const unsigned NumBits) {
262
266
Expected<uint64_t > MaybeRead = Read (NumBits);
263
267
if (!MaybeRead)
264
268
return MaybeRead;
265
269
uint32_t Piece = MaybeRead.get ();
270
+ assert (NumBits <= 32 && NumBits >= 1 && " Invalid NumBits value" );
271
+ const uint32_t MaskBitOrder = (NumBits - 1 );
272
+ const uint32_t Mask = 1UL << MaskBitOrder;
266
273
267
- if ((Piece & ( 1U << (NumBits- 1 )) ) == 0 )
274
+ if ((Piece & Mask ) == 0 )
268
275
return uint64_t (Piece);
269
276
270
277
uint64_t Result = 0 ;
271
278
unsigned NextBit = 0 ;
272
279
while (true ) {
273
- Result |= uint64_t (Piece & (( 1U << (NumBits- 1 ))- 1 )) << NextBit;
280
+ Result |= uint64_t (Piece & (Mask - 1 )) << NextBit;
274
281
275
- if ((Piece & ( 1U << (NumBits- 1 )) ) == 0 )
282
+ if ((Piece & Mask ) == 0 )
276
283
return Result;
277
284
278
285
NextBit += NumBits-1 ;
0 commit comments