<small id='CjjiM'></small><noframes id='CjjiM'>

<i id='CjjiM'><tr id='CjjiM'><dt id='CjjiM'><q id='CjjiM'><span id='CjjiM'><b id='CjjiM'><form id='CjjiM'><ins id='CjjiM'></ins><ul id='CjjiM'></ul><sub id='CjjiM'></sub></form><legend id='CjjiM'></legend><bdo id='CjjiM'><pre id='CjjiM'><center id='CjjiM'></center></pre></bdo></b><th id='CjjiM'></th></span></q></dt></tr></i><div id='CjjiM'><tfoot id='CjjiM'></tfoot><dl id='CjjiM'><fieldset id='CjjiM'></fieldset></dl></div>

      • <bdo id='CjjiM'></bdo><ul id='CjjiM'></ul>
      <legend id='CjjiM'><style id='CjjiM'><dir id='CjjiM'><q id='CjjiM'></q></dir></style></legend>

      1. <tfoot id='CjjiM'></tfoot>

        将零、负和正映射到 0、1、2 的无分支代码

        时间:2023-09-16
            <tbody id='hdDyk'></tbody>
          1. <tfoot id='hdDyk'></tfoot>

                <bdo id='hdDyk'></bdo><ul id='hdDyk'></ul>
                  <legend id='hdDyk'><style id='hdDyk'><dir id='hdDyk'><q id='hdDyk'></q></dir></style></legend>
                  <i id='hdDyk'><tr id='hdDyk'><dt id='hdDyk'><q id='hdDyk'><span id='hdDyk'><b id='hdDyk'><form id='hdDyk'><ins id='hdDyk'></ins><ul id='hdDyk'></ul><sub id='hdDyk'></sub></form><legend id='hdDyk'></legend><bdo id='hdDyk'><pre id='hdDyk'><center id='hdDyk'></center></pre></bdo></b><th id='hdDyk'></th></span></q></dt></tr></i><div id='hdDyk'><tfoot id='hdDyk'></tfoot><dl id='hdDyk'><fieldset id='hdDyk'></fieldset></dl></div>

                  <small id='hdDyk'></small><noframes id='hdDyk'>

                  本文介绍了将零、负和正映射到 0、1、2 的无分支代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  编写一个无分支函数,如果两个有符号整数之间的差为零、负数或正数,则返回 0、1 或 2.

                  Write a branchless function that returns 0, 1, or 2 if the difference between two signed integers is zero, negative, or positive.

                  这是一个带有分支的版本:

                  Here's a version with branching:

                  int Compare(int x, int y)
                  {
                      int diff = x - y;
                      if (diff == 0)
                          return 0;
                      else if (diff < 0)
                          return 1;
                      else
                          return 2;
                  }
                  

                  这是一个可能更快的版本,具体取决于编译器和处理器:

                  Here's a version that may be faster depending on compiler and processor:

                  int Compare(int x, int y)
                  {
                      int diff = x - y;
                      return diff == 0 ? 0 : (diff < 0 ? 1 : 2);
                  }
                  

                  你能想出一个没有分支的更快的吗?

                  Can you come up with a faster one without branches?

                  总结

                  我进行基准测试的 10 个解决方案具有相似的性能.实际数字和获胜者因编译器 (icc/gcc)、编译器选项(例如,-O3、-march=nocona、-fast、-xHost)和机器而异.佳能的解决方案在许多基准测试中表现良好,但性能优势同样微乎其微.我很惊讶在某些情况下,某些解决方案比带有分支的幼稚解决方案慢.

                  The 10 solutions I benchmarked had similar performance. The actual numbers and winner varied depending on compiler (icc/gcc), compiler options (e.g., -O3, -march=nocona, -fast, -xHost), and machine. Canon's solution performed well in many benchmark runs, but again the performance advantage was slight. I was surprised that in some cases some solutions were slower than the naive solution with branches.

                  推荐答案

                  int Compare(int x, int y) {
                       return (x < y) + (y < x) << 1;
                  }
                  

                  仅按位?猜猜并且 > 不计算,然后?

                  Bitwise only? Guess < and > don't count, then?

                  int Compare(int x, int y) {
                      int diff = x - y;
                      return (!!diff) | (!!(diff & 0x80000000) << 1);
                  }
                  

                  但是有那个讨厌的-.

                  换个方向.

                  嗯,再试一次:

                  int Compare(int x, int y) {
                      int diff = y - x;
                      return (!!diff) << ((diff >> 31) & 1);
                  }
                  

                  但我猜测 !! 没有标准的 ASM 指令.此外,<< 可以替换为 +,具体取决于哪个更快...

                  But I'm guessing there's no standard ASM instruction for !!. Also, the << can be replaced with +, depending on which is faster...

                  点点滴滴很有趣!

                  嗯,我刚刚了解了 setnz.

                  我还没有检查汇编器的输出(但这次我确实测试了一下),如果幸运的话,它可以节省一整条指令!:

                  I haven't checked the assembler output (but I did test it a bit this time), and with a bit of luck it could save a whole instruction!:

                  subl  %edi, %esi
                  setnz %eax
                  sarl  $31, %esi
                  andl  $1, %esi
                  sarl  %eax, %esi
                  mov   %esi, %eax
                  ret
                  

                  漫步很有趣.

                  我需要睡觉.

                  这篇关于将零、负和正映射到 0、1、2 的无分支代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:执行时间为零的循环 下一篇:std::ifstream 是否比 FILE 慢得多?

                  相关文章

                  最新文章

                    <bdo id='dCozH'></bdo><ul id='dCozH'></ul>

                    <small id='dCozH'></small><noframes id='dCozH'>

                    <tfoot id='dCozH'></tfoot>

                  1. <legend id='dCozH'><style id='dCozH'><dir id='dCozH'><q id='dCozH'></q></dir></style></legend>

                      <i id='dCozH'><tr id='dCozH'><dt id='dCozH'><q id='dCozH'><span id='dCozH'><b id='dCozH'><form id='dCozH'><ins id='dCozH'></ins><ul id='dCozH'></ul><sub id='dCozH'></sub></form><legend id='dCozH'></legend><bdo id='dCozH'><pre id='dCozH'><center id='dCozH'></center></pre></bdo></b><th id='dCozH'></th></span></q></dt></tr></i><div id='dCozH'><tfoot id='dCozH'></tfoot><dl id='dCozH'><fieldset id='dCozH'></fieldset></dl></div>