C++ attribute: likely, unlikely (since C++20)

From cppreference.com

Allow the compiler to optimize for the case where paths of execution including that statement are more or less likely than any alternative path of execution that does not include such a statement.

Syntax

[[likely]](1) [[unlikely]](2) Explanation

These attributes may be applied to labels and statements (other than declaration-statements). They may not be simultaneously applied to the same label or statement.

1) Applies to a statement to allow the compiler to optimize for the case where paths of execution including that statement are more likely than any alternative path of execution that does not include such a statement.

2) Applies to a statement to allow the compiler to optimize for the case where paths of execution including that statement are less likely than any alternative path of execution that does not include such a statement.

A path of execution is deemed to include a label if and only if it contains a jump to that label:

intf(inti){switch(i){case1:[[fallthrough]];[[likely]]case2:return1;}return2;}i==2 is considered more likely than any other value of i, but the [[likely]] has no effect on the i==1 case even though it falls through the case2: label.

Example

Run this code

#include<chrono>#include<cmath>#include<iomanip>#include<iostream>#include<random>namespacewith_attributes{constexprdoublepow(doublex,longlongn)noexcept{if(n>0)[[likely]]returnx*pow(x,n-1);else[[unlikely]]return1;}constexprlonglongfact(longlongn)noexcept{if(n>1)[[likely]]returnn*fact(n-1);else[[unlikely]]return1;}constexprdoublecos(doublex)noexcept{constexprlonglongprecision{16LL};doubley{};for(auton{0LL};n<precision;n+=2LL)[[likely]]y+=pow(x,n)/(n&2LL?-fact(n):fact(n));returny;}}// namespace with_attributesnamespaceno_attributes{constexprdoublepow(doublex,longlongn)noexcept{if(n>0)returnx*pow(x,n-1);elsereturn1;}constexprlonglongfact(longlongn)noexcept{if(n>1)returnn*fact(n-1);elsereturn1;}constexprdoublecos(doublex)noexcept{constexprlonglongprecision{16LL};doubley{};for(auton{0LL};n<precision;n+=2LL)y+=pow(x,n)/(n&2LL?-fact(n):fact(n));returny;}}// namespace no_attributesdoublegen_random()noexcept{staticstd::random_devicerd;staticstd::mt19937gen(rd());staticstd::uniform_real_distribution<double>dis(-1.0,1.0);returndis(gen);}volatiledoublesink{};// ensures a side effectintmain(){for(constautox:{0.125,0.25,0.5,1./(1<<26)})std::cout<<std::setprecision(53)<<"x = "<<x<<'\n'<<std::cos(x)<<'\n'<<with_attributes::cos(x)<<'\n'<<(std::cos(x)==with_attributes::cos(x)?"equal":"differ")<<'\n';autobenchmark=[](autofun,autorem){constautostart=std::chrono::high_resolution_clock::now();for(autosize{1ULL};size!=10'000'000ULL;++size)sink=fun(gen_random());conststd::chrono::duration<double>diff=std::chrono::high_resolution_clock::now()-start;std::cout<<"Time: "<<std::fixed<<std::setprecision(6)<<diff.count()<<" sec "<<rem<<std::endl;};benchmark(with_attributes::cos,"(with attributes)");benchmark(no_attributes::cos,"(without attributes)");benchmark([](doublet){returnstd::cos(t);},"(std::cos)");}Possible output:

x = 0.125 0.99219766722932900560039115589461289346218109130859375 0.99219766722932900560039115589461289346218109130859375 equal x = 0.25 0.96891242171064473343022882545483298599720001220703125 0.96891242171064473343022882545483298599720001220703125 equal x = 0.5 0.8775825618903727587394314468838274478912353515625 0.8775825618903727587394314468838274478912353515625 equal x = 1.490116119384765625e-08 0.99999999999999988897769753748434595763683319091796875 0.99999999999999988897769753748434595763683319091796875 equal Time: 0.579122 sec (with attributes) Time: 0.722553 sec (without attributes) Time: 0.425963 sec (std::cos) References

C++23 standard (ISO/IEC 14882:2024):

9.12.7 Likelihood attributes [dcl.attr.likelihood]

C++20 standard (ISO/IEC 14882:2020):

9.12.6 Likelihood attributes [dcl.attr.likelihood]