Vector Math Class Storage: Array vs. Separate Members

Started by
16 comments, last by Ectara 11 years, 4 months ago

[quote name='Ectara' timestamp='1355936352' post='5012489']
I've seen this solution, before, and despite it looking fancy, it ignores what was also stated above "Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1)." It may be implementation defined to have 4 byte int members aligned to 8 byte boundaries. Such a machine that requires it would be inefficient, but that makes no difference in the fact that the standard allows for an implementation that would cause this example to break. Thus, I've avoided it.


What's harder to imagine is an architecture that would require alignment of members of the same type different from the alignment required of elements in an array. That's why I said that they will both work in practice.

If you worry that your compiler might put an infinite loop before returning from main because the standard allows it (at least that's my reading of the section on "observable behavior"), then you may also worry about a compiler putting padding between members of the same basic type. Otherwise, not really something to stress about.
[/quote]
I agree with you, you won't likely encounter this behavior. I'm just saying, if I have a choice between standards compliance and not standards compliance, and they both produce the same results for roughly the same cost, then I have no reason to be non-compliant.
Advertisement

[quote name='iMalc' timestamp='1355899486' post='5012351']
Ah, I've held onto this link for many years, for just such an occasion.
In this thread you'll find the perfect answer I believe. You can have your cake and eat it too...
http://www.gamedev.n...ick-trick-in-c/

The content looks interesting, but I'm having a hard time understanding how the pointer array winds up pointing to the actual data, unless I'm misreading it.
[/quote]

I just tried this and it seems to work:
#include <iostream>

struct Vector {
float x, y, z;
static float Vector::* const a[3];

float operator[](size_t i) const {
return this->*a;
}

float &operator[](size_t i) {
return this->*a;
}
};

float Vector::* const Vector::a[3] = {&Vector::x, &Vector::y, &Vector::z};

int main() {
Vector v = {1.0, 2.0, 3.0};
for (size_t i=0; i!=3; ++i)
std::cout << v << '\n';
}

The content looks interesting, but I'm having a hard time understanding how the pointer array winds up pointing to the actual data, unless I'm misreading it.

The important thing is that they aren't regular pointers, they are pointers to members. Pointers to members are effectively offsets into the class. So &Vector::x would resolve to an offset to the beginning of the class, &Vector::y would be something like 4 bytes into the class and &Vector::z would be something like 8 bytes into the class. The ->* operator then takes a pointer to a class object and uses the pointer to member to get at the member at that offset.

I just tried this and it seems to work:


[quote name='Ectara' timestamp='1355937014' post='5012496']
The content looks interesting, but I'm having a hard time understanding how the pointer array winds up pointing to the actual data, unless I'm misreading it.

The important thing is that they aren't regular pointers, they are pointers to members. Pointers to members are effectively offsets into the class. So &Vector::x would resolve to an offset to the beginning of the class, &Vector::y would be something like 4 bytes into the class and &Vector::z would be something like 8 bytes into the class. The ->* operator then takes a pointer to a class object and uses the pointer to member to get at the member at that offset.
[/quote]
Hm... I guess it's something that has no analogue in C. I was curious about ->* being its own operator, and being overloadable. I'll have to look more into this concept. Thank you all, for the new information. This may allow me to implement something new.
The closest C analogue to pointers to members is the offsetof() macro, though in order to do anything useful with offsetof() you need to do manual pointer manipulation that ->* takes care of for you in C++.

[quote name='Álvaro' timestamp='1355502452' post='5010648']
I think either method would work fine, and in practice you wouldn't have to worry about things like padding or alignment. The standard only guarantees that method 2 would work. Whether that's important to you is mostly a matter of personal preference.

I plan to use SSE or whichever intrinsics are available if the processor has it, so there's a chance that the data must be non-padded and 16 byte aligned to use SSE, for example. Sounds like method 2 is the way to go for me; I try to go for standards compliance when possible.
[/quote]
If you are moving to SSE why even implement this or care about it's speed, as moving from SSE to FPU involves a LHS anyway. Most libraries i have seen just use the SSE intrinsics for the platform and have an array version for normal vectors and point types, no alligned allocations required. However they all caution to not move from the SSE intrinsics when you can.

Optimised C++ uses the SSE instructions anyway(when enabled in settings) as soon as you do any floating point work. Even if it is just a lonely dived where you just need to float result, this confused my when debugging a release build for something else.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion


If you are moving to SSE why even implement this or care about it's speed, as moving from SSE to FPU involves a LHS anyway.


Last time I checked LHS isn't a significant issue unless you happen to be using a PowerPC based processor...

If you are moving to SSE why even implement this or care about it's speed, as moving from SSE to FPU involves a LHS anyway. Most libraries i have seen just use the SSE intrinsics for the platform and have an array version for normal vectors and point types, no alligned allocations required. However they all caution to not move from the SSE intrinsics when you can.

I don't understand what you are saying. I have a math vector that uses SSE if it can, if not, then it uses the same interface to do all of the calculations normally. It uses compiler intrinsics for SSE. This is worth implementing, and worth care. Just about all of the operations will be done SSE-side, anyway; all operations in the SSE specialization will use SSE only. I've already written it, and the FPU gets no real use after initializing the vector, until the results are inspected, where it gets loaded back into FPU registers to operate upon each element outside of the vector.

In short, the vector isn't the problem here. There will not be a bottleneck until another part of the code decides it needs to read or write the floating point data individually, which will have the penalties that you describe. However, if one is going to do so few mathematical operations that more time is spent converting or moving between processing units on the processor, then they probably should just do the math normally, without a specialized, high-speed vector class. In my opinion, when the code that needs these actions performed incurs this penalty, it accepts the risks and the responsibility. If it ever gets to the point that the vector is the true bottleneck, not anything that uses it, then I will take action.


Optimised C++ uses the SSE instructions anyway(when enabled in settings) as soon as you do any floating point work. Even if it is just a lonely dived where you just need to float result, this confused my when debugging a release build for something else.

The current version of GCC emits aligned SSE instructions even if the data is unaligned, causing the program to crash on the highest two optimization settings. Relying on the compiler to do the heavy lifting is not always an option. The compiler can fail, and is failing right now for the very case you state.

This topic is closed to new replies.

Advertisement