【发布时间】:2023-03-26 17:26:01
【问题描述】:
我从未为 SSE 优化编写过汇编代码,如果这是一个菜鸟问题,请见谅。在this aritcle 中解释了如何使用条件语句向量化for。但是,我的代码(取自 here)的形式是:
for (int j=-halfHeight; j<=halfHeight; ++j)
{
for(int i=-halfWidth; i<=halfWidth; ++i)
{
const float rx = ofsx + j * a12;
const float ry = ofsy + j * a22;
float wx = rx + i * a11;
float wy = ry + i * a21;
const int x = (int) floor(wx);
const int y = (int) floor(wy);
if (x >= 0 && y >= 0 && x < width && y < height)
{
// compute weights
wx -= x; wy -= y;
// bilinear interpolation
*out++ =
(1.0f - wy) * ((1.0f - wx) * im.at<float>(y,x) + wx * im.at<float>(y,x+1)) +
( wy) * ((1.0f - wx) * im.at<float>(y+1,x) + wx * im.at<float>(y+1,x+1));
} else {
*out++ = 0;
}
}
}
因此,据我了解,与链接的文章有几个不同之处:
- 这里我们有一个嵌套的
for:我一直在 vectroization 中看到一层for,从未见过嵌套循环 - if 条件基于标量值(x 和 y)而不是数组:如何使链接的示例适应这种情况?
-
out索引不是基于i或j(所以它不是out[i]或out[j]):如何以这种方式填充out?
特别是我很困惑,因为for 索引总是用作数组索引,而这里用于计算变量,而向量是循环递增的
我正在使用 icpc 和 -O3 -xCORE-AVX2 -qopt-report=5 以及一堆其他优化标志。根据 Intel Advisor 的说法,这不是矢量化的,使用 #pragma omp simd 会生成 warning #15552: loop was not vectorized with "simd"
【问题讨论】:
-
您使用哪个编译器?您是否确认您的编译器尚未为您自动矢量化?
-
@Jonas 感谢您的评论。请看我更新的问题
-
很像你之前的问题stackoverflow.com/questions/43136182/…有什么变化?
-
@RichardCritten 哦,天哪,我完全忘记了那个(你应该明白这让我发疯了),我刚刚删除了它
-
@RichardCritten 我试着写更多关于它让我更困惑的东西
标签: c++ x86 vectorization sse simd