gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Coding style: abbreviated data types?

#13682 - poslundc - Sat Dec 20, 2003 5:14 pm

When I first started coding for the GBA I was quick to pick up on and use the abbreviated data types defined in gba.h, namely u8, u16, u32, s8, s16, s32 and the variants thereof.

Now that I've been programming a while longer I've noticed that I've pretty much abandoned those in favour of using the full definitions (unsigned char, short, int, etc.). Now I even find that I'm going back to a lot of my old code and changing occurrences of the former to the latter.

It's kind of interesting, actually. I'm not entirely sure why I prefer the latter style. The former is both brief and descriptive and easy to read, and potentially more compatible with other platforms that have different word sizes. Maybe in my old age I just prefer to avoid unnecessary data type definitions, or something. :P

I was wondering if anyone else had followed a similar pattern that I have. Which style do you use, and why?

Dan.

#13683 - sgeos - Sat Dec 20, 2003 5:44 pm

Thee u16, s8, etc types are the correct way of doing things. If the code needs a varaible to have at least a certain number of bits, the type should be hidden behind a typedef. That way if the code is ported, the typedef can be changed if need be and the code will still work.

Needless to say, I use the descriptive types as well. =P I suspect I'll keep doing that until I get burned by it.

-Brendan

#13684 - Miked0801 - Sat Dec 20, 2003 5:58 pm

I've been at this awhile and I've never been burned by using s8/s16/s32 notation. It's the way to go. Using this, it made it real easy to port some (generic) PS1 code to the GBA.

#13685 - poslundc - Sat Dec 20, 2003 6:13 pm

Miked0801 wrote:
Using this, it made it real easy to port some (generic) PS1 code to the GBA.


Why? My understanding is that the PS1 had a 32-bit word length, just like the GBA.

I guess another way of phrasing the issue would be to ask what the point is of having the primitive types, if you are just going to typedef them anyway. Maybe C shouldn't have generic primitives in favour of bit-specific primitives?

Dan.

#13686 - sgeos - Sat Dec 20, 2003 6:15 pm

Miked0801 wrote:
I've been at this awhile and I've never been burned by using s8/s16/s32 notation. It's the way to go. Using this, it made it real easy to port some (generic) PS1 code to the GBA.


I don't suspect you'll get burned using the typedefs. I'm saying that I'll probably keep using "signed short" instead of "s16" until I get burned. If I'm really lucky, I'll learn from this conversation and start using s8/s16/s32 notation right away before I get burned by not doing so.

-Brendan

#13692 - sajiimori - Sat Dec 20, 2003 7:41 pm

I use int for everything unless there's a need for a particular size, in which case I use typedefs.

I used to try to use the smallest size that I figured I would need, and unsigned types when a negative value wouldn't make sense, but I frequently discovered that my program actually creates more than 256 widges on occasion, or that -1 is a useful label for "invalid state", or that counting down to 0 is easier to do if the terminating condition is "i < 0".

Besides, int is generally the native word size of a platform, and it can sometimes be faster to work with them.

#13693 - tepples - Sat Dec 20, 2003 8:18 pm

sajiimori wrote:
or that counting down to 0 is easier to do if the terminating condition is "i < 0"

Unless you decrement your index variable either after testing it for termination:
Code:
do {
  /* ... */
} while(i--);

or before the body of the loop:
Code:
do {
  i--;
  /* ... */
} while(i);

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#13716 - sajiimori - Sun Dec 21, 2003 8:03 am

Technically, that expresses a slightly different idea -- the body of your loops always execute at least once. Anyway, the point is that I don't like worrying about such details, which is why I just use plain ints 99% of the time.

#13719 - torne - Sun Dec 21, 2003 2:34 pm

sajiimori wrote:
Technically, that expresses a slightly different idea -- the body of your loops always execute at least once. Anyway, the point is that I don't like worrying about such details, which is why I just use plain ints 99% of the time.


If you don't want them to execute at least once, just move the while to the beginning. Both examples work perfectly that way around too.

#13722 - DekuTree64 - Sun Dec 21, 2003 4:25 pm

I love u32/s32/etc types, less typing for me. Especially when doing stuff like loading files, where some things are 8 bits, some are 32, and you can just keep a u8 pointer and increment it as you load things, casting to a u32 pointer if you need a 32 bit value or whatever. Plus I prefer more low-level programming than high-level, so they make it perfectly clear what kind of assembly instructions will be generated to deal with it.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#13724 - poslundc - Sun Dec 21, 2003 4:46 pm

sajiimori wrote:
I use int for everything unless there's a need for a particular size, in which case I use typedefs.


I think that perhaps so far this is the most sensible suggestion I've heard. If you don't care about the size/signage, then you may as well just use an int, which is the widely recognized type for "just another variable". If you do care, then use a typedef to be specific about it.

Sad that I can't seem to stop writing the entire type declarations out. I think I just find the longer versions more aesthetic.

Dan.

#13737 - torne - Mon Dec 22, 2003 12:35 am

int will always generate a 32-bit variable with an ARM target, however, and access to EWRAM is 16-bit. Thus, you will make your code unneccecarily slow if you use int for values in EWRAM which do not actually require more than 16 bits.

#13739 - poslundc - Mon Dec 22, 2003 2:45 am

Yeah, but the stack defaults to IWRAM in DKA, where you need to keep 32-bit alignment anyway.

Dan.

#13760 - torne - Mon Dec 22, 2003 1:27 pm

Only local vars go on the stack, tho. =)

#13767 - poslundc - Mon Dec 22, 2003 3:50 pm

And global vars still get put in IWRAM. :)

Dan.

#13792 - torne - Mon Dec 22, 2003 11:18 pm

Not for me; perhaps I have a different link script. There wouldn't be room for them all in IWRAM. =)