有没有办法将整数值限制在一定范围内而不分支?
只是出于好奇。如果我有类似的问题:
if(x < 0)
x = 0;
if(x > some_maximum)
x = some_maximum;
return x;
有没有办法不分支?这是c++。
附录:我的意思是程序集中没有分支指令。这是一个MIPS 架构。
Just out of curiosity. If I have something like:
if(x < 0)
x = 0;
if(x > some_maximum)
x = some_maximum;
return x;
Is there a way to not branch? This is c++.
Addendum: I mean no branch instructions in the assembly. It's a MIPS architecture.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
有一些位技巧可以查找两个数字的最小值或最大值,因此您可以使用它们来查找
min(max(x, 0), some_maximum)
。来自此处:正如消息来源所述,这样做可能会更快正常方式,尽管有分支
There are bit-tricks to find the minimum or maximum of two numbers, so you could use those to find
min(max(x, 0), some_maximum)
. From here:As the source states though, it's probably faster to do it the normal way, despite the branch
这将取决于编译器和处理器,但如果您使用
?:
它可以转换为不使用分支的条件移动(至少在基于 Intel 的处理器上)。<代码>x = x < 0 ? 0:x;
x=x>最大限度 ? max : x;
这可以使用
CMOV
指令(请参阅http://www.intel.com/software/products/documentation/vlin/mergedprojects/analyzer_ec/mergedprojects/reference_olh/mergedProjects/instructions /instruct32_hh/vc35.htm),其目的是避免分支(从而避免分支预测惩罚)。编辑:此帖子 您可能感兴趣。基准测试表明,条件移动只会在不太可预测的分支上为您带来速度增益,而高度可预测的分支(例如长时间运行的循环)更喜欢标准方法。
This is going to be compiler- and processor-dependent, but if you use
?:
it can be translated to a conditional move (at least on Intel-based processors) which does not use a branch.x = x < 0 ? 0 : x;
x = x > max ? max : x;
This can use the
CMOV
instruction (see http://www.intel.com/software/products/documentation/vlin/mergedprojects/analyzer_ec/mergedprojects/reference_olh/mergedProjects/instructions/instruct32_hh/vc35.htm), whose purpose is to avoid branching (and thus branch prediction penalties).Edit: this thread may be of interest to you. Benchmarks show that conditional moves will give you speed gains only on branches that are not very predictable, whereas highly predictable branches (such as that of a long-running loop) prefer the standard approach.
在 C++17 中,您可以使用
std::clamp
In C++17 you can use
std::clamp
使用三元运算符:)
Using the ternary operator :)
取决于你的架构。至少对于 ARM,编译器可能会发出条件执行指令,并且生成的机器代码不会包含分支。但我想不出一个好方法来在 C 程序中明确这一点。
Depends on your architecture. For ARM, at least, the compiler would probably emit conditionally executed instructions and the resulting machine code wouldn't contain a branch. I can't think of a good way to make that explicit in the C program though.
对于未来类似的问题,位黑客页面可能有用:http://graphics.stanford .edu/~seander/bithacks.html。
由于 min 和 max 的 bithack 已经发布,这里有一个不同的:
我刚刚尝试过,它适用于我的几个测试用例。
这是我的计算机(intel arch)的汇编代码,它没有显示任何分支。
For future problems like this, the bit hack page might be useful: http://graphics.stanford.edu/~seander/bithacks.html.
Since the bithack for min and max was already posted, here is a different one:
I just tried it out, and it worked for the few test cases i had.
Here is the assembly code from my computer (intel arch) which shows no branches.
如果可以限制为 2 的幂(不包括在内),那么只需使用
int newx = x & ((2 的最高次方) - 1)
不太确定处理(如果 x < 0 情况)或通用(x < n 情况)
If it's possible to limit to powers of 2 (non inclusive), then just go with
int newx = x & ((highest power of 2) - 1)
not quite sure to handle the (if x < 0 case) or the generic (x < n case)
分支很好地隐藏在具有正常名称的函数内。
建议创建一个clip_by模板。
The branching is hidden away nicely inside functions with normal names.
Suggesting to create a clip_by template.