forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

__attribute__ ((aligned()))

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
KaylaKaze



Joined: 05 May 2004
Posts: 75
Location: NC, USA

PostPosted: Thu Dec 09, 2004 11:04 pm    Post subject: __attribute__ ((aligned())) Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
pixel



Joined: 30 Jan 2004
Posts: 791

PostPosted: Thu Dec 09, 2004 11:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
Guest






PostPosted: Thu Dec 09, 2004 11:48 pm    Post subject: Reply with quote

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

PostPosted: Fri Dec 10, 2004 2:15 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
pixel



Joined: 30 Jan 2004
Posts: 791

PostPosted: Fri Dec 10, 2004 2:19 am    Post subject: Reply with quote

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
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Fri Dec 10, 2004 3:23 am    Post subject: Reply with quote

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
View user's profile Send private message
ooPo
Site Admin


Joined: 17 Jan 2004
Posts: 2032
Location: Canada

PostPosted: Fri Dec 10, 2004 5:31 am    Post subject: Reply with quote

Well then. I think we all learned our lesson and will never do that again.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group