Home | History | Annotate | Download | only in jni
      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 /* return current time in milliseconds */
     34 static double
     35 now_ms(void)
     36 {
     37     struct timespec res;
     38     clock_gettime(CLOCK_REALTIME, &res);
     39     return 1000.0*res.tv_sec + (double)res.tv_nsec/1e6;
     40 }
     41 
     42 
     43 /* this is a FIR filter implemented in C */
     44 static void
     45 fir_filter_c(short *output, const short* input, const short* kernel, int width, int kernelSize)
     46 {
     47     int  offset = -kernelSize/2;
     48     int  nn;
     49     for (nn = 0; nn < width; nn++) {
     50         int sum = 0;
     51         int mm;
     52         for (mm = 0; mm < kernelSize; mm++) {
     53             sum += kernel[mm]*input[nn+offset+mm];
     54         }
     55         output[nn] = (short)((sum + 0x8000) >> 16);
     56     }
     57 }
     58 
     59 #define  FIR_KERNEL_SIZE   32
     60 #define  FIR_OUTPUT_SIZE   2560
     61 #define  FIR_INPUT_SIZE    (FIR_OUTPUT_SIZE + FIR_KERNEL_SIZE)
     62 #define  FIR_ITERATIONS    600
     63 
     64 static const short  fir_kernel[FIR_KERNEL_SIZE] = {
     65     0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10,
     66     0x10, 0x20, 0x40, 0x70, 0x8c, 0xa2, 0xce, 0xf0, 0xe9, 0xce, 0xa2, 0x8c, 070, 0x40, 0x20, 0x10 };
     67 
     68 static short        fir_output[FIR_OUTPUT_SIZE];
     69 static short        fir_input_0[FIR_INPUT_SIZE];
     70 static const short* fir_input = fir_input_0 + (FIR_KERNEL_SIZE/2);
     71 static short        fir_output_expected[FIR_OUTPUT_SIZE];
     72 
     73 /* This is a trivial JNI example where we use a native method
     74  * to return a new VM String. See the corresponding Java source
     75  * file located at:
     76  *
     77  *   apps/samples/hello-neon/project/src/com/example/neon/HelloNeon.java
     78  */
     79 jstring
     80 Java_com_example_neon_HelloNeon_stringFromJNI( JNIEnv* env,
     81                                                jobject thiz )
     82 {
     83     char*  str;
     84     uint64_t features;
     85     char buffer[512];
     86     char tryNeon = 0;
     87     double  t0, t1, time_c, time_neon;
     88 
     89     /* setup FIR input - whatever */
     90     {
     91         int  nn;
     92         for (nn = 0; nn < FIR_INPUT_SIZE; nn++) {
     93             fir_input_0[nn] = (5*nn) & 255;
     94         }
     95         fir_filter_c(fir_output_expected, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE);
     96     }
     97 
     98     /* Benchmark small FIR filter loop - C version */
     99     t0 = now_ms();
    100     {
    101         int  count = FIR_ITERATIONS;
    102         for (; count > 0; count--) {
    103             fir_filter_c(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE);
    104         }
    105     }
    106     t1 = now_ms();
    107     time_c = t1 - t0;
    108 
    109     asprintf(&str, "FIR Filter benchmark:\nC version          : %g ms\n", time_c);
    110     strlcpy(buffer, str, sizeof buffer);
    111     free(str);
    112 
    113     strlcat(buffer, "Neon version   : ", sizeof buffer);
    114 
    115     if (android_getCpuFamily() != ANDROID_CPU_FAMILY_ARM) {
    116         strlcat(buffer, "Not an ARM CPU !\n", sizeof buffer);
    117         goto EXIT;
    118     }
    119 
    120     features = android_getCpuFeatures();
    121     if ((features & ANDROID_CPU_ARM_FEATURE_ARMv7) == 0) {
    122         strlcat(buffer, "Not an ARMv7 CPU !\n", sizeof buffer);
    123         goto EXIT;
    124     }
    125 
    126     /* HAVE_NEON is defined in Android.mk ! */
    127 #ifdef HAVE_NEON
    128     if ((features & ANDROID_CPU_ARM_FEATURE_NEON) == 0) {
    129         strlcat(buffer, "CPU doesn't support NEON !\n", sizeof buffer);
    130         goto EXIT;
    131     }
    132 
    133     /* Benchmark small FIR filter loop - Neon version */
    134     t0 = now_ms();
    135     {
    136         int  count = FIR_ITERATIONS;
    137         for (; count > 0; count--) {
    138             fir_filter_neon_intrinsics(fir_output, fir_input, fir_kernel, FIR_OUTPUT_SIZE, FIR_KERNEL_SIZE);
    139         }
    140     }
    141     t1 = now_ms();
    142     time_neon = t1 - t0;
    143     asprintf(&str, "%g ms (x%g faster)\n", time_neon, time_c / (time_neon < 1e-6 ? 1. : time_neon));
    144     strlcat(buffer, str, sizeof buffer);
    145     free(str);
    146 
    147     /* check the result, just in case */
    148     {
    149         int  nn, fails = 0;
    150         for (nn = 0; nn < FIR_OUTPUT_SIZE; nn++) {
    151             if (fir_output[nn] != fir_output_expected[nn]) {
    152                 if (++fails < 16)
    153                     D("neon[%d] = %d expected %d", nn, fir_output[nn], fir_output_expected[nn]);
    154             }
    155         }
    156         D("%d fails\n", fails);
    157     }
    158 #else /* !HAVE_NEON */
    159     strlcat(buffer, "Program not compiled with ARMv7 support !\n", sizeof buffer);
    160 #endif /* !HAVE_NEON */
    161 EXIT:
    162     return (*env)->NewStringUTF(env, buffer);
    163 }
    164