C++ 在两个ASM GCC内联块之间传播进位
尊敬的Assembly/C++开发人员: 问题是:在两个ASM块之间传播进位(或任何标志)是真实的还是完全疯狂的,即使它有效 几年前,我为小于512位的大型算术(编译时)开发了一个整数库。我当时没有使用GMP,因为对于这种规模,由于内存分配和二进制表示的模型选择,GMP变得较慢 我必须承认,我使用C++ 在两个ASM GCC内联块之间传播进位,c++,assembly,inline-assembly,clang++,C++,Assembly,Inline Assembly,Clang++,尊敬的Assembly/C++开发人员: 问题是:在两个ASM块之间传播进位(或任何标志)是真实的还是完全疯狂的,即使它有效 几年前,我为小于512位的大型算术(编译时)开发了一个整数库。我当时没有使用GMP,因为对于这种规模,由于内存分配和二进制表示的模型选择,GMP变得较慢 我必须承认,我使用BOOST\u PP创建了我的ASM(字符串块),它不是很光彩(好奇的人可以看看)。图书馆运转良好 然而,我在此时指出,不可能在两个ASM内联块之间传播状态寄存器的进位标志。这是合乎逻辑的,因为对于编译
BOOST\u PP
创建了我的ASM(字符串块),它不是很光彩(好奇的人可以看看)。图书馆运转良好
然而,我在此时指出,不可能在两个ASM内联块之间传播状态寄存器的进位标志。这是合乎逻辑的,因为对于编译器在两个块之间生成的任何助记符,寄存器都会被重置(除了mov
指令(根据我的汇编知识))
昨天我有了一个想法,在两个ASM块之间传播进位有点棘手(使用递归算法)。这是工作,但我认为我是幸运的
#include <iostream>
#include <array>
#include <cassert>
#include <algorithm>
//forward declaration
template<std::size_t NumBits>
struct integer;
//helper using object function, partial specialization is forbiden on functions
template <std::size_t NumBits, std::size_t W, bool K = W == integer<NumBits>::numwords>
struct helper {
static inline void add(integer<NumBits> &a, const integer<NumBits> &b){
helper<NumBits, integer<NumBits>::numwords>::add(a,b);
}
};
// first addition (call first)
template<std::size_t NumBits, std::size_t W>
struct helper<NumBits, W, 1> {
static inline void add(integer<NumBits> &a, const integer<NumBits> &b){
__asm__ (
"movq %1, %%rax \n"
"addq %%rax, %0 \n"
: "+m"(a[0]) // output
: "m" (b[0]) // input only
: "rax", "cc", "memory");
helper<NumBits,W-1>::add(a,b);
}
};
//second and more propagate the carry (call next)
template<std::size_t NumBits, std::size_t W>
struct helper<NumBits, W, 0> {
static inline void add(integer<NumBits> &a, const integer<NumBits> &b){
__asm__ (
"movq %1, %%rax \n"
"adcq %%rax, %0 \n"
: "+m"(a[integer<NumBits>::numwords-W])
: "m" (b[integer<NumBits>::numwords-W])
: "rax", "cc", "memory");
helper<NumBits,W-1>::add(a,b);
}
};
//nothing end reccursive process (call last)
template<std::size_t NumBits>
struct helper<NumBits, 0, 0> {
static inline void add(integer<NumBits> &a, const integer<NumBits> &b){};
};
// tiny integer class
template<std::size_t NumBits>
struct integer{
typedef uint64_t value_type;
static const std::size_t numbits = NumBits;
static const std::size_t numwords = (NumBits+std::numeric_limits<value_type>::digits-1)/std::numeric_limits<value_type>::digits;
using container = std::array<uint64_t, numwords>;
typedef typename container::iterator iterator;
iterator begin() { return data_.begin();}
iterator end() { return data_.end();}
explicit integer(value_type num = value_type()){
assert( -1l >> 1 == -1l );
std::fill(begin(),end(),value_type());
data_[0] = num;
}
inline value_type& operator[](std::size_t n){ return data_[n];}
inline const value_type& operator[](std::size_t n) const { return data_[n];}
integer& operator+=(const integer& a){
helper<numbits,numwords>::add(*this,a);
return *this;
}
integer& operator~(){
std::transform(begin(),end(),begin(),std::bit_not<value_type>());
return *this;
}
void print_raw(std::ostream& os) const{
os << "(" ;
for(std::size_t i = numwords-1; i > 0; --i)
os << data_[i]<<" ";
os << data_[0];
os << ")";
}
void print(std::ostream& os) const{
assert(false && " TO DO ! \n");
}
private:
container data_;
};
template <std::size_t NumBits>
std::ostream& operator<< (std::ostream& os, integer<NumBits> const& i){
if(os.flags() & std::ios_base::hex)
i.print_raw(os);
else
i.print(os);
return os;
}
int main(int argc, const char * argv[]) {
integer<256> a; // 0
integer<256> b(1);
~a; //all the 0 become 1
std::cout << " a: " << std::hex << a << std::endl;
std::cout << " ref: (ffffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffff) " << std::endl;
a += b; // should propagate the carry
std::cout << " a+=b: " << a << std::endl;
std::cout << " ref: (0 0 0 0) " << std::endl; // it works but ...
return 0;
}
我意识到它正在工作,因为在优化过程中,编译器会删除ASM块之间所有无用的指令(在调试模式下失败)
你觉得怎么样?绝对不安全?编译人员知道它有多稳定吗
总结:我这样做只是为了好玩:)是的,GMP是大型算术的解决方案 使用
\uuuuu volatile\uuuu
是一种滥用
\uuuu volatile\uuuu
的目的是强制编译器在写入位置发出汇编代码,而不是依赖数据流分析来解决这个问题。如果您在用户空间中执行普通的数据操作,通常不应该使用\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,如果您需要\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
是的,操作数的指定不正确。让我们看看第一个街区
__asm__ __volatile__ (
"movq %1, %%rax \n"
"addq %%rax, %0 \n"
: "=m"(a[0]) // output
: "m" (b[0]) // input only
: "rax", "memory");
这里有两个错误
- 对输出
“=m”(a[0])
的约束不正确。回想一下,addq
的目标既是输入又是输出,因此正确的约束是+,所以使用“+m”(a[0])
。如果您告诉编译器仅输出a[0]
,编译器可能会安排a[0]
包含垃圾值(通过死存储消除),这不是您想要的
- 组件规范中缺少这些标志。在不告诉编译器标志已修改的情况下,编译器可能会假定标志在整个程序集块中保留,这将导致编译器在其他地方生成不正确的代码
不幸的是,这些标志只能作为汇编块的输出或缓冲操作数使用,而不能作为输入使用。因此,在正确地指定操作数以避免使用\uuuuuvolatile\uuuuu
..这一切之后。。。事实证明,无论如何,指定操作数并不是一个好方法
因此,这里的建议是,您至少应该修复可以修复的操作数,并将“cc”
指定为clobber。但是有两个更好的解决方案根本不需要\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
解决方案#1:使用GMP。
用于添加的mpn
功能不分配内存。mpz
函数是围绕mpn
函数的包装,带有一些额外的逻辑和内存分配
解决方案#2:将所有内容写在一个组装块中。
如果在一个程序集块中编写整个循环,则不必担心在块之间保留标志。可以使用程序集宏来执行此操作。请原谅,我不是一个很好的汇编程序员:
template <int N>
void add(unsigned long long *dest, unsigned long long *src) {
__asm__(
"movq (%1), %%rax"
"\n\taddq %%rax, (%0)"
"\n.local add_offset"
"\n.set add_offset,0"
"\n.rept %P2" // %P0 means %0 but without the $ in front
"\n.set add_offset,add_offset+8"
"\n\tmovq add_offset(%1), %%rax"
"\n\tadcq %%rax, add_offset(%0)"
"\n.endr"
:
: "r"(dest), "r"(src), "n"(N-1)
: "cc", "memory", "rax");
}
模板
无效添加(无符号长*dest,无符号长*src){
__asm__(
movq(%1),%%rax
\n\taddq%%rax,(%0)
“\n.local add\u offset”
\n.set添加偏移量,0
“\n.rept%P2”//%P0表示%0,但前面没有$
\n.设置添加偏移量,添加偏移量+8
\n\tmovq添加偏移量(%1),%%rax
\n\tadcq%%rax,添加偏移量(%0)
“\n.endr”
:
:“r”(目的地),“r”(src),“n”(n-1)
:“cc”、“memory”、“rax”);
}
这样做的目的是使用.rept
汇编指令计算循环。您最终将获得1份addq
和N-1份adcq
,尽管如果您使用-S
查看GCC的汇编输出,您将只看到其中一份。汇编程序本身将创建副本,展开循环
参见要点:非常感谢您的回答:-关于GMP,我犯了一个错误,我知道GMP不使用2补全法,而是使用无符号整数和一位符号。它有助于乘法,但会减慢加法你的ASM很好,我学到了新的想法,但这不是真正的问题。在两个ASM块之间传播进位(或状态寄存器中的任何标志)是否有意义,或者它只是疯了:)我几乎100%确定mpn\u
函数确实为较大的输入分配了乘法内存。没有不需要辅助内存的已知次二次乘法算法。但这有点离题了,因为OP询问的是加法。@Timocafé:内联汇编“意外”工作是很常见的。只是旁注。在x86和x86-64上,“cc”
clobber实际上毫无意义。GCC假定这些标志总是被内联程序集删除。其他目标平台并非如此。为了保持一致性和自我记录,使用它不是一个坏主意,但它不是必需的。我们告诉编译器dest是一个由N个元素组成的数组,它将被修改,而不是使用内存clobber。src是一个由N个元素组成的数组,它将从中读取
template <int N>
void add(unsigned long long *dest, unsigned long long *src) {
__asm__(
"movq (%1), %%rax"
"\n\taddq %%rax, (%0)"
"\n.local add_offset"
"\n.set add_offset,0"
"\n.rept %P2" // %P0 means %0 but without the $ in front
"\n.set add_offset,add_offset+8"
"\n\tmovq add_offset(%1), %%rax"
"\n\tadcq %%rax, add_offset(%0)"
"\n.endr"
:
: "r"(dest), "r"(src), "n"(N-1)
: "cc", "memory", "rax");
}