Sign in to follow this  
  • entries
    17
  • comments
    21
  • views
    17260

Found How to use member references today =)

Sign in to follow this  

239 views

I was looking into adding more SSE support to my engine today, and finally found how to have Member Variable References.

You see, my CVector3 class is defined with float x,y,z member variables, and x,y,z is the way I like to use when manipulating Vectors, however some stuff can be optimized by having the variables defines as float v[3].

I dont want to use a union because that cluters the class too much, (I mean who wants code that looks like: Normal.u.v[2]), so I have been looking to use references for a while.

Turns out it is NOT hard at all, all you need to do is set the references on the constructor Initialization list, without further ado I give you the dual vector3 class:


#ifndef ALIGN16
#ifdef __GNUC__
#define ALIGN16 __attribute__ ((aligned (16)))
#elif _MSC_VER
#define ALIGN16 __declspec(align(16))
#else
#define ALIGN16
#endif
#endif

class vector3
{
vector3() : x(v[0]),y(v[1]),z(v[2]) {};
public:
ALIGN16 float v[3];
float &x;
float &y;
float &z;
};






Now you can amaze your friends by accessing the vector by its x,y,z references or by using the powerfull SSE MOVAPS instruction to load your vector back and forth to a SSE register


__asm__
(
"movaps %0,%%xmm0\n\t"
"movaps %1,%%xmm1\n\t"
"mulps %%xmm1,%%xmm0\n\t"
"movaps %%xmm0,%2\n\t"
:
: "m" (vector1.v),"m" (vector2.v),"m" (vector3.v)
: "%xmm0","%xmm1"
);






That Multiplies vector1 and vector2 and saves the result into vector3 (I Think [smile])

Edit: This will add at least 12 bytes to each object, so may not be a good idea memory wise [sad], if you can live with that, go for it.

More Edit:

Well What do you know, this approach gives you a 32 byte vector object, whereas if you just omit the references and use ALIGN16 float x,y,z, you get a 48 byte object, the ALIGN16 float[3] would probably give you the best size for doing SSE stuff though.

I am not losing my time converting over now, but I might later.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now