c++ - AVX intrinsic _mm256_rsqrt_ps 的相对误差比根据 intrinsics guide 应该有的要大得多

标签 c++ floating-point intrinsics avx

Intel Intrinsics Guide表示内在 _mm256_rsqrt_ps 的相对误差至多为 1.5*2^-12。但是,当我将 _mm256_rsqrt_ps 的结果与平方根倒数的标准 C++ 计算 (1.0/sqrt(x)) 进行比较时,我得到了一个相对误差大于 1.5*2^-12

我用下面的程序来测试这个:

#include <immintrin.h>
#include <iostream>
#include <math.h>

void test(float x) {
  float resP = _mm256_cvtss_f32(_mm256_rsqrt_ps(_mm256_set1_ps(x)));
  float res = 1.0 / sqrt(x);
  float relErr = fabs(resP - res) / res;
  std::cout << "x = " << x << std::endl;
  std::cout << "resP = " << resP << std::endl;
  std::cout << "res = " << res << std::endl;
  std::cout << "relErr = " << relErr << std::endl;
}

int main() {
  test(1e30);
  test(1e-30);
  test(1e17);
  test(1e-17);
}

输出如下:

    x = 1e+30
    resP = 1.00007e-15
    res = 1e-15
    relErr = 6.80803e-05
    x = 1e-30
    resP = 9.99868e+14
    res = 1e+15
    relErr = 0.0001316
    x = 1e+17
    resP = 3.16186e-09
    res = 3.16228e-09
    relErr = 0.000132569
    x = 1e-17
    resP = 3.16277e+08
    res = 3.16228e+08
    relErr = 0.000154825

如您所见,相对误差明显大于 1.5*2^-12

指令 _mm256_rcp_ps 的相对误差似乎也比内在函数指南所说的要大得多。

我做错了什么吗?我误解了内在指南吗?还是内部指导错误?

最佳答案

您的相对误差范围内。

1.5*2^-12 = 0.000366

它只是 2 的幂而不是 10 的幂。

也没有声称与单精度 1/sqrt(x) 相比有这种相对误差,但与精确结果相比。

关于c++ - AVX intrinsic _mm256_rsqrt_ps 的相对误差比根据 intrinsics guide 应该有的要大得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73867157/

相关文章:

c - SSE2 指令将一个 8x16 寄存器转换为两个具有偶数和奇数索引元素的 4x32 寄存器

C++:如何专门针对左值和右值

haskell - 在 Haskell 中获取数字的除数列表时出现问题

matlab - 计算Logistic损失函数的值AND梯度时避免数值溢出

c - Neon 内在功能是通过从所有元素中减去最小元素来防止溢出[无循环]

c++ - _mm_crc32 给出与手动版本不同的结果

c++ - 单个 XMLHTTPRequest 对象发出多个请求

c++ - Clang CMAKE 预编译 header

c++ - Qt Creator 在尝试运行 OpenCV 程序时崩溃。 [ntdll.dll 崩溃]

python - Python为什么要四舍五入?