两种方法将Android NDK samples中hello-neon改成C++
一、第一種方法:
1.修改helloneon.c 中代碼
?a.將
??char*? str;
?? 改為
???? char str[512] = {0};
?b.將
??asprintf(&str, "FIR Filter benchmark:\nC version????????? : %g ms\n", time_c);
?? 改為
???? sprintf(str, "FIR Filter benchmark:\nC version??? : %g ms\n", time_c);
?? 同時刪除 free(str);
?? (b 項 有兩處都要修改)
?c.將
??return (*env)->NewStringUTF(env, buffer);
?改為
??return env->NewStringUTF(buffer);
2.將helloneon.c改名為helloneon.cpp
? 將helloneon-intrinsics.c改名為helloneon-intrinsics.cpp
3.將Android.mk中
?將
??LOCAL_SRC_FILES := helloneon.c
?改為
??LOCAL_SRC_FILES := helloneon.cpp
?將
??LOCAL_SRC_FILES += helloneon-intrinsics.c.neon
?改為
??LOCAL_SRC_FILES += helloneon-intrinsics.cpp.neon
4.打開 helloneon.cpp
? 在#include 和 各種 #define 后面添加:
?#ifdef __cplusplus
?extern "C" {
?#endif
? 在代碼段最后面添加:
? ?#ifdef __cplusplus
?}
?#endif
? 不修改這一條的話,會提示找不到stringFromJNI。
不明問題:
1.經常提示一些jni和cpu-features的函數未定義。在路徑中添加jni.h和cpu-features.h
2.即便是添加了arm_neon.h,還是會提示:
Type 'int16x4_t' could not be resolved?helloneon-intrinsics.cpp?/HelloNeon/jni?line 35
但是 重啟eclipse 一般就不提示了。一旦打開了helloneon-intrinsics.cpp還是會提示。
最終 hello-neon.cpp
1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 #include <jni.h> 18 #include <time.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <cpu-features.h> 22 #include "helloneon-intrinsics.h" 23 24 #define DEBUG 0 25 26 #if DEBUG 27 #include <android/log.h> 28 # define D(x...) __android_log_print(ANDROID_LOG_INFO,"helloneon",x) 29 #else 30 # define D(...) do {} while (0) 31 #endif 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 /* return current time in milliseconds */ 38 static double 39 now_ms(void) 40 { 41 struct timespec res; 42 clock_gettime(CLOCK_REALTIME, &res); 43 return 1000.0*res.tv_sec + (double)res.tv_nsec/1e6; 44 } 45 46 47 /* this is a FIR filter implemented in C */ 48 static void 49 fir_filter_c(short *output, const short* input, const short* kernel, int width, int kernelSize) 50 { 51 int offset = -kernelSize/2; 52 int nn; 53 for (nn = 0; nn < width; nn++) { 54 int sum = 0; 55 int mm; 56 for (mm = 0; mm < kernelSize; mm++) { 57 sum += kernel[mm]*input[nn+offset+mm]; 58 } 59 output[nn] = (short)((sum + 0x8000) >> 16); 60 } 61 } 62 63 #define FIR_KERNEL_SIZE 32 64 #define FIR_OUTPUT_SIZE 2560 65 #define FIR_INPUT_SIZE (FIR_OUTPUT_SIZE + FIR_KERNEL_SIZE) 66 #define FIR_ITERATIONS 600 67 68 static const short fir_kernel[FIR_KERNEL_SIZE] = { 69 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10, 70 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10 }; 71 72 static short fir_output[FIR_OUTPUT_SIZE]; 73 static short fir_input_0[FIR_INPUT_SIZE]; 74 static const short* fir_input = fir_input_0 + (FIR_KERNEL_SIZE/2); 75 static short fir_output_expected[FIR_OUTPUT_SIZE]; 76 77 /* This is a trivial JNI example where we use a native method 78 * to return a new VM String. See the corresponding Java source 79 * file located at: 80 * 81 * apps/samples/hello-neon/project/src/com/example/neon/HelloNeon.java 82 */ 83 jstring 84 Java_com_example_neon_HelloNeon_stringFromJNI( JNIEnv* env, 85 jobject thiz ) 86 { 87 //char* str; 88 char str[512] = {0}; 89 uint64_t features; 90 char buffer[512]; 91 char tryNeon = 0; 92 double t0, t1, time_c, time_neon; 93 94 /* setup FIR input - whatever */ 95 { 96 int nn; 97 for (nn = 0; nn < FIR_INPUT_SIZE; nn++) { 98 fir_input_0[nn] = (5*nn) & 255; 99 } 100 fir_filter_c(fir_output_expected, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE); 101 } 102 103 /* Benchmark small FIR filter loop - C version */ 104 t0 = now_ms(); 105 { 106 int count = FIR_ITERATIONS; 107 for (; count > 0; count--) { 108 fir_filter_c(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE); 109 } 110 } 111 t1 = now_ms(); 112 time_c = t1 - t0; 113 114 sprintf(str, "FIR Filter benchmark:\nC version : %g ms\n", time_c); 115 //asprintf(&str, "FIR Filter benchmark:\nC version : %g ms\n", time_c); 116 strlcpy(buffer, str, sizeof buffer); 117 118 //free(str); 119 120 strlcat(buffer, "Neon version : ", sizeof buffer); 121 122 if (android_getCpuFamily() != ANDROID_CPU_FAMILY_ARM) { 123 strlcat(buffer, "Not an ARM CPU !\n", sizeof buffer); 124 goto EXIT; 125 } 126 127 features = android_getCpuFeatures(); 128 if ((features & ANDROID_CPU_ARM_FEATURE_ARMv7) == 0) { 129 strlcat(buffer, "Not an ARMv7 CPU !\n", sizeof buffer); 130 goto EXIT; 131 } 132 133 /* HAVE_NEON is defined in Android.mk ! */ 134 #ifdef HAVE_NEON 135 if ((features & ANDROID_CPU_ARM_FEATURE_NEON) == 0) { 136 strlcat(buffer, "CPU doesn't support NEON !\n", sizeof buffer); 137 goto EXIT; 138 } 139 140 /* Benchmark small FIR filter loop - Neon version */ 141 t0 = now_ms(); 142 { 143 int count = FIR_ITERATIONS; 144 for (; count > 0; count--) { 145 fir_filter_neon_intrinsics(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE); 146 } 147 } 148 t1 = now_ms(); 149 time_neon = t1 - t0; 150 sprintf(str, "%g ms (x%g faster)\n", time_neon, time_c / (time_neon < 1e-6 ? 1. : time_neon)); 151 //asprintf(&str, "%g ms (x%g faster)\n", time_neon, time_c / (time_neon < 1e-6 ? 1. : time_neon)); 152 strlcat(buffer, str, sizeof buffer); 153 //free(str); 154 155 /* check the result, just in case */ 156 { 157 int nn, fails = 0; 158 for (nn = 0; nn < FIR_OUTPUT_SIZE; nn++) { 159 if (fir_output[nn] != fir_output_expected[nn]) { 160 if (++fails < 16) 161 D("neon[%d] = %d expected %d", nn, fir_output[nn], fir_output_expected[nn]); 162 } 163 } 164 D("%d fails\n", fails); 165 } 166 #else /* !HAVE_NEON */ 167 strlcat(buffer, "Program not compiled with ARMv7 support !\n", sizeof buffer); 168 #endif /* !HAVE_NEON */ 169 EXIT: 170 //return (*env)->NewStringUTF(env, buffer); // C Version 171 return env->NewStringUTF(buffer); // C++ Version 172 } 173 174 #ifdef __cplusplus 175 } 176 #endif View Code?
二、第二種方法:
1. helloneon.cpp的修改如下
?
2.其他改動部分同方法一。
不明問題:
stringforneon 的返回類型 只能為 jstring 不能用 string。
參考
http://blog.csdn.net/martingang/article/details/8170940
如果提示錯誤:error: #error You must enable NEON instructions (e.g. -mfloat-abi=softfp -mfpu=neon) to use arm_neon.h
Android.mk 需要添加兩句
1.LOCAL_SRC_FILES := helloneon.cpp 下面添加
TARGET_ARCH_ABI := armeabi-v7a
2.LOCAL_CFLAGS := -DHAVE_NEON=1 后邊添加 -mfloat-abi=softfp -mfpu=neon -march=armv7-a
即是? LOCAL_CFLAGS := -DHAVE_NEON=1 -mfloat-abi=softfp -mfpu=neon -march=armv7-a
參考
http://blog.sina.com.cn/s/blog_92c104590101jiyi.html
轉載于:https://www.cnblogs.com/zzugyl/archive/2013/05/21/3090710.html
總結
以上是生活随笔為你收集整理的两种方法将Android NDK samples中hello-neon改成C++的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Javascript之创建对象(原型模式
- 下一篇: dnf奴剑士带什么武器好?