Apple 编程中如何普遍使用整数除法?

发布于 2025-01-09 21:35:36 字数 896 浏览 1 评论 0原文

我正在阅读对 LLVM 和 Swift 的作者 Chris Lattner 的有趣采访,并注意到一个非常奇怪的说法:

其他事情是,Apple 会定期向其 CPU 添加新指令 [11:00]。历史上的一个例子是它推出的名为“Swift”的芯片,这是第一款内部设计的 32 位 ARM 芯片。如果我没记错的话,这是 iPhone 5。

在这个芯片中,他们添加了整数除法指令。在此之前的所有芯片都不具备在硬件中进行整数除法的能力:您必须实际对其进行开放代码,并且有一个库函数可以做到这一点。 [11:30] 这一点,以及他们添加的其他一些指令,是一个相当大的问题,并且被广泛使用

现在这令人惊讶。据我了解,几乎不需要整数除法。我见过的可以使用它的情况分为以下几类:

  • 除以 2 的幂。改为右移。

  • 除以整数常量。而是乘以倒数。 (这是违反直觉的,但确实这在所有情况下都有效。)

  • 作为实数算术近似值的定点。请改用浮点数。

  • 定点,适用于在具有不同 CPU 架构的计算机上点对点运行的多人游戏,并且需要每台计算机就最后一位的结果达成一致。好的,但据我了解,iPhone 上的多人游戏不使用这种点对点设计。

  • 渲染 3D 纹理。请改为在 GPU 上执行此操作。

当浮点在硬件中可用后,我从未见过需要以很高的频率进行整数除法的工作负载。

我缺少什么?整数除法在 Apple 设备上的用途如此频繁,以至于值得将其添加为 CPU 指令?

I was reading an interesting interview with Chris Lattner, author of LLVM and Swift, and noticed a very curious claim:

Other things are that Apple does periodically add new instructions [11:00] to its CPUs. One example of this historically was the hilariously named “Swift” chip that it launched which was the first designed in-house 32-bit ARM chip. This was the iPhone 5, if I recall.

In this chip, they added an integer-divide instruction. All of the chips before that didn’t have the ability to integer-divide in hardware: you had to actually open-code it, and there was a library function to do that. [11:30] That, and a few other instructions they added, were a pretty big deal and used pervasively

Now that is surprising. As I understand it, integer divide is almost never needed. The cases I've seen where it could be used, fall into a few categories:

  • Dividing by a power of two. Shift right instead.

  • Dividing by an integer constant. Multiply by the reciprocal instead. (It's counterintuitive but true that this works in all cases.)

  • Fixed point as an approximation of real number arithmetic. Use floating point instead.

  • Fixed point, for multiplayer games that run peer-to-peer across computers with different CPU architectures and need each computer to agree on the results down to the last bit. Okay, but as I understand it, multiplayer games on iPhone don't use that kind of peer-to-peer design.

  • Rendering 3D textures. Do that on the GPU instead.

After floating point became available in hardware, I've never seen a workload that needed to do integer divide with significant frequency.

What am I missing? What was integer divide used for on Apple devices, so frequently that it was considered worth adding as a CPU instruction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文