Note that there are some explanatory texts on larger screens.

plurals
  1. POAlignment, total size and SSE
    primarykey
    data
    text
    <p>I'm trying to <a href="http://pointclouds.org/documentation/tutorials/adding_custom_ptype.php" rel="nofollow">define a custom point type</a> for the <code>PCL</code> library. In that tutorial, they're talking about memory alignment, so I started off by trying to understand how it works.</p> <p>In <a href="http://www.geeksforgeeks.org/structure-member-alignment-padding-and-data-packing/" rel="nofollow">this page</a>, they present a rather simple way of calculating the total alignment of a structure. For example, this structure</p> <pre><code>// 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; </code></pre> <p>will have a size of 24: </p> <pre><code>1 byte for the char + 7 bytes of padding + 8 bytes for the double + 4 bytes for the int + 4 bytes of padding </code></pre> <p>and for <code>g++ 4.8.1</code>, <em>sizeof</em> returns 24. So far, so good.</p> <p>Now, in <code>PCL</code> they're defining the point types with this scheme (here's the most simple point, that holds the position in each axis) for <code>SSE</code> alignment.</p> <pre><code>union { float data[4]; struct { float x; float y; float z; }; }; </code></pre> <p><em>sizeof</em> returns 16. With the union it is made sure that the <code>point type</code> is SSE aligned (I read <a href="http://www.codeproject.com/Articles/4522/Introduction-to-SSE-Programming" rel="nofollow">here</a> that is 16 byte alignment) and with the struct the axis values are accessible.</p> <p>Quoting from the <code>PCL</code> docs:</p> <blockquote> <p>The user can either access points[i].data[0] or points[i].x for accessing say, the x coordinate.</p> </blockquote> <p>Is my reasoning valid until here?</p> <hr> <p>In my case, I want to change the floats for doubles in order to have more precision in the <code>X</code> and <code>Y</code> axis.</p> <p>So, is it enough to declare the point type as:</p> <pre><code>union { float data[4]; struct { double x; double y; float z; }; }; </code></pre> <p>? <em>sizeof</em> returns 24, which is not a multiple of 16 (so I understand it's not SSE aligned) but it is "double aligned".</p> <p>My question is, how can I define my point type to be able to store the <code>X</code> and <code>Y</code> coordinates as double and still be <code>SSE aligned</code>?</p> <p><strong>PS:</strong> Also, if any of you know of a good resource for this, please tell me. I want to understand better this topic.</p> <p><strong>PS 2:</strong> I forgot to tell, the platform I'm trying all of this is a 64 bit one.</p> <p><strong>PS 3:</strong> If possible, I'm interested in <code>pre-C++11</code> solutions. A compiler as old as <code>g++ 4.4</code> (and its <code>MinGW</code> counterpart) must be able to build the new point type.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload