Found How to use member references today =)

Published June 07, 2005
Advertisement
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#endifclass 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.
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement