[Freeciv-Dev] Re: [PATCH] is_normal_tile function
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
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
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, (continued)
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Gregory Berkolaiko, 2001/08/15
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Raimar Falke, 2001/08/15
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Gaute B Strokkenes, 2001/08/15
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Gregory Berkolaiko, 2001/08/16
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Ross W. Wetmore, 2001/08/16
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Trent Piepho, 2001/08/15
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Tony Stuckey, 2001/08/15
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Miguel Farah F., 2001/08/15
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Roy Tate, 2001/08/15
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Raimar Falke, 2001/08/16
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function,
Ross W. Wetmore <=
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Raimar Falke, 2001/08/17
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Ross W. Wetmore, 2001/08/17
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Raimar Falke, 2001/08/17
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Ross W. Wetmore, 2001/08/16
- [Freeciv-Dev] Re: [PATCH] is_normal_tile function, Raimar Falke, 2001/08/17
|
|