Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown Newsgroups: comp.arch Subject: Re: Computer architects leaving Intel... Date: Fri, 6 Sep 2024 09:17:45 +0200 Organization: A noiseless patient Spider Lines: 54 Message-ID: References: <2024Aug30.161204@mips.complang.tuwien.ac.at> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 06 Sep 2024 09:17:45 +0200 (CEST) Injection-Info: dont-email.me; posting-host="6ee2407e3ebfa0481e4977a48a1e13c1"; logging-data="775875"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19AbFinMyhPoffjGo9nvlR6LJGbSi8FHko=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Cancel-Lock: sha1:abVP05BkXgba4AbGoMbn2XV4ggE= In-Reply-To: Content-Language: en-GB Bytes: 3195 On 05/09/2024 23:39, Scott Lurndal wrote: > Bernd Linsel writes: >> On 05.09.24 19:04, Terje Mathisen wrote: >>> One of my alternatives are >>> >>> unsigned u = start; // Cannot be less than zero >>> if (u) { >>> u++; >>> do { >>> u--; >>> data[u]... >>> while (u); >>> } >>> >>> This typically results in effectively the same asm code as the signed >>> version, except for a bottom JGE (Jump (signed) Greater or Equal instead >>> of JA (Jump Above or Equal, but my version is far more verbose. >>> >>> Alternatively, if you don't need all N bits of the unsigned type, then >>> you can subtract and check if the top bit is set in the result: >>> >>> %G�%@| for (unsigned u = start; (u & TOPBIT) == 0; u--) >>> >>> Terje >>> >> >> What about: >> >> for (unsigned u = start; u != ~0u; --u) > > This is the form we use most when we need > to work in reverse. > In a code review, I would reject that - and all the other nonsenses suggested here as a way to force all loop indices to be unsigned types as though that rule was the 11th commandment. Just write code that makes sense - it's /not/ hard in this case! for (int i = start; i >= 0; i--) ... If you need the loop counter to be an unsigned type inside the loop code, make an unsigned version: for (int i = start; i >= 0; i--) { const unsigned int u = i; ... } Sometimes it amazes me the kind of nonsense people write in code because of obsession about particular rules. Code clarity trumps /all/ stylistic rules.