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++ > abs() with math.h?

#24195 - cyberg - Thu Jul 29, 2004 3:16 pm

Hi, I would like to know if it's better to use abs() of math.h or to code myself the function. I don't need it for a time critical section of my code so a little overhead is not the end of the world, but I just need to know if the function won't be like 10 times slower than coding it myself. And I'd like to know if it's the same for all the functions implemented in math.h and other c++ libraries!

Thank you!
_________________
Etienne Houle

#24197 - FluBBa - Thu Jul 29, 2004 3:44 pm

I dont know how it's implemented in math.h

Here's how to do it in ARM code for 32bit ints:
Code:

tst r0,#0x80000000
rsbne r0,r0,#0

_________________
I probably suck, my not is a programmer.

#24198 - poslundc - Thu Jul 29, 2004 3:58 pm

You can code it yourself as a C macro:

#define abs(a) (((a) < 0) ? -(a) : (a))

At least by inlining it like this you save yourself the overhead of a function call when using the standard library version of it.

Dan.

#24203 - tepples - Thu Jul 29, 2004 5:21 pm

Except your abs(a) macro doesn't work when a has side effects. Rather, I'd code it like this, placed in the project's header file:
Code:
static inline unsigned int abs(int a)
{
  return (((a) < 0) ? -(a) : (a));
}

Still, I'd guess that the standard library uses C inline functions. However, be careful, as bringing in <math.h> may bring in the entire bulk of the floating-point library with it.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#24209 - Miked0801 - Thu Jul 29, 2004 7:52 pm

Beware of Macro ABS calls - you can get burned:

Code:

int i, j, k;

i = 10983240
j = -387594875

k = ABS(i /j + i*j + IamAHugeFunctionCallWithBigOverhead(i, j));



A macro will paste and run the above up to 3 times...

#24266 - sgeos - Sat Jul 31, 2004 6:06 am

Things get even worse with random numbers. Consider this:
Code:
hp = abs(hp - random_number(0, max_hp / 2));

Can anyone come up with a macro that gives the desired results?

-Brendan

#24269 - tepples - Sat Jul 31, 2004 6:22 am

The inline function will give the result you want, as inline functions evaluate each argument exactly once.

Or if you insist on a macro, you can use a GCC extension to create blocks of code within an expression:
Code:
#define abs(x) ({long int x_1 = x; (x_1 < 0 ? -x_1 : x_1)})

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

#24278 - wintermute - Sat Jul 31, 2004 11:01 am

abs is a gcc-builtin.

#include <math.h> won't link in anything you don't use

#24281 - f(DarkAngel) - Sat Jul 31, 2004 2:45 pm

Another bad case for normal macro call would be abs(x++);
_________________
death scream...

#24342 - Miked0801 - Sun Aug 01, 2004 9:30 pm

Lol - indeed :)

#24686 - wintermute - Mon Aug 09, 2004 7:46 pm

oh yeah, abs isn't in <math.h>

it's in <stdlib.h>