c++ - 使用 SSE 内在函数编译一个简单的 c++ 程序

标签 c++ x86 sse simd

我是 SSE 说明的新手,我试图从这个网站学习它们: http://www.codeproject.com/Articles/4522/Introduction-to-SSE-Programming

我在 Ubuntu 10.10 和 Intel Core i7 960 CPU 上使用 GCC 编译器

这是基于我尝试过的文章的代码:

对于长度为 ARRAY_SIZE 的两个数组,它计算

fResult[i] = sqrt( fSource1[i]*fSource1[i] + fSource2[i]*fSource2[i] ) + 0.5

这是代码

#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <xmmintrin.h> // Contain the SSE compiler intrinsics
#include <malloc.h>
void myssefunction(
          float* pArray1,                   // [in] first source array
          float* pArray2,                   // [in] second source array
          float* pResult,                   // [out] result array
          int nSize)                        // [in] size of all arrays
{
    int nLoop = nSize/ 4;

    __m128 m1, m2, m3, m4;

    __m128* pSrc1 = (__m128*) pArray1;
    __m128* pSrc2 = (__m128*) pArray2;
    __m128* pDest = (__m128*) pResult;


    __m128 m0_5 = _mm_set_ps1(0.5f);        // m0_5[0, 1, 2, 3] = 0.5

    for ( int i = 0; i < nLoop; i++ )
    {
        m1 = _mm_mul_ps(*pSrc1, *pSrc1);        // m1 = *pSrc1 * *pSrc1
        m2 = _mm_mul_ps(*pSrc2, *pSrc2);        // m2 = *pSrc2 * *pSrc2
        m3 = _mm_add_ps(m1, m2);                // m3 = m1 + m2
        m4 = _mm_sqrt_ps(m3);                   // m4 = sqrt(m3)
        *pDest = _mm_add_ps(m4, m0_5);          // *pDest = m4 + 0.5

        pSrc1++;
        pSrc2++;
        pDest++;
    }
}

int main(int argc, char *argv[])
{
  int ARRAY_SIZE = atoi(argv[1]);
  float* m_fArray1 = (float*) _aligned_malloc(ARRAY_SIZE * sizeof(float), 16);
  float* m_fArray2 = (float*) _aligned_malloc(ARRAY_SIZE * sizeof(float), 16);
  float* m_fArray3 = (float*) _aligned_malloc(ARRAY_SIZE * sizeof(float), 16);

  for (int i = 0; i < ARRAY_SIZE; ++i)
    {
      m_fArray1[i] = ((float)rand())/RAND_MAX;
      m_fArray2[i] = ((float)rand())/RAND_MAX;
    }

  myssefunction(m_fArray1 , m_fArray2 , m_fArray3, ARRAY_SIZE);

  _aligned_free(m_fArray1);
   _aligned_free(m_fArray2);
   _aligned_free(m_fArray3);

  return 0;
}

我得到以下编译错误

[Programming/SSE]$ g++ -g -Wall -msse sseintro.cpp 
sseintro.cpp: In function ‘int main(int, char**)’:
sseintro.cpp:41: error: ‘_aligned_malloc’ was not declared in this scope
sseintro.cpp:53: error: ‘_aligned_free’ was not declared in this scope
[Programming/SSE]$ 

我哪里搞砸了?我是否缺少一些头文件?我似乎已经包括了所有相关的内容。

最佳答案

_aligned_malloc_aligned_free是微软主义。使用 posix_memalignmemalign在 Linux 上。对于 Mac OS X,您可以只使用 malloc,因为它始终是 16 字节对齐的。对于可移植的 SSE 代码,您通常希望为对齐的内存分配实现包装函数,例如

void * malloc_simd(const size_t size)
{
#if defined WIN32           // WIN32
    return _aligned_malloc(size, 16);
#elif defined __linux__     // Linux
    return memalign(16, size);
#elif defined __MACH__      // Mac OS X
    return malloc(size);
#else                       // other (use valloc for page-aligned memory)
    return valloc(size);
#endif
}

free_simd 的实现留给读者作为练习。

关于c++ - 使用 SSE 内在函数编译一个简单的 c++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12055822/

相关文章:

c++ - C++ 标准是否允许对具有 const 成员的 POD 对象进行零初始化?

c++ - 如何在 C++ 中处理字符串中的非 ASCII 字符?

c - 高效的整数比较函数

assembly - 如何在汇编中播种随机数生成器?

c++ - 如何在 GCC x86 中使用 RDTSC 计算时钟周期?

opencv - OpenCV 中的 Mat 矩阵和 SSE 的 16 字节对齐

c++ - 如果在生成 std​​::thread 后引发异常,则未捕获异常

c++ - 使用父类代码实现纯虚函数

c# - 缺少 System.Numerics.Vectors.Vector<T>

c++ - 使用 RGB 源和 RGBA 叠加实现近乎实时的 CPU 功能,如 glAlphaFunc(GL_GREATER)