pandoragami
pandoragami

Reputation: 5575

SSE1 Using divps intrinsic xmmintrin.h to divide four 32-bit floats and using printf?

I need help dividing four numbers as an example and printing them. I'm using g++ as my compiler. The following code does compile with the -msse3 -mmmx flags, I'm not even sure I need those but it works. I know I have to set the numbers with a function call before dividing but I'm not positive which function to call (I think the link has the set functions for only int). If there is a way to print the result using std::cout that would be better but printf works fine for this (I'm not sure if the print128_num is correct for this case, it was written for int originally). Heres the code.

#include <emmintrin.h>
#include <xmmintrin.h>
#include <stdio.h>
#include <stdint.h>

void print128_num(__m128i var)
{
    uint16_t *val = (uint16_t*) &var;
    printf("Numerical: %i %i %i %i %i %i %i %i \n",
       val[0], val[1], val[2], val[3], val[4], val[5],
       val[6], val[7]);
}
__m128 divide_4_32_bit_values(__m128 __A, __m128 __B)
{
    return _mm_div_ps (__A, __B);
}
int main(void)
{


    return 0;
}

Upvotes: 1

Views: 425

Answers (1)

Paul R
Paul R

Reputation: 213160

I've fixed a few problems and I think this now does what you want:

#include <xmmintrin.h>
#include <stdio.h>

void print128_num(const char * label, __m128 var)
{
    float *val = (float *) &var;
    printf("%s: %f %f %f %f\n",
       label, val[0], val[1], val[2], val[3]);
}

__m128 divide_4_32_bit_values(__m128 __A, __m128 __B)
{
    return _mm_div_ps (__A, __B);
}

int main(void)
{
    __m128 v1 = _mm_set_ps(4.0f, 3.0f, 2.0f, 1.0f);
    __m128 v2 = _mm_set_ps(1.0f, 2.0f, 3.0f, 4.0f);
    __m128 v = divide_4_32_bit_values(v1, v2);

    print128_num("v1", v1);
    print128_num("v2", v2);
    print128_num("v ", v);

    return 0;
}

Test:

$ gcc -Wall -msse3 m128_print.c 
$ ./a.out
v1: 1.000000 2.000000 3.000000 4.000000
v2: 4.000000 3.000000 2.000000 1.000000
v : 0.250000 0.666667 1.500000 4.000000
$ 

Upvotes: 3

Related Questions