Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Found How to use member references today =)

Sign in to follow this  


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))
#define ALIGN16

class vector3
vector3() : x(v[0]),y(v[1]),z(v[2]) {};
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

"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  


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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!