Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2002:
[Freeciv-Dev] Re: (PR#1180) [PATCH] cleanup to canvas_pos_to_map_pos
Home

[Freeciv-Dev] Re: (PR#1180) [PATCH] cleanup to canvas_pos_to_map_pos

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: jdorje@xxxxxxxxxxxxxxxxxxxxx
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#1180) [PATCH] cleanup to canvas_pos_to_map_pos
From: "Gregory Berkolaiko via RT" <rt@xxxxxxxxxxxxxx>
Date: Sun, 24 Nov 2002 08:00:35 -0800
Reply-to: rt@xxxxxxxxxxxxxx

On Sat, 23 Nov 2002, Ross W. Wetmore wrote:

> It at least recognizes the positive denominator :-).
> 
> But try this as a way to overcome the "%" anti-pattern bias ...
> 
> /*
>  * DIVIDE() divide and round down (== more negative), rather than just
>  *   divide (round toward 0).  It is assumed the divisor is positive.
>  *   Arguments are used multiple times, so must not have side effects.
>  * The full case would be ...
>  * DIVIDE_GENERIC(n, d)  ( (d) < 0 ? DIVIDE(-(n), -(d)) : DIVIDE((n), (d)) )
>  */
> #define DIVIDE(n, d) \
>   ( (n) < 0 ? ((n) - (d) + 1) / (d) : (n) / (d) )
>   -- or --
>   ( (n) < 0 ? ((n) + 1) / (d) - 1 : (n) / (d) )


Stupid me!  I knew there is a way to do it without a % b !!

However, it doesn' make much difference in terms of time:

with 
#define DIVIDE_G1(a, b) ( a/b - ( a<0 && (a%b != 0) ) )

test code takes
2.150u 0.000s 0:02.14 100.4%    0+0k 0+0io 75pf+0w

and with 

#define DIVIDE_R(n, d) ( n < 0 ? (n - d + 1) / d : n / d )

takes
2.060u 0.000s 0:02.04 100.9%    0+0k 0+0io 75pf+0w


The test code:

  for( i = 0; i < 10000000; i++) {
    int v, d = (rand() - RAND_MAX/2);
    v = DIVIDE(d, 7);
  }





[Prev in Thread] Current Thread [Next in Thread]