??? 02/13/09 12:33 Read: times Msg Score: +1 +1 Good Answer/Helpful |
#162353 - Shifts and masks often resulting in optimum native code Responding to: ???'s previous message |
As Andy notes, the code optimization steps of a compiler can be quite useful.
When the target variable is max 8 bits large, many compilers can detect that the final "a & 0xff" will not have any bits to mask away, so the compiler can strip out this step without changing the behaviour. The source code does a compatibility step that need not result in corresponding native instructions. And many compilers are smart enough to notice that a shift of n*8 bits represents a full byte shift. They may then look at their available addressing modes to figure out if they can pick up a byte in the middle instead of loading the larger word and shifting. On 32-bit compilers, it isn't uncommon that the compiler looks at code and detects four byte writes that are shifted and ored together and detects that the code represents a big-endian or little-endian load (or store) and replaces a lot of shifts and oring with either a single 32-bit native read or a 32-bit native read followed by a built-in byte swap. Or possibly a single "read 32-bit swapped" instruction. It is very easy to look at source code and consider shifts and masking to be big, ugly and expensive. But since it is "the" portable way to handle the operations, many compilers looks for this kind of code and translates it to the optimum native code. |
Topic | Author | Date |
LSB byte of 24 bits | 01/01/70 00:00 | |
a union | 01/01/70 00:00 | |
union = fast, but not portable | 01/01/70 00:00 | |
Shift-and-Mask = Portable, and possibly not slow | 01/01/70 00:00 | |
Shifts and masks often resulting in optimum native code | 01/01/70 00:00 | |
can be portable | 01/01/70 00:00 | |
Can be portable | 01/01/70 00:00 | |
More to it than that! | 01/01/70 00:00 | |
which is why I do this very thing | 01/01/70 00:00 | |
for completeness there is the pointer | 01/01/70 00:00 | |
Split the word? | 01/01/70 00:00 | |
Tautology? | 01/01/70 00:00 | |
re: Tautology | 01/01/70 00:00 | |
possible, but not universal![]() | 01/01/70 00:00 | |
thank you | 01/01/70 00:00 |