2021-12-21 15:18:25 +00:00
|
|
|
/*
|
|
|
|
bi_mersenne.c
|
|
|
|
|
|
|
|
Ruamoko Mersenne Twister api
|
|
|
|
|
|
|
|
Copyright (C) 2021 Bill Currie
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2021/12/21
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/mersenne.h"
|
|
|
|
#include "QF/progs.h"
|
|
|
|
|
|
|
|
#include "rua_internal.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
//FIXME each mtstate_t is 2500 bytes and the map has 1024 elements
|
|
|
|
//per row, so having only one mtstate_t has an overhead of about 2.5MB
|
|
|
|
PR_RESMAP (mtstate_t) state_map;
|
|
|
|
} mtwist_resources_t;
|
|
|
|
|
|
|
|
static mtstate_t *
|
|
|
|
state_new (mtwist_resources_t *res)
|
|
|
|
{
|
|
|
|
return PR_RESNEW (res->state_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
state_free (mtwist_resources_t *res, mtstate_t *state)
|
|
|
|
{
|
|
|
|
PR_RESFREE (res->state_map, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
state_reset (mtwist_resources_t *res)
|
|
|
|
{
|
|
|
|
PR_RESRESET (res->state_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline mtstate_t *
|
|
|
|
state_get (mtwist_resources_t *res, int index)
|
|
|
|
{
|
|
|
|
return PR_RESGET(res->state_map, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int __attribute__((pure))
|
|
|
|
state_index (mtwist_resources_t *res, mtstate_t *state)
|
|
|
|
{
|
|
|
|
return PR_RESINDEX(res->state_map, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_mtwist_new (progs_t *pr, void *_res)
|
2021-12-21 15:18:25 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
mtwist_resources_t *res = (mtwist_resources_t *) _res;
|
2021-12-21 15:18:25 +00:00
|
|
|
mtstate_t *mt = state_new (res);
|
|
|
|
mtwist_seed (mt, P_INT (pr, 0));
|
|
|
|
R_INT (pr) = state_index (res, mt);
|
|
|
|
}
|
|
|
|
|
2022-02-14 10:30:50 +00:00
|
|
|
static mtstate_t * __attribute__((pure))
|
2022-02-14 03:28:38 +00:00
|
|
|
get_state (progs_t *pr, mtwist_resources_t *res, const char *name, int index)
|
2021-12-21 15:18:25 +00:00
|
|
|
{
|
|
|
|
mtstate_t *mt = state_get (res, index);
|
|
|
|
|
|
|
|
if (!mt)
|
|
|
|
PR_RunError (pr, "invalid Mersenne Twister state index passed to %s",
|
|
|
|
name + 3);
|
|
|
|
return mt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_mtwist_delete (progs_t *pr, void *_res)
|
2021-12-21 15:18:25 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
mtwist_resources_t *res = _res;
|
|
|
|
mtstate_t *mt = get_state (pr, res, __FUNCTION__, P_INT (pr, 0));
|
2021-12-21 15:18:25 +00:00
|
|
|
state_free (res, mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_mtwist_seed (progs_t *pr, void *_res)
|
2021-12-21 15:18:25 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
mtwist_resources_t *res = _res;
|
|
|
|
mtstate_t *mt = get_state (pr, res, __FUNCTION__, P_INT (pr, 0));
|
2021-12-21 15:18:25 +00:00
|
|
|
mtwist_seed (mt, P_INT (pr, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_mtwist_rand (progs_t *pr, void *_res)
|
2021-12-21 15:18:25 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
mtwist_resources_t *res = _res;
|
|
|
|
mtstate_t *mt = get_state (pr, res, __FUNCTION__, P_INT (pr, 0));
|
2021-12-21 15:18:25 +00:00
|
|
|
R_INT (pr) = mtwist_rand (mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_mtwist_rand_0_1 (progs_t *pr, void *_res)
|
2021-12-21 15:18:25 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
mtwist_resources_t *res = _res;
|
|
|
|
mtstate_t *mt = get_state (pr, res, __FUNCTION__, P_INT (pr, 0));
|
2021-12-21 15:18:25 +00:00
|
|
|
R_FLOAT (pr) = mtwist_rand_0_1 (mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_mtwist_rand_m1_1 (progs_t *pr, void *_res)
|
2021-12-21 15:18:25 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
mtwist_resources_t *res = _res;
|
|
|
|
mtstate_t *mt = get_state (pr, res, __FUNCTION__, P_INT (pr, 0));
|
2021-12-21 15:18:25 +00:00
|
|
|
R_FLOAT (pr) = mtwist_rand_m1_1 (mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-14 03:28:38 +00:00
|
|
|
bi_mtwist_clear (progs_t *pr, void *_res)
|
2021-12-21 15:18:25 +00:00
|
|
|
{
|
2022-02-14 03:28:38 +00:00
|
|
|
mtwist_resources_t *res = (mtwist_resources_t *) _res;
|
2021-12-21 15:18:25 +00:00
|
|
|
state_reset (res);
|
|
|
|
}
|
|
|
|
|
2022-05-12 09:23:32 +00:00
|
|
|
static void
|
|
|
|
bi_mtwist_destroy (progs_t *pr, void *_res)
|
|
|
|
{
|
2023-03-03 10:36:28 +00:00
|
|
|
free (_res);
|
2022-05-12 09:23:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 12:54:03 +00:00
|
|
|
#define bi(x,np,params...) {#x, bi_##x, -1, np, {params}}
|
|
|
|
#define p(type) PR_PARAM(type)
|
2021-12-21 15:18:25 +00:00
|
|
|
static builtin_t builtins[] = {
|
2022-01-23 12:54:03 +00:00
|
|
|
bi(mtwist_new, 1, p(int)),
|
|
|
|
bi(mtwist_delete, 1, p(ptr)),
|
|
|
|
bi(mtwist_seed, 2, p(ptr), p(int)),
|
|
|
|
bi(mtwist_rand, 1, p(ptr)),
|
|
|
|
bi(mtwist_rand_0_1, 1, p(ptr)),
|
|
|
|
bi(mtwist_rand_m1_1, 1, p(ptr)),
|
2021-12-21 15:18:25 +00:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
RUA_Mersenne_Init (progs_t *pr, int secure)
|
|
|
|
{
|
|
|
|
mtwist_resources_t *res = calloc (1, sizeof (mtwist_resources_t));
|
|
|
|
|
2022-05-12 09:23:32 +00:00
|
|
|
PR_Resources_Register (pr, "Mersenne Twister", res, bi_mtwist_clear,
|
|
|
|
bi_mtwist_destroy);
|
2022-01-23 15:20:05 +00:00
|
|
|
PR_RegisterBuiltins (pr, builtins, res);
|
2021-12-21 15:18:25 +00:00
|
|
|
}
|