Can't get absolute values using macros.

Started by
6 comments, last by KurtCPP 20 years, 8 months ago
Hi Avery-1. (wouhouu!! what a fashion writin'' !) Since I don''t wanna use a function I don''t even know what''s inside, I thought writing a simple macro to get abs. values would be better (or faster than using the overriden-generic-superpowerful-and-so-on function coming from Microsoft). However, it seems that my macro doesnt work (??!!).

 Here are some of the codes I''ve tried :
#define ABS(a)  ( (a>0) ? (a) : (-a))
#define ABS(a)  ((a)>(0)) ? (a) : (-a))
#define ABS(a)	( ( ( a ) > 0  ) ? ( a ) : ( -a ) )
/*I''ve also tried all sorts of combinations with the parenthesis (put one there but omit the other one and so on) but it really doesnt work.
You can see below the code that uses this macro : */

#define  X(a)		( ( a ) % 8  )
#define  Y(a)		( ( a ) / 8  )
/*I also tried to stick more parenthesis above, but nothing...*/

return(ABS(X(nDest) - X(nSrc)) == 1 && ABS(Y(nDest) - Y(nSrc)) == 2
		|| ABS(X(nDest) - X(nSrc)) == 2 && ABS(Y(nDest) - Y(nSrc)) == 1);

I suppose the error comes from the combination of macros, but I can''t fix it. Yet, everything works OK with the function abs(...) but I suppose it is a bit slower since it features things I dont need. Prog, Hex & Rock''n''Roll : I don''t like the Prog but the Prog likes me. Some nice poetry to sweeten your spirit and relax a bit before programming
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
Advertisement
Hello,

Why don't you use the already built-in C++ abs function?
Or, you can also write yours. Don't use macro, it's not really worth it and it will be optimised to the same in the end.

In any case, I wouldn't really worry about efficiency as the optimiser will take care of it.

And you're missing parenthesis in your macro definition. Try this instead:

#define ABS(a)  ( (a>0) ? (a) : (-(a)))  


So that the unary minus is applied to the whole 'a'. That's why macros are very evil.

Robin


[edited by - Lawgiver on August 5, 2003 2:39:22 PM]
Not worth it. abs is an intrinsic function of the MSVC compiler. This means it is fully inlined with no function call being generated.

Using abs() will probably be faster than yours also as using the trinary operator will result in a branch or a cmov whereas abs will probably compile to an x86 specific optimisation such as clearing the high bit or issuing the fabs opcode
Macros are evil. Do not use them. The function call to abs() shouldn't be a performance drain on your application. If your only objection is the black-box nature of the stand library routine you can always write your own:

int MyAbs(int val){    return (val > 0 ? val : -val);} 


If your using macros as an optimization technique, you should first profile your code and figured where the bottle-neck is (premature optimization is even more evil than macros). If it turns out that the calls to abs() or MyAbs() are slowing you down, an inline function (assuming this is C++) is much better than a macro in all respects (type safety, evaluating arguments once and only once, spaning multiple lines, ability to debug, ability to put in a namespace, ...):

inline int MyAbs(int val){    return (val > 0 ? val : -val);} 


Here's a good discussion of your exact situation (minus the mod and division operations) with links to several other macro gotcha's.


[edited by - GrinningGator on August 5, 2003 6:35:16 PM]
Hi. You may not read this post anymore but I still want to thank you all for your help. I didnt really know my macro could be that bad (although I knew it couldnt be much faster than abs() note that I have seen the code of this fxn it was in ASM and looked kinda quick too).
Actually, my true problem was rather that the macro didnt work whereas everything looked OK in it. LawGiver gave me the solution to this.
Now I think I won''t use so much macros anymore.
Thanks again to you all !!

Prog, Hex & Rock''n''Roll :
I don''t like the Prog but the Prog likes me.
Some nice poetry to sweeten your spirit and relax a bit before programming
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
quote:Original post by Lawgiver
#define ABS(a)  ( (a>0) ? (a) : (-(a)))   


Close, but still not good enough -- "(a>0)" needs to be "(a)>0". Also, a minor improvement is to use ">=" rather than :
#define ABS(a)  ( (a) >= 0 ? (a) : -(a) )  
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Thank you for this precision, indeed you look right, but I cant really get why you prefer using >= instead of >. Do you hace more explanations please??
Thanks in advance. :-P

Prog, Hex & Rock''n''Roll :
I don''t like the Prog but the Prog likes me.
Some nice poetry to sweeten your spirit and relax a bit before programming
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.
quote:Original post by KurtCPP
Thank you for this precision, indeed you look right, but I cant really get why you prefer using >= instead of >. Do you hace more explanations please??
Thanks in advance. :-P


It''s a small optimization-you don''t spend any time inverting 0, since that just gives you 0 again.

-Odd the Hermit

This topic is closed to new replies.

Advertisement