Topic: [[likely]] attribute for switch case


Author: david.bolvansky@gmail.com
Date: Tue, 8 May 2018 08:48:53 -0700 (PDT)
Raw View
------=_Part_28583_1760963793.1525794533020
Content-Type: multipart/alternative;
 boundary="----=_Part_28584_1917398783.1525794533020"

------=_Part_28584_1917398783.1525794533020
Content-Type: text/plain; charset="UTF-8"

Hello,

Since in C++20 we can write:

char * b(int e) {
   if  ([[likely]] e == 0)
      return "0";
   else if (e == 1)
      return "1";
   else return "undef";
}

Can likely attribute be added even for switch case? Like for example:

switch(e) {
    case 0 [[likely]]:
          return "0";
    case 1:
         return "1";
    default:
         return "undef";

}


I read the "likely" attribute
proposal http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0479r0.html
and they do not mention this use case for switches.

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9adde2c8-8306-4708-96f2-edb621d906c7%40isocpp.org.

------=_Part_28584_1917398783.1525794533020
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hello,<div><br></div><div>Since in C++20 we can write:</di=
v><div><div style=3D"color: rgb(0, 0, 0);" liberation=3D"" mono",=3D"" cour=
ier,=3D"" monospace;font-weight:=3D"" normal;font-size:=3D"" 14px;line-heig=
ht:=3D"" 19px;white-space:=3D"" pre;"=3D""><br><div style=3D"background-col=
or: rgb(255, 255, 254);"><span style=3D"color: #0000ff;">char</span> * b(<s=
pan style=3D"color: #0000ff;">int</span> e) {</div><div>    <span style=3D"=
background-color: rgb(255, 255, 254); color: rgb(0, 0, 255);">=C2=A0 =C2=A0=
</span><span style=3D"background-color: rgb(255, 255, 254); color: rgb(0, 0=
, 255);">if</span><span style=3D"background-color: rgb(255, 255, 254);">=C2=
=A0 (</span><span style=3D"background-color: rgb(255, 255, 254);">[[likely]=
]=C2=A0</span><span style=3D"background-color: rgb(255, 255, 254);">e =3D=
=3D </span><span style=3D"background-color: rgb(255, 255, 254); color: rgb(=
9, 136, 90);">0</span><span style=3D"background-color: rgb(255, 255, 254);"=
>)</span></div><div style=3D"background-color: rgb(255, 255, 254);">       =
 <span style=3D"color: rgb(0, 0, 255);">=C2=A0 =C2=A0</span><span style=3D"=
color: rgb(0, 0, 255);">=C2=A0 =C2=A0</span><span style=3D"color: rgb(0, 0,=
 255);">return</span> <span style=3D"color: rgb(163, 21, 21);">&quot;0&quot=
;</span>;</div><div style=3D"background-color: rgb(255, 255, 254);">    <sp=
an style=3D"color: rgb(0, 0, 255);">=C2=A0 =C2=A0</span><span style=3D"colo=
r: #0000ff;">else</span> <span style=3D"color: #0000ff;">if</span> (e =3D=
=3D <span style=3D"color: #09885a;">1</span>)</div><div style=3D"background=
-color: rgb(255, 255, 254);">        <span style=3D"color: rgb(0, 0, 255);"=
>=C2=A0 =C2=A0</span><span style=3D"color: rgb(0, 0, 255);">=C2=A0 =C2=A0</=
span><span style=3D"color: rgb(0, 0, 255);">return</span> <span style=3D"co=
lor: rgb(163, 21, 21);">&quot;1&quot;</span>;</div><div style=3D"background=
-color: rgb(255, 255, 254);">    <span style=3D"color: #0000ff;">=C2=A0 =C2=
=A0else</span> <span style=3D"color: #0000ff;">return</span> <span style=3D=
"color: #a31515;">&quot;undef&quot;</span>;</div><div style=3D"background-c=
olor: rgb(255, 255, 254);">}</div><div style=3D"background-color: rgb(255, =
255, 254);"><br></div><div style=3D"background-color: rgb(255, 255, 254);">=
Can likely attribute be added even for switch case? Like for example:</div>=
<div style=3D"background-color: rgb(255, 255, 254);"><br></div><div style=
=3D"background-color: rgb(255, 255, 254);">switch(e) {</div><div style=3D"b=
ackground-color: rgb(255, 255, 254);">=C2=A0 =C2=A0 case 0 [[likely]]:</div=
><div style=3D"background-color: rgb(255, 255, 254);">=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 return &quot;0&quot;;</div><div style=3D"background-color: rg=
b(255, 255, 254);">=C2=A0 =C2=A0 case 1:</div><div style=3D"background-colo=
r: rgb(255, 255, 254);">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return &quot;1&qu=
ot;;</div><div style=3D"background-color: rgb(255, 255, 254);">=C2=A0 =C2=
=A0 default:</div><div style=3D"background-color: rgb(255, 255, 254);">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return &quot;undef&quot;;</div><div style=3D=
"background-color: rgb(255, 255, 254);"><br></div><div style=3D"background-=
color: rgb(255, 255, 254);">}</div><div style=3D"background-color: rgb(255,=
 255, 254);"><br></div><div style=3D"background-color: rgb(255, 255, 254);"=
><br></div><div style=3D"background-color: rgb(255, 255, 254);">I read the =
&quot;likely&quot; attribute proposal=C2=A0http://www.open-std.org/jtc1/sc2=
2/wg21/docs/papers/2016/p0479r0.html and they do not mention this use case =
for switches.</div></div></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/9adde2c8-8306-4708-96f2-edb621d906c7%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/9adde2c8-8306-4708-96f2-edb621d906c7=
%40isocpp.org</a>.<br />

------=_Part_28584_1917398783.1525794533020--

------=_Part_28583_1760963793.1525794533020--

.


Author: Andrey Semashev <andrey.semashev@gmail.com>
Date: Tue, 8 May 2018 19:08:44 +0300
Raw View
On 05/08/18 18:48, david.bolvansky@gmail.com wrote:
> Hello,
>=20
> Since in C++20 we can write:
>=20
> char * b(int e) {
> if=C2=A0 ([[likely]] e =3D=3D 0)
> return "0";
> else if (e =3D=3D 1)
> return "1";
>  =C2=A0 =C2=A0else return "undef";
> }
>=20
> Can likely attribute be added even for switch case? Like for example:
>=20
> switch(e) {
>  =C2=A0 =C2=A0 case 0 [[likely]]:
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return "0";
>  =C2=A0 =C2=A0 case 1:
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return "1";
>  =C2=A0 =C2=A0 default:
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return "undef";
>=20
> }
>=20
>=20
> I read the "likely" attribute=20
> proposal=C2=A0http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p04=
79r0.html=20
> and they do not mention this use case for switches.

N4741 includes and example with `switch`/`case` in [dcl.attr.likelihood]/3.

I suspect your example with an `if` statement is not doing what you=20
think it does. The [[likely]] applies to the condition expression, which=20
basically says "this condition is likely to be executed" and not "this=20
condition is likely to be true". The correct example is also given in=20
the aforementioned section.

This is the most unfortunate aspect of this proposal as it makes it=20
incompatible with __builtin_expect and tons of code that wraps it in macros=
..

--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/0918afa6-6f04-fdc1-fd80-f2843a5e63f1%40gmail.com=
..

.


Author: david.bolvansky@gmail.com
Date: Tue, 8 May 2018 09:29:26 -0700 (PDT)
Raw View
------=_Part_3928_1786510966.1525796966258
Content-Type: multipart/alternative;
 boundary="----=_Part_3929_329272358.1525796966258"

------=_Part_3929_329272358.1525796966258
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Thanks, N4741 really has it already.

D=C5=88a utorok, 8. m=C3=A1ja 2018 18:08:48 UTC+2 Andrey Semashev nap=C3=AD=
sal(-a):
>
> On 05/08/18 18:48, david.b...@gmail.com <javascript:> wrote:=20
> > Hello,=20
> >=20
> > Since in C++20 we can write:=20
> >=20
> > char * b(int e) {=20
> > if  ([[likely]] e =3D=3D 0)=20
> > return "0";=20
> > else if (e =3D=3D 1)=20
> > return "1";=20
> >     else return "undef";=20
> > }=20
> >=20
> > Can likely attribute be added even for switch case? Like for example:=
=20
> >=20
> > switch(e) {=20
> >      case 0 [[likely]]:=20
> >            return "0";=20
> >      case 1:=20
> >           return "1";=20
> >      default:=20
> >           return "undef";=20
> >=20
> > }=20
> >=20
> >=20
> > I read the "likely" attribute=20
> > proposal=20
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0479r0.html=20
> > and they do not mention this use case for switches.=20
>
> N4741 includes and example with `switch`/`case` in=20
> [dcl.attr.likelihood]/3.=20
>
> I suspect your example with an `if` statement is not doing what you=20
> think it does. The [[likely]] applies to the condition expression, which=
=20
> basically says "this condition is likely to be executed" and not "this=20
> condition is likely to be true". The correct example is also given in=20
> the aforementioned section.=20
>
> This is the most unfortunate aspect of this proposal as it makes it=20
> incompatible with __builtin_expect and tons of code that wraps it in=20
> macros.=20
>

--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/9fa8e6c3-f3c7-468e-bb0f-b9197a88eb80%40isocpp.or=
g.

------=_Part_3929_329272358.1525796966258
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Thanks, N4741 really has it already.<br><br>D=C5=88a utoro=
k, 8. m=C3=A1ja 2018 18:08:48 UTC+2 Andrey Semashev nap=C3=ADsal(-a):<block=
quote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-le=
ft: 1px #ccc solid;padding-left: 1ex;">On 05/08/18 18:48, <a href=3D"javasc=
ript:" target=3D"_blank" gdf-obfuscated-mailto=3D"KTwhuBEiBgAJ" rel=3D"nofo=
llow" onmousedown=3D"this.href=3D&#39;javascript:&#39;;return true;" onclic=
k=3D"this.href=3D&#39;javascript:&#39;;return true;">david.b...@gmail.com</=
a> wrote:
<br>&gt; Hello,
<br>&gt;=20
<br>&gt; Since in C++20 we can write:
<br>&gt;=20
<br>&gt; char * b(int e) {
<br>&gt; if=C2=A0 ([[likely]] e =3D=3D 0)
<br>&gt; return &quot;0&quot;;
<br>&gt; else if (e =3D=3D 1)
<br>&gt; return &quot;1&quot;;
<br>&gt; =C2=A0=C2=A0 =C2=A0else return &quot;undef&quot;;
<br>&gt; }
<br>&gt;=20
<br>&gt; Can likely attribute be added even for switch case? Like for examp=
le:
<br>&gt;=20
<br>&gt; switch(e) {
<br>&gt; =C2=A0=C2=A0 =C2=A0 case 0 [[likely]]:
<br>&gt; =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return &quot;0&quot;;
<br>&gt; =C2=A0=C2=A0 =C2=A0 case 1:
<br>&gt; =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return &quot;1&quot;;
<br>&gt; =C2=A0=C2=A0 =C2=A0 default:
<br>&gt; =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return &quot;undef&quot;;
<br>&gt;=20
<br>&gt; }
<br>&gt;=20
<br>&gt;=20
<br>&gt; I read the &quot;likely&quot; attribute=20
<br>&gt; proposal=C2=A0<a href=3D"http://www.open-std.org/jtc1/sc22/wg21/do=
cs/papers/2016/p0479r0.html" target=3D"_blank" rel=3D"nofollow" onmousedown=
=3D"this.href=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.open-s=
td.org%2Fjtc1%2Fsc22%2Fwg21%2Fdocs%2Fpapers%2F2016%2Fp0479r0.html\x26sa\x3d=
D\x26sntz\x3d1\x26usg\x3dAFQjCNFxGsWJ-xIPfVshaBevbFktekHjfQ&#39;;return tru=
e;" onclick=3D"this.href=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2=
Fwww.open-std.org%2Fjtc1%2Fsc22%2Fwg21%2Fdocs%2Fpapers%2F2016%2Fp0479r0.htm=
l\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFxGsWJ-xIPfVshaBevbFktekHjfQ&#39;=
;return true;">http://www.open-std.<wbr>org/jtc1/sc22/wg21/docs/<wbr>papers=
/2016/p0479r0.html</a>=20
<br>&gt; and they do not mention this use case for switches.
<br>
<br>N4741 includes and example with `switch`/`case` in [dcl.attr.likelihood=
]/3.
<br>
<br>I suspect your example with an `if` statement is not doing what you=20
<br>think it does. The [[likely]] applies to the condition expression, whic=
h=20
<br>basically says &quot;this condition is likely to be executed&quot; and =
not &quot;this=20
<br>condition is likely to be true&quot;. The correct example is also given=
 in=20
<br>the aforementioned section.
<br>
<br>This is the most unfortunate aspect of this proposal as it makes it=20
<br>incompatible with __builtin_expect and tons of code that wraps it in ma=
cros.
<br></blockquote></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/9fa8e6c3-f3c7-468e-bb0f-b9197a88eb80%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/9fa8e6c3-f3c7-468e-bb0f-b9197a88eb80=
%40isocpp.org</a>.<br />

------=_Part_3929_329272358.1525796966258--

------=_Part_3928_1786510966.1525796966258--

.


Author: david.bolvansky@gmail.com
Date: Tue, 8 May 2018 09:33:50 -0700 (PDT)
Raw View
------=_Part_28728_2113129960.1525797230644
Content-Type: multipart/alternative;
 boundary="----=_Part_28729_267890549.1525797230645"

------=_Part_28729_267890549.1525797230645
Content-Type: text/plain; charset="UTF-8"


>
> Clang 6.0/trunk ignores it yet, GCC takes this hint.
>
>  https://godbolt.org/g/dpSDqd

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/8eda2052-399f-4367-a16f-5059f88c44a2%40isocpp.org.

------=_Part_28729_267890549.1525797230645
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;bor=
der-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><blockquote c=
lass=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #c=
cc solid;padding-left:1ex">Clang 6.0/trunk ignores it yet, GCC takes this h=
int.</blockquote></div></blockquote><div>=C2=A0https://godbolt.org/g/dpSDqd=
</div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/8eda2052-399f-4367-a16f-5059f88c44a2%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/8eda2052-399f-4367-a16f-5059f88c44a2=
%40isocpp.org</a>.<br />

------=_Part_28729_267890549.1525797230645--

------=_Part_28728_2113129960.1525797230644--

.