Unix Programming - Byte alignment at compile time

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2007 > Byte alignment at compile time





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Byte alignment at compile time
Andrew Falanga

2007-01-29, 7:23 pm

Is there a way using GNU GCC, to double word align an array?
Something like a macro or similar (or whatever it might be if it
exists)? I'd like to have my integer array start on a double word
address boundary for testing a theory on a program I'm writing.

My understanding of byte alignmens is quite limited and if I'm not
asking the question correctly, please forgive me. What I'm trying to
avoid is over burden code to build an array and then double word align
the array after it's been defined.

Thanks,
Andy

Paul Pluzhnikov

2007-01-29, 7:23 pm

"Andrew Falanga" <af300wsm@gmail.com> writes:

> Is there a way using GNU GCC, to double word align an array?


Sure:

int foo[10] __attribute__((aligned(8)));


> Something like a macro or similar (or whatever it might be if it
> exists)? I'd like to have my integer array start on a double word
> address boundary for testing a theory on a program I'm writing.


A portable way to achieve the same:

union {
double d;
int foo[10];
} u;

#define foo u.foo /* so you wouldn't have to rewrite all code that uses "foo" */

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
David T. Ashley

2007-01-29, 7:23 pm

"Andrew Falanga" <af300wsm@gmail.com> wrote in message
news:1170105531.486275.154120@h3g2000cwc.googlegroups.com...
> Is there a way using GNU GCC, to double word align an array?
> Something like a macro or similar (or whatever it might be if it
> exists)? I'd like to have my integer array start on a double word
> address boundary for testing a theory on a program I'm writing.
>
> My understanding of byte alignmens is quite limited and if I'm not
> asking the question correctly, please forgive me. What I'm trying to
> avoid is over burden code to build an array and then double word align
> the array after it's been defined.


Usually, you don't have worry about this at all.

If you simply define an array, i.e.

int x[20];

or

long x [20];

or

p = malloc(20 * sizeof(long));

the compiler handles the alignment issues so as to avoid illegal addressing
or performance issues. In the first two cases, the C compiler will generate
the right code. In the last case, the result of malloc() is guaranteed to
have the most stringent alignment ever necessary.

If you want to have an "int" aligned on a boundary necessary for a "long",
then you've generally got other problems using the C language (i.e. there is
something else about your problem I'm not understanding).

There is generally no performance advantage to aligning data more
stringently than required for the data type. If such advantages exist, the
compiler generally knows about them.
--
David T. Ashley (dta@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)


SM Ryan

2007-01-30, 7:29 am

"Andrew Falanga" <af300wsm@gmail.com> wrote:
# Is there a way using GNU GCC, to double word align an array?

GCC advertises an aligned attribute for variables with the example

int x __attribute__ ((aligned (16))) = 0;

You can look it up in the GCC compiler guide which is available
online and elsewhere. It includes the warning the maximum
alignment is dependent on the linker and other software: the
object file format might simply provide no way to align beyond say
4 or 8 bytes.

# My understanding of byte alignmens is quite limited and if I'm not
# asking the question correctly, please forgive me. What I'm trying to
# avoid is over burden code to build an array and then double word align
# the array after it's been defined.

The compiler guarentees that you do not have to specify any alignment
of variables or malloc calls in order to access array elements with
[] or *. Whatever the man has to do behind the curtain, you do not have
to worry about. And while a compiler can theoretically do this with
unnecessarily abyssmal performance, such compilers don't long
survive market forces, even among free software.

Unless you have very special performance issues or you are trying
to do something not defined in the standard (like doing page aligned
memory frames), you shouldn't have to worry about alignment. In all
my years, the only time I've worried about alignment is with an idiot
interface that did I/O with structs.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I think that's kinda of personal; I don't think I should answer that.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com