Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch 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> 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 writes: > Scott Lurndal schrieb: > >> David Brown 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.