[ODE] Why is dVector3 defined as dReal[4] ???

Michal Bacik michal at lonelycatgames.com
Thu Feb 13 04:24:01 2003


> In physics a SIMD optimization is something that will happen sooner
> or later so better plan for it. Things you can run into
> otherwise when using only 3 floats is that all your routines assume
> thats the
> case so you do a "pointer += 3" and other things like that. Now if you
> later
> would change that to 4 floats per vector you would have a very hard time
> finding
> all those cases where you move through the float arrays. Missing just
> one
> could cause you a world of strange problems.
> ...

You're right. Maybe the wrong concept is typedef-ing dVector3 as an array of
floats, rather than more elegant:

struct dVector3{
   dReal x, y, z;
   dReal _pad;
#ifdef __cplusplus
   dReal &operator[](int);
#endif
};

(Here, the clear advantage is using C++, because C users won't have
array-access operator[], which works now with the array concept.)

Then no user would use "pointer += 3", but rather "++pointer" (assuming
'pointer' points to dVector3, not to dReal). In general, it's bad practice
to depend on implementation details though, assuming the dVector3 has or has
not the 'padding' element.

- michal