This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

the best way to shift

<Code>

#define BITS_TO_ROTATE 2

b=(a>>(8-BITS_TO_ROTATE))+(a<<BITS_TO_ROTATE);

After lots of research and experimentation we have determined that this is often the best way as it gives the compiler a chance of recognising it as a rotate and therefore allowing it to generate a RRC/RLC or better instruction.

You could also use inline ASM if the optimiser doesn't produce the correct instructions, which would guarantee each step of the rotate to execute in no more than a single cycle.

  • "After lots of research ... this is often ..."

    It's not worth to spend time on this. The C compiler can create any code, as long as it is correct. The code created depends on the compiler used, optimization level, compiler version...

    I see three options here:
    1) Do it in C, and let the compiler do the rest.
    2) Do it in assembler, if you want a specific instruction sequence
    3) Use an intrinsic function (if the compiler provides that), like _crol_(), _cror_().

  • Exactly. A classic case of premature optimization, which is the root of all evil, as we know.