From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 29443 invoked from network); 25 Jun 1999 21:19:02 -0000 Received: from mail.redhat.com (199.183.24.239) by lists.redhat.com with SMTP; 25 Jun 1999 21:19:02 -0000 Received: from delta.private.ds2.pg.gda.pl (root@delta.ds2.pg.gda.pl [153.19.61.1] (may be forged)) by mail.redhat.com (8.8.7/8.8.7) with ESMTP id QAA02314 for ; Fri, 25 Jun 1999 16:23:04 -0400 Received: from localhost (macro@localhost) by delta.private.ds2.pg.gda.pl (8.8.8/8.8.8) with SMTP id WAA11913 for ; Fri, 25 Jun 1999 22:21:06 +0200 (MET DST) Date: Fri, 25 Jun 1999 22:21:06 +0200 (MET DST) From: "Maciej W. Rozycki" Sender: macro@delta.private.ds2.pg.gda.pl To: axp-list@redhat.com Subject: Re: problem with unaligned access on Alpha In-Reply-To: <199906240702.BAA06113@ellpspace.math.ualberta.ca> Message-ID: Organization: Technical University of Gdansk MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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 +