{"id":296702,"date":"2020-01-03T15:00:15","date_gmt":"2020-01-03T15:00:15","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=296702"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=296702","title":{"rendered":"\u041e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u0445 \u043c\u0438\u043a\u0440\u043e\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/482766\/\">\n<h2>\u041f\u0440\u0435\u0434\u044b\u0441\u0442\u043e\u0440\u0438\u044f<\/h2>\n<p>  \u041a\u0430\u043a-\u0442\u043e \u0440\u0430\u0437 \u0443 \u043c\u0435\u043d\u044f \u0441 \u043a\u043e\u043b\u043b\u0435\u0433\u043e\u0439 \u0437\u0430\u0432\u044f\u0437\u0430\u043b\u0441\u044f \u0440\u0430\u0437\u0433\u043e\u0432\u043e\u0440 \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0438\u0442\u043e\u0432\u044b\u043c\u0438 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 \u0432 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u0445 C++. \u041d\u0430 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0431\u044b\u043b\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f IsEnumFlagSet, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u043d\u0430 \u0432\u0445\u043e\u0434 \u043f\u0435\u0440\u0432\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u0432\u0442\u043e\u0440\u044b\u043c \u2014 \u043d\u0430\u0431\u043e\u0440 \u0444\u043b\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438. \u0427\u0435\u043c \u0436\u0435 \u043e\u043d\u0430 \u043b\u0443\u0447\u0448\u0435 \u0441\u0442\u0430\u0440\u043e\u0433\u043e \u0434\u043e\u0431\u0440\u043e\u0433\u043e \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u043e\u0433\u043e \u0418?<\/p>\n<pre><code class=\"cpp\">if (IsEnumFlagSet(state, flag))  {  } \/\/ vs if (state & flag)  {  } <\/code><\/pre>\n<p><a name=\"habracut\"><\/a><br \/>  \u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u2014 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c\u044e. \u042f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0440\u0435\u0434\u043a\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u044e \u0441 \u0431\u0438\u0442\u043e\u0432\u044b\u043c\u0438 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 \u0438 \u0431\u0438\u0442\u043e\u0432\u044b\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438 \u0432\u043e\u043e\u0431\u0449\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435 \u0447\u0443\u0436\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0435\u0433\u0447\u0435 \u0432\u043e\u0441\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u0438\u043c\u0435\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043d\u0435\u0436\u0435\u043b\u0438 \u0437\u0430\u0433\u0430\u0434\u043e\u0447\u043d\u044b\u0435 &#038; \u0438 |, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 window.alert() c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u043e\u043c \u00ab\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435! \u0422\u0443\u0442, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043a\u0430\u043a\u0430\u044f-\u0442\u043e \u043c\u0430\u0433\u0438\u044f\u00bb.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0433\u0440\u0443\u0441\u0442\u0438<\/b><\/p>\n<div class=\"spoiler_text\">\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0421++ \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u044b-\u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f (\u0445\u043e\u0442\u044f <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2014\/n4174.pdf\">\u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/a> \u0443\u0436\u0435 \u0431\u044b\u043b\u043e) \u2014 \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c \u0431\u044b\u043b \u0431\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0435\u0442\u043e\u0434 \u0430-\u043b\u044f std::bitset:<\/p>\n<pre><code class=\"cpp\">if (state.Test(particularFlags)) {}<\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0438\u043b\u0438 \u0441\u043d\u044f\u0442\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u0432. \u0421\u0440\u0430\u0432\u043d\u0438\u0442\u0435:<\/p>\n<pre><code class=\"cpp\">state |= flag; \/\/ \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 enum class, \u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 |= state &= ~flag; \/\/vs RaiseEnumFlag(state, flag); ClearEnumFlag(state, flag); <\/code><\/pre>\n<p>  \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u0436\u0435 \u0431\u044b\u043b\u0430 \u0432\u044b\u0441\u043a\u0430\u0437\u0430\u043d\u0430 \u0438\u0434\u0435\u044f \u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>SetEnumFlag(state, flag, isSet)<\/code>: \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u0440\u0435\u0442\u044c\u0435\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430, \u0443 <code>state<\/code> \u0431\u044b \u043b\u0438\u0431\u043e \u0432\u043e\u0437\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u0444\u043b\u0430\u0433\u0438, \u043b\u0438\u0431\u043e \u0441\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u044c.<\/p>\n<p>  \u0422\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435, \u0442\u043e \u0431\u0435\u0437 \u043e\u0432\u0435\u0440\u0445\u0435\u0434\u0430 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043f\u0430\u0440\u043e\u0439 <code>RaiseEnumFlag\/ClearEnumFlag<\/code>, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u043d\u0435 \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c. \u041d\u043e \u0440\u0430\u0434\u0438 \u0430\u043a\u0430\u0434\u0435\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0430 \u043c\u043d\u0435 \u0437\u0430\u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0441\u0432\u0435\u0441\u0442\u0438 \u0435\u0433\u043e \u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0443 \u043f\u0443\u0442\u0435\u043c \u0441\u043f\u0443\u0441\u043a\u0430 \u0432 <a href=\"https:\/\/youtu.be\/FJJTYQYB1JQ?t=1932\">\u0448\u0430\u0445\u0442\u0443 \u043a \u0434\u044c\u044f\u0432\u043e\u043b\u0443<\/a> \u043c\u0438\u043a\u0440\u043e\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>  <\/p>\n<h3>1. \u041d\u0430\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h3>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u0432\u0435\u0434\u0435\u043c \u043d\u0430\u0448\u0435 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 (\u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c enum class \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f):<\/p>\n<pre><code class=\"cpp\">#include &lt;limits&gt; #include &lt;random&gt;  enum Flags : uint32_t {   One = 1u &lt;&lt; 1,   Two = 1u &lt;&lt; 2,   Three = 1u &lt;&lt; 3,   OneOrThree = One | Three,   Max = 1u &lt;&lt; 31,   All = std::numeric_limits&lt;uint32_t&gt;::max() };<\/code><\/pre>\n<p>  \u0418 \u0441\u0430\u043c\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">void SetFlagBranched(Flags& x, Flags y, bool cond) {   if (cond) {     x = Flags(x | y);   } else {     x = Flags(x & (~y));   } }<\/code><\/pre>\n<p>  <\/p>\n<h3>2. \u041c\u0438\u043a\u0440\u043e\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f<\/h3>\n<p>  \u041d\u0430\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043c\u0435\u0435\u0442 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u0432 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0443, \u0447\u0442\u043e \u043c\u044b \u0441\u0435\u0439\u0447\u0430\u0441 \u0438 \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c.<\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043e\u0431\u0440\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440,<\/p>\n<pre><code class=\"plaintext\">(x | y) & ~p<\/code><\/pre>\n<p>  <\/p>\n<ol>\n<li>\u041f\u0440\u0438 <code>p = 0<\/code> \u0432\u043e\u0437\u0432\u043e\u0434\u0438\u043c \u0444\u043b\u0430\u0433\u0438:\n<pre><code class=\"plaintext\">(x | y) & ~0 \u2261 (x | y) & 1 \u2261 x | y<\/code><\/pre>\n<p>  <\/li>\n<li>\u041f\u0440\u0438 <code>p = y<\/code> \u0444\u043b\u0430\u0433\u0438 \u0441\u043d\u0438\u043c\u0430\u044e\u0442\u0441\u044f:\n<pre><code class=\"plaintext\">(x | y) & ~y \u2261 (x & ~y) | (y & ~y) \u2261 (x & ~y) | 0 \u2261 x & ~y<\/code><\/pre>\n<p>  <\/li>\n<\/ol>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u043a-\u0442\u043e \u00ab\u0443\u043f\u0430\u043a\u043e\u0432\u0430\u0442\u044c\u00bb \u0432 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0443 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>cond<\/code> (\u043f\u043e\u043c\u043d\u0438\u043c \u2014 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u044b). <\/p>\n<p>  \u041f\u0443\u0441\u0442\u044c \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e <code>p = y<\/code>, \u0438 \u0435\u0441\u043b\u0438 <code>cond<\/code> \u0438\u0441\u0442\u0438\u043d\u0430, \u0442\u043e \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043e\u0431\u043d\u0443\u043b\u0438\u0442\u044c <code>p<\/code>, \u0435\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0441\u0435 \u043a\u0430\u043a \u0435\u0441\u0442\u044c.<\/p>\n<p>  \u041d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>cond<\/code> \u0442\u043e\u043b\u043a\u043e\u043c \u043d\u0435 \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c: \u043f\u0440\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0442\u0438\u043f \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 true \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0443 \u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u0432 \u043c\u043b\u0430\u0434\u0448\u0435\u043c \u0440\u0430\u0437\u0440\u044f\u0434\u0435, \u0430 \u043d\u0430\u043c \u0432 \u0438\u0434\u0435\u0430\u043b\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0434\u0438\u043d\u0438\u0446\u044b \u0432\u043e \u0432\u0441\u0435\u0445 \u0440\u0430\u0437\u0440\u044f\u0434\u0430\u0445. \u041f\u043e \u0438\u0442\u043e\u0433\u0443, \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043b\u0443\u0447\u0448\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u044b\u0445 \u0441\u0434\u0432\u0438\u0433\u043e\u0432. <\/p>\n<p>  \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 \u0441\u0434\u0432\u0438\u0433\u0430: \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0441\u0440\u0430\u0437\u0443 \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 \u0431\u0438\u0442\u044b \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0437\u0430 \u043e\u0434\u043d\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043e\u0431\u043d\u0443\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>p<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 \u0442\u0440\u0435\u0431\u0443\u0435\u0442, \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u0441\u0434\u0432\u0438\u0433\u0430 \u0431\u044b\u043b\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0442\u0438\u043f\u0430.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u0435\u0431\u0435\u0437\u043e\u0441\u043d\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e<\/b><\/p>\n<div class=\"spoiler_text\">\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043f\u043e asm \u043a\u043e\u043c\u0430\u043d\u0434\u0435 shift arithmetic left (SAL) \u0442\u0430\u043a \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u00abThe count range is limited to 0 to 31 (or 63 if 64-bit mode and REX.W is used)\u00bb  <\/div>\n<\/div>\n<p>  \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0441\u0434\u0432\u0438\u0433\u0430, \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435<\/p>\n<pre><code class=\"cpp\">constexpr auto shiftSize = sizeof(std::underlying_type_t&lt;Flags&gt;) * 8 - 1; (x | y) & ~ ( y &gt;&gt; shiftSize * cond); <\/code><\/pre>\n<p>  \u0418 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043c\u043b\u0430\u0434\u0448\u0438\u0439 \u0431\u0438\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f <code>y &gt;&gt; shiftSize * cond<\/code>:<\/p>\n<pre><code class=\"cpp\">(x | y) & ~ (( y &gt;&gt; shiftSize * cond) & ~cond);<\/code><\/pre>\n<p>  \u0423\u043f\u0430\u043a\u043e\u0432\u043a\u0430 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0430 \u0432 <code>shiftSize * cond<\/code> \u2014 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 false \u0438\u043b\u0438 true \u0432 cond, \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u0441\u0434\u0432\u0438\u0433\u0430 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438\u0431\u043e 0, \u043b\u0438\u0431\u043e 31 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0438 \u043d\u0430\u0448 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438\u0431\u043e \u0440\u0430\u0432\u0435\u043d <code>y<\/code>, \u043b\u0438\u0431\u043e 0.<\/p>\n<p>  \u0427\u0442\u043e \u0442\u0443\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u0438 <code>shiftSize = 31<\/code>: <\/p>\n<ol>\n<li>\u041f\u0440\u0438 <code>cond = true<\/code> \u043c\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u043c \u0431\u0438\u0442\u044b <code>y<\/code> \u043d\u0430 31 \u0432\u043f\u0440\u0430\u0432\u043e, \u0432\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0447\u0435\u0433\u043e \u0441\u0430\u043c\u044b\u0439 \u0441\u0442\u0430\u0440\u0448\u0438\u0439 \u0431\u0438\u0442 <code>y<\/code> \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u043c \u043c\u043b\u0430\u0434\u0448\u0438\u043c, \u0430 \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043e\u0431\u043d\u0443\u043b\u044f\u044e\u0442\u0441\u044f. \u0423 <code>~cond<\/code> \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u0441\u0430\u043c\u044b\u0439 \u043c\u043b\u0430\u0434\u0448\u0438\u0439 \u0431\u0438\u0442 \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 0, \u0430 \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u2014 \u0435\u0434\u0438\u043d\u0438\u0446\u0430. \u041f\u043e\u0431\u0438\u0442\u043e\u0432\u043e\u0435 \u043f\u0435\u0440\u0435\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0434\u0430\u0441\u0442 \u0447\u0438\u0441\u0442\u044b\u0439 0.<\/li>\n<li>\u041f\u0440\u0438 <code>cond = false<\/code> \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0441\u0434\u0432\u0438\u0433\u0430 \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, <code>~cond<\/code> \u0432\u043e \u0432\u0441\u0435\u0445 \u0440\u0430\u0437\u0440\u044f\u0434\u0430\u0445 \u0438\u043c\u0435\u0435\u0442 1 \u0438 \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u043e\u0435 \u043f\u0435\u0440\u0435\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0434\u0430\u0441\u0442 <code>y<\/code>.<\/li>\n<\/ol>\n<p>  \u0425\u043e\u0447\u0435\u0442\u0441\u044f \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c trade-off \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0441\u0440\u0430\u0437\u0443 \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430: \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u0439 \u043c\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c <code>x | y<\/code> (\u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0434\u043d\u0443 \u0438\u0437 \u0432\u0435\u0442\u043e\u043a \u043d\u0430\u0438\u0432\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430) \u0432 <i>\u043b\u044e\u0431\u043e\u043c<\/i> \u0441\u043b\u0443\u0447\u0430\u0435, \u0430 \u0434\u0430\u043b\u044c\u0448\u0435, \u0437\u0430 \u0441\u0447\u0435\u0442 \u00ab\u043b\u0438\u0448\u043d\u0438\u0445\u00bb \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u0435\u0435 \u0432 \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0418 \u0432\u0441\u0435 \u044d\u0442\u043e \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b, \u0435\u0441\u043b\u0438 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043d\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0443 \u0431\u0443\u0434\u0443\u0442 \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c:<\/p>\n<pre><code class=\"cpp\">void SetFlagsBranchless(Flags& x, Flags y, bool cond) {   constexpr uint32_t shiftSize = sizeof(uint32_t) * 8 - 1;   x = Flags((x | y) & ~(( y &gt;&gt; shiftSize * cond) & ~cond)); }<\/code><\/pre>\n<p>  <\/p>\n<h3>3. \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f, \u044f \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u043d\u0430 <a href=\"http:\/\/quick-bench.com\/\">quick-bench.com<\/a>, \u0447\u0442\u043e\u0431\u044b \u0443\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u0442\u044c\u0441\u044f \u0432 \u0435\u0433\u043e \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0435. \u0414\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043c\u044b \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c clang, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u043e\u0433\u043e\u043d\u044f\u0442\u044c \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u043d\u0430 \u043d\u0435\u043c (clang-9.0). \u041d\u043e \u0442\u0443\u0442 \u043c\u0435\u043d\u044f \u0436\u0434\u0430\u043b \u0441\u044e\u0440\u043f\u0440\u0438\u0437\u2026<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/j8\/ar\/se\/j8arset8k9nbzroo2ws7a2flzeu.png\"\/><\/p>\n<p>  \u0418 \u044d\u0442\u043e \u0441 -O3. \u0411\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u0432\u0441\u0435 \u0435\u0449\u0435 \u0445\u0443\u0436\u0435. \u041a\u0430\u043a \u0442\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c? \u041a\u0442\u043e \u0432\u0438\u043d\u043e\u0432\u0430\u0442 \u0438 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c?<\/p>\n<p>  \u041a\u043e\u043c\u0430\u043d\u0434\u0443\u0435\u043c \u00ab\u043e\u0442\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u043d\u0438\u043a\u0443!\u00bb \u0438 \u0438\u0434\u0435\u043c \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u043d\u0430 <a href=\"https:\/\/godbolt.org\/\">godbolt.org<\/a> (quick-bench \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 asm \u043b\u0438\u0441\u0442\u0438\u043d\u0433, \u043d\u043e godbolt \u0432 \u044d\u0442\u043e\u043c \u043f\u043b\u0430\u043d\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0443\u0434\u043e\u0431\u043d\u0435\u0435).<\/p>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0434\u0442\u0438 \u0440\u0435\u0447\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e \u0443\u0440\u043e\u0432\u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 -O3. \u0418\u0442\u0430\u043a, \u043a\u0430\u043a\u043e\u0439 \u0436\u0435 \u043a\u043e\u0434 \u043d\u0430\u043c \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043b clang \u0434\u043b\u044f \u043d\u0430\u0438\u0432\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438?<\/p>\n<pre><code class=\"plaintext\">SetFlagBranched(Flags&, Flags, bool):          # @SetFlagBranched(Flags&, Flags, bool)         mov     eax, dword ptr [rdi]         mov     ecx, esi         not     ecx         and     ecx, eax         or      eax, esi         test    edx, edx         cmove   eax, ecx         mov     dword ptr [rdi], eax         ret<\/code><\/pre>\n<p>  \u041d\u0435\u043f\u043b\u043e\u0445\u043e, \u043f\u0440\u0430\u0432\u0434\u0430? Clang \u0442\u043e\u0436\u0435 \u0443\u043c\u0435\u0435\u0442 \u0432 trade-off, \u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c <i>\u043e\u0431\u0435<\/i> \u0432\u0435\u0442\u043a\u0438 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 conditional move, \u043a\u043e\u0442\u043e\u0440\u0430\u044f <a href=\"https:\/\/stackoverflow.com\/questions\/14131096\/why-is-a-conditional-move-not-vulnerable-for-branch-prediction-failure\">\u043d\u0435 \u0432\u043e\u0432\u043b\u0435\u043a\u0430\u0435\u0442<\/a> \u0432 \u0440\u0430\u0431\u043e\u0442\u0443 branch predictor, \u0431\u0443\u0434\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u043c\u0430\u043d\u0434 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432 (conditional jump).<\/p>\n<p>  \u041a\u043e\u0434 branchless \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"plaintext\">SetFlag(Flags&, Flags, bool):                   # @SetFlag(Flags&, Flags, bool)         mov     eax, dword ptr [rdi]         or      eax, esi         test    edx, edx         mov     ecx, 31         cmove   ecx, edx         shr     esi, cl         not     esi         or      esi, edx         and     esi, eax         mov     dword ptr [rdi], esi         ret<\/code><\/pre>\n<p>  \u041f\u043e\u0447\u0442\u0438 \u00abbranchless\u00bb \u2014 \u044f, \u0432\u0440\u043e\u0434\u0435 \u0431\u044b, \u0437\u0430\u043a\u0430\u0437\u044b\u0432\u0430\u043b \u0442\u0443\u0442 \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435, \u0430 \u0432\u044b \u043c\u043d\u0435, \u0431\u0430\u0442\u0435\u043d\u044c\u043a\u0430, conditional move \u043f\u0440\u0438\u043d\u0435\u0441\u043b\u0438. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u0440\u0430\u0432, \u0438 test + cmove \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c imul, \u043d\u043e \u044f \u043d\u0435 \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u044e\u0441\u044c \u0432 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u0435 (\u0437\u043d\u0430\u044e\u0449\u0438\u0435 \u043b\u044e\u0434\u0438, \u043f\u043e\u0434\u0441\u043a\u0430\u0436\u0438\u0442\u0435, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445).<\/p>\n<p>  \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0434\u0440\u0443\u0433\u043e\u0435 \u2014 \u043f\u043e \u0441\u0443\u0442\u0438, \u0434\u043b\u044f \u043e\u0431\u043e\u0438\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u043f\u043e\u0441\u043b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043b \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u043f\u0440\u043e\u0441\u0438\u043b\u0438, \u0438 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u043d\u0435\u0447\u0442\u043e \u0441\u0440\u0435\u0434\u043d\u0435\u0435: \u0432 \u043e\u0431\u043e\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f cmove, \u043f\u0440\u043e\u0441\u0442\u043e \u0432 branchless \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0443 \u043d\u0430\u0441 \u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438 \u0437\u0430\u0432\u0430\u043b\u0438\u0432\u0430\u0435\u0442 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a.<\/p>\n<p>  Clang \u0432\u043e\u0441\u044c\u043c\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0438 \u0441\u0442\u0430\u0440\u0448\u0435 \u0432\u043e\u043e\u0431\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b, \u00ab\u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f\u00bb \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u00abbranchless\u00bb \u0432\u0435\u0440\u0441\u0438\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0447\u0443\u0442\u044c \u043b\u0438 \u043d\u0435 \u0432 \u043f\u043e\u043b\u0442\u043e\u0440\u0430 \u0440\u0430\u0437\u0430 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435:<\/p>\n<pre><code class=\"plaintext\">SetFlag(Flags&, Flags, bool):                   # @SetFlag(Flags&, Flags, bool)         mov     eax, dword ptr [rdi]         or      eax, esi         mov     cl, 31         test    edx, edx         jne     .LBB0_2         xor     ecx, ecx .LBB0_2:         shr     esi, cl         not     esi         or      esi, edx         and     eax, esi         mov     dword ptr [rdi], eax         ret<\/code><\/pre>\n<p>  \u041a\u0430\u043a\u043e\u0439 \u0432\u044b\u0432\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c? \u041f\u043e\u043c\u0438\u043c\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0433\u043e \u00ab\u043d\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0439\u0442\u0435\u0441\u044c \u043c\u0438\u043a\u0440\u043e\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f\u043c\u0438 \u0431\u0435\u0437 \u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u0438\u00bb, \u0440\u0430\u0437\u0432\u0435 \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043e\u0432\u0435\u0442\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0432 \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u2014 \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0442\u0430\u043a, \u0447\u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0430 \u0432\u0430\u0448\u0438 \u00ab\u0433\u0435\u043d\u0438\u0430\u043b\u044c\u043d\u044b\u0435\u00bb \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0435\u043c\u0443 \u0431\u0443\u0434\u0443\u0442 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u044b, \u0438 \u043d\u0430\u0437\u043b\u043e \u0432\u0430\u043c \u043e\u043d \u043f\u043e\u043d\u0430\u0442\u044b\u043a\u0430\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u043e \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0438 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442\u044c, \u0435\u0441\u043b\u0438 \u0431\u044b \u043d\u0435 \u043e\u0434\u043d\u043e \u00ab\u043d\u043e\u00bb. \u041a\u043e\u0434 gcc \u0434\u043b\u044f \u043d\u0430\u0438\u0432\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d clang-\u043e\u0432\u0441\u043a\u043e\u043c\u0443, \u0430 \u0432\u043e\u0442 branchless \u0432\u0435\u0440\u0441\u0438\u044f..:<\/p>\n<pre><code class=\"plaintext\">SetFlag(Flags&, Flags, bool):         movzx   edx, dl         mov     eax, esi         or      eax, DWORD PTR [rdi]         mov     ecx, edx         sal     ecx, 5         sub     ecx, edx         shr     esi, cl         not     esi         or      esi, edx         and     esi, eax         mov     DWORD PTR [rdi], esi         ret<\/code><\/pre>\n<p>  \u041c\u043e\u0435 \u043f\u043e\u0447\u0442\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c \u0437\u0430 \u0441\u0442\u043e\u043b\u044c \u0438\u0437\u044f\u0449\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u0438 <code>imul<\/code>, \u043d\u0438 <code>cmove<\/code>. \u0427\u0442\u043e \u0442\u0443\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442: bool \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f cond \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u043e \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0432\u043b\u0435\u0432\u043e \u043d\u0430 5 \u0437\u043d\u0430\u043a\u043e\u0432 (\u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0438\u043f \u043d\u0430\u0448\u0435\u0433\u043e enum \u2014 uint32_t, \u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440 32 \u0431\u0438\u0442\u0430, \u0442\u043e \u0435\u0441\u0442\u044c 100000<sub>2<\/sub>), \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0437 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0432\u044b\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u043d\u0430 \u0436\u0435. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c 11111<sub>2<\/sub> = 31<sub>10<\/sub> \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 cond = true, \u0438 0 \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c. \u041d\u0430\u0434\u043e \u043b\u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043d\u0430\u0438\u0432\u043d\u043e\u0433\u043e, \u0434\u0430\u0436\u0435 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0435\u0433\u043e conditional move \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438?<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/il\/-k\/sv\/il-ksv3hfdrbu0npoffcva7c67o.png\" alt=\"image\"\/><\/p>\n<p>  \u0427\u0442\u043e \u0436, \u0438\u0442\u043e\u0433 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0441\u043e\u0432\u0441\u0435\u043c \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u0439 \u2014 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0431\u0435\u0437 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0442\u0430\u043a \u0438 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f\u043c\u0438. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u043c\u043e\u0447\u044c clang \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043f\u043e \u043c\u0435\u0442\u043e\u0434\u0443 gcc (\u0437\u0430\u043e\u0434\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043c \u0447\u0430\u0441\u0442\u044c <code>~((y &gt;&gt; shiftSize * cond) & ~cond)<\/code> \u043f\u043e \u0434\u0435 \u041c\u043e\u0440\u0433\u0430\u043d\u0443 \u2014 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0438 clang, \u0438 gcc):<\/p>\n<pre><code class=\"cpp\">void SetFlagVerbose(Flags& x, Flags y, bool b) {   constexpr auto shiftSize = sizeof(std::underlying_type_t&lt;Flags&gt;) + 1;   x = Flags( (x | y) & ( ~(y &gt;&gt; ((b &lt;&lt; shiftSize) - b)) | b) ); }<\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0430\u044f \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 trunk \u0432\u0435\u0440\u0441\u0438\u044e clang, \u0433\u0434\u0435 \u043e\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u0434 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 gcc (\u043f\u0440\u0430\u0432\u0434\u0430, \u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u00abbranchless\u00bb \u2014 \u0432\u0441\u0435 \u0442\u043e\u0442 \u0436\u0435 test + \u0441move)<\/p>\n<p>  \u0410 \u0447\u0442\u043e \u0442\u0430\u043c \u0443 MSVC? \u0412 \u043e\u0431\u043e\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u0431\u0435\u0437 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0441\u0442\u043d\u044b\u0439 imul (\u043d\u0435 \u0437\u043d\u0430\u044e, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u043d \u0431\u044b\u0441\u0442\u0440\u0435\u0435\/\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 clang\/gcc \u2014 quick-bench \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440), \u0430 \u0432 \u043d\u0430\u0438\u0432\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f conditional jump. Sad but true.<\/p>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>  \u0413\u043b\u0430\u0432\u043d\u044b\u043c \u0432\u044b\u0432\u043e\u0434\u043e\u043c, \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u0442\u044c \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430 \u0432 \u0432\u044b\u0441\u043e\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u043e\u043c \u043a\u043e\u0434\u0435 \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0442\u0441\u044f \u0432 \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u2014 \u0438 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u043c\u0438\u043a\u0440\u043e\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0431\u0435\u0437 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u0432 \u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u043b\u0438\u0441\u0442\u0438\u043d\u0433\u043e\u0432. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043c\u0438\u043a\u0440\u043e\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u043b\u0443\u0447\u0448\u0435, \u0442\u0430\u043a \u0438 \u0445\u0443\u0436\u0435 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u2014 \u0432\u0441\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0435\u043a\u0442 \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0439.<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/482766\/\"> https:\/\/habr.com\/ru\/post\/482766\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/482766\/\">\n<h2>\u041f\u0440\u0435\u0434\u044b\u0441\u0442\u043e\u0440\u0438\u044f<\/h2>\n<p>  \u041a\u0430\u043a-\u0442\u043e \u0440\u0430\u0437 \u0443 \u043c\u0435\u043d\u044f \u0441 \u043a\u043e\u043b\u043b\u0435\u0433\u043e\u0439 \u0437\u0430\u0432\u044f\u0437\u0430\u043b\u0441\u044f \u0440\u0430\u0437\u0433\u043e\u0432\u043e\u0440 \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0438\u0442\u043e\u0432\u044b\u043c\u0438 \u0444\u043b\u0430\u0433\u0430\u043c\u0438 \u0432 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u0445 C++. \u041d\u0430 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0443 \u043d\u0430\u0441 \u0443\u0436\u0435 \u0431\u044b\u043b\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f IsEnumFlagSet, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u043d\u0430 \u0432\u0445\u043e\u0434 \u043f\u0435\u0440\u0432\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u0432\u0442\u043e\u0440\u044b\u043c \u2014 \u043d\u0430\u0431\u043e\u0440 \u0444\u043b\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438. \u0427\u0435\u043c \u0436\u0435 \u043e\u043d\u0430 \u043b\u0443\u0447\u0448\u0435 \u0441\u0442\u0430\u0440\u043e\u0433\u043e \u0434\u043e\u0431\u0440\u043e\u0433\u043e \u043f\u043e\u0431\u0438\u0442\u043e\u0432\u043e\u0433\u043e \u0418?<\/p>\n<pre><code class=\"cpp\">if (IsEnumFlagSet(state, flag))  {  } \/\/ vs if (state &#038; flag)  {  } <\/code><\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-296702","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/296702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=296702"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/296702\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=296702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=296702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=296702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}