Tuesday, January 4, 2022

[SOLVED] What is the equivalent of v4sf and __attribute__ in Visual Studio C++?

Issue

typedef float v4sf __attribute__ ((mode(V4SF)));

This is in GCC. Anyone knows the equivalence syntax?

VS 2010 will show __attribute__ has no storage class of this type, and mode is not defined.

I searched on the Internet and it said

Equivalent to __attribute__( aligned( size ) ) in GCC

It is helpful for former unix developers or people writing code that works on multiple platforms that in GCC you achieve the same results using attribute( aligned( ... ) )

See here for more information: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Type-Attributes.html#Type-Attributes

The full GCC code is here: http://pastebin.com/bKkTTmH1


Solution

If you're looking for the alignment directive in VC++ it's __declspec(align(16)). (or whatever you want the alignment to be)

And example usage is this:

__declspec(align(16)) float x[] = {1.,2.,3.,4.};

http://msdn.microsoft.com/en-us/library/83ythb65.aspx

Note that both attribute (in GCC) and __declspec (in VC++) are compiler-specific extensions.

EDIT :

Now that I take a second look at the code, it's gonna take more work than just replacing the __attribute__ line with the VC++ equivalent to get it to compile in VC++.

VC++ doesn't have any if these macros/functions that you are using:

  • __builtin_ia32_xorps
  • __builtin_ia32_loadups
  • __builtin_ia32_mulps
  • __builtin_ia32_addps
  • __builtin_ia32_storeups

You're better off just replacing all of those with SSE intrinsics - which will work on both GCC and VC++.


Here's the code converted to intrinsics:

float *mv_mult(float mat[SIZE][SIZE], float vec[SIZE]) {
    static float ret[SIZE];
    float temp[4];
    int i, j;
    __m128 m, v, r;

    for (i = 0; i < SIZE; i++) {
        r = _mm_xor_ps(r, r);

        for (j = 0; j < SIZE; j += 4) {
            m = _mm_loadu_ps(&mat[i][j]);
            v = _mm_loadu_ps(&vec[j]);
            v = _mm_mul_ps(m, v);
            r = _mm_add_ps(r, v);
        }

        _mm_storeu_ps(temp, r);
        ret[i] = temp[0] + temp[1] + temp[2] + temp[3];
    }

    return ret;
}


Answered By - Mysticial