Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2001:
[Freeciv-Dev] Re: [PATCH] is_normal_tile function
Home

[Freeciv-Dev] Re: [PATCH] is_normal_tile function

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: rf13@xxxxxxxxxxxxxxxxxxxxxx
Cc: Roy Tate <roy_tate@xxxxxxxxx>, Trent Piepho <xyzzy@xxxxxxxxxxxxx>, freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: [PATCH] is_normal_tile function
From: "Ross W. Wetmore" <rwetmore@xxxxxxxxxxxx>
Date: Thu, 16 Aug 2001 20:15:04 -0400

At 09:32 AM 01/08/16 +0200, Raimar Falke wrote:
>On Wed, Aug 15, 2001 at 04:51:56PM -0700, Roy Tate wrote:
>> Perhaps we should have a macro
>> 
>> #define RANGE_CHECK (p1, p2) ((unsigned)(p1) < (unsigned)(p2))
>> 
>> Then the code would be
>> 
>>  return RANGE_CHECK(y, map.ysize) && RANGE_CHECK(x, map.xsize);
>
>I thought about the same.
>
>> Although this doesn't really tell the reader that y is checked
>> against map size.
>
>Another way:
>
>/*
> * Returns true iff n \element [min_n,max_n)
> */
>#define RANGE_CHECK(min_n, n, max_n) (min_n==0?(unsigned int)n<(unsigned
int)max_n:(n>=min_n && n<max_n))
>
>       Raimar
>
>-- 
> email: rf13@xxxxxxxxxxxxxxxxx
> "Premature optimization is the root of all evil."
>    -- D. E. Knuth in "Structured Programming with go to Statements"

I know you hate this, but if you look at most machine instruction sets
they have indexing operations that are perverted into 3-way adds by gcc
and other compilers. So, ...

#define RANGE_CHECK(min_n, max_n, nn)  \
  ((unsigned)((nn) - (min_n)) < (unsigned)((max_n) -(min_n)))

is often just as efficient as the zero-based version, i.e. when both
of the two range values are constants, and sometimes when not. The loads 
do the indexed add at the same time.

There are also only 3 operation in this, so it is comparable to or 
better than the other.

This suffers from needing to access min_n twice, which makes it pretty
much necessary to have it be a simple value and not expression.

This also means that when people cleanup map.xsize-1 they are usually 
wasting their time :-).

Cheers,
RossW




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