Tim*_*afé 10 c++ assembly inline-assembly clang++
亲爱的Assembly/C++ dev,
问题是:传播两个ASM块之间的进位(或任何标志)是现实的还是完全疯狂的,即使它有效?
几年前,我为低于512位的大型算术开发了一个整数库(在编译时).我此时没有使用GMP,因为对于这种规模,GMP由于内存分配而变慢,并且模型选择二进制表示工作台.
我必须承认我创建了我的ASM(字符串块)使用BOOST_PP,它不是非常光荣(好奇的看看它vli).图书馆运作良好.
但是我注意到,此时不可能在两个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;
}
Run Code Online (Sandbox Code Playgroud)
我得到一个正确的结果(它必须在-O2或-O3版本中编译!)ASM是正确的(在我的Mac上使用clang ++:Apple LLVM版本9.0.0(clang-900.0.39.2))
movq -96(%rbp), %rax
addq %rax, -64(%rbp)
## InlineAsm End
## InlineAsm Start
movq -88(%rbp), %rax
adcq %rax, -56(%rbp)
## InlineAsm End
## InlineAsm Start
movq -80(%rbp), %rax
adcq %rax, -48(%rbp)
## InlineAsm End
## InlineAsm Start
movq -72(%rbp), %rax
adcq %rax, -40(%rbp)
Run Code Online (Sandbox Code Playgroud)
我很有道理它正在工作,因为在优化期间,编译器删除了ASM块之间的所有无用指令(在调试模式下它失败了).
你怎么看 ?绝对不安全?编译器的人知道它会稳定多少?
总结:我只是为了好玩而做到这一点:)是的,GMP是大算术的解决方案!
使用__volatile__是一种滥用。
目的__volatile__是强制编译器在写入的位置发出汇编代码,而不是依靠数据流分析来解决这个问题。如果您在用户空间中对数据进行普通操作,通常您不应该使用__volatile__,并且如果您需要__volatile__让代码正常工作,则几乎总是意味着您的操作数指定不正确。
是的,操作数指定不正确。让我们看看第一个块。
__asm__ __volatile__ (
"movq %1, %%rax \n"
"addq %%rax, %0 \n"
: "=m"(a[0]) // output
: "m" (b[0]) // input only
: "rax", "memory");
Run Code Online (Sandbox Code Playgroud)
这里有两个错误。
对输出的约束"=m"(a[0])不正确。回想一下, 的目标addq既是输入又是输出,因此正确的约束是 +,因此使用"+m"(a[0])。如果您告诉编译器a[0]仅输出,编译器可能会安排a[0]包含垃圾值(通过死存储消除),这不是您想要的。
装配规范中缺少这些标志。在不告诉编译器标志已修改的情况下,编译器可能会假设标志在整个汇编块中保留,这将导致编译器在其他地方生成不正确的代码。
不幸的是,这些标志只能用作汇编块的输出或破坏操作数,而不能用作输入。因此,在为正确指定操作数而大惊小怪之后,您就不会使用__volatile__... 事实证明,无论如何都没有一个好的方法来指定您的操作数!
所以这里的建议是你至少应该修复你可以修复的操作数,并指定"cc"为一个破坏者。__volatile__但有一些更好的解决方案根本不需要......
加法函数mpn_不分配内存。这些mpz_函数是函数的包装器,mpn_具有一些额外的逻辑和内存分配。
如果将整个循环写入一个汇编块中,则不必担心在块之间保留标志。您可以使用汇编宏来执行此操作。请原谅混乱,我不是一个汇编程序员:
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");
}
Run Code Online (Sandbox Code Playgroud)
它的作用是使用汇编指令评估循环.rept。您最终将获得 1 个副本addq和 N-1 个副本adcq,尽管如果您查看 GCC 的汇编输出,-S您将只能看到其中的一个。汇编器本身将创建副本,展开循环。
请参阅要点:https://gist.github.com/depp/966fc1f4d535e31d9725cc71d97daf91