Sujet : Re: Computer architects leaving Intel...
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.archDate : 07. Sep 2024, 13:51:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86seubmxsy.fsf@linuxsc.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Thomas Koenig <
tkoenig@netcologne.de> writes:
Scott Lurndal <scott@slp53.sl.home> schrieb:
>
David Brown <david.brown@hesbynett.no> writes:
>
On 05/09/2024 11:12, Terje Mathisen wrote:
>
That last paragraph enumerates pretty much all the uses I have for
integer-type variables, with (like Mitch) a few apis that use (-1) as an
error signal that has to be handled with special code.
>
You don't have loop counters, array indices, or integer arithmetic?
>
We do. There is no issue using unsigned loop counters,
>
I find counting down from n to 0 using unsigned variables
unintuitive. Or do you always count up and then calculate
what you actually use? Induction variable optimization
should take care of that, but it would be more complicated
to use.
In most cases of counting down the upper bound is one more
than the value to be used, reflecting a half-open interval.
These ranges are analogous to pointers traversing arrays
downwards:
int stuff[20];
for( int *p = stuff+20; p > stuff; ){
p--;
.. do something with *p ..
}
For pointers it's important that the pointer not "fall off the
bottom" of the array. That needn't apply to unsigned index
variables, so the decrement can be absorbed into the test:
int stuff[20];
for( unsigned i = 20; i-- > 0; ){
.. do something with stuff[i] ..
}
If you adopt patterns similar to this one I think you will
get used to it quickly and it will start to seem quite
natural. Counting down is the mirror image of counting
up. When counting up we "point at" and increment after using.
When counting down we "point after" and decrement before using.
Using half-open intervals also comes up in binary search:
int stuff[N];
unsigned low = 0, limit = N;
while( low+1 != limit ){
unsigned m = low + (limit-low)/2;
.. test stuff[m] and pick one of ..
.. low = m .. (or)
.. limit = m ..
}
.. stuff[low] has the answer, if there is one ..
At each point in the search we are considering a half-open
interval. That makes writing (or reading) invariants for
the code very easy. When low+1 == limit then there is only
one element to consider.