Computer Chess Club Archives


Search

Terms

Messages

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

Author: Vincent Diepeveen

Date: 10:49:25 07/05/03

Go up one level in this thread


On July 05, 2003 at 10:58:00, Rafael Andrist 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?
>
>I also tried to optimize the abs() function and found out that the compiler
>already optimized it the right way in most of the cases. The compiler usually
>inlines the abs() function and generates the fastest code. You should look at
>the asm code which may be different from case to case.
>
>If you use abs() to measure (small) distances, you can also try a look-up-table

Assuming that table isn't used much the assembly code is going to run faster of
course.

>regards
>Rafael B. Andrist



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.