* Re: problem with unaligned access on Alpha @ 1999-06-24 8:54 Christian Groessler 0 siblings, 0 replies; 14+ messages in thread From: Christian Groessler @ 1999-06-24 8:54 UTC (permalink / raw) To: Richard Henderson; +Cc: egcs, axp-list On 6/23/99 6:35:21 PM Richard Henderson wrote: > >On Tue, Jun 22, 1999 at 11:59:46AM +0100, Christian Groessler wrote: >> #define READ_WORD(a) (*(unsigned short *)(a)) >... >> I would expect the "val=READ_WORD()" line to generate an unaligned access >> exception, but this doesn't happen. > >You expect incorrectly. > >In general you'll only get an unaligned access trap if we'd issued >an `ldwu' instruction, which we can only do when compiling for EV56 >or higher. I assume for shorts only. If I replace "unsigned short *" with "unsigned int *" I do get unaligned traps. >> I looked at the generated assembler code (with my little only alpha >> assembler knowledge), and it seems that the compiler is generating >> code for unaligned access but somehow forgets to get the byte at (adress+1). > >Nope, we're generating code for an _aligned_ word access in >the only way that is possible on EV4. An unaligned word >access doubles the length of the sequence. Now I see. The code for aligned word access breaks when the word is unaligned at address+7, if I understand the assembly code correctly. My problem is/was, that the code breaks _silently_, but maybe this case is hard to detect (to give a warning etc.). Thanks for the info. regards, chris ^ permalink raw reply [flat|nested] 14+ messages in thread
* RE: problem with unaligned access on Alpha @ 1999-06-29 9:12 Wessels, Hans 1999-06-29 15:07 ` Michal Jaegermann 0 siblings, 1 reply; 14+ messages in thread From: Wessels, Hans @ 1999-06-29 9:12 UTC (permalink / raw) To: 'axp-list@redhat.com' On 25 juni 1999 Maciej W. Rozycki wrote: <snip> >It may even be completely ignored for archs that support unaligned >accesses in hardware, which is a true win -- no arch dependent >conditionals are needed (I do not even know how to fetch information about >alignment needed for a particular target -- "alignof" is useless here and >it's not portable, anyway). <snip> > > Using unions is good when you are sure all data is aligned. If that's >not the case, the problem remains. The "packed" approach is reasonable >but not necessarily the best. As C is relatively low-level language (some >people call it "a portable assembler") I wouldn't mind if some way of >alignment hinting became standard one day. I am using the following macro to get alignment of elements in an malloced memory block: /* * ALIGN is a macro to align elements of a certain size in a malloced * block. The start of the block is guaranteed aligned with every possible * object. So an object is aligned when it's position relative to the start * of the array is a multiple of it's size. Instead of the modulo operator * the & operator is used. This assumes that the alignment of an object * is the smalest power of two of its size; ie: sizeof(object)==3 -> alignment * is 1. sizeof(object)==6 ->alignment is 2. */ #define ALIGN(start,ptr,size) ptr+=((size)-((ptr)-(start)))&((size)-1) some code.... { ... uint8 *base = malloc(memneed); if (base == NULL) { return GUP_OUT_OF_MEM; } else { uint8 *cp = base; com->dictionary = (void *)(cp + 4); /* vier extra om terug te kunnen kijken */ cp += (DIC_SIZE + MAX_MATCH*4 + 6UL + 4UL) * sizeof (uint8); /* sliding dictionary + 4 voor terugkijken */ ALIGN(base, cp, sizeof(node_type)); /* allign buffer on node_type allignment */ com->root.big = (void *)cp; /* cp += sizeof (node_type) * HASH_SIZE; /* normal root */ ALIGN(base, cp, sizeof(unsigned long)); /* align on unsigned long for output buffer */ com->buffer_start=(void*)cp; cp += com->buffer_size; ALIGN(base, cp, sizeof(node_struct)); com->tree.big = (void *)cp; cp += sizeof (node_struct) * TREE_SIZE; /* sld tree */ ALIGN(base, cp, sizeof(uint8)); com->charlen = (void *)cp; cp += sizeof (uint8) * (NC + NC); /* karakter lengte */ ALIGN(base, cp, sizeof(uint16)); com->ptr2huffman = (void *)cp; cp += sizeof (uint16) * (MAX_NPT); /* huffman codes van de pointers */ ALIGN(base, cp, sizeof(pointer_type)); com->pointers = (void *)(cp); cp += buf * sizeof (pointer_type); ... } This code works fine from MC68K and a compiler using 68bit int's, going through GCC on MC68K and i386 to GCC on the Alpha. I know it isn't perfect but it works fine for me. Bye, Hans ---------------------------------------------------------------------- /| /\/\/\ |\ ir. Hans Wessels / | / /\/\ \ | \ H.Wessels@Energie-Delfland.nl / | / / /\ \ \ | \ tel.: 015 251 4397 / |/ / / \ \ \| \ \ |\ \ \ / / /| / prive: \ | \ \ \/ / / | / mr_ni@mbh.org \ | \ \/\/ / | / Fidelus Bittus et Fuckus Clockcyclus! \| \/\/\/ |/ ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha 1999-06-29 9:12 Wessels, Hans @ 1999-06-29 15:07 ` Michal Jaegermann 1999-07-05 17:23 ` Maciej W. Rozycki 0 siblings, 1 reply; 14+ messages in thread From: Michal Jaegermann @ 1999-06-29 15:07 UTC (permalink / raw) To: axp-list Wessels, Hans wrote: > > On 25 juni 1999 Maciej W. Rozycki wrote: > > > Using unions is good when you are sure all data is aligned. If that's > >not the case, the problem remains. The "packed" approach is reasonable > >but not necessarily the best. .... > I am using the following macro to get alignment of elements in an > malloced memory block: [ snip ] 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. Michal ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha 1999-06-29 15:07 ` Michal Jaegermann @ 1999-07-05 17:23 ` Maciej W. Rozycki 0 siblings, 0 replies; 14+ messages in thread From: Maciej W. Rozycki @ 1999-07-05 17:23 UTC (permalink / raw) To: axp-list 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 + ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha
@ 1999-06-24 8:29 Christian Groessler
1999-06-24 15:26 ` Michal Jaegermann
0 siblings, 1 reply; 14+ messages in thread
From: Christian Groessler @ 1999-06-24 8:29 UTC (permalink / raw)
To: axp-list
On 6/23/99 3:56:17 PM Michal Jaegermann wrote:
>
>Try to see if -fno-strict-aliasing compiler flag will make any difference.
>Likely it should.
Makes no difference.
But my problem isn't "how to fix it" but "why does the compiler silently
generate
garbage code" ....
regards,
chris
^ permalink raw reply [flat|nested] 14+ messages in thread* Re: problem with unaligned access on Alpha 1999-06-24 8:29 Christian Groessler @ 1999-06-24 15:26 ` Michal Jaegermann 0 siblings, 0 replies; 14+ messages in thread From: Michal Jaegermann @ 1999-06-24 15:26 UTC (permalink / raw) To: axp-list Christian Groessler wrote: > > > > On 6/23/99 3:56:17 PM Michal Jaegermann wrote: > > > >Try to see if -fno-strict-aliasing compiler flag will make any difference. > >Likely it should. > > Makes no difference. > But my problem isn't "how to fix it" but "why does the compiler silently > generate garbage code" .... One possible answer was "because it has a bug in strict aliasing resolution". Apparently there is some other reason. Or you are looking for answers of some different kind? Michal ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha
@ 1999-06-23 10:23 Christian Groessler
1999-06-23 14:55 ` Maciej W. Rozycki
1999-06-23 14:56 ` Michal Jaegermann
0 siblings, 2 replies; 14+ messages in thread
From: Christian Groessler @ 1999-06-23 10:23 UTC (permalink / raw)
To: axp-list; +Cc: Stefan Katzenbeisser
Hello Stefan,
On 6/22/99 1:32:39 PM Stefan Katzenbeisser wrote:
>
>That's definitively a compiler bug. I solved the problem by upgrading
>to egcs 1.1.2
But it also happens with egcs-1.1.2 (on NetBSD, but I don't think this
makes a difference).
OK, now that I know whats the problem I can avoid the unaligned access and
fix the program. But I initially relied on unaligned accesses being
signalled
by the kernel, and now that the compiler works around unaligned accesses
(in a buggy way) makes it much more difficult to get alignment-unaware
(intel)
apps running on Alpha....
regards,
chris
^ permalink raw reply [flat|nested] 14+ messages in thread* Re: problem with unaligned access on Alpha 1999-06-23 10:23 Christian Groessler @ 1999-06-23 14:55 ` Maciej W. Rozycki 1999-06-23 14:56 ` Michal Jaegermann 1 sibling, 0 replies; 14+ messages in thread From: Maciej W. Rozycki @ 1999-06-23 14:55 UTC (permalink / raw) To: axp-list On Wed, 23 Jun 1999, Christian Groessler wrote: > But it also happens with egcs-1.1.2 (on NetBSD, but I don't think this > makes a difference). > > OK, now that I know whats the problem I can avoid the unaligned access and > fix the program. But I initially relied on unaligned accesses being > signalled > by the kernel, and now that the compiler works around unaligned accesses > (in a buggy way) makes it much more difficult to get alignment-unaware > (intel) > apps running on Alpha.... Well, unless you interchange non-byte data between architectures all accesses should be aligned by the compiler. If you cast between types of different alignments, you may make the compiler be aware of wrong alignment by casting the type to an unaligned one. E.g. you may cast unaligned "int32_t" to "struct { int32_t __attribute__((packed)); }" (it's not written formally but it should give you the idea); I'm told it works with egcs 1.1.2 which will add a necessary code to fetch the whole object properly. If you interchange non-byte data between architectures then you have to take care of different endianness anyway and you can simply apply alignment fixes to all places where you swap bytes for correct endianness. This is what I did in the program I am currently porting to multi-arch. I also observed the compiler behaviour you described. I believe it's a bug unless egcs people have a proof it's a feature. I think the most reasonable (i.e. performance-wise) solution is to change the unaligned load to the aligned one and let the kernel trap it. Given the compiler can be told explicitly to handle unaligned data this solution seems to be the right one. Have you reported the bug to egcs folks? -- + Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: macro@ds2.pg.gda.pl, PGP key available + ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha 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 1 sibling, 1 reply; 14+ messages in thread From: Michal Jaegermann @ 1999-06-23 14:56 UTC (permalink / raw) To: axp-list Christian Groessler wrote: > > Hello Stefan, > > On 6/22/99 1:32:39 PM Stefan Katzenbeisser wrote: > > > >That's definitively a compiler bug. I solved the problem by upgrading > >to egcs 1.1.2 > > But it also happens with egcs-1.1.2 (on NetBSD, but I don't think this > makes a difference). Try to see if -fno-strict-aliasing compiler flag will make any difference. Likely it should. Legality aside peple who write such "nice" code should be run out of town. Unfortunately such way is quite popular; mostly because in most situations one can get away with it. Michal ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha 1999-06-23 14:56 ` Michal Jaegermann @ 1999-06-23 16:23 ` Maciej W. Rozycki 1999-06-24 7:02 ` Michal Jaegermann 0 siblings, 1 reply; 14+ messages in thread From: Maciej W. Rozycki @ 1999-06-23 16:23 UTC (permalink / raw) To: axp-list On Wed, 23 Jun 1999, Michal Jaegermann wrote: > Legality aside peple who write such "nice" code should be run out of > town. Unfortunately such way is quite popular; mostly because in most > situations one can get away with it. Let me give you a real example. I have a stream of compressed data created on a 16-bit machine. It is a mixture of 8-bit and 16-bit unsigned quantities. How would you handle it most efficiently? I would define a "uint8_t *x" pointer (a "void *x" would be an option here, but the former one allows pointer arithmetics in a portable way) and depending on the current necessity I would use it as is for 8-bit quantities or use "((uint16_s *)&(x))->v" cast (where "uint16_s" is defined as "typedef struct { uint16_t v __attribute__((packed)); } uint16_s;") to access 16-bit ones. As of egcs 1.1 this should inform the compiler, data is potentially unaligned and result in a possibly most efficient code, i.e. a single opcode for machines that do not have any alignment constraints (like i386) or two opcodes for unaligned fetch for machines that do not support unaligned bus accesses (like Alpha or MIPS). One can use "uint8_t" fetches with appropriate shifting and ORing (it's still necessary on different-endian machines even though some archs have explicit support for most common endianness convertions) but this wastes a lot of unnecessary cycles. If you have any other ideas, please do not hesitate to share them. -- + Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: macro@ds2.pg.gda.pl, PGP key available + ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha 1999-06-23 16:23 ` Maciej W. Rozycki @ 1999-06-24 7:02 ` Michal Jaegermann 1999-06-25 20:21 ` Maciej W. Rozycki 0 siblings, 1 reply; 14+ messages in thread From: Michal Jaegermann @ 1999-06-24 7:02 UTC (permalink / raw) To: axp-list Maciej W. Rozycki wrote: > > Let me give you a real example. I have a stream of compressed data > created on a 16-bit machine. It is a mixture of 8-bit and 16-bit > unsigned quantities. How would you handle it most efficiently? Looking at a generated assembler it seems to me that an efficiency of reading that as a stream of unsigned bytes and composing required 16-bit values by "shift and add" is grossly underestimated. :-) I grant that there are tougher cases than that. OTOH Intel processors also take a performance hit for unaligned acceses although, I understand, this overhead is quite a bit smaller in an Intel case and they are also quiet about it. > or use "((uint16_s *)&(x))->v" cast (where "uint16_s" is > defined as "typedef struct { uint16_t v __attribute__((packed)); } > uint16_s;") to access 16-bit ones. Hm, I never thought about "__attribute__((packed))" trick. This would make code, obviosly, quite gcc specific but likely this can be hidden in some reasonably looking macros defined differently for gcc and others. But did you look at what happens on an assembler level (I did not - for your examples); "packed" is not coming for free. > One can use "uint8_t" fetches with appropriate shifting and ORing > (it's still necessary on different-endian machines even though some > archs have explicit support for most common endianness convertions) > but this wastes a lot of unnecessary cycles. I bet that a lot less than an unaligned trap. :-) C Standard is also full of dire warnings about mixing different pointer types (or using casts to hide that practice). I guess that this is not without a reason even if sounds like anal in places. Somebody send me a while ago samples of a code which were supposedly showing up an aliasing bug of egcs-1.1.2. I did not try them on Intel but there was not a trace of bad results on Alpha. Maybe one needs much more of a register presure before bad things will start to happen? I do not know. Out of sheer curiosity I rewrote one example to use unions instead of playing "efficient" pointer games. Resulting assembler was smaller and better for union approach as opposed to the "efficient" one. In this case everything was aligned anyway as variables were defined on stack. But this IS a food for though. Michal ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: problem with unaligned access on Alpha 1999-06-24 7:02 ` Michal Jaegermann @ 1999-06-25 20:21 ` Maciej W. Rozycki 0 siblings, 0 replies; 14+ messages in thread From: Maciej W. Rozycki @ 1999-06-25 20:21 UTC (permalink / raw) To: axp-list On Thu, 24 Jun 1999, Michal Jaegermann wrote: > > Let me give you a real example. I have a stream of compressed data > > created on a 16-bit machine. It is a mixture of 8-bit and 16-bit > > unsigned quantities. How would you handle it most efficiently? > > Looking at a generated assembler it seems to me that an efficiency of > reading that as a stream of unsigned bytes and composing required 16-bit > values by "shift and add" is grossly underestimated. :-) I grant that Well, I suppose it's architecture-dependent. I don't know all architectures. ;-) At least on i386 merging bytes to a doubleword is at least about four times slower than fetching unaligned data directly (also depending on the particular CPU model). > there are tougher cases than that. OTOH Intel processors also take a > performance hit for unaligned acceses although, I understand, this > overhead is quite a bit smaller in an Intel case and they are also quiet > about it. You need an extra bus cycle anyway. I386 does it transparently and the performance hit should be the smallest possible as it consists of the cycle only. At least some RISC processors can take the advantage of the smallest possible performance hit of a single extra bus cycle but in their case it needs special handling, i.e. using a sequence of two special opcodes that deal with unaligned data as opposed to a single opcode that deals with aligned data. Compilers usually generate the latter as they are unaware of run-time misalignment possibilities and in most cases the 100% performance hit is undesired. One can make i386 report unaligned accesses in a mannes RISCs do, BTW. Linux even configures the CPU in such a way it's up to the process to choose between standard operation and misalignment trapping. I tried to enable this feature in one of my test programs but it turned out that glibc is not alignment-clean on this arch and no useful results could be achieved. :-( > > or use "((uint16_s *)&(x))->v" cast (where "uint16_s" is > > defined as "typedef struct { uint16_t v __attribute__((packed)); } > > uint16_s;") to access 16-bit ones. > > Hm, I never thought about "__attribute__((packed))" trick. This would > make code, obviosly, quite gcc specific but likely this can be hidden > in some reasonably looking macros defined differently for gcc and > others. But did you look at what happens on an assembler level > (I did not - for your examples); "packed" is not coming for free. I am told the case of a packed struct that contains the single field is treated specially as of egcs 1.1 (see 'linux/include/asm-alpha/unaligned.h'). Look at the semantics of "packed" -- it makes perfectly no sense for single-member structs, apart from signalling a potential misalignment. I think using this feature is better than inventing own portable solutions. "Packed" enables the compiler to use the best known solution known for the target arch -- see my note on opcodes for unaligned accesses above. It may even be completely ignored for archs that support unaligned accesses in hardware, which is a true win -- no arch dependent conditionals are needed (I do not even know how to fetch information about alignment needed for a particular target -- "alignof" is useless here and it's not portable, anyway). I don't have appropriate code available at the moment; I'll check it next week. I don't expect anything but changing a single opcode into two ones, e.g. "load" on MIPS would be changed to a pair of "load unaligned left" and "load unaligned right" (I don't remember names of the relevant opcodes) -- two cycles instead of one. And gcc/egcs is free so a portability is almost non-issue. Even if one insists on using a different compiler, there is always autoconf available for the task of source configuration. > > One can use "uint8_t" fetches with appropriate shifting and ORing > > (it's still necessary on different-endian machines even though some > > archs have explicit support for most common endianness convertions) > > but this wastes a lot of unnecessary cycles. > > I bet that a lot less than an unaligned trap. :-) C Standard is also Of course it is, but for archs that support unaligned bus accesses it becames a noticeable loss. ;-) > full of dire warnings about mixing different pointer types (or using > casts to hide that practice). I guess that this is not without a reason > even if sounds like anal in places. Well, that's why I like "packed" described above -- at least it's well-defined. > Somebody send me a while ago samples of a code which were supposedly > showing up an aliasing bug of egcs-1.1.2. I did not try them on Intel > but there was not a trace of bad results on Alpha. Maybe one needs much > more of a register presure before bad things will start to happen? I do > not know. Out of sheer curiosity I rewrote one example to use unions > instead of playing "efficient" pointer games. Resulting assembler was > smaller and better for union approach as opposed to the "efficient" one. > In this case everything was aligned anyway as variables were defined on > stack. But this IS a food for though. Using unions is good when you are sure all data is aligned. If that's not the case, the problem remains. The "packed" approach is reasonable but not necessarily the best. As C is relatively low-level language (some people call it "a portable assembler") I wouldn't mind if some way of alignment hinting became standard one day. -- + Maciej W. Rozycki, Technical University of Gdansk, Poland + +--------------------------------------------------------------+ + e-mail: macro@ds2.pg.gda.pl, PGP key available + ^ permalink raw reply [flat|nested] 14+ messages in thread
* problem with unaligned access on Alpha
@ 1999-06-22 10:59 Christian Groessler
1999-06-22 12:32 ` Stefan Katzenbeisser
0 siblings, 1 reply; 14+ messages in thread
From: Christian Groessler @ 1999-06-22 10:59 UTC (permalink / raw)
To: egcs, axp-list
Hello,
while fixing a (intel-) program to work on Alpha, I've encountered
something,
I think it's a compiler bug.
Consider the following example program:
-----------------------
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#define READ_WORD(a) (*(unsigned short *)(a))
int main(void)
{
char * sepp,* xadr;
int fd;
int i;
unsigned short val;
fd=open("/dev/zero",O_RDWR);
if (fd == -1) {
xxx:
fprintf(stderr,"cannot open : %s\n",strerror(errno));
return(99);
}
sepp = mmap(0,0x10000,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
if (sepp == -1) goto xxx;
printf("mapped at address %p\n",sepp);
xadr = sepp + 0x7bf;
for (i=0; i< 0x7d0; i++) *(sepp + i) = 0xcc;
printf("OK, now accessing %p....\n",xadr);
val = READ_WORD(xadr);
printf("val read = %04x\n",val);
return(1);
}
-----------------------
I would expect the "val=READ_WORD()" line to generate an unaligned access
exception,
but this doesn't happen. Instead, only one _byte_ at address xadr is read,
resulting
in val=0xcc. Val is 0xcccc on intel machines.
I looked at the generated assembler code (with my little only alpha
assembler knowledge),
and it seems that the compiler is generating code for unaligned access but
somehow
forgets to get the byte at (adress+1).
This was reproduced on RH Linux 5.2 with egcs-1.0.3 and NetBSD 1.4C with
egcs-1.1.2.
Any comments?
regards,
chris
^ permalink raw reply [flat|nested] 14+ messages in thread* Re: problem with unaligned access on Alpha 1999-06-22 10:59 Christian Groessler @ 1999-06-22 12:32 ` Stefan Katzenbeisser 0 siblings, 0 replies; 14+ messages in thread From: Stefan Katzenbeisser @ 1999-06-22 12:32 UTC (permalink / raw) To: axp-list On Tue, 22 Jun 1999, Christian Groessler wrote: > I think it's a compiler bug. It is, see below. > I would expect the "val=READ_WORD()" line to generate an unaligned access > exception, > but this doesn't happen. Instead, only one _byte_ at address xadr is read, > resulting > in val=0xcc. Val is 0xcccc on intel machines. I encountered a similar problem some weeks ago; it's the way egcs generates load-instructions for bytes and shorts: your code results in ldq_u $1,0($2) extwl $1,$2,$1 This can't work if the adress is not aligned (ldq_u loads the quadword statring at $2 with the low three bits cleared...). That's definitively a compiler bug. I solved the problem by upgrading to egcs 1.1.2 Best, Stefan. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- | Stefan Katzenbeisser | e-mail: skatzenbeisser@acm.org | http://stud3.tuwien.ac.at/~e9625414 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- Public PGP key available on my homepage or upon request. ^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~1999-07-08 18:33 UTC | newest] Thread overview: 14+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 1999-06-24 8:54 problem with unaligned access on Alpha Christian Groessler -- strict thread matches above, loose matches on Subject: below -- 1999-06-29 9:12 Wessels, Hans 1999-06-29 15:07 ` Michal Jaegermann 1999-07-05 17:23 ` Maciej W. Rozycki 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox