将低级 x86 优化代码移植到 ARM Cortex-A8 架构
将 C++ x86 代码移植到 ARM 处理器时有哪些主要注意事项?
我知道/听说过的(但我不知道它们是否真的是一个问题,甚至是真的 - 请验证):
- SSE -> NEON
- 64 位整数变成 32 位
- 小端 -> big endian
程序员还应该注意的其他差异和陷阱吗?
What are the main caveats in porting C++ x86 code to an ARM processor?
The ones I know / have heard of (but I don't know if they are really a problem, or even true - please verify):
- SSE -> NEON
- 64bit integers become 32bit
- little endian -> big endian
Any other differences and pitfalls a programmer should be aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何像样的编译器都支持 ARM 上的 64 位数学,因此可能没有必要缩小变量的范围。然而,ARM 本身是 32 位的,因此如果您不需要全范围,那么使用 32 位变量会更快。此外,32 位变量比 8 位和 16 位变量更快,因此如果您有任何
char
或short
循环计数器,可能值得更新它们转换为 int(或者更好的是,unsigned int)。字节序通常不是问题 - 大多数 ARM 芯片要么以小字节序运行,要么可以在大字节序和小字节序之间切换。 的一个问题是一致性。当您访问未对齐的数据时,x86 非常宽容。在 ARM 上,这要么会产生异常,要么(在以后的架构上)使代码运行速度变慢。这通常由编译器处理,但您需要注意是否使用汇编或打包结构。
另一件可能让你困惑的事情是有符号/无符号变量。直到最近,ARM 还没有用于加载/存储有符号
char
的快速指令,因此传统上char
在 ARM 上是无符号。如果您的代码依赖于char
的符号性,则可能会遇到一些问题。快速修复可能是使用有符号字符的编译器开关,但在使用库函数时要小心。此外,在旧芯片上,签名char
会变慢。编辑:我刚刚在 Debian Wiki 上偶然发现了很棒的页面。它主要涉及从旧的 ARM ABI 到 EABI 的移植,但提到的很多内容仍然适用于 x86->ARM。它还链接到一个很好的有关结构对齐的常见问题解答(尽管对于 ARMv6/ v7)。
Any decent compiler supports 64-bit math on ARM, so it might be not necessary to reduce the range of your variables. However, ARM is natively 32-bit so if you don't need full range then using 32-bit variables will be faster. As well, the 32-bit variables are faster than 8-bit and 16-bit ones, so if you have any
char
orshort
loop counters, it might be worth updating them toint
s (or, better,unsigned int
s).Endianness is generally not an issue - most ARM chips either run in little-endian or can switch between big and little endian. What is an issue is alignment. x86 is very forgiving when you access unaligned data. On ARM this either produces an exception or (on later archs) makes your code run slower. This is usually taken care of by the compiler, but you need to watch out if you use assembly or packed structures.
Another thing that might trip you is signed/unsigned variables. Until recently, ARM did not have fast instructions for loading/storing signed
char
s, so traditionallychar
is unsigned on ARM. If your code relies onchar
's signedness, you might have some issues. A quick fix might be to use the compiler switch for signed chars, but take care when using library functions. Also, signedchar
will be slower on older chips.Edit: I just stumbled upon a great page on the Debian Wiki. It deals mostly with porting from old ARM ABI to EABI, but a lot of things mentioned still apply to x86->ARM. It also links to a nice FAQ about structure alignment (although it's not completely correct for ARMv6/v7).