我对在 C++ 代码中使用内联汇编非常陌生。我想要做的基本上是一种大小为 32 的指针的 memcopy。
在 C++ 中,代码通常是这样的:
void my_memcpy(const std::uint8_t* in,std::uint8_t* out,const std::size_t& sz)
{
assert((sz%32 == 0));
for(const std::uint8_t* it = beg; it != (beg+sz);it+=32,out+=32)
{
__m256i = _mm256_stream_load_si256(reinterpret_cast<__m256i*>(it));
_mm256_stream_si256(reinterpret_cast<__m256i*>(out),tmp);
}
}
我已经做了一点内联汇编,但每次我都事先知道输入选项卡和输出选项卡的大小。
所以我尝试了这个:
void my_memcpy(const std::uint8_t* in,std::uint8_t* out,const std::size_t& sz)
{
assert((sz%32 == 0));
__asm__ volatile(
"mov %1, %%eax \n"
"mov $0, %%ebx \n"
"L1: \n"
"vmovntdqa (%[src],%%ebx), %%ymm0 \n"
"vmovntdq %%ymm0, (%[dst],%%ebx) \n"
"add %%ebx, $32 \n"
"cmp %%eax, %%ebx \n"
"jz L1 \n"
:[dst]"=r"(out)
:[src]"r"(in),"m"(sz)
:"memory"
);
}
G++ 告诉我:
Error: unsupported instruction `mov'
Error: `(%rdi,%ebx)' is not a valid base/index expression
Error: `(%rdi,%ebx)' is not a valid base/index expression
Error: operand type mismatch for `add'
所以我尝试了这个:
void my_memcpy(const std::uint8_t* in,std::uint8_t* out,const std::size_t& sz)
{
assert((sz%32 == 0));
__asm__ volatile(
"mov %1, %%eax \n"
"mov $0, %%ebx \n"
"L1: \n"
"vmovntdqa %%ebx(%[src]), %%ymm0 \n"
"vmovntdq %%ymm0, (%[dst],%%ebx) \n"
"add %%ebx, $32 \n"
"cmp %%eax, %%ebx \n"
"jz L1 \n"
:[dst]"=r"(out)
:[src]"r"(in),"m"(sz)
:"memory"
);
}
我从 G++ 获得:
Error: unsupported instruction `mov'
Error: junk `(%rdi)' after register
Error: `(%rdi,%ebx)' is not a valid base/index expression
Error: operand type mismatch for `add'
在每种情况下,我都试图找到没有成功的解决方案。我也体验过这个解决方案:
void my_memcpy(const std::uint8_t* in,std::uint8_t* out,const std::size_t& sz)
{
__asm__ volatile (
".intel_syntax noprefix;"
"mov eax, [SZ];"
"mov ebx, 0;"
"L1 : "
"vmovntdqa ymm0, [src+ebx];"
"vmovntdq [dst+ebx], ymm0;"
"add ebx, 32 \n"
"cmp ebx, eax \n"
"jz L1 \n"
".att_syntax;"
: [dst]"=r"(out)
: [SZ]"m"(sz),[src]"r"(in)
: "memory");
}
G++:
undefined reference to `SZ'
undefined reference to `src'
undefined reference to `dst'
那个消息看起来很常见,但我不知道在这种情况下如何修复它。
我也知道我的尝试并不严格代表我用 C++ 编写的代码。
我想了解我的尝试有什么问题,以及如何尽可能接近我的 C++ 函数。
提前致谢。