使用GCC组件向左/向右旋转移动次数

时间:2013-11-21 00:20:52

标签: c++ visual-studio gcc assembly x86

我有以下代码来计算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程序集中编写它?

1 个答案:

答案 0 :(得分:1)

我在这里找到了答案:

Poor _rotl performance under minGW

并将我的代码重写为:

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和gnometorule的_rotl / _rotr _rotl64 / _rotr64的有用评论