I was wondering how I can turn this C code into C ++ for memory alignment.
float *pResult = (float*) _aligned_malloc(length * sizeof(float), 16);
I looked here and then I tried this
float *pResult = (float*) __attribute__((aligned(16)));
and this one
float *pResult = __attribute__((aligned(16)));
but both of them gave similar errors.
error: expected primary-expression before '__attribute__'|
error: expected ',' or ';' before '__attribute__'|
Full code
#include "stdafx.h"
#include <xmmintrin.h> // Need this for SSE compiler intrinsics
#include <math.h> // Needed for sqrt in CPU-only version
#include "stdio.h"
int main(int argc, char* argv[])
{
printf("Starting calculation...\n");
const int length = 64000;
float *pResult = (float*) __attribute__((aligned(16)));
__m128 x;
__m128 xDelta = _mm_set1_ps(4.0f);
__m128 *pResultSSE = (__m128*) pResult;
const int SSELength = length / 4;
for (int stress = 0; stress < 100000; stress++)
{
#define TIME_SSE
#ifdef TIME_SSE
x = _mm_set_ps(4.0f, 3.0f, 2.0f, 1.0f);
for (int i=0; i < SSELength; i++)
{
__m128 xSqrt = _mm_sqrt_ps(x);
#define USE_DIVISION_METHOD
#ifdef USE_FAST_METHOD
__m128 xRecip = _mm_rcp_ps(x);
pResultSSE[i] = _mm_mul_ps(xRecip, xSqrt);
#endif
#ifdef USE_DIVISION_METHOD
pResultSSE[i] = _mm_div_ps(xSqrt, x);
#endif
x = _mm_add_ps(x, xDelta);
}
#endif
#ifndef TIME_SSE
float xFloat = 1.0f;
for (int i=0 ; i < length; i++)
{
pResult[i] = sqrt(xFloat) / xFloat;
xFloat += 1.0f;
}
#endif
}
for (int i=0; i < 20; i++)
{
printf("Result[%d] = %f\n", i, pResult[i]);
}
return 0;
}
source
share