Near constant time rotate that does not violate the standards

后端 未结 4 1379
孤街浪徒
孤街浪徒 2020-11-27 20:05

I\'m having a heck of a time trying to come up with a constant time rotate that does not violate the C/C++ standards.

The problem is the edge/corner cases, where ope

4条回答
  •  一整个雨季
    2020-11-27 20:35

    You can add one additional modulo operation to prevent the shifting by 32 bits, but I'm not convinced this is faster than using an if check in conjunction with branch predictors.

    template  inline T rotlMod(T x, unsigned int y)
    {
        y %= sizeof(T)*8;
        return T((x<>((sizeof(T)*8-y) % (sizeof(T)*8))));
    }
    

提交回复
热议问题