嵌入汇编程序以在可移植 C++ 中操作 64 位寄存器
我有一个简单(但性能关键)的 C (嵌入在 C++ 中)算法来操作数据缓冲区......该算法自然"使用 64 位大端寄存器值 - 我想优化这使用汇编程序来直接访问进位标志和 BSWAP,因此避免了一次操作一个字节的 64 位值.
I have a simple (but performance critical) algorithm in C (embedded in C++) to manipulate a data buffer... the algorithm 'naturally' uses 64-bit big-endian register values - and I'd like to optimise this using assembler to gain direct access to the carry flag and BSWAP and, hence, avoid having to manipulate the 64-bit values one byte at a time.
我希望解决方案能够在操作系统/编译器之间移植――最低限度地支持 GNU g++ 和 Visual C++――以及分别在 Linux 和 Windows 之间移植.对于这两个平台,显然,我假设处理器支持 x86-64 指令集.
I want the solution to be portable between OS/Compilers - minimally supporting GNU g++ and Visual C++ - and between Linux and Windows respectively. For both platforms, obviously, I'm assuming a processor that supports the x86-64 instruction set.
我发现 这篇关于 MSVC/Windows 内联汇编器的文档,以及来自 Google 的几个片段,详细说明了 g++ 的不兼容语法.我接受我可能需要在每种方言中单独实现此功能.我无法找到足够详细的关于语法/工具的文档来应对这一发展.
I've found this document about inline assembler for MSVC/Windows, and several fragments via Google detailing an incompatible syntax for g++. I accept that I might need to implement this functionality separately in each dialect. I've not been able to find sufficiently detailed documentation on syntax/facilities to tackle this development.
我正在寻找的是详细说明我可用的工具的清晰文档 - 包括 MS 和 GNU 工具集.虽然我多年前编写了一些 32 位汇编程序,但我已经生疏了 - 我会从汇编级别提供的简明文档详细说明中受益.
What I'm looking for is clear documentation detailing the facilities available to me - both with MS and GNU tool sets. While I wrote some 32-bit assembler many years ago, I'm rusty - I'd benefit from a concise document detailing facilities are available at an assembly level.
更复杂的是,我想使用 Visual C++ Express Edition 2010 为 Windows 编译...我知道这是一个 32 位编译器 - 但我想知道是否可以嵌入 64 位汇编成它的可执行文件?在我打算手工编码的部分,我只关心 64 位性能.
A further complication is that I'd like to compile for windows using the Visual C++ Express Edition 2010... I recognise that this is a 32-bit compiler - but, I wondered, is it possible to embed 64-bit assembly into its executables? I only care about 64-bit performance in the section I plan to hand-code.
任何人都可以提供任何指针(请原谅双关语...)?
Can anyone offer any pointers (please pardon the pun...)?
推荐答案
只是为了让你体验一下你前进道路上的障碍,这里有一个简单的内联汇编函数,有两种方言.首先,Borland C++ Builder 版本(我认为这也可以在 MSVC++ 下编译):
Just to give you a taste of the obstacles that lie in your path, here is a simple inline assembler function, in two dialects. First, the Borland C++ Builder version (I think this compiles under MSVC++ too):
int BNASM_AddScalar (DWORD* result, DWORD x)
{
int carry = 0 ;
__asm
{
mov ebx,result
xor eax,eax
mov ecx,x
add [ebx],ecx
adc carry,eax // Return the carry flag
}
return carry ;
}
现在,g++ 版本:
int BNASM_AddScalar (DWORD* result, DWORD x)
{
int carry = 0 ;
asm volatile (
" addl %%ecx,(%%edx)
"
" adcl $0,%%eax
" // Return the carry flag
: "+a"(carry) // Output (and input): carry in eax
: "d"(result), "c"(x) // Input: result in edx and x in ecx
) ;
return carry ;
}
如您所见,差异很大.而且没有办法绕过它们.这些来自我为 32 位环境编写的大型整数算术库.
As you can see, the differences are major. And there is no way around them. These are from a large integer arithmetic library that I wrote for a 32-bit environment.
至于在 32 位可执行文件中嵌入 64 位指令,我认为这是禁止的.据我了解,32 位可执行文件以 32 位模式运行,任何 64 位指令只会生成一个陷阱.
As for embedding 64-bit instructions in a 32-bit executable, I think this is forbidden. As I understand it, a 32-bit executable runs in 32-bit mode, any 64-bit instruction just generates a trap.
相关文章