Emulating variable bit-shift using only constant shifts?

后端 未结 8 1255
清歌不尽
清歌不尽 2020-12-09 19:19

I\'m trying to find a way to perform an indirect shift-left/right operation without actually using the variable shift op or any branches.

The particular PowerPC pro

8条回答
  •  伪装坚强ぢ
    2020-12-09 19:45

    How about this:

    if (y & 16) x <<= 16;
    if (y & 8) x <<= 8;
    if (y & 4) x <<= 4;
    if (y & 2) x <<= 2;
    if (y & 1) x <<= 1;
    

    will probably take longer yet to execute but easier to interleave if you have other code to go between.

提交回复
热议问题