使用GCC程序集按移动次数向左/向右旋转



我有以下代码来计算Visual Studio程序集中的左/右旋转。

template<class T>
inline T rotr(T x, unsigned char moves){
    unsigned char temp;
    __asm{
        mov temp, CL
            mov CL, moves
            ror x, CL
            mov CL, temp
    };
    return x;
}
template<class T>
inline T rotl(T x, unsigned char moves){
    unsigned char temp;
    __asm{
        mov temp, CL
            mov CL, moves
            rol x, CL
            mov CL, temp
    };
    return x;
}

1-我们如何为gcc编写等效的asm代码。

2-有没有更好的方法在Visual Studio程序集中编写它?

我在这里找到了答案:

minGW 下较差的otl性能(_R)

并将我的代码改写为:

template<class T> 
inline  T rotr(T x, uint8_t r) {
      asm("rorl %1,%0" : "+r" (x) : "c" (r));
      return x;
    }
template<class T> 
inline  T rotl(T x, uint8_t r) {
      asm("roll %1,%0" : "+r" (x) : "c" (r));
      return x;
    }

感谢Jerry Coffin和gnometoroule 对_rotl/_rotr_rotl64/rotr64的有用评论

最新更新