__attribute__ ((aligned()))

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
KaylaKaze
Posts: 75
Joined: Wed May 05, 2004 3:25 pm
Location: NC, USA
Contact:

__attribute__ ((aligned()))

Post by KaylaKaze »

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.
pixel
Posts: 791
Joined: Fri Jan 30, 2004 11:43 pm

Post by pixel »

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.
Guest

Post by Guest »

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.
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

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.
pixel
Posts: 791
Joined: Fri Jan 30, 2004 11:43 pm

Post by pixel »

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.
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

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..."
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Well then. I think we all learned our lesson and will never do that again.
Post Reply