Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into mc
[simgrid.git] / src / bindings / java / jmsg_rngstream.c
1 /* Functions related to the RngStream Java port                         */
2
3 /* Copyright (c) 2007-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "jmsg_rngstream.h"
10 #include "jxbt_utilities.h"
11
12 jfieldID jrngstream_bind;
13
14 RngStream jrngstream_to_native(JNIEnv *env, jobject jrngstream) {
15   RngStream rngstream =
16     (RngStream)(intptr_t)(*env)->GetLongField(env, jrngstream, jrngstream_bind);
17   if (!rngstream) {
18     jxbt_throw_notbound(env, "rngstream", jrngstream);
19     return NULL;
20   }
21   return rngstream;
22 }
23
24 JNIEXPORT void JNICALL
25 Java_org_simgrid_msg_RngStream_nativeInit(JNIEnv *env, jclass cls) {
26   jclass class_RngStream = (*env)->FindClass(env, "org/simgrid/msg/RngStream");
27
28   jrngstream_bind = jxbt_get_jfield(env, class_RngStream, "bind", "J");
29 }
30
31 JNIEXPORT void JNICALL
32 Java_org_simgrid_msg_RngStream_create(JNIEnv *env, jobject jrngstream, jstring jname) {
33   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
34   RngStream rngstream = RngStream_CreateStream(name);
35   //Bind the RngStream object
36   (*env)->SetLongField(env, jrngstream, jrngstream_bind, (intptr_t)rngstream);
37
38   (*env)->ReleaseStringUTFChars(env, jname, name);
39 }
40 JNIEXPORT void JNICALL
41 Java_org_simgrid_msg_RngStream_nativeFinalize(JNIEnv *env, jobject jrngstream) {
42   RngStream rngstream = jrngstream_to_native(env, jrngstream);
43   RngStream_DeleteStream(&rngstream);
44   (*env)->SetLongField(env, jrngstream, jrngstream_bind, (intptr_t)NULL);
45 }
46 JNIEXPORT jboolean JNICALL
47 Java_org_simgrid_msg_RngStream_setPackageSeed(JNIEnv *env, jobject jrngstream, jintArray jseed) {
48   jint buffer[6];
49
50   (*env)->GetIntArrayRegion(env, jseed, 0, 6, buffer);
51
52   RngStream rngstream = jrngstream_to_native(env, jrngstream);
53   if (!rngstream)
54     return JNI_FALSE;
55
56   int result = RngStream_SetPackageSeed((unsigned long*)buffer);
57
58   return result == -1 ? JNI_FALSE : JNI_TRUE;
59 }
60 JNIEXPORT void JNICALL
61 Java_org_simgrid_msg_RngStream_resetStart(JNIEnv *env, jobject jrngstream) {
62   RngStream rngstream = jrngstream_to_native(env, jrngstream);
63   if (!rngstream)
64     return;
65
66   RngStream_ResetStartStream(rngstream);
67 }
68 JNIEXPORT void JNICALL
69 Java_org_simgrid_msg_RngStream_resetStartSubstream(JNIEnv *env, jobject jrngstream) {
70   RngStream rngstream = jrngstream_to_native(env, jrngstream);
71   if (!rngstream)
72     return;
73
74   RngStream_ResetStartSubstream(rngstream);
75 }
76 JNIEXPORT void JNICALL
77 Java_org_simgrid_msg_RngStream_resetNextSubstream(JNIEnv *env, jobject jrngstream) {
78   RngStream rngstream = jrngstream_to_native(env, jrngstream);
79   if (!rngstream)
80     return;
81
82   RngStream_ResetNextSubstream(rngstream);
83 }
84 JNIEXPORT void JNICALL
85 Java_org_simgrid_msg_RngStream_setAntithetic(JNIEnv *env, jobject jrngstream, jboolean ja) {
86   RngStream rngstream = jrngstream_to_native(env, jrngstream);
87   if (!rngstream)
88     return;
89
90   if (ja == JNI_TRUE) {
91     RngStream_SetAntithetic(rngstream,-1);
92   }
93   else {
94     RngStream_SetAntithetic(rngstream,0);
95   }
96 }
97 JNIEXPORT jboolean JNICALL
98 Java_org_simgrid_msg_RngStream_setSeed(JNIEnv *env, jobject jrngstream, jintArray jseed) {
99   jint buffer[6];
100
101   (*env)->GetIntArrayRegion(env, jseed, 0, 6, buffer);
102
103   RngStream rngstream = jrngstream_to_native(env, jrngstream);
104   if (!rngstream)
105     return JNI_FALSE;
106
107
108   int result = RngStream_SetSeed(rngstream, (unsigned long*)buffer);
109
110   return result == -1 ? JNI_FALSE : JNI_TRUE;
111 }
112 JNIEXPORT void JNICALL
113 Java_org_simgrid_msg_RngStream_advanceState(JNIEnv *env, jobject jrngstream, jint e, jint g) {
114   RngStream rngstream = jrngstream_to_native(env, jrngstream);
115   if (!rngstream)
116     return;
117
118   RngStream_AdvanceState(rngstream, (long)e, (long)g);
119 }
120 JNIEXPORT jdouble JNICALL
121 Java_org_simgrid_msg_RngStream_randU01(JNIEnv *env, jobject jrngstream) {
122   RngStream rngstream = jrngstream_to_native(env, jrngstream);
123   if (!rngstream)
124     return 0;
125
126   return (jdouble)RngStream_RandU01(rngstream);
127 }
128 JNIEXPORT jint JNICALL
129 Java_org_simgrid_msg_RngStream_randInt(JNIEnv *env, jobject jrngstream, jint i, jint j) {
130   RngStream rngstream = jrngstream_to_native(env, jrngstream);
131   if (!rngstream)
132     return 0;
133
134   return (jint)RngStream_RandInt(rngstream, (int)i, (int)j);
135 }