Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The University of California Statistics Department; where mean is normal, and deviation standard.


devel / comp.lang.c / Re: Shortcut operators

SubjectAuthor
* Shortcut operatorsStefan Ram
+* Re: Shortcut operatorsAndrey Tarasevich
|`- Re: Shortcut operatorsScott Lurndal
+- Re: Shortcut operatorsBen Bacarisse
`- Re: Shortcut operatorsStefan Ram

1
Shortcut operators

<Shortcut-20220408113128@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21102&group=comp.lang.c#21102

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Shortcut operators
Date: 8 Apr 2022 10:37:37 GMT
Organization: Stefan Ram
Lines: 10
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <Shortcut-20220408113128@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 8fcioLBRMeQYPLTyJcr8RQrLcQtV6d5i11ed0E/iDf7fA0
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 8 Apr 2022 10:37 UTC

The shortcut operators are the pride and joy of C programmers
because they can speed up the execution of programs.

But nowadays I often read that branches are expensive.

So, could it be that today some code such as "a > 2 & b < 7"
is usually faster than "a > 2 && b < 7" because it's free from
branches?

Re: Shortcut operators

<t2pqe4$f77$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21118&group=comp.lang.c#21118

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: Shortcut operators
Date: Fri, 8 Apr 2022 10:12:34 -0700
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <t2pqe4$f77$1@dont-email.me>
References: <Shortcut-20220408113128@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 8 Apr 2022 17:12:36 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d476d2804e85046277a885444e651f4b";
logging-data="15591"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/6IwHcKtz8ByJ152DaSzg"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:B3Bz5kUJ/8dAhKmq7OGntPcEo9s=
In-Reply-To: <Shortcut-20220408113128@ram.dialup.fu-berlin.de>
Content-Language: en-US
 by: Andrey Tarasevich - Fri, 8 Apr 2022 17:12 UTC

On 4/8/2022 3:37 AM, Stefan Ram wrote:
> The shortcut operators are the pride and joy of C programmers
> because they can speed up the execution of programs.
>
> But nowadays I often read that branches are expensive.
>
> So, could it be that today some code such as "a > 2 & b < 7"
> is usually faster than "a > 2 && b < 7" because it's free from
> branches?

Huh?

Firstly, if `b < 7` is a lightweight evaluation with no side-effects any
self-respecting compiler will optimize the code in exactly the same way.

If, on the other hand, `b < 7` is heavy then your "optimization" is
actually a pessimization. If `b < 7` has side-effects then your
"optimization" is not applicable at all.

Secondly, in the middle of nowhere both expressions are equally
pointless. Meanwhile, in a conditional part of an `if` or `while` they
still imply branching most of the time, meaning that the above alleged
"branch elimination" achieves virtually nothing.

And in contexts that don't necessarily imply branching see "firstly".

--
Best regards,
Andrey Tarasevich

Re: Shortcut operators

<PL_3K.242901$H_t7.60890@fx40.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21121&group=comp.lang.c#21121

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx40.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Shortcut operators
Newsgroups: comp.lang.c
References: <Shortcut-20220408113128@ram.dialup.fu-berlin.de> <t2pqe4$f77$1@dont-email.me>
Lines: 32
Message-ID: <PL_3K.242901$H_t7.60890@fx40.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 08 Apr 2022 18:01:19 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 08 Apr 2022 18:01:19 GMT
X-Received-Bytes: 1647
 by: Scott Lurndal - Fri, 8 Apr 2022 18:01 UTC

Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
>On 4/8/2022 3:37 AM, Stefan Ram wrote:
>> The shortcut operators are the pride and joy of C programmers
>> because they can speed up the execution of programs.
>>
>> But nowadays I often read that branches are expensive.
>>
>> So, could it be that today some code such as "a > 2 & b < 7"
>> is usually faster than "a > 2 && b < 7" because it's free from
>> branches?
>
>Huh?
>
>Firstly, if `b < 7` is a lightweight evaluation with no side-effects any
>self-respecting compiler will optimize the code in exactly the same way.
>

In any case, in modern architectures conditional set instructions
will likely be used, e.g. on ARMv8:

LDR x10, a
LDR X11, b
CMP x10, #2
CSET x1, GT
CMP x11, #7
CSET x0, LT
ANDS x0, x0, x1
B.EQ else_clause
then_clause:

Intel has similar conditional move instructions.

Re: Shortcut operators

<87sfqna1by.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21123&group=comp.lang.c#21123

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Shortcut operators
Date: Fri, 08 Apr 2022 19:15:29 +0100
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <87sfqna1by.fsf@bsb.me.uk>
References: <Shortcut-20220408113128@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="ada6a80a29d158862aa79e3192f967b1";
logging-data="12985"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183AjkNk48eDB+UIPa/zCBGKW4vv6qICfM="
Cancel-Lock: sha1:yccGYiVurIhbdo963CBVv+4gW/Y=
sha1:e1O9E6gKzA5G/ct0zaKMmKLbowQ=
X-BSB-Auth: 1.4529345541ed88bae0f4.20220408191529BST.87sfqna1by.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 8 Apr 2022 18:15 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> The shortcut operators are the pride and joy of C programmers
> because they can speed up the execution of programs.
>
> But nowadays I often read that branches are expensive.
>
> So, could it be that today some code such as "a > 2 & b < 7"
> is usually faster than "a > 2 && b < 7" because it's free from
> branches?

If you can trust the compiler, let it decide.

At -O2 gcc generates the same code for both.

--
Ben.

Re: Shortcut operators

<branchless-20220524175215@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21728&group=comp.lang.c#21728

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.freedyn.de!speedkom.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: Shortcut operators
Date: 24 May 2022 16:53:04 GMT
Organization: Stefan Ram
Lines: 15
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <branchless-20220524175215@ram.dialup.fu-berlin.de>
References: <Shortcut-20220408113128@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 8HyxEnbjkt9MhmV6nFv+igpLd3b5+aDxFuDjG2mcrfGdZR
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Tue, 24 May 2022 16:53 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>So, could it be that today some code such as "a > 2 & b < 7"
>is usually faster than "a > 2 && b < 7" because it's free from
>branches?

No. Branches are not expensive, unless mispredicted.
Use "perf stat" to find out whether you have many
mispredictions. You should have much less than 1 %.

/If/ you have many mispredictions, you can try indeed
to replace code by branchless versions, but then you
should measure the performance to find out whether this
really helped.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor