使用GCC装配体向左/向右旋转移动数量 [英] Rotate left/right with number of moves using GCC assembly

查看:262
本文介绍了使用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程序集中有更好的写法吗?

解决方案

p>我在这里找到了答案:



minGW下可能的_rotl性能



并将我的代码重写为:

  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发表的关于_rotl / _rotr _rotl64 / _rotr64的实用意见!和gnometorule


I have the following code to calculate rotate left/right in Visual Studio assembly.

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- How can we write the equivalent asm code for gcc.

2- Is there a better way to write it in Visual Studio assembly?

解决方案

I have found the answer here:

Poor _rotl performance under minGW

and rewritten my code to be:

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;
    }

Thank you for the useful comments about _rotl/_rotr _rotl64/_rotr64 from Jerry Coffin and gnometorule

这篇关于使用GCC装配体向左/向右旋转移动数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆