<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Exchange Server">
<!-- converted from text --><style><!-- .EmailQuote { margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; } --></style>
</head>
<body>
<meta content="text/html; charset=UTF-8">
<style type="text/css" style="">
<!--
p
        {margin-top:0;
        margin-bottom:0}
-->
</style>
<div dir="ltr">
<div id="x_divtagdefaultwrapper" dir="ltr" style="font-size:12pt; color:#000000; font-family:Calibri,Helvetica,sans-serif">
<p>Thanks Guys,</p>
<p><br>
</p>
<p> using memcpy resolves this.</p>
<p><br>
</p>
<p>Thanks.<br>
</p>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="x_divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Tim Northover <t.p.northover@gmail.com><br>
<b>Sent:</b> 10 August 2017 14:57:24<br>
<b>To:</b> Dan Walmsley<br>
<b>Cc:</b> mats petersson; cfe-dev@lists.llvm.org<br>
<b>Subject:</b> Re: [cfe-dev] Cortex-M4F Assembly Code Generation Incorrect for float casting, causing HardFaults</font>
<div> </div>
</div>
</div>
<font size="2"><span style="font-size:10pt;">
<div class="PlainText">On 10 August 2017 at 07:25, Dan Walmsley via cfe-dev<br>
<cfe-dev@lists.llvm.org> wrote:<br>
> So its not currently possible to read a uint32 value from a buffer at an unaligned position. This should work shouldn’t it?<br>
<br>
People have already pointed out that you're violating alignment<br>
requirements which makes the code wrong. There's actually a second<br>
issue called "strict aliasing". Essentially, if you have a buffer or<br>
other object of one type it's invalid to cast a pointer to a different<br>
type and access it even if the alignment requirements are met.<br>
<br>
The portable way to write your Read function is:<br>
<br>
    template <typename T> inline T Read (uint8_t* data)<br>
    {<br>
        T value;<br>
        memcpy(&value, data, sizeof(Val));<br>
        return Val;<br>
    }<br>
<br>
because "memcpy" is one of the very few ways of legitimately playing<br>
those kinds of tricks. It'll also completely avoid the alignment<br>
problems you were having and the compiler should optimize this to the<br>
best permitted code sequence (for example it might use an unaligned<br>
load followed by an aligned store if it thinks that's profitable).<br>
<br>
Cheers.<br>
<br>
Tim.<br>
</div>
</span></font>
</body>
</html>