Is there any way to do 128-bit shifts on gcc <4.4?

前端 未结 4 1251
暖寄归人
暖寄归人 2020-12-06 12:28

gcc 4.4 seems to be the first version when they added int128_t. I need to use bit shifting and I have run out of room for some bit fields.

4条回答
  •  感动是毒
    2020-12-06 13:06

    Bit shifting is very easy in any arbitrary number of bits. Just remember to shift the overflowed bits to the next limb. That's all

    typedef struct {
       int64_t high;
       uint64_t low;
    } int128_t;
    
    
    int128_t shift_left(int128_t v, unsigned shiftcount)
    {
       int128_t result;
       result.high = (v.high << shiftcount) | (v.low >> (64 - shiftcount));
       result.low  =  v.low  << shiftcount;
       return result;
    }
    

    Similar for shift right

    int128_t shift_right(int128_t v, unsigned shiftcount)
    {
       int128_t result;
       result.low  = (v.low  >> shiftcount) | (v.high << (64 - shiftcount));
       result.high =  v.high >> shiftcount;
       return result;
    }
    

提交回复
热议问题