是否可以使用SIMD指令进行替换? [英] Is it possible to use SIMD instruction for replace?
问题描述
我有 int 的向量,我需要查找和替换一些具有特定值的元素。它们都是 same 。
例如:将所有元素替换为4到8。
I have vector of int and I need to find and replace some elements with specific value. Both of them are the same.
For example: replace 4 to 8 for all elements.
我是尝试在c ++中循环进行直接内存访问。
I'm trying direct memory access in loop in c++. But it still to slow for me.
更新:
我正在使用OpenCV
x86
上的垫子</ code>对象:
Update:
I'm working with OpenCV Mat
object on x86
:
for (int i = 0; i < labels.rows; ++i) {
for (int j = 0; j < labels.cols; ++j) {
int& label = labels.at<int>(i, j);
if (label == oldValue) {
label = newValue;
}
}
}
Mat.at()
函数仅在释放模式下通过指针返回值
Mat.at()
function just return value by pointer in release mode
template<typename _Tp> inline
_Tp& Mat::at(int i0, int i1)
{
CV_DbgAssert(dims <= 2);
CV_DbgAssert(data);
CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
return ((_Tp*)(data + step.p[0] * i0))[i1];
}
推荐答案
您没有提到您要为其开发的体系结构,因此无法告诉您要使用哪些内部函数。幸运的是,您的编译器应该能够自动向量化
You didn't mention what architecture you're developing for, so it's impossible to tell you which intrinsics to use. Luckily your compiler should be able to auto-vectorize something like
for (int i = 0 ; i < N ; i++)
foo[i] = (foo[i] == 4) ? 8 : foo[i];
假设您的数据已充分对齐,且 -mavx2 -O3
GCC将使用vpcmpeqd和vpblendvb。
Assuming your data is sufficiently aligned, with -mavx2 -O3
GCC will use vpcmpeqd and vpblendvb.
这篇关于是否可以使用SIMD指令进行替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!