I'm trying to define a custom point type for the PCL
library. In that tutorial, they're talking about memory alignment, so I started off by trying to understand how it works.
In this page, they present a rather simple way of calculating the total alignment of a structure. For example, this structure
// Alignment requirements
// (typical 32 bit machine)
// char 1 byte
// short int 2 bytes
// int 4 bytes
// double 8 bytes
// structure C
typedef struct structc_tag
{
char c;
double d;
int s;
} structc_t;
will have a size of 24:
1 byte for the char + 7 bytes of padding + 8 bytes for the double + 4 bytes for the int + 4 bytes of padding
and for g++ 4.8.1
, sizeof returns 24. So far, so good.
Now, in PCL
they're defining the point types with this scheme (here's the most simple point, that holds the position in each axis) for SSE
alignment.
union
{
float data[4];
struct
{
float x;
float y;
float z;
};
};
sizeof returns 16. With the union it is made sure that the point type
is SSE aligned (I read here that is 16 byte alignment) and with the struct the axis values are accessible.
Quoting from the PCL
docs:
The user can either access points[i].data[0] or points[i].x for accessing say, the x coordinate.
Is my reasoning valid until here?
In my case, I want to change the floats for doubles in order to have more precision in the X
and Y
axis.
So, is it enough to declare the point type as:
union {
float data[4];
struct {
double x;
double y;
float z;
};
};
? sizeof returns 24, which is not a multiple of 16 (so I understand it's not SSE aligned) but it is "double aligned".
My question is, how can I define my point type to be able to store the X
and Y
coordinates as double and still be SSE aligned
?
PS: Also, if any of you know of a good resource for this, please tell me. I want to understand better this topic.
PS 2: I forgot to tell, the platform I'm trying all of this is a 64 bit one.
PS 3: If possible, I'm interested in pre-C++11
solutions. A compiler as old as g++ 4.4
(and its MinGW
counterpart) must be able to build the new point type.