分裂有多快?
我曾经应该编写一个简短的汇编代码来除以不是 2 的幂的数字。我的解决方案是以周期数减去除法器,周期数就是实际结果。有什么更快的吗?解决这个问题的通常方法是什么?
i was once supposed to make a short assembler code for dividing with numbers that are not power of 2. My solution was subtracting the divider in cycles and the number of cycles was the actual result. Is there anything faster? What's the usual way to sort this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重复减法是一种危险且低效的除法方法。在最坏的情况下,N 位除法可能需要
O(2**N)
减法!@Johannes 答案有一个链接,可以为您提供比这更好的算法。
如果我被要求在汇编程序中实现除法,我可能会广泛搜索现有的数字例程库。对于这种问题,需要大量的专业知识才能提出接近最佳的代码。
编辑:回应OP的评论:
我建议您只使用除法,并将其留给 C++ 编译器来生成最有效的指令序列,以实现特定目标平台所需的结果。
Repeated subtraction is a dangerously inefficient way of doing division. In the worst case, an N-bit division can take
O(2**N)
subtractions!!@Johannes answer has a link that gives you algorithms that work much better than that.
If I was asked to implement division in assembler, I'd probably do an extensive search for an existing library of numeric routines. This is the kind of problem where it takes a lot of expertise to come up with close to optimal code.
EDIT : in response to the OP's comment:
I suggest that you just use division, and leave it to the C++ compiler to generate the most efficient instruction sequence to achieve the required result for your particular target platform.
维基百科 上提到并详细介绍了许多算法。
There are a bunch of algorithms mentioned and detailed on Wikipedia.