Advertisement
Guest User

Untitled

a guest
May 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. #include <jni.h>
  2. #include <stdio.h>
  3. #include "HelloJNI.h"
  4.  
  5. // Implementation of native method sayHello() of HelloJNI class
  6. JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
  7.    printf("Hello World!\n");
  8.    fflush(stdout);
  9.    return;
  10. }
  11.  
  12. // Implementation of native method isPrime() of HelloJNI class
  13. JNIEXPORT jboolean JNICALL Java_HelloJNI_isPrime(JNIEnv *env, jobject thisObj, jint num) {
  14.     if (num <= 1) return 0;
  15.     if (num % 2 == 0 && num > 2) return 0;
  16.     for(int i = 3; i < num / 2; i+= 2)
  17.     {
  18.         if (num % i == 0)
  19.             return 0;
  20.     }
  21.     return 1;
  22. }
  23.  
  24. // Implementation of native method forEachElement() of HelloJNI class
  25. JNIEXPORT void JNICALL Java_HelloJNI_forEachElement(JNIEnv *env, jobject thisObj, jfloatArray array, jfloat val, jstring op) {
  26.     jfloat* flt = (*env)->GetFloatArrayElements(env, array, NULL);
  27.     jint i = 0;
  28.     const char *str = (*env)->GetStringUTFChars(env, op, 0);
  29.     for(i = 0; i < 5; i++) {
  30.         if(!strncmp(str, "add")) {
  31.             flt[i] += val;
  32.         }
  33.         else if(!strncmp(str, "subtract")) {
  34.             flt[i] -= val;
  35.         }
  36.         else if(!strncmp(str, "multiply")) {
  37.             flt[i] *= val;
  38.         }
  39.         else if(!strncmp(str, "divide")) {
  40.             flt[i] /= val;
  41.         }
  42.     }
  43.     (*env)->ReleaseFloatArrayElements(env, array, flt, 0);
  44.     (*env)->ReleaseStringUTFChars(env, op, str);
  45.     return;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement