vic*_*234 1 c++ algorithm replace simd
我有int的向量,我需要找到并替换一些具有特定值的元素.两者都是一样的.
例如:为所有元素替换4到8.
我在c ++中尝试循环内存访问.但它对我来说仍然很慢.
更新:
我正在使用OpenCV Mat对象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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mat.at() 函数只是在释放模式下通过指针返回值
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];
}
Run Code Online (Sandbox Code Playgroud)
您没有提到您正在开发的架构,因此无法告诉您使用哪些内在函数.幸运的是,您的编译器应该能够自动矢量化类似的东西
for (int i = 0 ; i < N ; i++)
foo[i] = (foo[i] == 4) ? 8 : foo[i];
Run Code Online (Sandbox Code Playgroud)
假设您的数据充分对齐,-mavx2 -O3GCC将使用vpcmpeqd和vpblendvb.