From: "Maciej W. Rozycki" <macro@ds2.pg.gda.pl>
To: axp-list@redhat.com
Subject: Re: problem with unaligned access on Alpha
Date: Mon, 5 Jul 1999 19:23:06 +0200 (MET DST) [thread overview]
Message-ID: <Pine.GSO.3.96.990629205947.17807E-100000@delta.private.ds2.pg.gda.pl> (raw)
In-Reply-To: <199906291507.JAA15833@ellpspace.math.ualberta.ca>
On Tue, 29 Jun 1999, Michal Jaegermann wrote:
> Aligning your own data is usually not a very big problem. egcs does a
> pretty good job with it by itself; sometimes too good. :-) But from time
> to time you have to pick apart "alien" structures - like ethernet packet
> headers, or file system descriptors, or you name it. They have their
> own layout and alignment requirements, which you cannot change, and one
> has to be a bit careful not to run afoul of a compiler.
As I promised, I am posting the code I am getting using various ways of
accessing unaligned data.
The programs I used to test are horrible (don't try to run them ;-) ) but
I believe they're about minimal necessary to stop egcs making any
assumptions on addresses of variables. The programs were compiled using
"gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)" with optimization
level set to "-O3" (no other compiler options) for ev4 and for i386.
For 32-bit quantities the program is as follows:
typedef struct { uint32_t v __attribute__((packed)); } uint32_s;
volatile int32_t *src, *dst;
int main(int argc)
{
src = (void *)argc;
dst = (void *)~argc;
*src = argc;
itoh32_u(*dst, *src);
return (src - dst) + *dst;
}
itoh32_u is a macro.
1. Accessing as if data were aligned:
itoh32_u(*dst, *src)
is expanded into:
*((uint32_t *)&(*dst)) = *((uint32_t *)&(*src))
The resulting code:
Alpha:
itoh32_u(*dst, *src);
120000400: 00 00 45 a4 ldq t1,0(t4)
120000404: 00 00 24 a4 ldq t0,0(t3)
120000408: 00 00 42 89 lds $f10,0(t1)
12000040c: 00 00 41 99 sts $f10,0(t0)
ia32:
itoh32_u(*dst, *src);
80483cf: a1 5c 95 04 08 movl 0x804955c,%eax
80483d4: 8b 15 58 95 04 08 movl 0x8049558,%edx
80483da: 8b 00 movl (%eax),%eax
80483dc: 89 02 movl %eax,(%edx)
2. Accessing using pointer casting:
itoh32_u(*dst, *src)
is expanded into:
*((uint32_t *)&(*dst)) =
(((uint32_t)((((uint8_t *)&(*src)))[0])) |
((uint32_t)((((uint8_t *)&(*src)))[1]) << 8) |
((uint32_t)((((uint8_t *)&(*src)))[2]) << 16) |
((uint32_t)((((uint8_t *)&(*src)))[3]) << 24))
The resulting code:
Alpha:
itoh32_u(*dst, *src);
120000400: 00 00 57 a4 ldq t1,0(t9)
120000404: 00 00 16 a7 ldq t10,0(t8)
120000408: 01 00 c2 2c ldq_u t5,1(t1)
12000040c: 07 34 40 40 addq t1,0x1,t6
120000410: 02 00 a2 2c ldq_u t4,2(t1)
120000414: 01 54 40 40 addq t1,0x2,t0
120000418: 00 00 82 2c ldq_u t3,0(t1)
12000041c: 08 74 40 40 addq t1,0x3,t7
120000420: 03 00 62 2c ldq_u t2,3(t1)
120000424: c6 00 c7 48 extbl t5,t6,t5
120000428: c5 00 a1 48 extbl t4,t0,t4
12000042c: c4 00 82 48 extbl t3,t1,t3
120000430: c3 00 68 48 extbl t2,t7,t2
120000434: 21 17 c1 48 sll t5,0x8,t0
120000438: 27 17 a2 48 sll t4,0x10,t6
12000043c: 22 17 63 48 sll t2,0x18,t1
120000440: 01 04 81 44 or t3,t0,t0
120000444: 01 04 27 44 or t0,t6,t0
120000448: 01 04 22 44 or t0,t1,t0
12000044c: 00 00 38 b0 stl t0,0(t10)
ia32:
itoh32_u(*dst, *src);
80483d1: 8b 0d 7c 95 04 08 movl 0x804957c,%ecx
80483d7: 0f b6 41 01 movzbl 0x1(%ecx),%eax
80483db: 0f b6 11 movzbl (%ecx),%edx
80483de: c1 e0 08 shll $0x8,%eax
80483e1: 09 c2 orl %eax,%edx
80483e3: 0f b6 41 02 movzbl 0x2(%ecx),%eax
80483e7: c1 e0 10 shll $0x10,%eax
80483ea: 09 c2 orl %eax,%edx
80483ec: 0f b6 41 03 movzbl 0x3(%ecx),%eax
80483f0: 8b 1d 78 95 04 08 movl 0x8049578,%ebx
80483f6: c1 e0 18 shll $0x18,%eax
80483f9: 09 c2 orl %eax,%edx
80483fb: 89 13 movl %edx,(%ebx)
3. Accessing using packed structures:
itoh32_u(*dst, *src)
is expanded into:
*((uint32_t *)&(*dst)) = (((uint32_s *)&(*src)))->v
The resulting code:
Alpha:
itoh32_u(*dst, *src);
120000400: 00 00 65 a4 ldq t2,0(t4)
120000404: 00 00 c4 a4 ldq t5,0(t3)
120000408: 00 00 43 2c ldq_u t1,0(t2)
12000040c: 03 00 23 2c ldq_u t0,3(t2)
120000410: c2 04 43 48 extll t1,t2,t1
120000414: 41 0d 23 48 extlh t0,t2,t0
120000418: 02 04 41 44 or t1,t0,t1
12000041c: 00 00 46 b0 stl t1,0(t5)
ia32:
itoh32_u(*dst, *src);
80483cf: a1 5c 95 04 08 movl 0x804955c,%eax
80483d4: 8b 15 58 95 04 08 movl 0x8049558,%edx
80483da: 8b 00 movl (%eax),%eax
80483dc: 89 02 movl %eax,(%edx)
For 16-bit quantities the program is as follows:
typedef struct { uint16_t v __attribute__((packed)); } uint16_s;
volatile int16_t *src, *dst;
int main(int argc)
{
src = (void *)argc;
dst = (void *)~argc;
*src = argc;
itoh16_u(*dst, *src);
return (src - dst) + *dst;
}
1. Accessing as if data were aligned:
itoh16_u(*dst, *src)
is expanded into:
*((uint16_t *)&(*dst)) = *((uint16_t *)&(*src))
The resulting code:
Alpha:
itoh16_u(*dst, *src);
120000414: 00 00 a7 a4 ldq t4,0(t6)
120000418: 00 00 86 a4 ldq t3,0(t5)
12000041c: 00 00 45 2c ldq_u t1,0(t4)
120000420: 00 00 64 2c ldq_u t2,0(t3)
120000424: c2 02 45 48 extwl t1,t4,t1
120000428: 43 02 64 48 mskwl t2,t3,t2
12000042c: 61 03 44 48 inswl t1,t3,t0
120000430: 01 04 23 44 or t0,t2,t0
120000434: 00 00 24 3c stq_u t0,0(t3)
ia32:
itoh16_u(*dst, *src);
80483d0: a1 5c 95 04 08 movl 0x804955c,%eax
80483d5: 8b 15 58 95 04 08 movl 0x8049558,%edx
80483db: 66 8b 00 movw (%eax),%ax
80483de: 66 89 02 movw %ax,(%edx)
2. Accessing using pointer casting:
itoh16_u(*dst, *src)
is expanded into:
*((uint16_t *)&(*dst)) =
(((uint16_t)((((uint8_t *)&(*src)))[0])) |
((uint16_t)((((uint8_t *)&(*src)))[1]) << 8))
The resulting code:
Alpha:
itoh16_u(*dst, *src);
120000414: 00 00 76 a4 ldq t2,0(t8)
120000418: 00 00 c8 a4 ldq t5,0(t7)
12000041c: 01 00 23 2c ldq_u t0,1(t2)
120000420: 07 34 60 40 addq t2,0x1,t6
120000424: 00 00 43 2c ldq_u t1,0(t2)
120000428: 00 00 86 2c ldq_u t3,0(t5)
12000042c: c1 00 27 48 extbl t0,t6,t0
120000430: c2 00 43 48 extbl t1,t2,t1
120000434: 25 17 21 48 sll t0,0x8,t4
120000438: 44 02 86 48 mskwl t3,t5,t3
12000043c: 02 04 45 44 or t1,t4,t1
120000440: 61 03 46 48 inswl t1,t5,t0
120000444: 01 04 24 44 or t0,t3,t0
120000448: 00 00 26 3c stq_u t0,0(t5)
ia32:
itoh16_u(*dst, *src);
80483d1: a1 6c 95 04 08 movl 0x804956c,%eax
80483d6: 66 0f b6 10 movzbw (%eax),%dx
80483da: 66 0f b6 40 01 movzbw 0x1(%eax),%ax
80483df: 8b 0d 68 95 04 08 movl 0x8049568,%ecx
80483e5: 66 c1 e0 08 shlw $0x8,%ax
80483e9: 09 c2 orl %eax,%edx
80483eb: 66 89 11 movw %dx,(%ecx)
3. Accessing using packed structures:
itoh16_u(*dst, *src)
is expanded into:
*((uint16_t *)&(*dst)) = (((uint16_s *)&(*src)))->v
The resulting code:
Alpha:
itoh16_u(*dst, *src);
120000414: 00 00 68 a4 ldq t2,0(t7)
120000418: 00 00 c7 a4 ldq t5,0(t6)
12000041c: 00 00 43 2c ldq_u t1,0(t2)
120000420: 01 00 83 2c ldq_u t3,1(t2)
120000424: 00 00 a6 2c ldq_u t4,0(t5)
120000428: c2 02 43 48 extwl t1,t2,t1
12000042c: 44 0b 83 48 extwh t3,t2,t3
120000430: 45 02 a6 48 mskwl t4,t5,t4
120000434: 02 04 44 44 or t1,t3,t1
120000438: 61 03 46 48 inswl t1,t5,t0
12000043c: 01 04 25 44 or t0,t4,t0
120000440: 00 00 26 3c stq_u t0,0(t5)
ia32:
itoh16_u(*dst, *src);
80483d0: a1 5c 95 04 08 movl 0x804955c,%eax
80483d5: 8b 15 58 95 04 08 movl 0x8049558,%edx
80483db: 66 8b 00 movw (%eax),%ax
80483de: 66 89 02 movw %ax,(%edx)
As you see both the second and the third case give correct program
behaviour though "byte fiddling" is more expensive than packed structures.
For Alpha the first case is certainly the fastest but it fails in some
cases.
For ia32 the compiler knows the bus interface supports unaligned accesses
and produces the fastest code possible for both the first and the third
case.
--
+ Maciej W. Rozycki, Technical University of Gdansk, Poland +
+--------------------------------------------------------------+
+ e-mail: macro@ds2.pg.gda.pl, PGP key available +
next prev parent reply other threads:[~1999-07-05 17:25 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-06-29 9:12 problem with unaligned access on Alpha Wessels, Hans
1999-06-29 15:07 ` Michal Jaegermann
1999-07-05 17:23 ` Maciej W. Rozycki [this message]
-- strict thread matches above, loose matches on Subject: below --
1999-06-24 8:54 Christian Groessler
1999-06-24 8:29 Christian Groessler
1999-06-24 15:26 ` Michal Jaegermann
1999-06-23 10:23 Christian Groessler
1999-06-23 14:55 ` Maciej W. Rozycki
1999-06-23 14:56 ` Michal Jaegermann
1999-06-23 16:23 ` Maciej W. Rozycki
1999-06-24 7:02 ` Michal Jaegermann
1999-06-25 20:21 ` Maciej W. Rozycki
1999-06-22 10:59 Christian Groessler
1999-06-22 12:32 ` Stefan Katzenbeisser
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox