Memory alignment question

Started by
5 comments, last by Antheus 12 years, 5 months ago
If I write a Vector3 class and declare the x,y,z members as _declspec(allign_16) (I might have got the syntax wrong there). Do member functions count towards data size? I want to have a dot product, and a cross product, and some other utility functions. Would I have to make sure the functions were aligned as well? And also, how do you make vectors take advantage of SIMD?
Advertisement
Member functions does not affect the size of the object.

If I write a Vector3 class and declare the x,y,z members as _declspec(allign_16) (I might have got the syntax wrong there). Do member functions count towards data size? I want to have a dot product, and a cross product, and some other utility functions. Would I have to make sure the functions were aligned as well? And also, how do you make vectors take advantage of SIMD?


A way to find out for yourself :)


In your code:
1. Create 2 Vector3's on the stack as an array (Vector3 v[2])
2. Set the Vector3 x, y, zs to unique values (e.g. v1 = {1, 2, 3} v2 = {4, 5, 6})
3. (optional but might be less confusing) Get the address of the 1st element of the Vector3 (&(v[0])) array and set it to some variable

Debugging:
4. Set a breakpoint right after the above code.
5. Debug your application

Viewing memory:
6. When the breakpoint hits, open Memory View. (Debug -> Windows -> Memory)
7. Drag the address of the 1st element in the Vector3 array into the Memory View's address textbox.
8. Right click in the middle of the Memory View (the area with a whole bunch of numbers) and select your data format (typically 32 bit, signed for a Vector3)
9. Look at how 1, 2, 3, 4, 5, 6 are laid out. Padding / alignment are clearly shown.
This might be a good time to research how member functions work. Essentially, non-virtual member functions are almost exactly the same as a stand-alone or "free" function. You can think of a member function as a free function with an implicit "this" pointer parameter to the type. The "this" pointer would be const if the member function is const. The main difference is that member functions can access the private and protected members of the type.

If you're interested in how virtual member functions work, pretty much all compilers implement them with virtual member function lookup tables, or "vtable". Googling that term will be illuminating.

This might be a good time to research how member functions work. Essentially, non-virtual member functions are almost exactly the same as a stand-alone or "free" function. You can think of a member function as a free function with an implicit "this" pointer parameter to the type. The "this" pointer would be const if the member function is const. The main difference is that member functions can access the private and protected members of the type.

If you're interested in how virtual member functions work, pretty much all compilers implement them with virtual member function lookup tables, or "vtable". Googling that term will be illuminating.


Ah. I see now. How can I make my vector class take advantage of SIMD instruction sets?

[quote name='rip-off' timestamp='1319015802' post='4874214']
This might be a good time to research how member functions work. Essentially, non-virtual member functions are almost exactly the same as a stand-alone or "free" function. You can think of a member function as a free function with an implicit "this" pointer parameter to the type. The "this" pointer would be const if the member function is const. The main difference is that member functions can access the private and protected members of the type.

If you're interested in how virtual member functions work, pretty much all compilers implement them with virtual member function lookup tables, or "vtable". Googling that term will be illuminating.


Ah. I see now. How can I make my vector class take advantage of SIMD instruction sets?
[/quote]

Let the compiler do it's job.


Let the compiler do it's job.


Which will make it shrug and move on.


SSE design. Follow the links from there on previous articles and stuff on gamasutra and original site.

Then just use the library from github, it's a solved problem.

This topic is closed to new replies.

Advertisement