c++ - 高斯模糊的SSE优化

标签 c++ optimization sse simd gaussianblur

我正在做一个学校项目,我必须优化 SSE 中的部分代码,但我现在在一个部分停留了几天。

我没有看到在此代码(它是高斯模糊算法的一部分)中使用 vector SSE 指令(内联汇编程序/instric f)的任何聪明方法。如果有人能给我一个小提示,我会很高兴

for (int x = x_start; x < x_end; ++x)     // vertical blur...
    {
        float sum = image[x + (y_start - radius - 1)*image_w];
        float dif = -sum;

        for (int y = y_start - 2*radius - 1; y < y_end; ++y)
        {                                                   // inner vertical Radius loop           
            float p = (float)image[x + (y + radius)*image_w];   // next pixel
            buffer[y + radius] = p;                         // buffer pixel
            sum += dif + fRadius*p;
            dif += p;                                       // accumulate pixel blur

            if (y >= y_start)
            {
                float s = 0, w = 0;                         // border blur correction
                sum -= buffer[y - radius - 1]*fRadius;      // addition for fraction blur
                dif += buffer[y - radius] - 2*buffer[y];    // sum up differences: +1, -2, +1

                // cut off accumulated blur area of pixel beyond the border
                // assume: added pixel values beyond border = value at border
                p = (float)(radius - y);                   // top part to cut off
                if (p > 0)
                {
                    p = p*(p-1)/2 + fRadius*p;
                    s += buffer[0]*p;
                    w += p;
                }
                p = (float)(y + radius - image_h + 1);               // bottom part to cut off
                if (p > 0)
                {
                    p = p*(p-1)/2 + fRadius*p;
                    s += buffer[image_h - 1]*p;
                    w += p;
                }
                new_image[x + y*image_w] = (unsigned char)((sum - s)/(weight - w)); // set blurred pixel
            }
            else if (y + radius >= y_start)
            {
                dif -= 2*buffer[y];
            }
        } // for y
    } // for x

最佳答案

  1. 您可以使用的另一个功能是逻辑运算和掩码:

例如代替:

  // process only 1
if (p > 0)
    p = p*(p-1)/2 + fRadius*p;

你可以写

  // processes 4 floats
const __m128 &mask = _mm_cmplt_ps(p,0);
const __m128 &notMask = _mm_cmplt_ps(0,p);
const __m128 &p_tmp = ( p*(p-1)/2 + fRadius*p );
p = _mm_add_ps(_mm_and_ps(p_tmp, mask), _mm_and_ps(p, notMask)); // = p_tmp & mask + p & !mask
  1. 我还可以推荐您使用一个特殊的库,它会重载指令。例如:http://code.compeng.uni-frankfurt.de/projects/vc

  2. dif 变量使内部循环的迭代依赖。您应该尝试并行化外循环。但是如果没有重载指令,代码将变得难以管理。

  3. 同时考虑重新考虑整个算法。目前的看起来并不平行。也许您可以忽略精度,或者稍微增加标量时间?

关于c++ - 高斯模糊的SSE优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20666109/

相关文章:

c++ - g++ -MG 标志的意外行为

python - 根查找偶尔会失败。 fSolve 可以给出二维优化的限制吗?

python - 使用字符串匹配慢来切片 Pandas 行

sse - 所有支持 AVX2 的 CPU 也都支持 SSE4.2 和 AVX 吗?

c++ - ProcessExit - DLL 卸载和静态的顺序

c++ - 在 Windows 构建中使用 pthread.h

c++ - 使用固定大小类型时强制位字段符号 (pre-C++14)

javascript - IIS/.NET的一些类似JAWR的软件(consolidate and minify Javascript)

c - C 中的 SSE 编程 — 大小不能被 4 整除怎么办?

c - 将 4 个整数同时反向相乘