Emulating variable bit-shift using only constant shifts?

后端 未结 8 1265
清歌不尽
清歌不尽 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:56

    How about this:

    int[] multiplicands = { 1, 2, 4, 8, 16, 32, ... etc ...};
    
    int ShiftByVar( int x, int y )
    {
        //return x << y;
        return x * multiplicands[y];
    }
    

提交回复
热议问题