forked from Telodendria/Telodendria
Jordan Bancino
d933d12e1b
This implementation is loosely inspired by the original paper on the Mersenne Twister, and borrows code from a public-domain implementation of it, adapting it to fit the style of Telodendria's code, and fixing a few bugs regarding the size of the data type used. Neither C nor POSIX provide a good, thread-safe pseudorandom number generator. The OpenBSD linker started complaining about the use of rand_r(), and no standard alternative presented itself as worthy of consideration, so I finally decided it was time to roll my own PRNG.
73 lines
1.5 KiB
C
73 lines
1.5 KiB
C
#ifndef TELODENDRIA_INT_H
|
|
#define TELODENDRIA_INT_H
|
|
|
|
#include <limits.h>
|
|
|
|
#define BIT64_MAX 18446744073709551615
|
|
#define BIT32_MAX 4294967295
|
|
#define BIT16_MAX 65535
|
|
#define BIT8_MAX 255
|
|
|
|
#ifndef UCHAR_MAX
|
|
#error Size of char data type is unknown. Define UCHAR_MAX.
|
|
#endif
|
|
|
|
#ifndef USHRT_MAX
|
|
#error Size of short data type is unknown. Define USHRT_MAX.
|
|
#endif
|
|
|
|
#ifndef UINT_MAX
|
|
#error Size of int data type is unknown. Define UINT_MAX.
|
|
#endif
|
|
|
|
#ifndef ULONG_MAX
|
|
#error Size of long data type is unknown. Define ULONG_MAX.
|
|
#endif
|
|
|
|
#if UCHAR_MAX == BIT8_MAX
|
|
typedef signed char Int8;
|
|
typedef unsigned char UInt8;
|
|
|
|
#else
|
|
#error Unable to determine suitable data type for 8-bit integers.
|
|
#endif
|
|
|
|
#if UINT_MAX == BIT16_MAX
|
|
typedef signed int Int16;
|
|
typedef unsigned int UInt16;
|
|
|
|
#elif USHRT_MAX == BIT16_MAX
|
|
typedef signed short Int16;
|
|
typedef unsigned short UInt16;
|
|
|
|
#elif UCHAR_MAX == BIT16_MAX
|
|
typedef signed char Int16;
|
|
typedef unsigned char UInt16;
|
|
|
|
#else
|
|
#error Unable to determine suitable data type for 16-bit integers.
|
|
#endif
|
|
|
|
#if ULONG_MAX == BIT32_MAX
|
|
typedef signed long Int32;
|
|
typedef unsigned long UInt32;
|
|
|
|
#elif UINT_MAX == BIT32_MAX
|
|
typedef signed int Int32;
|
|
typedef unsigned int UInt32;
|
|
|
|
#elif USHRT_MAX == BIT32_MAX
|
|
typedef signed short Int32;
|
|
typedef unsigned short UInt32;
|
|
|
|
#elif UCHAR_MAX == BIT32_MAX
|
|
typedef signed char Int32;
|
|
typedef unsigned char UInt32;
|
|
|
|
#else
|
|
#error Unable to determine suitable data type for 32-bit integers.
|
|
#endif
|
|
|
|
/* The ANSI C standard only guarantees a data size of up to 32 bits. */
|
|
|
|
#endif
|