Why memalign?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
maroxe
Posts: 18
Joined: Tue Sep 01, 2009 6:28 pm

Why memalign?

Post by maroxe »

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: Select all

struct Vertex {
     float u, v;
     unsigned int color;
     float x, y, z;
};
this is how i should allocate memory:

Code: Select all

 vertices = (Vertex*) memalign(16, 3*sizeof(Vertex));
and this is the call of drawing function:

Code: Select all

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?
User avatar
Ameen
Posts: 10
Joined: Sun Jun 14, 2009 6:28 am
Location: Bahrain
Contact:

Post by Ameen »

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.
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

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
psPea
Posts: 60
Joined: Sat Sep 01, 2007 12:51 pm

Post by psPea »

I know that malloc allocates memory aligned on 16 bytes boundary, so maybe new does too.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

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.
psPea
Posts: 60
Joined: Sat Sep 01, 2007 12:51 pm

Post by psPea »

>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.
maroxe
Posts: 18
Joined: Tue Sep 01, 2009 6:28 pm

Post by maroxe »

thanks for you reply.
so i must understand that i always have to use memalign(16, whatever) instead of new/malloc?
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

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. :)
maroxe
Posts: 18
Joined: Tue Sep 01, 2009 6:28 pm

Post by maroxe »

I red in the doc of sceGuDrawArray:
Every vertex must align to 32 bits, which means that you HAVE to pad if it does not add up!
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

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..."
maroxe
Posts: 18
Joined: Tue Sep 01, 2009 6:28 pm

Post by maroxe »

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

Post by mrbrown »

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..."
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

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.
maroxe
Posts: 18
Joined: Tue Sep 01, 2009 6:28 pm

Post by maroxe »

Thank's all, it's clearer know
Post Reply