5 #include <cpu-features.h>
6 #include "helloneon-intrinsics.h"
11 #include <android/log.h>
12 # define D(x...) __android_log_print(ANDROID_LOG_INFO,"helloneon",x)
14 # define D(...) do {} while (0)
17 /* return current time in milliseconds */
22 clock_gettime(CLOCK_REALTIME, &res);
23 return 1000.0*res.tv_sec + (double)res.tv_nsec/1e6;
27 /* this is a FIR filter implemented in C */
29 fir_filter_c(short *output, const short* input, const short* kernel, int width, int kernelSize)
31 int offset = -kernelSize/2;
33 for (nn = 0; nn < width; nn++) {
36 for (mm = 0; mm < kernelSize; mm++) {
37 sum += kernel[mm]*input[nn+offset+mm];
39 output[nn] = (short)((sum + 0x8000) >> 16);
43 #define FIR_KERNEL_SIZE 32
44 #define FIR_OUTPUT_SIZE 2560
45 #define FIR_INPUT_SIZE (FIR_OUTPUT_SIZE + FIR_KERNEL_SIZE)
46 #define FIR_ITERATIONS 600
48 static const short fir_kernel[FIR_KERNEL_SIZE] = {
49 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10,
50 0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10 };
52 static short fir_output[FIR_OUTPUT_SIZE];
53 static short fir_input_0[FIR_INPUT_SIZE];
54 static const short* fir_input = fir_input_0 + (FIR_KERNEL_SIZE/2);
55 static short fir_output_expected[FIR_OUTPUT_SIZE];
57 /* This is a trivial JNI example where we use a native method
58 * to return a new VM String. See the corresponding Java source
61 * apps/samples/hello-neon/project/src/com/example/neon/HelloNeon.java
64 Java_com_example_neon_HelloNeon_stringFromJNI( JNIEnv* env,
71 double t0, t1, time_c, time_neon;
73 /* setup FIR input - whatever */
76 for (nn = 0; nn < FIR_INPUT_SIZE; nn++) {
77 fir_input_0[nn] = (5*nn) & 255;
79 fir_filter_c(fir_output_expected, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE);
82 /* Benchmark small FIR filter loop - C version */
85 int count = FIR_ITERATIONS;
86 for (; count > 0; count--) {
87 fir_filter_c(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE);
93 asprintf(&str, "FIR Filter benchmark:\nC version : %g ms\n", time_c);
94 strlcpy(buffer, str, sizeof buffer);
97 strlcat(buffer, "Neon version : ", sizeof buffer);
99 if (android_getCpuFamily() != ANDROID_CPU_FAMILY_ARM) {
100 strlcat(buffer, "Not an ARM CPU !\n", sizeof buffer);
104 features = android_getCpuFeatures();
105 if ((features & ANDROID_CPU_ARM_FEATURE_ARMv7) == 0) {
106 strlcat(buffer, "Not an ARMv7 CPU !\n", sizeof buffer);
110 /* HAVE_NEON is defined in Android.mk ! */
112 if ((features & ANDROID_CPU_ARM_FEATURE_NEON) == 0) {
113 strlcat(buffer, "CPU doesn't support NEON !\n", sizeof buffer);
117 /* Benchmark small FIR filter loop - Neon version */
120 int count = FIR_ITERATIONS;
121 for (; count > 0; count--) {
122 fir_filter_neon_intrinsics(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE);
127 asprintf(&str, "%g ms (x%g faster)\n", time_neon, time_c / (time_neon < 1e-6 ? 1. : time_neon));
128 strlcat(buffer, str, sizeof buffer);
131 /* check the result, just in case */
134 for (nn = 0; nn < FIR_OUTPUT_SIZE; nn++) {
135 if (fir_output[nn] != fir_output_expected[nn]) {
137 D("neon[%d] = %d expected %d", nn, fir_output[nn], fir_output_expected[nn]);
140 D("%d fails\n", fails);
142 #else /* !HAVE_NEON */
143 strlcat(buffer, "Program not compiled with ARMv7 support !\n", sizeof buffer);
144 #endif /* !HAVE_NEON */
146 return (*env)->NewStringUTF(env, buffer);