64 位除以 32 位
我正在寻找一种快速方法来执行以下除法:
- 被除数是一个有符号的 64 位整数。
- 除数是一个有符号的 32 位整数。
- 商应该是有符号的 64 位整数,不需要余数。
- 被除数的低双字为零。
我仅使用 32 位数据类型,因为编译器对 64 位数据类型的支持很差,并且没有汇编。为了速度,准确性可能会有所妥协。
关于这个有什么指示吗?
I am looking for a fast way to perform the following divison:
- Dividend is a signed 64 bit integer.
- Divisor is a signed 32 bit integer.
- Quotient should be a signed 64 bit integer, remainder is unnecessary.
- Low dword of the dividend is zero.
I am using only 32 bit data types, since 64 bit ones are poorly supported by the compiler, and no assembly. Accuracy can be somewhat compromised in favor of speed.
Any pointers on this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
i386 和可能的其他机器直接支持 64/32 除法,只要被除数的高位字小于除数(即被除数在 32x32->64 乘以除数的范围内)。如果您的编译器对 64 位类型的支持最低限度,它可能能够识别这种情况并利用它。
假设你已经检查过生成的asm并发现它没有利用这一点,或者如果你知道你的cpu没有这样的除法指令,那么你只需要像你在小学学到的那样进行长除法。只不过它的基数是 4294967296 而不是基数 10。
您可以尝试阅读
libgcc
的源代码,因为它包含针对没有本机支持的计算机的 64/64 除法代码。编辑:实际上,由于您没有 64/32 除法运算,因此您可能需要使用 base-65536。这是因为简单的长除法需要在每一步将“2 位数字”除以“1 位数字”。当然,现在您必须执行更多步骤......
64/32 division is supported directly by i386 and possibly other machines, as long as the high word of the dividend is less than the divisor (i.e. the dividend is in the range of a 32x32->64 multiply by the divisor). If your compiler has minimal support for 64 bit types, it may be able to recognize this situation and take advantage of it.
Assuming you've already checked the generated asm and found that it does not take advantage of this, or if you know your cpu does not have such a division instruction, then you simply need to do long division like you learned in grade school.. except that it's base-4294967296 instead of base-10.
You might try reading the source to
libgcc
, since it contains code for 64/64 division for machines that don't have native support.Edit: Actually, since you don't have a 64/32 divide operation, you may want to use base-65536. This is because naive long division requires dividing a "2-digit" number by a "1-digit" number at each step. Of course, now you're stuck doing more steps..