• ### Announcements

#### Archived

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

# Absolute Value

## Recommended Posts

kelaklub    160
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 on other sites
Miserable    606
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 on other sites
quasar3d    814
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 on other sites
davepermen    1047
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

##### Share on other sites
kelaklub    160
Thanks guys, regardless of whichever works better or faster, it''s always nice to have options.

##### Share on other sites
kelaklub    160
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 on other sites
quasar3d    814
fast way to calculate the absolute value for an int:

mov eax,i

cdq
xor eax,edx
sub eax,edx

mov i,eax

My Site

##### 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 on other sites
quasar3d    814
branching is slow

My Site

##### Share on other sites
kelaklub    160
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 on other sites
drslush    122
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 on other sites
kelaklub    160
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 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 on other sites
quasar3d    814
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