简体   繁体   English

Android将C ++转换/编译为Neon

[英]Android convert/compile C++ as Neon

I am currently writing a simple Android program using C++ with the help of JNI and OpenCV. 我目前正在借助JNI和OpenCV使用C ++编写一个简单的Android程序。 The input is a image stored as a Mat. 输入是存储为Mat的图像。 Instead of using OpenCV's normalize function, I wish to write my own normalize function in C++. 我希望不使用OpenCV的规范化功能,而是希望使用C ++编写自己的规范化功能。 From what I know, there is support for NEON. 据我了解,NEON受到支持。 However, looking at HelloNeon sample in the NDK folder, I realised that the code is written in NEON instrinsics. 但是,通过查看NDK文件夹中的HelloNeon示例,我意识到该代码是用NEON内在函数编写的。

Question: Is there a way to directly compile my C++ code into NEON code? 问题:有没有办法将我的C ++代码直接编译为NEON代码? ie I wish to avoid writing my function in NEON intrinsics. 即我希望避免用NEON内部函数编写函数。

Thank you. 谢谢。

Largely depends on the compiler. 很大程度上取决于编译器。 Both gcc and clang do support "automatic vectorizing" in recent versions, but the quality of the generated code is very variable - mostly depending on the actual source code. 在最近的版本中,gcc和clang都支持“自动矢量化”,但是生成的代码的质量变化很大-主要取决于实际的源代码。 As always, the compiler is firstly responsible for generating correct code, secondly to generate fast/efficient code. 与往常一样,编译器首先负责生成正确的代码,其次负责生成快速/高效的代码。 If in doubt, go for "safe" option. 如有疑问,请选择“安全”选项。

It should, however, work to use -mfpu=neon -ftree-vectorize . 但是,应该使用-mfpu=neon -ftree-vectorize

I do expect that you need to "massage" the code to make it vectorize well, however - at least that's my experience on x86, where the compiler will attempt to build SSE instructions when vectorizing. 我确实希望您需要“按摩”代码以使其很好地向量化,但是-至少这是我在x86上的经验,编译器将在向量化时尝试构建SSE指令。 It succeeds in fairly simple cases, but falls back to "regular code" when the code is more complex. 它在相当简单的情况下会成功,但是在代码更复杂时会退回到“常规代码”。 Later compilers do work better than older versions, as a rule. 通常,更高版本的编译器比旧版本的性能更好。

If you really need to get good vectorizing results, you will probably end up needing to use Neon intrinsics. 如果确实需要获得良好的矢量化结果,则可能最终需要使用Neon内部函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM