Inline-Assembler-Code in C, copy values from Array to xmm

578 Views Asked by At

I have two Arrays and I want to get the dot product. How do I get the values of vek and vec into xmm0 and xmm1? And how do I get the Value standing in xmm1 (??) so that I can use it for "printf"?

#include <stdio.h>
main(){

float vek[4] = {4.0, 3.0, 2.0, 1.0};

float vec[4] = {1.0, 2.0, 3.0, 4.0};

asm(

"DPPS $0xFF, %xmm0, %xmm1"

??

);

printf( "Result: %f\n, ??)
}
1

There are 1 best solutions below

0
On

As @Mysticial rightly says, use intrinsics rather than raw assembler:

#include <stdio.h>
#include <smmintrin.h> // SSE 4.1

int main()
{

    __m128 vek = _mm_set_ps(4.0, 3.0, 2.0, 1.0);
    __m128 vec = _mm_set_ps(1.0, 2.0, 3.0, 4.0);

    __m128 result = _mm_dp_ps(vek, vec, 0xff);

    printf("result = { %vf }\n", result);

    return 0;
}

Note that not all compilers support the %v format specifier for SIMD values - if your compiler doesn't then you'll need to implement a suitable method for printing the result, e.g. use a union.