使用 iPhone SIMD 浮点单元进行浮点到整数转换

发布于 2024-07-26 22:55:38 字数 642 浏览 1 评论 0原文

我目前正在尝试用 Shark 优化一些与 DSP 相关的代码,发现我在浮点到整数转换上浪费了很多时间:

SInt16 nextInt = nextFloat * 32768.0f + 0.5f;

由于 iPhone 似乎有一个 ARM11 FP 协处理器,我想知道是否可以替换我的代码与 FTOSI 指令。 有一些文档 可在 ARM 网站上找到,但我没有内联手动优化汇编的经验。 以前有人这样做过吗?
我想我可以内联代码

__asm__ volatile

但是如何检查指令是否可用?
我怎样才能传递我的值呢?

编辑1: 正如路易斯已经指出的那样,我忘记提及我正在关闭“Compile for Thumb”的情况下进行编译。

编辑2: 由于我想将 float 转换为有符号 Int16 而不是无符号 Int,因此我将 ARM 指令从 FTOUI 更改为 FTOSI。 这是原帖中的一个错误。

I am currently trying to optimize some DSP related code with Shark and found that I am wasting a lot of time in a float to integer conversion:

SInt16 nextInt = nextFloat * 32768.0f + 0.5f;

As the iPhone seems to have an ARM11 FP co-processor, I am wondering if I can replace my code with the FTOSI instruction.
There is some documentation available at the ARM website, but I have no experience in inlining hand optimized assembly.
Has someone done that before?
I think I could inline the code with

__asm__ volatile

But how do I check if the instruction is available?
How can I pass my value in?

EDIT1:
As Louis already pointed out, I forgot to mention that I am compiling with "Compile for Thumb" turned off.

EDIT2:
As I want to convert float to signed Int16 and not unsigned Int, I changed the ARM instruction from FTOUI to FTOSI. This was a mistake in the original post.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2024-08-02 22:55:38

这可能是一个显而易见的问题,但你确定你的目标是 ARM 吗? 默认情况下,iPhone SDK 编译 THUMB 的所有应用程序,它使用软件浮点所有内容(包括浮点/整数转换)。

无论如何,如果设备有 VFP 协处理器,它就有指令。 您可以通过读取 FPSID 寄存器并确保它是受支持的型号来检查它是否具有合适的协处理器。

我怀疑可以肯定地假设所有 iPhone 都支持它。 除此之外,Apple 的汇编器支持操作码,并且 LLVM ARM 后端使用它进行类型转换,这意味着当 Apple 最终在手机上支持 LLVM 时,他们的编译器将生成 FTOUI 指令。

This may be an an obvious question, but are you sure you are targeting ARM? By default the iPhone SDK compiles all apps for THUMB, which uses software floating point everything (including float/int conversion).

Anyway, if the device has a VFP coprocessor it has the instruction. You can check if it has an appropriate coprocessor by reading the FPSID register and making sure it is a supported model.

I suspect it is safe to assume all iPhones support it. Among other things Apple's assembler has support for the opcode, and the LLVM ARM backend uses it for the type conversions, which means when Apple eventually supports LLVM on the phone their compiler is going to generate FTOUI instructions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文