在 gcc 编译器中使用 bts 汇编指令 [英] Using bts assembly instruction with gcc compiler

查看:46
本文介绍了在 gcc 编译器中使用 bts 汇编指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Mac 上使用 bts 和 bt x86 汇编指令来加速我的 C++ 代码中的位操作.在 Windows 上,_bittestandset 和 _bittest 内部函数运行良好,并提供显着的性能提升.在 Mac 上,gcc 编译器似乎不支持这些,所以我尝试直接在汇编程序中进行.

I want to use the bts and bt x86 assembly instructions to speed up bit operations in my C++ code on the Mac. On Windows, the _bittestandset and _bittest intrinsics work well, and provide significant performance gains. On the Mac, the gcc compiler doesn't seem to support those, so I'm trying to do it directly in assembler instead.

这是我的 C++ 代码(注意位"可以 >= 32):

Here's my C++ code (note that 'bit' can be >= 32):

typedef unsigned long LongWord;
#define DivLongWord(w) ((unsigned)w >> 5)
#define ModLongWord(w) ((unsigned)w & (32-1))

inline void SetBit(LongWord array[], const int bit)
{
   array[DivLongWord(bit)] |= 1 << ModLongWord(bit);
}

inline bool TestBit(const LongWord array[], const int bit)
{
    return (array[DivLongWord(bit)] & (1 << ModLongWord(bit))) != 0;
}

以下汇编代码可以工作,但不是最优的,因为编译器无法优化寄存器分配:

The following assembler code works, but is not optimal, as the compiler can't optimize register allocation:

inline void SetBit(LongWord* array, const int bit)
{
   __asm {
      mov   eax, bit
      mov   ecx, array
      bts   [ecx], eax
   }
}

问题:如何让编译器围绕 bts 指令进行全面优化?以及如何用 bt 指令替换 TestBit?

Question: How do I get the compiler to fully optimize around the bts instruction? And how do I replace TestBit by a bt instruction?

推荐答案

inline void SetBit(*array, bit) {
    asm("bts %1,%0" : "+m" (*array) : "r" (bit));
}

这篇关于在 gcc 编译器中使用 bts 汇编指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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