Jump to content
  • Advertisement

Archived

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

kelaklub

Absolute Value

This topic is 5490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!