# Memory alignment question

This topic is 2917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
Member functions does not affect the size of the object.

##### Share on other sites

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

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.

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.

##### Share on other sites
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.

##### Share on other sites

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?

##### Share on other sites

[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.

##### Share on other sites

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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 14
• 30
• 9
• 16
• 22