Archived

This topic is now archived and is closed to further replies.

kelaklub

Absolute Value

Recommended Posts

Is it all right to use the standard C library absolute value functions for game and graphic programming or are there better more effecient ways to compute the absolute value of a given number. Thank you.

Share this post


Link to post
Share on other sites
The people who wrote your compiler presumably used the best and most efficient way available. Generally, unless library routines are very general-purpose in nature and your needs are very specific (or your compiler vendor is sub-par), attempting to beat your compiler''s library functions is probably a waste of time (except possibly as a learning experience).

(The exception to the above rule is, I suppose, if you are able to take advantage of assembly instructions not available to your compiler, which I doubt is going to apply in this case.)

Finally, remember that premature optimisation is the root of all evil.

Share this post


Link to post
Share on other sites
I don''t know how the compiler does it, but masking out the sign bit is much faster for me.

you know the highest bit of a floating point number is the sign bit, so if you make that bit 0, you get the absolute value:

float f = -5;
(*((DWORD*)&f)) &= 0x7FFFFFFF;

it could be that the version of my compiler is so slow because of the function call. I have msvc6 standard edition, and inline functions don''t work with it. maybe most compilers use an inline version, which is be faster than this one. I suggest you benchmarking it yourself.

My Site

Share this post


Link to post
Share on other sites
quote:
Original post by kelaklub
Is it all right to use the standard C library absolute value functions for game and graphic programming or are there better more effecient ways to compute the absolute value of a given number. Thank you.


you calculate the absolute number several million to billion times per frame? if so, you can start thinking about optimization of this function. but big chance is that you do something wrong then anyways, and should optimize the outer algorithm..

if its some 10 - 1000 times per frame, don''t even BOTHER!

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
So this will mask the sign bit of a floating point number, is there a way to also do it for integers, doubles?


float f = -5;
(*((DWORD*)&f)) &= 0x7FFFFFFF;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
how about the old fascion way?

if i < 0
i *= -1

i is the variable in question, but maybe thats just to slow.

Share this post


Link to post
Share on other sites
Would anyone know how to fix this. I am using the bloodshed dev-c++ compiler, and they claim that you have to enter inline assembly instructions in the AT&T not the Intel syntax, which I have done, but only if I make i global will this compile. Then too the result is inncorrect. Thank You.


int main()
{
int i = -5;

__asm("mov %eax,_i");
__asm("cdq");
__asm("xor %eax,%edx");
__asm("sub %eax,%edx");
__asm("mov _i,%eax");

cout<<"\n"<<i<<"\n";

system("PAUSE");
return 0;
}

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
how about the old fascion way?

if i < 0
i *= -1

i is the variable in question, but maybe thats just to slow.



I''d never do it that way, even if I don''t need speed. Why use a multiplication? Whats wrong with i = -i? i = -i will most likely translate into NEG AX (or some other register).

Share this post


Link to post
Share on other sites
quote:
Original post by drslush
quote:
Original post by Anonymous Poster
how about the old fascion way?

if i < 0
i *= -1

i is the variable in question, but maybe thats just to slow.



I''d never do it that way, even if I don''t need speed. Why use a multiplication? Whats wrong with i = -i? i = -i will most likely translate into NEG AX (or some other register).


Is this guaranteed that the following code will translate into NEG AX?

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable
The people who wrote your compiler presumably used the best and most efficient way available.


This generalization is not right in all situations. Most notably the stock compiler implementation of rand() is notoriously poor, even in otherwise good compilers'' C libraries. I don''t know about fabs() in particular (though I would assume your generalization does indeed hold), but applying this statement to the whole of the C library can lead to trouble.

quote:
Original post by Miserable
Finally, remember that premature optimisation is the root of all evil.


I couldn''t agree with this more. Before even thinking about optimizing at such a small level, run a profiler on your code to see where the optimizations are needed.


GG

Share this post


Link to post
Share on other sites
quote:
Original post by kelaklub
Would anyone know how to fix this. I am using the bloodshed dev-c++ compiler, and they claim that you have to enter inline assembly instructions in the AT&T not the Intel syntax, which I have done, but only if I make i global will this compile. Then too the result is inncorrect. Thank You.


int main()
{
int i = -5;

__asm("mov %eax,_i");
__asm("cdq");
__asm("xor %eax,%edx");
__asm("sub %eax,%edx");
__asm("mov _i,%eax");

cout<<"\n"<<i<<"\n";

system("PAUSE");
return 0;
}



in at&t syntax it''s

__asm("movl _i,%eax");
__asm("cdq");
__asm("xorl %edx,%eax");
__asm("subl %edx,%eax");
__asm("movl %eax,_i");

at&t uses the first operant as source and second as dest. intel uses the first operant as dest and the second as source.

My Site

Share this post


Link to post
Share on other sites