This commit is contained in:
2025-11-29 16:43:03 -06:00
parent efe93b7206
commit 9f696a0342
7 changed files with 400 additions and 220 deletions

View File

@@ -34,8 +34,6 @@ typedef struct letter {
};
} letter;
#define STATE_VECTOR_LENGTH 312
#define STATE_VECTOR_M 156
#define ACTOR_IDLE 0 // Actor not doing anything
#define ACTOR_READY 1 // Actor ready for a turn
@@ -46,11 +44,6 @@ typedef struct letter {
extern int tracy_profiling_enabled;
typedef struct tagMTRand {
uint64_t mt[STATE_VECTOR_LENGTH];
int32_t index;
} MTRand;
typedef struct cell_rt {
JSContext *context;
#ifdef HAVE_MIMALLOC
@@ -67,7 +60,6 @@ mi_heap_t *heap;
SDL_Mutex *msg_mutex; /* For message queue and timers queue */
char *id;
MTRand mrand;
int idx_count;
@@ -116,6 +108,8 @@ JSValue bool2js(JSContext *js, int b);
double js2number(JSContext *js, JSValue v);
JSValue number2js(JSContext *js, double g);
double cell_random();
#ifdef __cplusplus
}
#endif

View File

@@ -20,67 +20,6 @@
#include <dlfcn.h>
#endif
// Random number generation constants for MT19937-64
#define NN STATE_VECTOR_LENGTH
#define MM STATE_VECTOR_M
#define MATRIX_A 0xB5026F5AA96619E9ULL
#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
#define LM 0x7FFFFFFFULL /* Least significant 31 bits */
// Random number generation functions
void m_seedRand(MTRand* rand, uint64_t seed) {
rand->mt[0] = seed;
for(rand->index = 1; rand->index < NN; rand->index++) {
rand->mt[rand->index] = (6364136223846793005ULL * (rand->mt[rand->index-1] ^ (rand->mt[rand->index-1] >> 62)) + rand->index);
}
}
int64_t genRandLong(MTRand* rand) {
int i;
uint64_t x;
static uint64_t mag01[2] = {0ULL, MATRIX_A};
if (rand->index >= NN) { /* generate NN words at one time */
/* if init_genrand64() has not been called, */
/* a default initial seed is used */
if (rand->index == NN+1)
m_seedRand(rand, 5489ULL);
for (i = 0; i < NN-MM; i++) {
x = (rand->mt[i] & UM) | (rand->mt[i+1] & LM);
rand->mt[i] = rand->mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
}
for (; i < NN-1; i++) {
x = (rand->mt[i] & UM) | (rand->mt[i+1] & LM);
rand->mt[i] = rand->mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
}
x = (rand->mt[NN-1] & UM) | (rand->mt[0] & LM);
rand->mt[NN-1] = rand->mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
rand->index = 0;
}
x = rand->mt[rand->index++];
x ^= (x >> 29) & 0x5555555555555555ULL;
x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
x ^= (x << 37) & 0xFFF7EEE000000000ULL;
x ^= (x >> 43);
return (int64_t)(x & 0x000FFFFFFFFFFFFFULL); /* return 52-bit value safe for JS */
}
double genRand(MTRand* rand) {
/* generates a random number on [0,1)-real-interval */
return (genRandLong(rand) >> 11) * (1.0/9007199254740992.0);
}
double rand_range(JSContext *js, double min, double max)
{
MTRand *mrand = &((cell_rt*)JS_GetContextOpaque(js))->mrand;
return genRand(mrand) * (max-min)+min;
}
#define JS_GETNUM(JS,VAL,I,TO,TYPE) { \
JSValue val = JS_GetPropertyUint32(JS,VAL,I); \
TO = js2##TYPE(JS, val); \
@@ -157,33 +96,11 @@ static uint32_t xorshift32(){
return rng_state = x;
}
JSC_CCALL(os_rand,
MTRand *mrand = &((cell_rt*)JS_GetContextOpaque(js))->mrand;
return number2js(js, genRand(mrand));
)
JSC_CCALL(os_randi,
MTRand *mrand = &((cell_rt*)JS_GetContextOpaque(js))->mrand;
return JS_NewInt64(js, genRandLong(mrand));
)
JSC_CCALL(os_srand,
MTRand *mrand = &((cell_rt*)JS_GetContextOpaque(js))->mrand;
m_seedRand(mrand, js2number(js,argv[0]));
)
void ffi_load(JSContext *js)
{
cell_rt *rt = JS_GetContextOpaque(js);
JS_FreeValue(js, js_blob_use(js));
uint64_t rr;
// randombytes is defined in qjs_crypto.c which is linked, but header was removed.
// We declare it here to avoid implicit declaration error.
extern int randombytes(void *buf, size_t n);
randombytes(&rr,4);
m_seedRand(&rt->mrand, rr);
JSValue globalThis = JS_GetGlobalObject(js);
JSValue prosp = JS_NewObject(js);

View File

@@ -538,6 +538,16 @@ void *js_get_blob_data(JSContext *js, size_t *size, JSValue v)
return b->data;
}
void *js_get_blob_data_bits(JSContext *js, size_t *bits, JSValue v)
{
blob *b = js2blob(js, v);
if (!b || !b->is_stone)
return NULL;
*bits = b->length;
return b->data;
}
int js_is_blob(JSContext *js, JSValue v)
{
blob *b = js2blob(js,v);