Skip to main content
gamedev.net
🔒 Locked

32bit Alignment

Started by jacksonh Aug 7, 2001 at 10:13 AM 4 replies 900+ views
Original Post
jacksonh
jacksonh
If I had a function: size_t size; byte *p, *newp; newp = p + size; how can I guarantee that newp is 32bit aligned?
BeerNutts
BeerNutts
newp = ((newp >> 2) << 2);

That will put you on a 4 byte alignment.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
jacksonh
jacksonh
Hey thanks I was expecting to get flamed for that question.
Kylotan
Kylotan
I think the fact that size_t and byte* are (almost certainly) 32-bit variables will mean that they are aligned on a 32-bit boundary anyway. But I don''t technically know how to force this.
BeerNutts
BeerNutts
Kylotan,

While (byte *) is a 4 byte value (since it holds an address), it''s the address that needs to be on a 4-byte alignment. It''s a big deal for anyone who has done any MIPS low-level programming (since all Longs must be on 4 byte boundary, Words on 2 byte boundary, etc.).
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
Kylotan
Kylotan
BeerNutts, I know what you''re getting at, but I was just making the point that compilers tend to start 4-byte variables on 4-byte boundaries anyway (they are just as conscious of speed as assembly programmers ). For example, I don''t think Visual C++ puts any data across a 4-byte boundary by default: you have to use #pragma pack to get it to do so, otherwise it uses empty padding inbetween.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.