Evenly distribute a value that spans a given number of bits into 8 bits. */
| 611 | /* Evenly distribute a value that spans a given number of bits into 8 bits. |
| 612 | */ |
| 613 | static uint32_t Make8Bits(uint32_t value, uint32_t bitspan) |
| 614 | { |
| 615 | uint32_t output = 0; |
| 616 | |
| 617 | if(bitspan == 8) |
| 618 | return value; |
| 619 | if(bitspan > 8) |
| 620 | return value >> (bitspan - 8); |
| 621 | |
| 622 | value <<= (8 - bitspan); /* Shift it up into the most significant bits. */ |
| 623 | while(value) |
| 624 | { |
| 625 | /* Repeat the bit pattern down into the least significant bits. This |
| 626 | * gives an even distribution when extrapolating from [0, 2^bitspan-1] |
| 627 | * into [0, 2^8-1], and avoids both floating point and awkward integer |
| 628 | * multiplication. Unfortunately, because we don't enforce a whitelist |
| 629 | * of bit patterns we support and can hard-code for, it necessitates a |
| 630 | * loop. I believe this is a fairly efficient way to express the idea, |
| 631 | * but it'd still be nice if the compiler could optimize this whole |
| 632 | * function heavily, since it's called in a tight decode loop. |
| 633 | */ |
| 634 | output |= value; |
| 635 | value >>= bitspan; |
| 636 | } |
| 637 | |
| 638 | return output; |
| 639 | } |
| 640 | |
| 641 | /* Reads four bytes out of a memory buffer and converts it to a uint32_t. |
| 642 | */ |