{"id":85,"date":"2011-02-16T16:45:00","date_gmt":"2011-02-16T16:45:00","guid":{"rendered":"https:\/\/www.root42.de\/blog\/?p=85"},"modified":"2011-02-16T16:45:00","modified_gmt":"2011-02-16T16:45:00","slug":"alignment-of-double-values-on-arm-architectures","status":"publish","type":"post","link":"https:\/\/www.root42.de\/blog\/?p=85","title":{"rendered":"Alignment of double values on ARM architectures"},"content":{"rendered":"<p>Consider the following code:<\/p>\n<pre>class C {<br \/>...<br \/>uint8_t *data;<br \/>size_t size;<br \/>...<br \/><br \/>void writeDouble(double v)<br \/>{<br \/>    ...<br \/>    reinterpret_cast&lt;double*&gt;(this-&gt;data)[this-&gt;size] = v;<br \/>    this-&gt;size += sizeof(double);<br \/>    ...<br \/>}<br \/><br \/>};<br \/><\/pre>\n<p>Looks harmless enough, doesn&#8217;t it? But here is the problem: It&#8217;s not necessarily portable or well behaving code. After debugging for about three hours, I found out something interesting. Namely that on ARM platforms, such as the iPhone, double values on the stack need to be stored at <a href=\"http:\/\/infocenter.arm.com\/help\/index.jsp?topic=\/com.arm.doc.faqs\/ka4127.html\">8 byte aligned memory addresses<\/a>. This was some existing code I was using, so it took me a while to get to this function and to its problem. Single precision float values have no such restriction, by the way. There are two workarounds in the above case. 1) Write a small for-loop that casts the double value to a <span style=\"font-family: 'Courier New', Courier, monospace;\">uint8_t *<\/span> and copy the double value byte-wise, or 2) use malloc to copy the value. I did the for-loop, since I thought that maybe the malloc call has too much overhead. I guess you could also cast the double to a <span style=\"font-family: 'Courier New', Courier, monospace;\">uint32_t *<\/span> pointing to two 32 bit words. Anyway, take care when doing such drastic casts. Some platforms might want alignment for some datatypes!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider the following code: class C {&#8230;uint8_t *data;size_t size;&#8230;void writeDouble(double v){ &#8230; reinterpret_cast&lt;double*&gt;(this-&gt;data)[this-&gt;size] = v; this-&gt;size += sizeof(double); &#8230;}}; Looks harmless enough, doesn&#8217;t it? But here is the problem: It&#8217;s not necessarily portable or well behaving code. After debugging for about three hours, I found out something interesting. Namely that on ARM platforms, such as &hellip; <a href=\"https:\/\/www.root42.de\/blog\/?p=85\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Alignment of double values on ARM architectures&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,62,58,46],"tags":[],"_links":{"self":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/85"}],"collection":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85"}],"version-history":[{"count":0,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}