快速按字节替换,如果 [英] Fast byte-wise replace if
问题描述
我有一个功能,可以将二进制数据从一个区域复制到另一个区域,但前提是字节与特定值不同.这是一个代码示例:
I have a function that copies binary data from one area to another, but only if the bytes are different from a specific value. Here is a code sample:
void copy_if(char* src, char* dest, size_t size, char ignore)
{
for (size_t i = 0; i < size; ++i)
{
if (src[i] != ignore)
dest[i] = src[i];
}
}
问题是,这对于我当前的需求而言太慢了.有没有一种方法可以更快地获得相同的结果?
The problem is that this is too slow for my current need. Is there a way to obtain the same result in a faster way?
更新: 根据答案,我尝试了两种新的实现方式:
Update: Based on answers I tried two new implementations:
void copy_if_vectorized(const uint8_t* src, uint8_t* dest, size_t size, char ignore)
{
for (size_t i = 0; i < size; ++i)
{
char temps = src[i];
char tempd = dest[i];
dest[i] = temps == ignore ? tempd : temps;
}
}
void copy_if_SSE(const uint8_t* src, uint8_t* dest, size_t size, uint8_t ignore)
{
const __m128i vignore = _mm_set1_epi8(ignore);
size_t i;
for (i = 0; i + 16 <= size; i += 16)
{
__m128i v = _mm_loadu_si128((__m128i *)&src[i]);
__m128i vmask = _mm_cmpeq_epi8(v, vignore);
vmask = _mm_xor_si128(vmask, _mm_set1_epi8(-1));
_mm_maskmoveu_si128(v, vmask, (char *)&dest[i]);
}
for (; i < size; ++i)
{
if (src[i] != ignore)
dest[i] = src[i];
}
}
我得到了以下结果:
Naive:
Duration: 2.04844s
Vectorized:
Pass: PASS
Duration: 3.18553s
SIMD:
Pass: PASS
Duration: 0.481888s
我猜我的编译器无法向量化(最后一个MSVC),但是SIMD解决方案已经足够好了,谢谢!
I guess my compiler failed to vectorized (last MSVC), but the SIMD solution is good enough, thanks!
更新(之二) 我设法使用一些编译指示对它进行矢量化,以进行编译(MSVC),实际上,使用SIMD的速度实际上更快,这是最终代码:
Update (bis) I managed to vectorized it using some pragma instructions for my compile (MSVC) and indeed it is actually faster that SIMD, here is the final code:
void copy_if_vectorized(const uint8_t* src, uint8_t* dest, size_t size, char ignore)
{
#pragma loop(hint_parallel(0))
#pragma loop(ivdep)
for (int i = 0; i < size; ++i) // Sadly no parallelization if i is unsigned, but more than 2Go of data is very unlikely
{
char temps = src[i];
char tempd = dest[i];
dest[i] = temps == ignore ? tempd : temps;
}
}
推荐答案
我的gcc 4.8.4将以下代码向量化:
My gcc 4.8.4 vectorizes the following code:
#include <stddef.h>
void copy_if(char* src, char* dest, size_t size, char ignore)
{
for (size_t i = 0; i < size; ++i)
{
char temps = src[i];
char tempd = dest[i];
dest[i] = temps == ignore ? tempd : temps;
}
}
请注意,从-的加载和对dest[i]
的赋值都是无条件的,因此编译器不受禁止在多线程程序中发明存储的限制.
Note that both the load from- and the assignment to dest[i]
are unconditional, so the compiler is not restricted by the prohibition against inventing stores in a multi-threaded program.
对于-march=core-avx2
,生成的程序集包含此向量化循环,一次处理32个字节:
For -march=core-avx2
, the generated assembly contains this vectorized loop, working on 32 bytes at a time:
.L9:
vmovdqu (%rdi,%rcx), %ymm1
addq $1, %r10
vmovdqu (%rsi,%rcx), %ymm2
vpcmpeqb %ymm0, %ymm1, %ymm3
vpblendvb %ymm3, %ymm2, %ymm1, %ymm1
vmovdqu %ymm1, (%rsi,%rcx)
addq $32, %rcx
cmpq %r10, %r8
ja .L9
对于通用x86-64,生成的程序集包含此向量化循环,一次处理16个字节:
For generic x86-64, the generated assembly contains this vectorized loop, working on 16 bytes at a time:
.L9:
movdqu (%rdi,%r8), %xmm3
addq $1, %r10
movdqa %xmm3, %xmm1
movdqu (%rsi,%r8), %xmm2
pcmpeqb %xmm0, %xmm1
pand %xmm1, %xmm2
pandn %xmm3, %xmm1
por %xmm2, %xmm1
movdqu %xmm1, (%rsi,%r8)
addq $16, %r8
cmpq %r9, %r10
jb .L9
对于armv7l-neon,clang-3.7
生成以下循环,一次处理16个字节:
For armv7l-neon, clang-3.7
generates the following loop, working on 16 bytes at a time:
.LBB0_9: @ %vector.body
@ =>This Inner Loop Header: Depth=1
vld1.8 {d18, d19}, [r5]!
subs.w lr, lr, #16
vceq.i8 q10, q9, q8
vld1.8 {d22, d23}, [r4]
vbsl q10, q11, q9
vst1.8 {d20, d21}, [r4]!
bne .LBB0_9
因此,该代码不仅比汇编或内部函数更具可读性,而且还可移植到多种体系结构和编译器.重新编译可以轻松利用新的体系结构和指令集扩展.
So, the code is not only more readable than assembly or intrinsics, it's also portable to multiple architectures and compilers. New architectures and instruction-set extensions can easily be utilized by recompilation.
这篇关于快速按字节替换,如果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!