1. Open eclipse,file-->new-->androidapplication project-->application Name:hello-neon, Project Name: Hello-neon,package Name:com.hello_neon.android, Minimum Required sdk:api 9:android 2.3 (Gingerbread),next--> Remove the tick of Create custom launcher icon, Next-->next-->activityname:hello_neonprojectactivity,finish.
2. Open the Src-->com.hello_neon.android-->hello_neonprojectactivity.java under the Hello-neon project and change its contents to:
Package Com.hello_neon.android;import Android.os.bundle;import Android.app.activity;import Android.widget.TextView ;p Ublic class Hello_neonprojectactivity extends activity {/** Called when the activity is first created. */@Overri De public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); /* Create a TextView and set its content. * The text is retrieved by calling a native function. */TextView TV = new TextView (this); Tv.settext (Stringfromjni ()); Setcontentview (TV); }/* A Native method that's implemented by the * ' helloneon ' native library, which are packaged with this Applicati On. */Public native String Stringfromjni (); /* Used to load the ' Helloneon ' Library on application * startup. The library has already been unpacked to */data/data/com.example.neon/lib/libhelloneon.so at * installation Tim E by the package manager. */Static {System.loadlibrary ("Helloneon"); }}
3, save the Hello_neonprojectactivity.java file, open a command line window, locate it in the \bin\classes directory, enter the command: Javah–classpath D:\ProgramFiles\Android\ Android-sdk\platforms\android-9\android.jar;com.hello_neon.android.hello_neonprojectactivity, will be in \bin\ Generate Com_hello_neon_android_hello_neonprojectactivity.h files under the classes directory (Note: *.jar can also be other versions);
4, select Hello-neon Project, right click-->new-->folder Create a new JNI folder, add Android.mk, Application.mk, helloneon.c in this folder, HELLONEON-INTRINSICS.C, helloneon-intrinsics.h Five files, the contents of which are:
ANDROID.MK:
Local_path: = $ (call My-dir) include $ (clear_vars) local_module : = helloneon# Fill in the source file path to compile local_src_files: = HELLONEON.C helloneon-intrinsics.c# Default contains the header file path local_c_includes: = $ (Local_path) $ (local_path)/. #-g a series of items later added to use the Arm_neon-h header file,-mfloat-abi=softfp-mfpu=neon use arm_neon.h must local_cflags: =-G-MFLOAT-ABI=SOFTFP- Mfpu=neon-march=armv7-a-mtune=cortex-a8local_ldlibs: =-lz-llogtarget_arch_abi: = armeabi-v7a LOCAL_ARM_MODE: = Armifeq ($ (Target_arch_abi), armeabi-v7a) #采用NEON优化技术 Local_arm_neon: = True #LOCAL_CFLAGS: =-dhave_neon= 1endiflocal_static_libraries: = cpufeatures# generate dynamic call library include $ (build_shared_library) $ (Calls Import-module,cpufeatures )
APPLICATION.MK:
App_project_path: = $ (call My-dir)/. App_platform: = Android-10#choose which library to compile against in your makefileapp_stl: = Stlport_static#app_abi This sentence specifies a Target platform type, can be optimized for different platforms, x86 or armeabi-v7a# Build both ARMv5TE and Armv7-a machine code. App_abi: = Armeabi armeabi-v7aapp_cppflags + =-fexceptions#for using C + + features,you need to enable these in your Makefil Eapp_cpp_features + = Exceptions Rtti
HELLONEON.C:
#include <jni.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #include < cpu-features.h> #include "helloneon-intrinsics.h" #define DEBUG 0#define have_neon#if debug#include <android/ log.h># define D (x ...) __android_log_print (Android_log_info, "Helloneon", x) #else # define D (...) Do {} and (0) #endif/* Return current time in milliseconds */static Doublenow_ms (void) {struct TIMESPEC res; Clock_gettime (Clock_realtime, &res); return 1000.0*res.tv_sec + (double) Res.tv_nsec/1e6;} /* This is a FIR filter implemented-C */static voidfir_filter_c (short *output, const short* input, const short* kernel, int width, int kernelsize) {int offset =-KERNELSIZE/2; int nn; for (nn = 0; nn < width; nn++) {int sum = 0; int mm; for (mm = 0; mm < kernelsize; mm++) {sum + = kernel[mm]*input[nn+offset+mm]; } Output[nn] = (short) ((sum + 0x8000) >> 16); }} #define Fir_kernel_size 32#define fir_output_size 2560#define fir_input_size (fir_output_size + fir_kernel_size) #define FIR_ITERATIONS 600static const short Fir_kernel[fir_kernel_size] = {0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa 2, 0x8c, 070, 0x40, 0x20, 0x10, 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20 , 0x10};static short fir_output[fir_output_size];static short fir_input_0[fir_input_size];static const Shor t* fir_input = fir_input_0 + (FIR_KERNEL_SIZE/2); static short fir_output_expected[fir_output_size];/* This is a tri Vial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * Apps/samples/hello-neon/project/src/com/example/neon/helloneon. Java */jniexport jstring jnicall java_com_hello_1neon_android_hello_1neonprojectactivity_stringfromjni (JNIEnv *env, Jobject thiz) {char* str; uint64_t features; Char buffer[512]; CHar tryneon = 0; Double t0, T1, Time_c, Time_neon; /* Setup FIR Input-whatever */{int nn; for (nn = 0; nn < fir_input_size; nn++) {Fir_input_0[nn] = (5*NN) & 255; } fir_filter_c (fir_output_expected, Fir_input, Fir_kernel, Fir_output_size, fir_kernel_size); }/* Benchmark small FIR filter loop-c version */t0 = Now_ms (); {int count = fir_iterations; for (; count > 0; count--) {fir_filter_c (Fir_output, Fir_input, Fir_kernel, Fir_output_size, Fir_kernel_siz E); }} T1 = Now_ms (); Time_c = t1-t0; asprintf (&str, "FIR Filter benchmark:\nc version:%g ms\n", Time_c); strlcpy (buffer, str, sizeof buffer); Free (str); Strlcat (Buffer, "Neon version:", sizeof buffer); if (android_getcpufamily () = Android_cpu_family_arm) {strlcat (buffer, "not a ARM CPU!\n", sizeof buffer); Goto EXIT; } features = Android_getcpufeatures (); if ((features & android_cpu_arm_feature_armv7) = = 0) {strlcat (buffer, "not a ARMv7 CPU!\n", sizeof Buffe R); Goto EXIT; }/* Have_neon is defined in ANDROID.MK! */#ifdef Have_neon if ((features & android_cpu_arm_feature_neon) = = 0) {strlcat (buffer, "CPU doesn ' t suppor T NEON!\n ", sizeof buffer); Goto EXIT; }/* Benchmark small FIR filter Loop-neon version */t0 = Now_ms (); {int count = fir_iterations; for (; count > 0; count--) {fir_filter_neon_intrinsics (Fir_output, Fir_input, Fir_kernel, Fir_output_size, Fir_kernel_size); }} T1 = Now_ms (); Time_neon = t1-t0; asprintf (&str, "%g MS (x%g faster) \ n", Time_neon, Time_c/(Time_neon < 1e-6?) 1.: Time_neon)); Strlcat (buffer, str, sizeof buffer); Free (str); /* Check the result, just in case */{int nn, fails = 0; for (nn = 0; nn < fir_output_size; nn++) {if (Fir_output[nn]! = Fir_output_expected[nn]) {if (++fails <) D ("neon[%d] =%d expected%d", nn, fi R_OUTPUT[NN], fir_output_expected[nn]); }} D ("%d fails\n", fails); } #else/*! Have_neon */strlcat (buffer, "program is not compiled with ARMV7 support!\n", sizeof buffer); #endif/*! Have_neon */exit:return (*env)->newstringutf (env, buffer);}
Helloneon-intrinsics.h:
#ifndef helloneon_intrinsics_h#define helloneon_intrinsics_hvoid fir_filter_neon_intrinsics (short *output, const short* input, const short* kernel, int width, int kernelsize); #endif/* Helloneon_intrinsics_h */
HELLONEON-INTRINSICS.C:
#include "helloneon-intrinsics.h" #include <arm_neon.h>/* this source file should is compiled by ANDROID.MK when Targeting * The armeabi-v7a ABI, and should is built in NEON mode */voidfir_filter_neon_intrinsics (short *output, const s hort* input, const short* kernel, int width, int kernelsize) {#if 1 int nn, offset =-KERNELSIZE/2; for (nn = 0; nn < width; nn++) {int mm, sum = 0; int32x4_t Sum_vec = vdupq_n_s32 (0); for (mm = 0; mm < KERNELSIZE/4; mm++) {int16x4_t Kernel_vec = VLD1_S16 (kernel + mm*4); int16x4_t Input_vec = VLD1_S16 (input + (nn+offset+mm*4)); Sum_vec = VMLAL_S16 (Sum_vec, Kernel_vec, Input_vec); } Sum + = Vgetq_lane_s32 (Sum_vec, 0); Sum + = Vgetq_lane_s32 (Sum_vec, 1); Sum + = Vgetq_lane_s32 (Sum_vec, 2); Sum + = Vgetq_lane_s32 (Sum_vec, 3); if (Kernelsize & 3) {for (mm = Kernelsize-(kernelsize & 3); mm < kernelsize; mm++) Sum + = kernel[mm] * INPUT[NN+OFFSET+MM]; } Output[nn] = (short) ((sum + 0x8000) >> 16); } #else/* For comparison purposes only */int nn, offset =-KERNELSIZE/2; for (nn = 0; nn < width; nn++) {int sum = 0; int mm; for (mm = 0; mm < kernelsize; mm++) {sum + = kernel[mm]*input[nn+offset+mm]; } Output[nn] = (short) ((sum + 0x8000) >> 16); } #endif}
5. So file with NDK: Select Project-->properties-->builders-->new--> Select Program-->ok,name:hello_neon_builder, Location:d:\programfiles\android\android-sdk\android-ndk-r9\ndk-build.cmd,working directory:e:\neon\eclipse\ Hello-neon-->apply, select Refresh, tick refreshresources upon completion, tick specific resources, click Specify Resources ..., Tick the Libs folder under Hello-neon Project, finish-->apply, select Buildoptions, tick allocate Console (necessary for input), During Manualbuilds, During Auto builds, specify working set of relevant resources, click Specifyresoures ..., Tick the Hello-neon project under the Jni folder, Finish-->apply-->ok-->ok, will generate libhelloneon.so files under the Libs folder;
6, check Hello-neon,-->run as-->androidapplication, run the result is:
Firfilter Benchmark:
C version:282.84 ms
Neon version:135985 MS (x2.07994 faster)
The above is the procedure for. c files, if the. c file is a. cpp file, you need to change two files:
1. Change Android.mk to:
Local_path: = $ (call My-dir) include $ (clear_vars) local_module : = helloneon# Fill in the source file path to compile local_src_files: = Helloneon.cpp helloneon-intrinsics.cpp# Default contains the header file path local_c_includes: = $ (Local_path) $ (local_path)/. #-g a series of items later added to use the Arm_neon-h header file,-mfloat-abi=softfp-mfpu=neon use arm_neon.h must local_cflags: =-G-MFLOAT-ABI=SOFTFP- Mfpu=neon-march=armv7-a-mtune=cortex-a8local_ldlibs: =-lz-llogtarget_arch_abi: = armeabi-v7a LOCAL_ARM_MODE: = Armifeq ($ (Target_arch_abi), armeabi-v7a) #采用NEON优化技术 Local_arm_neon: = True #LOCAL_CFLAGS: =-dhave_neon= 1endiflocal_static_libraries: = cpufeatures# generate dynamic call library include $ (build_shared_library) $ (Calls Import-module,cpufeatures )
2. Helloneon.c instead of:
#include <jni.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #include < cpu-features.h> #include "helloneon-intrinsics.h" #define DEBUG 0#define have_neon#ifdef __cplusplusextern "C" {# endif#if debug#include <android/log.h># define D (x ...) __android_log_print (Android_log_info, "Helloneon", x) #else # define D (...) Do {} and (0) #endif/* Return current time in milliseconds */static Doublenow_ms (void) {struct TIMESPEC res; Clock_gettime (Clock_realtime, &res); return 1000.0*res.tv_sec + (double) Res.tv_nsec/1e6;} /* This is a FIR filter implemented-C */static voidfir_filter_c (short *output, const short* input, const short* kernel, int width, int kernelsize) {int offset =-KERNELSIZE/2; int nn; for (nn = 0; nn < width; nn++) {int sum = 0; int mm; for (mm = 0; mm < kernelsize; mm++) {sum + = kernel[mm]*input[nn+offset+mm]; } Output[nn] = (short) ((sum + 0x8000) >>16); }} #define Fir_kernel_size 32#define fir_output_size 2560#define fir_input_size (fir_output_size + FIR_KERNEL_SIZ E) #define Fir_iterations 600static const short Fir_kernel[fir_kernel_size] = {0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10, 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10};static short fir_output[fir_output_size];static short Fir_input_0[fir_i nput_size];static const short* Fir_input = fir_input_0 + (FIR_KERNEL_SIZE/2); static short Fir_output_expected[fir_o utput_size];/* This was a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * Apps/samples/hello-neon/project/src/com/example/neon/helloneon. Java */jniexport jstring jnicall java_com_hello_1neon_android_hello_1neonprojectactivity_stringfromjni (JNIEnv *env, Jobject thiz) {char str[512] = {0}; uint64_t features; Char buffer[512]; char Tryneon = 0; Double t0, T1, Time_c, Time_neon; /* Setup FIR Input-whatever */{int nn; for (nn = 0; nn < fir_input_size; nn++) {Fir_input_0[nn] = (5*NN) & 255; } fir_filter_c (fir_output_expected, Fir_input, Fir_kernel, Fir_output_size, fir_kernel_size); }/* Benchmark small FIR filter loop-c version */t0 = Now_ms (); {int count = fir_iterations; for (; count > 0; count--) {fir_filter_c (Fir_output, Fir_input, Fir_kernel, Fir_output_size, Fir_kernel_siz E); }} T1 = Now_ms (); Time_c = t1-t0; sprintf (str, "FIR Filter benchmark:\nc version:%g ms\n", Time_c); strlcpy (buffer, str, sizeof buffer); Strlcat (Buffer, "Neon version:", sizeof buffer); if (android_getcpufamily () = Android_cpu_family_arm) {strlcat (buffer, "not a ARM CPU!\n", sizeof buffer); Goto EXIT; } features =Android_getcpufeatures (); if ((features & android_cpu_arm_feature_armv7) = = 0) {strlcat (buffer, "not a ARMv7 CPU!\n", sizeof buffer); Goto EXIT; }/* Have_neon is defined in ANDROID.MK! */#ifdef Have_neon if ((features & android_cpu_arm_feature_neon) = = 0) {strlcat (buffer, "CPU doesn ' t suppor T NEON!\n ", sizeof buffer); Goto EXIT; }/* Benchmark small FIR filter Loop-neon version */t0 = Now_ms (); {int count = fir_iterations; for (; count > 0; count--) {fir_filter_neon_intrinsics (Fir_output, Fir_input, Fir_kernel, Fir_output_size, Fir_kernel_size); }} T1 = Now_ms (); Time_neon = t1-t0; sprintf (str, "%g MS (x%g faster) \ n", Time_neon, Time_c/(Time_neon < 1e-6?) 1.: Time_neon)); Strlcat (buffer, str, sizeof buffer); /* Check the result, just in case */{int nn, fails = 0; for (nn = 0; nn < fir_output_size; nn++) {if (fir_output[NN]! = Fir_output_expected[nn]) {if (++fails <) D ("neon[%d] =%d expected%d", n N, Fir_output[nn], fir_output_expected[nn]); }} D ("%d fails\n", fails); } #else/*! Have_neon */strlcat (buffer, "program is not compiled with ARMV7 support!\n", sizeof buffer); #endif/*! Have_neon */exit:return env->newstringutf (buffer);} #ifdef __cplusplus} #endif
Reference documents:
1, http://blog.csdn.net/fengbingchun/article/details/11580983
2, android-ndk-r9-windows-x86_64 in the hello-neon Example code