This question already has an answer here:
How does this function compute the absolute value of a float through a NOT and AND operation?
                                
                                    (1个答案)
                                
                        
                                5年前关闭。
            
                    
我们正在尝试制作一个包含不同操作的向量内在库,其中之一是获取数字的绝对值。但是,我的教授将其仅限于double

我对x86内在指令集还很陌生,所以我希望有人能启发我。

这是我到目前为止的内容:

 void vectorAbs(double *x, double *y, unsigned int N);
 int main()
 {
     double x[] = { -1, -2, -3, -4, -5, -6 };
     double y[] = { 2, 2, 2, 2, 2, 2 };
     double *pX = x, *pY = y;

     vectorAbs(pX, pY, 6);
 }

 void vectorAbs(double *x, double *y, unsigned int N)
 {
     __m128d xVar;
     __m128d yVar;

     printf("\nSquare of x : \n");
     for (int i = 0; i < N; i += 2)
     {
       xVar = _mm_loadu_pd(&x[i]);  // load *x[i] to xVar

       yVar = _mm_abs_epi16(xVar); // abs of x
       _mm_storeu_pd(&y[i], yVar); // store yVar to y[i]

       printf("%lf, %lf, ", y[i], y[i + 1]);
     }
     system("pause");

}


我得到的错误是:


  没有运算符“ =”匹配这些操作数
  
  操作数类型为:__m128d = __m128i

最佳答案

您需要做的就是清除向量中两个double值的符号位。每个双精度符号的符号位在矢量位63和127中。这可以通过使用内在函数_mm_and_pd用一条指令(andpd)完成。另一种方法是将两个双打逻辑左移一位,然后右移一位。可以使用_mm_slli_epi64和_mm_srli_epi64内部函数并行移动这两个值。这是一个例子:

#include <stdio.h>
#include <stdlib.h>
#include <intrin.h>

 void vectorAbs(double *x, double *y, unsigned int N);
 int main()
 {
     double x[] = { -1, -2, -3, -4, -5, -6 };
     double y[] = { 2, 2, 2, 2, 2, 2 };
     double *pX = x, *pY = y;

     vectorAbs(pX, pY, 6);
 }

__m128d abs_sample1 (__m128d val)
    {
    return _mm_castsi128_pd (_mm_srli_epi64 (_mm_slli_epi64 (_mm_castpd_si128 (val), 1), 1));
    }

__m128d abs_sample2 (__m128d val)
    {
    const __m128d mask = _mm_castsi128_pd (_mm_set1_epi64x (0x7FFFFFFFFFFFFFFF));
    return _mm_and_pd (mask, val);
    }

 void vectorAbs(double *x, double *y, unsigned int N)
 {
     __m128d xVar;
     __m128d yVar;

     printf("\nSquare of x : \n");
     for (int i = 0; i < N; i += 2)
     {
       xVar = _mm_loadu_pd(&x[i]);  // load *x[i] to xVar

       yVar = abs_sample1(xVar); // abs of x
       _mm_storeu_pd(&y[i], yVar); // store yVar to y[i]
       printf("%lf, %lf, ", y[i], y[i + 1]);
     }
     system("pause");

}

关于c++ - 如何使用内在函数对double执行绝对值? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25590602/

10-11 18:28