structure alignment question
Daniel R. Levy
levy at ttrdc.UUCP
Thu Sep 25 10:41:20 AEST 1986
In article <1705 at mcc-pp.UUCP>, tiemann at mcc-pp.UUCP (Michael Tiemann) writes:
>> > Note that for speed, it is usually best to align data items on the most
>> > restrictive boundaries, even if a specific machine implementation doesn't
>> > really require it. Lots of machines let words be on arbitrary boundaries,
>> > but you pay for it in speed, if not now, then later, as CPUs come
>> > to have wider data paths.
>I was waiting for SOMEBODY to talk performace for once. The last 68000
>compiler I used aligned strings on WORD boundaries. This would cost
>one byte per string, half the time. But there was a big speed payoff:
>I could do word operations in my strnlen, strncmp, strncpy, and whatever
>other string processing functions I happened to write. All this code ported
>to a Sun-3 no (apparent) problem, but crashed on a Sun-2, because the SUN
>compiler allocated strings in a totally stingy 1-byte alignment.
>So now, all this "fast" code actually runs slower than a "dumb" byte-copy
>model, because the 68020 faults itself to death reading in 32-bit words
>on odd boundaries, and doesn't run at all on a Sun-2 because the 68010
>can read odd words.
^^^ can't, you mean?
What's more, what if there lay data in the byte after the end of a string
being copied to which was not an integral multiple of your copy-quantity
(4 bytes or whatever)? That byte (and possibly more) will get clobbered,
unless you stop short of that point and finish the operation using byte
operations. For that matter, compare of null-terminated string operations
also would need similar handling to make the compares valid (and you then
need to know the length of the string anyhow first). (If all strings involved
were counted strings a la fortran, that would be a different story.)
But for the functions where word-operations would make sense if possible,
one could check whether the pointer(s) being passed were appropriately
aligned to choose the method (word operations or byte operations). This
would be something like
func(ptr,count) char *ptr; int count;
{
if (ptr & 01) {
/* byte operations */
...
} else if (ptr & 02) {
/* short word operations */
...
} else {
/* long word operations */
...
}
}
I'm sure details would need to be worked out, (and why not use the
word operations in the middle of even a misaligned string?) but I wonder
if this would prove useful?
One could probably also force string alignment by using a union, but
it might be unwise to depend on it, as well as it being unwieldy for
places where inline data "texttexttext..." strings would normally be
used.
>Now I *know* you're not *supposed* to do things like that, but often
>high performance comes not by reading some 20 year old manual, but
>by analyzing the machine at hand, and trying to more than just
>accomodate it. Tune those compilers, comment those machine dependencies,
>and watch the code fly!
>I suppose that this letter was a bit of a flame, sorry.
>Y'all are welcome to flame me back,
>
>Michael
>tiemann at mcc.com
If you do this kind of thing, for the sake of portability #ifdef it for
the targeted machine(s) and supply a safely portable, if slow, alternative
for others. Otherwise somebody is gonna come along, port the code to
some machine you never dreamed of, and it will crash and burn.
--
------------------------------- Disclaimer: The views contained herein are
| dan levy | yvel nad | my own and are not at all those of my em-
| an engihacker @ | ployer or the administrator of any computer
| at&t computer systems division | upon which I may hack.
| skokie, illinois |
-------------------------------- Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa,
go for it! allegra,ulysses,vax135}!ttrdc!levy
More information about the Comp.lang.c
mailing list