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

  • <tfoot id='vlAAN'></tfoot>
      <legend id='vlAAN'><style id='vlAAN'><dir id='vlAAN'><q id='vlAAN'></q></dir></style></legend>

        • <bdo id='vlAAN'></bdo><ul id='vlAAN'></ul>

        生成从位置 i 开始的 n 个掩码的最快方法

        时间:2023-09-16
          <bdo id='CVEy8'></bdo><ul id='CVEy8'></ul>
          • <tfoot id='CVEy8'></tfoot>

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

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

                <tbody id='CVEy8'></tbody>
                  本文介绍了生成从位置 i 开始的 n 个掩码的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  从位置pos开始生成len位设置为1的掩码的最快方法是什么(就常见现代架构的cpu周期而言):

                  What is the fastest way (in terms of cpu cycles on common modern architecture), to produce a mask with len bits set to 1 starting at position pos:

                  template <class UIntType>
                  constexpr T make_mask(std::size_t pos, std::size_t len)
                  {
                      // Body of the function
                  }
                  
                  // Call of the function
                  auto mask = make_mask<uint32_t>(4, 10);
                  // mask = 00000000 00000000 00111111 11110000 
                  // (in binary with MSB on the left and LSB on the right)
                  

                  另外,是否有任何编译器内在函数或 BMI 函数可以提供帮助?

                  Plus, is there any compiler intrinsics or BMI function that can help?

                  推荐答案

                  如果用starting at pos"表示掩码的最低位在2对应的位置pos(如你的例子):

                  If by "starting at pos", you mean that the lowest-order bit of the mask is at the position corresponding with 2pos (as in your example):

                  ((UIntType(1) << len) - UIntType(1)) << pos
                  

                  如果 len 可能是 ≥UIntType 中的位数,通过测试避免未​​定义行为:

                  If it is possible that len is ≥ the number of bits in UIntType, avoid Undefined Behaviour with a test:

                  (((len < std::numeric_limits<UIntType>::digits)
                       ? UIntType(1)<<len
                       : 0) - UIntType(1)) << pos
                  

                  (如果 pos 也有可能是 ≥ std::numeric_limits,您将需要另一个三元运算测试.)

                  (If it is also possible that pos is ≥ std::numeric_limits<UIntType>::digits, you'll need another ternary op test.)

                  您也可以使用:

                  (UIntType(1)<<(len>>1)<<((len+1)>>1) - UIntType(1)) << pos
                  

                  以三个额外的移位运算符为代价避免了三元运算;我怀疑它是否会更快,但需要仔细的基准测试才能确定.

                  which avoids the ternary op at the cost of three extra shift operators; I doubt whether it would be faster but careful benchmarking would be necessary to know for sure.

                  这篇关于生成从位置 i 开始的 n 个掩码的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

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

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

                        • <tfoot id='T1PVm'></tfoot>