Suppose you've written a portable C++ code which runs smoothly on different platforms. To make some modifications to optimize performance, you use inline assembly inside your code. Is it a good practice (compiler optimization set aside) or will it make troubles for portability?
假设您编写了一个可在不同平台上顺利运行的可移植C ++代码。要进行一些修改以优化性能,可以在代码中使用内联汇编。这是一个好的做法(编译器优化预留)还是会给可移植性带来麻烦?
4 个解决方案
#1
6
Obviously the inline assembly isn't even close to portable. To maintain any portability at all, you generally have to use an #ifdef
(or something on that order) to determine when to use it at all.
显然,内联组件甚至不便于携带。为了保持任何可移植性,您通常必须使用#ifdef(或该订单上的某些内容)来确定何时使用它。
My own preference is to segregate the assembly language into a separate file, and in the makefile decide whether to build the portable version or the assembly language version.
我自己的偏好是将汇编语言分隔成一个单独的文件,并在makefile中决定是构建可移植版本还是汇编语言版本。
#2
13
Obviously it breaks portability - the code will only work on the specific architecture the assembly language is for. Also, it's normally a waste of time - the compiler's optimiser is almost certainly better at writing assembler code than you are.
显然它破坏了可移植性 - 代码只适用于汇编语言所针对的特定体系结构。此外,这通常是浪费时间 - 编译器的优化器几乎肯定比编写汇编代码更好。
#3
2
It depends.
If you have only x86 assembly, your application won't ever run on ARM and native x64. To solve this, you can surround it with #ifdef's depending on the architecture. This is the approach cross-platform, highly optimized libraries such as h264 use. In most cases, though, it's not worth it. Just use very specific C and it will behave very similarly to native assembly.
如果您只有x86程序集,则您的应用程序将无法在ARM和本机x64上运行。要解决此问题,您可以使用#ifdef将其包围,具体取决于架构。这是跨平台,高度优化的库,如h264使用的方法。但在大多数情况下,这是不值得的。只需使用非常具体的C,它的行为与本机程序集非常相似。
#4
2
The other obvious choice is to only implement inline assembly on certain architectures, and keep the original (unoptimized) C++ for any other architecture, rather than trying to generate assembly for all architectures. (Suitably #ifdefed, of course.) Then you get the benefit of the optimization on the one architecture, with the basic functionality on all.
另一个明显的选择是仅在某些体系结构上实现内联汇编,并为任何其他体系结构保留原始(未优化的)C ++,而不是尝试为所有体系结构生成程序集。 (当然,#ifdefed是合适的。)然后,您可以在一个体系结构上获得优化的好处,并在所有体系结构上具有基本功能。
However, when we've done this on projects I've worked on in the past, this was the worst part to maintain - some other piece of code would change, and exactly what was being passed into the isolated function(s) would change, and the original C++ and assembly wouldn't match any more, and there was much wailing and gnashing of teeth.
然而,当我们在过去曾经做过的项目上完成这项工作时,这是维护最糟糕的部分 - 其他一些代码会发生变化,而且确切地传递给隔离函数的内容会发生变化原来的C ++和程序集不再匹配了,而且还有许多嚎叫和咬牙切齿的牙齿。
#1
6
Obviously the inline assembly isn't even close to portable. To maintain any portability at all, you generally have to use an #ifdef
(or something on that order) to determine when to use it at all.
显然,内联组件甚至不便于携带。为了保持任何可移植性,您通常必须使用#ifdef(或该订单上的某些内容)来确定何时使用它。
My own preference is to segregate the assembly language into a separate file, and in the makefile decide whether to build the portable version or the assembly language version.
我自己的偏好是将汇编语言分隔成一个单独的文件,并在makefile中决定是构建可移植版本还是汇编语言版本。
#2
13
Obviously it breaks portability - the code will only work on the specific architecture the assembly language is for. Also, it's normally a waste of time - the compiler's optimiser is almost certainly better at writing assembler code than you are.
显然它破坏了可移植性 - 代码只适用于汇编语言所针对的特定体系结构。此外,这通常是浪费时间 - 编译器的优化器几乎肯定比编写汇编代码更好。
#3
2
It depends.
If you have only x86 assembly, your application won't ever run on ARM and native x64. To solve this, you can surround it with #ifdef's depending on the architecture. This is the approach cross-platform, highly optimized libraries such as h264 use. In most cases, though, it's not worth it. Just use very specific C and it will behave very similarly to native assembly.
如果您只有x86程序集,则您的应用程序将无法在ARM和本机x64上运行。要解决此问题,您可以使用#ifdef将其包围,具体取决于架构。这是跨平台,高度优化的库,如h264使用的方法。但在大多数情况下,这是不值得的。只需使用非常具体的C,它的行为与本机程序集非常相似。
#4
2
The other obvious choice is to only implement inline assembly on certain architectures, and keep the original (unoptimized) C++ for any other architecture, rather than trying to generate assembly for all architectures. (Suitably #ifdefed, of course.) Then you get the benefit of the optimization on the one architecture, with the basic functionality on all.
另一个明显的选择是仅在某些体系结构上实现内联汇编,并为任何其他体系结构保留原始(未优化的)C ++,而不是尝试为所有体系结构生成程序集。 (当然,#ifdefed是合适的。)然后,您可以在一个体系结构上获得优化的好处,并在所有体系结构上具有基本功能。
However, when we've done this on projects I've worked on in the past, this was the worst part to maintain - some other piece of code would change, and exactly what was being passed into the isolated function(s) would change, and the original C++ and assembly wouldn't match any more, and there was much wailing and gnashing of teeth.
然而,当我们在过去曾经做过的项目上完成这项工作时,这是维护最糟糕的部分 - 其他一些代码会发生变化,而且确切地传递给隔离函数的内容会发生变化原来的C ++和程序集不再匹配了,而且还有许多嚎叫和咬牙切齿的牙齿。