parent
625a6af459
commit
009a2eef6e
@ -0,0 +1,14 @@
|
||||
#include <stdlib.h>
|
||||
#include "tinymt32.h"
|
||||
|
||||
static tinymt32_t random;
|
||||
|
||||
void srand(unsigned int seed)
|
||||
{
|
||||
tinymt32_init(&random, seed);
|
||||
}
|
||||
|
||||
int rand(void)
|
||||
{
|
||||
return tinymt32_generate_uint32(&random) & 0x7fffffff;
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
/**
|
||||
* @file tinymt32.c
|
||||
*
|
||||
* @brief Tiny Mersenne Twister only 127 bit internal state
|
||||
*
|
||||
* @author Mutsuo Saito (Hiroshima University)
|
||||
* @author Makoto Matsumoto (The University of Tokyo)
|
||||
*
|
||||
* Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
|
||||
* Hiroshima University and The University of Tokyo.
|
||||
* All rights reserved.
|
||||
*
|
||||
* The 3-clause BSD License is applied to this software, see
|
||||
* LICENSE.txt
|
||||
*/
|
||||
|
||||
/* Note: this is a stripped-down version of TinyMT that only includes the
|
||||
32-bit integer generator. For the full version, please see
|
||||
<https://github.com/MersenneTwister-Lab/TinyMT>. */
|
||||
|
||||
#include "tinymt32.h"
|
||||
|
||||
#define TINYMT32_SH0 1
|
||||
#define TINYMT32_SH1 10
|
||||
#define TINYMT32_SH8 8
|
||||
#define TINYMT32_MASK 0x7fffffff
|
||||
|
||||
/**
|
||||
* This function changes internal state of tinymt32.
|
||||
* Users should not call this function directly.
|
||||
* @param random tinymt internal status
|
||||
*/
|
||||
static void tinymt32_next_state(tinymt32_t * random) {
|
||||
uint32_t x;
|
||||
uint32_t y;
|
||||
|
||||
y = random->status[3];
|
||||
x = (random->status[0] & TINYMT32_MASK)
|
||||
^ random->status[1]
|
||||
^ random->status[2];
|
||||
x ^= (x << TINYMT32_SH0);
|
||||
y ^= (y >> TINYMT32_SH0) ^ x;
|
||||
random->status[0] = random->status[1];
|
||||
random->status[1] = random->status[2];
|
||||
random->status[2] = x ^ (y << TINYMT32_SH1);
|
||||
random->status[3] = y;
|
||||
random->status[1] ^= -((int32_t)(y & 1)) & random->mat1;
|
||||
random->status[2] ^= -((int32_t)(y & 1)) & random->mat2;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function outputs 32-bit unsigned integer from internal state.
|
||||
* Users should not call this function directly.
|
||||
* @param random tinymt internal status
|
||||
* @return 32-bit unsigned pseudorandom number
|
||||
*/
|
||||
static uint32_t tinymt32_temper(tinymt32_t * random) {
|
||||
uint32_t t0, t1;
|
||||
t0 = random->status[3];
|
||||
#if defined(LINEARITY_CHECK)
|
||||
t1 = random->status[0]
|
||||
^ (random->status[2] >> TINYMT32_SH8);
|
||||
#else
|
||||
t1 = random->status[0]
|
||||
+ (random->status[2] >> TINYMT32_SH8);
|
||||
#endif
|
||||
t0 ^= t1;
|
||||
t0 ^= -((int32_t)(t1 & 1)) & random->tmat;
|
||||
return t0;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function outputs 32-bit unsigned integer from internal state.
|
||||
* @param random tinymt internal status
|
||||
* @return 32-bit unsigned integer r (0 <= r < 2^32)
|
||||
*/
|
||||
uint32_t tinymt32_generate_uint32(tinymt32_t * random) {
|
||||
tinymt32_next_state(random);
|
||||
return tinymt32_temper(random);
|
||||
}
|
||||
|
||||
#define MIN_LOOP 8
|
||||
#define PRE_LOOP 8
|
||||
|
||||
/**
|
||||
* This function certificate the period of 2^127-1.
|
||||
* @param random tinymt state vector.
|
||||
*/
|
||||
static void period_certification(tinymt32_t * random) {
|
||||
if ((random->status[0] & TINYMT32_MASK) == 0 &&
|
||||
random->status[1] == 0 &&
|
||||
random->status[2] == 0 &&
|
||||
random->status[3] == 0) {
|
||||
random->status[0] = 'T';
|
||||
random->status[1] = 'I';
|
||||
random->status[2] = 'N';
|
||||
random->status[3] = 'Y';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function initializes the internal state array with a 32-bit
|
||||
* unsigned integer seed.
|
||||
* @param random tinymt state vector.
|
||||
* @param seed a 32-bit unsigned integer used as a seed.
|
||||
*/
|
||||
void tinymt32_init(tinymt32_t * random, uint32_t seed) {
|
||||
random->status[0] = seed;
|
||||
random->status[1] = random->mat1;
|
||||
random->status[2] = random->mat2;
|
||||
random->status[3] = random->tmat;
|
||||
for (int i = 1; i < MIN_LOOP; i++) {
|
||||
random->status[i & 3] ^= i + UINT32_C(1812433253)
|
||||
* (random->status[(i - 1) & 3]
|
||||
^ (random->status[(i - 1) & 3] >> 30));
|
||||
}
|
||||
period_certification(random);
|
||||
for (int i = 0; i < PRE_LOOP; i++) {
|
||||
tinymt32_next_state(random);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
#ifndef TINYMT32_H
|
||||
#define TINYMT32_H
|
||||
/**
|
||||
* @file tinymt32.h
|
||||
*
|
||||
* @brief Tiny Mersenne Twister only 127 bit internal state
|
||||
*
|
||||
* @author Mutsuo Saito (Hiroshima University)
|
||||
* @author Makoto Matsumoto (University of Tokyo)
|
||||
*
|
||||
* Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
|
||||
* Hiroshima University and The University of Tokyo.
|
||||
* All rights reserved.
|
||||
*
|
||||
* The 3-clause BSD License is applied to this software, see
|
||||
* LICENSE.txt
|
||||
*/
|
||||
|
||||
/* Note: this is a stripped-down version of TinyMT that only includes the
|
||||
32-bit integer generator. For the full version, please see
|
||||
<https://github.com/MersenneTwister-Lab/TinyMT>. */
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* tinymt32 internal state vector and parameters
|
||||
*/
|
||||
struct TINYMT32_T {
|
||||
uint32_t status[4];
|
||||
uint32_t mat1;
|
||||
uint32_t mat2;
|
||||
uint32_t tmat;
|
||||
};
|
||||
|
||||
typedef struct TINYMT32_T tinymt32_t;
|
||||
|
||||
uint32_t tinymt32_generate_uint32(tinymt32_t *random);
|
||||
void tinymt32_init(tinymt32_t *random, uint32_t seed);
|
||||
|
||||
#endif
|
@ -1,29 +1,14 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/*
|
||||
** The calloc() function allocates memory for an array of nmemb elements of size
|
||||
** bytes each and returns a pointer to the allocated memory. The memory is set
|
||||
** to zero. If nmemb or size is 0, then calloc() returns either NULL, or a
|
||||
** unique pointer value that can later be successfully passed to free(). If the
|
||||
** multiplication of nmemb and size would result in integer overflow, then
|
||||
** calloc() returns an error. By contrast, an integer overflow would not be
|
||||
** detected in the following call to malloc(), with the result that an incorrectly
|
||||
** sized block of memory would be allocated: `malloc(nmemb * size);`
|
||||
*/
|
||||
void *calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
// check error
|
||||
if (size == 0 || nmemb == 0)
|
||||
return (NULL);
|
||||
unsigned int total_size;
|
||||
|
||||
// Try to allowate the area
|
||||
void *ret = malloc(nmemb * size);
|
||||
if (ret == NULL)
|
||||
return (NULL);
|
||||
if(__builtin_umul_overflow(nmemb, size, &total_size))
|
||||
return NULL;
|
||||
|
||||
// wipe the area
|
||||
memset(ret, 0x00, size);
|
||||
return (ret);
|
||||
void *mem = malloc(total_size);
|
||||
if(mem) memset(mem, 0, size);
|
||||
return mem;
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
extern void kfree(void *ptr);
|
||||
|
||||
void free(void *ptr)
|
||||
{
|
||||
return kfree(ptr);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
extern void *kmalloc(size_t size, char const *arena_name);
|
||||
|
||||
void *malloc(size_t size)
|
||||
{
|
||||
return kmalloc(size, NULL);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
extern void *krealloc(void *ptr, size_t size);
|
||||
|
||||
void *realloc(void *ptr, size_t size)
|
||||
{
|
||||
return krealloc(ptr, size);
|
||||
}
|
Loading…
Reference in new issue