问题描述
从位置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
:
另外,是否有任何编译器内在函数或 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):
如果 len
可能是 ≥UIntType
中的位数,通过测试避免未定义行为:
If it is possible that len
is ≥ the number of bits in UIntType
, avoid Undefined Behaviour with a test:
(如果 pos
也有可能是 ≥ std::numeric_limits
(If it is also possible that pos
is ≥ std::numeric_limits<UIntType>::digits
, you'll need another ternary op test.)
您也可以使用:
以三个额外的移位运算符为代价避免了三元运算;我怀疑它是否会更快,但需要仔细的基准测试才能确定.
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 个掩码的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!