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 

Why memalign?

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



Joined: 01 Sep 2009
Posts: 18

PostPosted: Tue Sep 01, 2009 6:34 pm    Post subject: Why memalign? Reply with quote

Hi,

I red in many tutorial that i should use memalign to allocate memory for my vertices.

for example, this is my Vertex class:
Code:
struct Vertex {
     float u, v;
     unsigned int color;
     float x, y, z;
};

this is how i should allocate memory:
Code:
 vertices = (Vertex*) memalign(16, 3*sizeof(Vertex));


and this is the call of drawing function:
Code:
ceGumDrawArray(GU_TRIANGLES, GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D | GU_TEXTURE_32BITF, 3, 0, vertices);


I tried to replacte memalign(16, 3*sizeof(Vertex)); by new Vector[3]; ad every thing worked allright. So why memalign?
Back to top
View user's profile Send private message
Ameen



Joined: 14 Jun 2009
Posts: 12
Location: Bahrain

PostPosted: Wed Sep 02, 2009 2:10 am    Post subject: Reply with quote

I recall using memalign on the PS3 in order to align the data for a faster data transfer using the DMA engine.

I'm not sure, but I think the alignment here is being used for the same concept.
Back to top
View user's profile Send private message Visit poster's website
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Wed Sep 02, 2009 6:41 am    Post subject: Reply with quote

Many bits of the PSP hardware require the data to be aligned to certain boundaries. If 'new' worked for you, then you just got lucky.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
psPea



Joined: 01 Sep 2007
Posts: 64

PostPosted: Wed Sep 02, 2009 6:59 am    Post subject: Reply with quote

I know that malloc allocates memory aligned on 16 bytes boundary, so maybe new does too.
_________________
Click ME!
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Wed Sep 02, 2009 9:01 am    Post subject: Reply with quote

psPea wrote:
I know that malloc allocates memory aligned on 16 bytes boundary, so maybe new does too.


That's not guaranteed! It depends on the implementation, and many allocate on 8 or 4 byte boundaries. Some are 16-byte boundary + 4 (allocate to a 16 byte boundary, then push a long, then return the result). That's why you use memalign instead of malloc/new - to GUARANTEE the boundary you need IS respected by the allocation.
Back to top
View user's profile Send private message AIM Address
psPea



Joined: 01 Sep 2007
Posts: 64

PostPosted: Wed Sep 02, 2009 9:22 am    Post subject: Reply with quote

>That's not guaranteed! It depends on the implementation
Well I was talking about the implimentation in the psp toolchain
I saw it on this forum but couldn't I find the thread.
_________________
Click ME!
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
maroxe



Joined: 01 Sep 2009
Posts: 18

PostPosted: Wed Sep 02, 2009 12:16 pm    Post subject: Reply with quote

thanks for you reply.
so i must understand that i always have to use memalign(16, whatever) instead of new/malloc?
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Wed Sep 02, 2009 12:34 pm    Post subject: Reply with quote

maroxe wrote:
thanks for you reply.
so i must understand that i always have to use memalign(16, whatever) instead of new/malloc?


Yes. If I remember correctly, the most you can count on for newlib is the largest natural type the CPU handles - for most CPUs, that's double, but on the PSP, it's just long/single.

Someone correct me if I'm wrong on that. :)
Back to top
View user's profile Send private message AIM Address
maroxe



Joined: 01 Sep 2009
Posts: 18

PostPosted: Wed Sep 02, 2009 12:44 pm    Post subject: Reply with quote

I red in the doc of sceGuDrawArray:
Quote:
Every vertex must align to 32 bits, which means that you HAVE to pad if it does not add up!
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Thu Sep 03, 2009 4:24 am    Post subject: Reply with quote

J.F. wrote:
maroxe wrote:
thanks for you reply.
so i must understand that i always have to use memalign(16, whatever) instead of new/malloc?


Yes. If I remember correctly, the most you can count on for newlib is the largest natural type the CPU handles - for most CPUs, that's double, but on the PSP, it's just long/single.

Someone correct me if I'm wrong on that. :)


You can specify the default alignment for malloc() when building newlib with the MALLOC_ALIGNMENT define. This is currently set to 16 bytes - it's that high for precisely the reason this thread was started - people don't often know which data structures need to be aligned for hardware.

I wanted to point out though, that when dealing with an array of structs allocated via new, you have to be extremely careful about passing them off to hardware. The compiler will insert a bit of housekeeping data at the beginning of your allocation, so that it can properly destroy things when you call delete[]. IOW, you can't count on 128-bit alignment when using array new.

Anyway, if sceGuDrawArray() requires 32-bit alignment then it shouldn't matter because whatever housekeeping that array new tacks on will likely be at least 32-bit aligned.
_________________
"He was warned..."
Back to top
View user's profile Send private message
maroxe



Joined: 01 Sep 2009
Posts: 18

PostPosted: Thu Sep 03, 2009 4:49 am    Post subject: Reply with quote

i steal don't understand, if a data is 32bits aligned, it will be 16bits aligned. So why in every tutorial for PSP we use memalign(16, ...)?
Also, the list passed to sceGu, f don't specify "__attribute__((aligned(16)))" no vertexis shown at the screen
Back to top
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Thu Sep 03, 2009 5:05 am    Post subject: Reply with quote

The argument passed to memalign() and __attribute__((aligned())) is specified in bytes, not bits.

The default alignment for PSP newlib malloc() is 16 bytes, or 128 bits.
_________________
"He was warned..."
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Thu Sep 03, 2009 8:00 am    Post subject: Reply with quote

mrbrown wrote:
The default alignment for PSP newlib malloc() is 16 bytes, or 128 bits.


That's good to know. As long as it stays that way, a malloc or new is fine.
Back to top
View user's profile Send private message AIM Address
maroxe



Joined: 01 Sep 2009
Posts: 18

PostPosted: Thu Sep 03, 2009 11:56 pm    Post subject: Reply with quote

Thank's all, it's clearer know
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP 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