www.digitalmars.com         C & C++   DMDScript  

D.gnu - Unaligned accesses?

reply Johannes Pfau <nospam example.com> writes:
I recently tested the ARM port on an older ARM system (Marvell
Kirkwood ARMv5TE) and found an issue which did not happen with the
raspberry. This source file:

----------------------------------------------
import std.stdio;
void main()
{
    ubyte[4] data =3D cast(ubyte[4])[1,1,1,1];
    *cast(ushort *)&data[1] =3D 0;
    writeln(data);
}
----------------------------------------------
(reduced from std.zip)

Produces this ASM:
----------------------------------------------
sub	r3, fp, #8         ;r3 =3D &data
add	r3, r3, #1         ;r3 =3D &data[1]
mov	r2, #0             ;r2 =3D 0
strh	r2, [r3]	   ;&data[1] =3D (ushort)0
----------------------------------------------

Output on raspberry:
----------------------------------------------
[1, 0, 0, 1]
----------------------------------------------

Output on kirkwood (same binary):
----------------------------------------------
[0, 0, 1, 1]
----------------------------------------------

Debugging shows that the 'strh' instruction corrupts the data. After
some research I found this:

http://infocenter.arm.com/help/index.jsp?topic=3D/com.arm.doc.dui0068b/BABD=
JCHA.html
"a non halfword-aligned 16-bit save corrupts two bytes at [address] and
[address=E2=80=931]."

And this is exactly what's happening. It seems newer ARM cores are more
forgiving with such stuff.


So my question is this: This is not the compilers fault, correct? If
the user code were using array ops the unaligned save were detected,
but if they use pointer arithmetic there's nothing the compiler
could do?
Jan 10 2014
next sibling parent "Mike" <none none.com> writes:
On Friday, 10 January 2014 at 23:02:03 UTC, Johannes Pfau wrote:
 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0068b/BABDJCHA.html
 "a non halfword-aligned 16-bit save corrupts two bytes at 
 [address] and
 [address–1]."

This link doesn't appear to be correct. Could you please verify?
Jan 10 2014
prev sibling parent Johannes Pfau <nospam example.com> writes:
Am Sat, 11 Jan 2014 00:52:31 +0000
schrieb "Mike" <none none.com>:

 On Friday, 10 January 2014 at 23:02:03 UTC, Johannes Pfau wrote:
 http://infocenter.arm.com/help/index.jsp?topic=3D/com.arm.doc.dui0068b/=


 "a non halfword-aligned 16-bit save corrupts two bytes at=20
 [address] and
 [address=E2=80=931]."

This link doesn't appear to be correct. Could you please verify? =20

Sorry, this is the correct link: http://infocenter.arm.com/help/topic/com.arm.doc.dui0068b/Chdbifed.html Section "Address alignment for halfword transfers"
Jan 11 2014