Extend bit width
Especially for graphic cards, values are often truncated to a few bits, i.e. 5 bits per color channel.
When used to actually draw pixels, these values have to be extended to 8 bits while preserving three important properties:
1. scaling must be as linear as possible
2. zero must remain zero
3. the maximum possible input must be mapped to the maximum possible output
In terms of a graphics designer, colors must not be falsified and especially White must remain White while Black remains Black.
A simple left shift suffices properties 1 and 2. However, it fails when it comes to requirement 3.
In the example given, 0x1F is the maximum value for 5 bits but a left shift by 3 bits generates 0xF8 which is clearly not 0xFF.
The presented method not only performs the shift (line 4 and line 8) but also maps the highest bits to the lowest bits, too,
and thus solves the problem (line 5 and line 8).
Note: if the input bit width is less than half the output bit width, requirement 3 is not always fulfilled.
Compilers convert lines 4 and 5 to constant values. The only actual code is to be found in line 8.
help me to pay
my server bills
• constant time, data independent
+ Intel® Pentium™ D:
• extend: ≈ 5.5 cycles per number
+ Intel® Core™ 2:
• extend: ≈ 3 cycles per number
+ Intel® Core™ i7:
• extend: ≈ 5 cycles per number
lower values are better)
|Download||The full source code including a test program is available for download.|
|More Twiddled Bits||
... or go to the index