Computer Chess Club Archives


Search

Terms

Messages

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

Author: Omid David Tabibi

Date: 17:33:18 07/05/03

Go up one level in this thread


On July 05, 2003 at 16:16:54, Russell Reagan 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?
>
>Maybe some of the instructions are very processor dependant, because yours was
>just as fast as the math.h abs() on my Athlon 2GHz.
>
>I grabbed several absolute value functions from various newsgroup archives and
>timed them. I ran the program five times to try and get an accurate timing, then
>took the fastest time (as long as it was consistent with the other times), then
>I sorted them.
>
>Regarding the table below: The checksum ensures that the function is working
>correctly, and that the compiler didn't inline away the calls because the result
>wasn't being used. The "nothing" timing just summed up the random numbers
>without calling any absolute value function, so we could get a kind of "base
>time". abs() is the math.h version. I used MSVC++ 6 Pro with "maximize speed",
>as well as "inline any suitable".
>
>     Function   Checksum   Time
>============== ========== ======
>       nothing 3951541892 4.776
>         abs() 1713113360 6.039
>    omid_abs() 1713113360 6.039
>     cdq_abs() 1713113360 6.279
>   cmovs_abs() 1713113360 6.289
>     sbb_abs() 1713113360 6.539
>   cmovl_abs() 1713113360 6.789
>     sar_abs() 1713113360 7.531
>  simple_abs() 1713113360 9.654
>    fish_abs() 1713113360 11.376
>
>You can get the file that with all of the functions and tests from:
>http://home.comcast.net/~russell.reagan/abs.c
>
>Maybe someone can run it on a different processor or with a different compiler
>and we can compare results (see which is fastest, not compare timings). I'll
>also post it here.
>

An interesting test! Here are my results on P3 733:
(with inlining):

       nothing 3951541892 19.658
         abs() 1713113360 20.700
  simple_abs() 1713113360 37.594
    omid_abs() 1713113360 24.565
     sbb_abs() 1713113360 28.571
     cdq_abs() 1713113360 24.515
    fish_abs() 1713113360 45.536
     sar_abs() 1713113360 26.448
   cmovl_abs() 1713113360 26.217
   cmovs_abs() 1713113360 26.759


The profiler I used (Intel VTune) indicated that my abs() is about twice slower
than math.h abs(), the reason is most probably because inlining was off, and I
was running the program in debug mode (no optimizations). I haven't managed to
use the VTune profiler without debug mode...





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.