Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <jni.h>
- #include <stdio.h>
- #include "HelloJNI.h"
- // Implementation of native method sayHello() of HelloJNI class
- JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
- printf("Hello World!\n");
- fflush(stdout);
- return;
- }
- // Implementation of native method isPrime() of HelloJNI class
- JNIEXPORT jboolean JNICALL Java_HelloJNI_isPrime(JNIEnv *env, jobject thisObj, jint num) {
- if (num <= 1) return 0;
- if (num % 2 == 0 && num > 2) return 0;
- for(int i = 3; i < num / 2; i+= 2)
- {
- if (num % i == 0)
- return 0;
- }
- return 1;
- }
- // Implementation of native method forEachElement() of HelloJNI class
- JNIEXPORT void JNICALL Java_HelloJNI_forEachElement(JNIEnv *env, jobject thisObj, jfloatArray array, jfloat val, jstring op) {
- jfloat* flt = (*env)->GetFloatArrayElements(env, array, NULL);
- jint i = 0;
- const char *str = (*env)->GetStringUTFChars(env, op, 0);
- for(i = 0; i < 5; i++) {
- if(!strncmp(str, "add")) {
- flt[i] += val;
- }
- else if(!strncmp(str, "subtract")) {
- flt[i] -= val;
- }
- else if(!strncmp(str, "multiply")) {
- flt[i] *= val;
- }
- else if(!strncmp(str, "divide")) {
- flt[i] /= val;
- }
- }
- (*env)->ReleaseFloatArrayElements(env, array, flt, 0);
- (*env)->ReleaseStringUTFChars(env, op, str);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement