为什么模运算符有必要吗? [英] Why is modulo operator necessary?

查看:150
本文介绍了为什么模运算符有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过您可以通过逻辑和这样的替换操作mod的文件中:

I've read in a document that you can replace mod operation by logical and like this :

相反:

int Limit = Value % Range;

您做的:

int Limit = Value & (Range-1);

但仍编译器生成mod的指示和我的问题基本上是:为什么如果他们的工作相同的编译器不使用最有效的方法?

But compilers still generate mod instructions and my question is basically : Why do compilers don't use the most efficient approach if they work the same ?

推荐答案

您可以与替代模只有当它是2.用初等数学的动力来取代它没有一个模

you can replace modulo with that only if it is a power of 2. Using elementary math to replace it without a modulo

a = b % c;

可以做到

x = b % c;
a = b / (x*c);

让我们用一个例子来检查这

Lets check this with an example

25 % 7 = 
25 / 7 = 3 (integer math)
25 - (3 * 7) =
25 - 21 = 4

这是我怎么也得做我的计算器反正因为我没有一个模运算符。

Which is how I have to do it on my calculator anyway as I dont have a modulo operator.

注意

25 & (7-6) = 
0x19 & 0x6 = 0x0

所以,你的替代是行不通的。

So your substitution does not work.

不仅大多数处理器没有模,很多没有鸿沟。退房黑客取悦的书。

Not only do most processors not have a modulo, many do not have a divide. Check out the hackers delight book.

为什么要取模?如果你已经烧毁硬件做一个除法,你可能愿意去额外英里加模为好。大多数处理器把你的问题到新的水平,你为什么会在硬件中实现除法时,它可以通过软件来完成。在回答你的问题是大多数的处理器家族没有一个模,而且很多没有鸿沟,因为它比软件解决方案是不值得的芯片房地产,电力消耗等。该软件解决方案是痛苦少/成本/风险。

WHY would you want modulo? If you have burned the hardware to make a divide, you might be willing to go that extra mile to add modulo as well. Most processors take your question to the next level, why would you implement a divide in hardware when it can be done in software. The answer to your question is most processor families do not have a modulo, and many do not have a divide because it is not worth the chip real estate, power consumed, etc compared to the software solution. The software solution is less painful/costly/risky.

现在我假设你的问题是不是中奖海报回答。对于情况下,范围为二的幂和身份没有工作......首先,如果范围不是在编译时已知,那么你必须做减法和和,两个操作,也许一个中间变量,也就是比模更昂贵,编译器将是错误的优化来代替模减法和和。如果范围为二的幂,在编译时已知您更好/票友编译器将优化。有次,尤指具有可变字长的指令集,其中的小指令可以在较大的指令一起使用,它可能是痛苦少加载范围,做一个模比加载非零位的数量较大(值符合您身份的范围内有一个单独的位值设置,其他位均为零,为0x100,0X40,为0x8000等),做模。负载直接加模数可能比负载直接加便宜和,或模立即可能比和直接便宜。你必须检查的指令集和编译器如何实现的解决方案。

Now I assume your question is not what the winning poster answered. For cases where the Range is a power of two and the identity does work... First off if range is not known at compile time then you have to do a subtract and an and, two operations, and maybe an intermediate variable, that is much more costly than a modulo, the compiler would be in error to optimize to a subtract and and instead of a modulo. If the range is a power of two and is known at compile time your better/fancier compilers will optimize. There are times, esp with a variable word length instruction set where the smaller instruction may be used over the larger instruction, it might be less painful to load Range and do a modulo than to load the larger number of non-zero bits (values of Range that match your identity have a single bit set in the value, the other bits are zero, 0x100, 0x40, 0x8000, etc) and do the modulo. the load immediate plus modulo might be cheaper than the load immediate plus and, or the modulo immediate might be cheaper than the and immediate. You have to examine the instruction set and how the compiler has implemented the solution.

我建议你张贴的地方是不是做一些优化的例子,我相信我们可以张贴的编译器在哪里做您所期望的最优化例子很多。

I suggest you post some examples of where it is not doing the optimization, and I assume we can post many examples of where the compiler has done the optimization you were expecting.

这篇关于为什么模运算符有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆