Deutsch   English   Français   Italiano  
<vc4r24$1lfnh$1@dont-email.me>

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

Path: ...!3.eu.feeder.erje.net!feeder.erje.net!news.in-chemnitz.de!news2.arglkargh.de!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bernd Linsel <bl1-thispartdoesnotbelonghere@gmx.com>
Newsgroups: comp.arch
Subject: Re: Computer architects leaving Intel...
Date: Sat, 14 Sep 2024 22:18:12 +0200
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <vc4r24$1lfnh$1@dont-email.me>
References: <2024Aug30.161204@mips.complang.tuwien.ac.at>
 <UxpCO.174965$Hld5.7714@fx15.iad> <vc41rl$1fhjd$1@dont-email.me>
 <vc4mgj$1khmk$1@dont-email.me> <vc4pqg$1l8mq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 14 Sep 2024 22:18:12 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="1797601827e0d8f73050fd17c9ac3a48";
	logging-data="1752817"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1++5fX0YIEErvHtGQSE16R0"
User-Agent: Betterbird (Linux)
Cancel-Lock: sha1:NvmEIO3P40NIykqyF3T1CTOHj8s=
In-Reply-To: <vc4pqg$1l8mq$1@dont-email.me>
Content-Language: en-US
Bytes: 2794

On 14.09.24 21:57, Kent Dickey wrote:
> In article <vc4mgj$1khmk$1@dont-email.me>,
> Thomas Koenig  <tkoenig@netcologne.de> wrote:
>> Kent Dickey <kegs@provalid.com> schrieb:
>>
>>> When you write code working on signed numbers and do something like:
>>>
>>> 	(a < 0) || (a >= max)
>>>
>>> Then the compiler realizes if you treat 'a' as unsigned, this is just:
>>>
>>> 	(unsigned)a >= max
>>
>> For which definition of a and max exactly?
>>
>> It coertainly does not do so for
>>
>> _Bool foo(int a, int max)
>> {
>>     return (a < 0) || (a >= max);
>> }
> 
> Sorry, I should have made it clear for max >= 0 (but not necessarily an
> unsigned variable), and for my code, a constant, which is how the
> compiler knows it's positive .  I have this in my code all the time to
> validate function inputs--a negative number is bad, and a number beyond
> a certain reasonable value is bad.  And I let the compiler optimize the
> check to (unsigned)a >= (unsigned)max.
> 
> Kent

And that's the information the compiler was missing to optimize foo() in 
the same way:

_Bool foo1(int a, int max)
{
     if (__builtin_expect(max < 0, 0)) __builtin_unreachable();

     return a < 0 || a >= max;
}


_Bool foo2(int a, int max)
{
     return (unsigned)a >= (unsigned)max;
}

compiles to:

foo1:
         cmp     edi, esi
         setnb   al
         ret
foo2:
         cmp     edi, esi
         setnb   al
         ret

(x64-64-gcc 14.2 -Wall -Wextra -Wpedantic -O3 -fexpensive-optimizations)

-- 
Bernd Linsel