SSE版本的结果不同
Different result with SSE version
我正在尝试重写一些代码以利用SSE。然而,由于某种原因,我的SSE版本产生了与原始版本不同的结果,例如209.1而不是1.47等
为什么?整个函数可以在这里找到。
struct vec_ps
{
__m128 value;
vec_ps(){}
vec_ps(float value) : value(_mm_set1_ps(value)) {}
vec_ps(__m128 value) : value(value) {}
vec_ps(const vec_ps& other) : value(other.value) {}
vec_ps& operator=(const vec_ps& other)
{
value = other.value;
return *this;
}
vec_ps& operator+=(const vec_ps& other)
{
value = _mm_add_ps(value, other.value);
return *this;
}
vec_ps& operator-=(const vec_ps& other)
{
value = _mm_sub_ps(value, other.value);
return *this;
}
vec_ps& operator*=(const vec_ps& other)
{
value = _mm_mul_ps(value, other.value);
return *this;
}
vec_ps& operator/=(const vec_ps& other)
{
value = _mm_div_ps(value, other.value);
return *this;
}
static vec_ps load(float* ptr)
{
return vec_ps(_mm_load_ps(ptr));
}
static void stream(float* ptr, const vec_ps& other)
{
_mm_stream_ps(ptr, other.value);
}
void stream(float* ptr)
{
_mm_stream_ps(ptr, value);
}
};
vec_ps operator+(const vec_ps& lhs, const vec_ps& rhs)
{
return vec_ps(lhs) += rhs;
}
vec_ps operator-(const vec_ps& lhs, const vec_ps& rhs)
{
return vec_ps(lhs) -= rhs;
}
vec_ps operator*(const vec_ps& lhs, const vec_ps& rhs)
{
return vec_ps(lhs) *= rhs;
}
vec_ps operator/(const vec_ps& lhs, const vec_ps& rhs)
{
return vec_ps(lhs) /= rhs;
}
void foo(/*...*/)
{
std::vector<float, tbb::cache_aligned_allocator<float>> ref_mu(w*h);
std::vector<float, tbb::cache_aligned_allocator<float>> cmp_mu(w*h);
std::vector<float, tbb::cache_aligned_allocator<float>> ref_sigma_sqd(w*h);
std::vector<float, tbb::cache_aligned_allocator<float>> cmp_sigma_sqd(w*h);
std::vector<float, tbb::cache_aligned_allocator<float>> sigma_both(w*h);
int size = w*h*sizeof(float);
/*...*/
float ssim_sum = 0.0;
float ssim_sum2 = 0.0;
vec_ps ssim_sum_ps(0.0f);
for(int n = 0; n < size/16; ++n)
{
auto ref_mu_ps = vec_ps::load(ref_mu.data() + n*4);
auto cmp_mu_ps = vec_ps::load(cmp_mu.data() + n*4);
auto sigma_both_ps = vec_ps::load(sigma_both.data() + n*4);
auto ref_sigma_sqd_ps = vec_ps::load(ref_sigma_sqd.data() + n*4);
auto cmp_sigma_sqd_ps = vec_ps::load(cmp_sigma_sqd.data() + n*4);
auto numerator = (2.0f * ref_mu_ps * cmp_mu_ps + C1) * (2.0f * sigma_both_ps + C2);
auto denominator = (ref_mu_ps*ref_mu_ps + cmp_mu_ps*cmp_mu_ps + C1) * (ref_sigma_sqd_ps + cmp_sigma_sqd_ps + C2);
ssim_sum_ps += numerator / denominator;
}
for(int n = 0; n < 4; ++n)
ssim_sum2 += ssim_sum_ps.value.m128_f32[n];
for (int y = 0; y < h; ++y)
{
int offset = y*w;
for (int x = 0; x < w; ++x, ++offset)
{
float numerator = (2.0f * ref_mu[offset] * cmp_mu[offset] + C1) * (2.0f * sigma_both[offset] + C2);
float denominator = (ref_mu[offset]*ref_mu[offset] + cmp_mu[offset]*cmp_mu[offset] + C1) * (ref_sigma_sqd[offset] + cmp_sigma_sqd[offset] + C2);
ssim_sum += numerator / denominator;
}
}
assert(ssim_sum2 == ssim_sum); // FAILS!
}
只是上面的评论,因为它似乎是这个问题的答案:有什么保证w*h可以被四整除吗?如果不是这样的话,SSE版本中的最后一次迭代将基于随机数。在一个地方使用sizeof(float),在另一个地方用16而不是4*sizeof(浮点)`有点令人困惑:为什么不去掉浮点的大小呢?此外,为什么非SSE版本不只是在区域上运行,而不是试图遵循矩阵的宽度和高度?
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 导入库可以跨dll版本工作吗
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 序列化,没有库的整数,得到奇怪的结果
- _mm_crc32给出与手动版本不同的结果
- 功能模板与Variadic模板过载:Intel C 编译器版本18产生的结果不同于其他编译器.英特尔是错误的
- 在发布版本中划分两个双精度的结果错误
- 用不同版本的Ubuntu上的GCC编译会产生不同的结果
- 使用不同版本编译的相同代码提供不同的结果
- OpenCL - 结果与 CPU 版本不同
- 顺序和并行版本给出不同的结果 - 为什么
- SSE版本的结果不同
- 为什么sprintf_s在不同版本的Visual Studio中给出不同的结果?