| View previous topic :: View next topic |
| Author |
Message |
KaylaKaze
Joined: 05 May 2004 Posts: 75 Location: NC, USA
|
Posted: Thu Dec 09, 2004 11:04 pm Post subject: __attribute__ ((aligned())) |
|
|
Apart from speed purposes, when is it necessary to align variables in PS2 development? I've been wondering if some of my problems I've been having could be from structures taking memory allocated to others because of some way in which the PS2 handles memory blocks, or just not being able to access the data as expected (I first noticed something like this when I had a 14 byte BMP header being reported as 16 bytes by sizeof).
And I know I've been asking a lot of questions lately, but that's what happens when working on a project, I guess. |
|
| Back to top |
|
 |
pixel
Joined: 30 Jan 2004 Posts: 791
|
Posted: Thu Dec 09, 2004 11:45 pm Post subject: |
|
|
Depends on what you are doing with the data. If it's to be dma-transferred, for example, or if it's to be accessed in asm using lq/sq, it has to be aligned on the corresponding alignment... _________________ pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department. |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Dec 09, 2004 11:48 pm Post subject: |
|
|
Well, also structures I think will be aligned, at a minimum, on word boundries. That would explain why your sizeof() operator returns 16 instead of 14. That normally shouldn't make a difference, so long as you aren't expecting something else to be in the next two bytes. Also, depending on any data you are reading, don't forget the role of endianness and byte swapping.
One more thing, the 16 bytes, and word alignment, can be caused by a two byte gap WITHIN the structure, if you have a structure like so:
struct blah
{
u32 foo;
u32 bar;
u16 oog;
// u16 sized gap here
u32 barf;
};
In memory, between oof and barf, there will be a two byte gap, because barf must start on a word boundry. |
|
| Back to top |
|
 |
ooPo Site Admin
Joined: 17 Jan 2004 Posts: 2032 Location: Canada
|
Posted: Fri Dec 10, 2004 2:15 am Post subject: |
|
|
You can stop this alignment, if you really want to, by using the packed attribute.
struct { char tub; int girl; } __attribute__((packed)) tubgirl;
I think its something along those lines. |
|
| Back to top |
|
 |
pixel
Joined: 30 Jan 2004 Posts: 791
|
Posted: Fri Dec 10, 2004 2:19 am Post subject: |
|
|
Problem is, gcc may fail producing "unaligned" fetching code in the latter example, thus could lead to a loading exception. _________________ pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department. |
|
| Back to top |
|
 |
mrbrown
Joined: 17 Jan 2004 Posts: 1536
|
Posted: Fri Dec 10, 2004 3:23 am Post subject: |
|
|
| pixel wrote: | | Problem is, gcc may fail producing "unaligned" fetching code in the latter example, thus could lead to a loading exception. |
You need to apply the "packed" attribute to each structure member individually. Then GCC will generate the proper unaligned load/store instructions. _________________ "He was warned..." |
|
| Back to top |
|
 |
ooPo Site Admin
Joined: 17 Jan 2004 Posts: 2032 Location: Canada
|
Posted: Fri Dec 10, 2004 5:31 am Post subject: |
|
|
| Well then. I think we all learned our lesson and will never do that again. |
|
| Back to top |
|
 |
|