From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 32430 invoked from network); 24 Jun 1999 07:58:00 -0000 Received: from mail.redhat.com (199.183.24.239) by lists.redhat.com with SMTP; 24 Jun 1999 07:58:00 -0000 Received: from ellpspace.math.ualberta.ca (michal@ellpspace.math.ualberta.ca [129.128.88.19]) by mail.redhat.com (8.8.7/8.8.7) with ESMTP id DAA24274 for ; Thu, 24 Jun 1999 03:02:46 -0400 Received: (from michal@localhost) by ellpspace.math.ualberta.ca (8.8.8/8.8.8) id BAA06113 for axp-list@redhat.com; Thu, 24 Jun 1999 01:02:42 -0600 From: Michal Jaegermann Message-Id: <199906240702.BAA06113@ellpspace.math.ualberta.ca> Subject: Re: problem with unaligned access on Alpha To: axp-list@redhat.com Date: Thu, 24 Jun 1999 01:02:42 -0600 (MDT) In-Reply-To: from "Maciej W. Rozycki" at Jun 23, 99 06:23:59 pm Content-Type: text 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