[LAD] Bit shift

Robin Gareus robin at gareus.org
Sun Mar 17 20:12:04 CET 2019


On 3/17/19 5:56 PM, Jonathan Brickman wrote:
> That is likely to change depending on GCC optimization setting, no?

Not usually. It depends on the target architecture more than anything.

Even with -O0, gcc translates integer addition and multiplications into
a combination of bitwise and arithmetic operations if that is
appropriate for the target CPU.


On 3/12/19 11:43 PM, Will Godfrey wrote:
> Does anyone know if GCC will replace power of 2 
> multiplications/divisions of unsigned integers with bit shifts?
Not only power of two, and it depends. In some cases the compiler
doesn't replace it because the resulting code is not faster. Many modern
CPU already implement integer multiplication using bitwise operations in
microcode.

I don't know if this your question is academic, but if you plan to
manually optimize code, I highly recommend against that.

Write semantically! If you mean a multiplication, use (a * 2), if you
mean bit-shift use (a << 1). Do not use bitwise-operations when you
really do integer-arithmetics.

Your future self and code-contributers will thank you for it; as will
compilers targeting future CPU architectures.
</rant>

2c,
robin


More information about the Linux-audio-dev mailing list