Emulating variable bit-shift using only constant shifts?

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

    Here's something that is trivially unrollable:

    int result= value;
    
    int shift_accumulator= value;
    
    for (int i= 0; i<5; ++i)
    {
        result += shift_accumulator & (-(k & 1)); // replace with isel if appropriate
        shift_accumulator += shift_accumulator;
        k >>= 1;
    }
    
    0 讨论(0)
  • 2020-12-09 19:59

    Let's assume that your max shift is 31. So the shift amount is a 5-bit number. Because shifting is cumulative, we can break this into five constant shifts. The obvious version uses branching, but you ruled that out.

    Let N be a number between 1 and 5. You want to shift x by 2N if the bit whose value is 2N is set in y, otherwise keep x intact. Here one way to do it:

    #define SHIFT(N) x = isel(((y >> N) & 1) - 1, x << (1 << N), x);
    

    The macro assigns to x either x << 2ᴺ or x, depending on whether the Nth bit is set in y or not.

    And then the driver:

    SHIFT(1); SHIFT(2); SHIFT(3); SHIFT(4); SHIFT(5)
    

    Note that N is a macro variable and becomes constant.

    Don't know though if this is going to be actually faster than the variable shift. If it would be, one wonders why the microcode wouldn't run this instead...

    0 讨论(0)
提交回复
热议问题