如何使用 32 位除法指令执行 64 位除法?
这是(AFAIK)这个一般主题。
情况是这样的:
我有一个基于 32 位 RISC 微控制器(NEC V810 的变体)的嵌入式系统(视频游戏控制台)。我想写一个定点数学库。我读了这篇文章,但是附带的源代码是用386汇编编写的,所以它是既不能直接使用,也不能轻易修改。
V810内置了整数乘法/除法,但我想使用上面文章中提到的18.14格式。这需要将 64 位 int 除以 32 位 int,而 V810 仅执行(有符号或无符号)32 位/32 位除法(产生 32 位商和 32 位余数)。
所以,我的问题是:如何用 32 位/32 位除法模拟 64 位/32 位除法(以允许被除数的预移位)?或者,从另一个角度看问题,使用标准 32 位算术/逻辑运算将 18.14 定点除以另一个定点的最佳方法是什么? (“最佳”意味着最快、最小或两者兼而有之)。
代数、(V810)汇编和伪代码都可以。我将从 C 调用代码。
提前致谢!
编辑:不知何故,我错过了这个问题...但是,它仍然需要一些修改才能变得超级高效(它必须比 v810 提供的浮点 div 更快,尽管它可能已经是......),所以请随意为我做我的工作,以换取声誉点;)(当然,还有我的图书馆文档中的信用点)。
This is (AFAIK) a specific question within this general topic.
Here's the situation:
I have an embedded system (a video game console) based on a 32-bit RISC microcontroller (a variant of NEC's V810). I want to write a fixed-point math library. I read this article, but the accompanying source code is written in 386 assembly, so it's neither directly usable nor easily modifiable.
The V810 has built-in integer multiply/divide, but I want to use the 18.14 format mentioned in the above article. This requires dividing a 64-bit int by a 32-bit int, and the V810 only does (signed or unsigned) 32-bit/32-bit division (which produces a 32-bit quotient and a 32-bit remainder).
So, my question is: how do I simulate a 64-bit/32-bit divide with a 32-bit/32-bit one (to allow for the pre-shifting of the dividend)? Or, to look at the problem from another way, what's the best way to divide an 18.14 fixed-point by another using standard 32-bit arithmetic/logic operations? ("best" meaning fastest, smallest, or both).
Algebra, (V810) assembly, and pseudo-code are all fine. I will be calling the code from C.
Thanks in advance!
EDIT: Somehow I missed this question... However, it will still need some modification to be super-efficient (it has to be faster than the floating-point div provided by the v810, though it may already be...), so feel free to do my work for me in exchange for reputation points ;) (and credit in my library documentation, of course).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GCC 为许多处理器提供了这样的例程,名为 _divdi3(通常使用通用的 divmod 调用来实现)。 这是一个。一些 Unix 内核也有一个实现,例如 FreeBSD。
GCC has such a routine for many processors, named _divdi3 (usually implemented using a common divmod call). Here's one. Some Unix kernels have an implementation too, e.g. FreeBSD.
如果您的被除数是无符号 64 位,除数是无符号 32 位,架构是 i386 (x86),则
div
汇编指令可以帮助您进行一些准备:如果上面的行带有
__udivdi3
无法为您编译,请使用 Linux 内核中的__div64_32
函数:https://github.com/torvalds/linux/blob/master/lib/div64.cIf your dividend is unsigned 64 bits, your divisor is unsigned 32 bits, the architecture is i386 (x86), the
div
assembly instruction can help you with some preparation:If the line above with
__udivdi3
doesn't compile for you, use the__div64_32
function from the Linux kernel: https://github.com/torvalds/linux/blob/master/lib/div64.c