Deutsch   English   Français   Italiano  
<86seubmxsy.fsf@linuxsc.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups: comp.arch
Subject: Re: Computer architects leaving Intel...
Date: Sat, 07 Sep 2024 05:51:41 -0700
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <86seubmxsy.fsf@linuxsc.com>
References: <2024Aug30.161204@mips.complang.tuwien.ac.at> <vb0kh2$12ukk$1@dont-email.me> <vb3smg$1ta6s$1@dont-email.me> <vb4q5o$12ukk$3@dont-email.me> <vb6a16$38aj5$1@dont-email.me> <vb7evj$12ukk$4@dont-email.me> <vb8587$3gq7e$1@dont-email.me> <vb91e7$3o797$1@dont-email.me> <vb9eeh$3q993$1@dont-email.me> <vb9l7k$3r2c6$2@dont-email.me> <vba26l$3te44$1@dont-email.me> <vbag2s$3vhih$1@dont-email.me> <vbbnf9$8j04$1@dont-email.me> <vbbsl4$9hdg$1@dont-email.me> <vbcbob$bd22$3@dont-email.me> <VViCO.60793$L1Tb.44990@fx11.iad> <vbcgve$cd1g$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Sat, 07 Sep 2024 14:51:44 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="15cdd4985ebd3d04d209b9ff3a6e4cfe";
	logging-data="1461032"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18BOjNekNcSAoDOLTPZAaghGAUwn709mAk="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:vPlycxiIU0VmwBUdHyahyo9qtd0=
	sha1:s5rCcN91Uq6cXiwyQohUAnQkb1I=
Bytes: 3763

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.