GP2X Max() And Min()


MattPurland

Still Fresh
Joined
Jun 26, 2006
Messages
38
Hi, I'm trying to port a game I wrote in C++ using the Windows API (so yeah, a total re-write I guess). Part of the game used max() and min() from the stdlib for movement. I've tried using this in my game (using SDL) and even with the stdlib included it doesn't recognise the function. Are they available in the gp2x devkit? If not, is there anything else I can use?

Cheers!

p.s: as you can guess I've only just started learning to program :p
 
MattPurland posted on Jul 9 2006 at 05:18 PM said:
Hi, I'm trying to port a game I wrote in C++ using the Windows API (so yeah, a total re-write I guess). Part of the game used max() and min() from the stdlib for movement. I've tried using this in my game (using SDL) and even with the stdlib included it doesn't recognise the function. Are they available in the gp2x devkit? If not, is there anything else I can use?

Cheers!

p.s: as you can guess I've only just started learning to program :p
you can probably just use something like

#define max(a,B) (((a)>(B)) ? (a) : (B))
#define min(a,B) (((a)<(B)) ? (a) : (B))
 
Last edited by a moderator:
Ah! All sorted now, just needed to define it myself! Someone delete this topic!

EDIT: dzz: Quick reply, cheers ;)
 
MattPurland posted on Jul 9 2006 at 05:26 PM said:
Ah! All sorted now, just needed to define it myself! Someone delete this topic!

EDIT: dzz: Quick reply, cheers ;)
whoops, my code got smilified
 
Last edited by a moderator:
there are alternatives for min and max that are faster/slower on architectures with different branch costs. i dont have a build environment to test, and im not very familiar with ARM yet, but try these:

#define min(x,y) (y) + (((x) - (y)) & -((x) < (y)))
#define max(x,y) (x) - (((x) - (y)) & -((x) < (y)))

or, if you know that INT_MIN <= x - y <= INT_MAX...

#define min(x,y) (y) + (((x) - (y)) & (((x) - (y)) >> (sizeof(int) * CHAR_BIT - 1)))
#define max(x,y) (x) - (((x) - (y)) & (((x) - (y)) >> (sizeof(int) * CHAR_BIT - 1)))

all thanks to the bit twiddling hacks page :)
 
properlly optimized, the turnary-operator version might actually be faster than that second version, Sparr. Anybody know what GCC outputs for those two?
 
Back
Top