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: "Ross W. Wetmore" <rwetmore@xxxxxxxxxxxx>
Cc: Roy Tate <roy_tate@xxxxxxxxx>, Trent Piepho <xyzzy@xxxxxxxxxxxxx>, freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: [PATCH] is_normal_tile function
From: Raimar Falke <hawk@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 17 Aug 2001 11:41:50 +0200
Reply-to: rf13@xxxxxxxxxxxxxxxxxxxxxx

On Thu, Aug 16, 2001 at 08:15:04PM -0400, Ross W. Wetmore wrote:
> 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.

I'm happy with all this if all this stuff is encapsulated like in the
RANGE_CHECK macro above. It would even better if we supply a simple
version of RANGE_CHECK which every programmer can process in 2 seconds
and on optimized version.

> 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.

No problem here: ALL UPPERCASE means macro.

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

I don't understand this one.

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx


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