Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Implementation of the abs() function [o.t.]

Author: Robert Hyatt

Date: 07:34:53 07/07/03

Go up one level in this thread


On July 06, 2003 at 14:05:03, Omid David Tabibi wrote:

>On July 06, 2003 at 13:12:25, Robert Hyatt wrote:
>
>>On July 05, 2003 at 10:17:38, Omid David Tabibi wrote:
>>
>>>In Genesis I heavily use the abs() function, and so tried to optimize it.
>>>Instead of using the abs() function defined in <math.h>, I wrote the following
>>>fucntion:
>>>
>>>long abs(long x) {
>>>    long y;
>>>    y = x >> 31;
>>>    return (x ^ y) - y;
>>>}
>>>
>>>Testing it using a profiler, I found out that my implementation is about twice
>>>slower than the math.h implementation of abs(). I haven't looked at the
>>>implementation in math.h, but I can't see how a more optimized version of abs()
>>>can be written.
>>>
>>>Any ideas?
>>
>>
>>#define abs(x)    ((x<0) ? -x : x)
>>
>>I think the compiler will do that about as well as anything can be done, if
>>you have a good compiler.  No branch needed if it is optimized right.
>
>What's the difference between the above macro and the function
>
>int simple_abs (int i) {
>    return (i >= 0) ? i : -i;
>}
>
>when inlined?

No difference that I know of, if you have inlining enabled.


>
>Russel's test (http://talkchess.com/forums/1/message.html?304930) shows that
>this simple_abs() is slow in comparison to various other abs implementations.

Maybe it depends on the compiler too.  Using CMOV ought to make this as fast
as any other approach.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.