Make xorshift128 re-entrant so that we do do have to suffer the overhad of serialization controls.

This commit is contained in:
Gregory Nutt
2016-08-21 07:47:53 -06:00
parent 757023a909
commit 60b70f7dbb
4 changed files with 91 additions and 143 deletions
+31 -19
View File
@@ -46,6 +46,28 @@
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Default XorShift128 state initializer */
#define XORSHIFT128_INITIALIZER { 97, 101, 97 << 17, 101 << 25 }
/****************************************************************************
* Public Types
****************************************************************************/
/* Provides the state of the XorShift128 PRNG */
struct xorshift128_state_s
{
uint32_t x;
uint32_t y;
uint32_t z;
uint32_t w;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
@@ -58,37 +80,27 @@ extern "C"
#define EXTERN extern
#endif
/****************************************************************************
* Name: xorshift128_seed
*
* Description:
* Seed the XorShift128 PRNG
*
* Input Parameters:
* w, x, y, z: Values for XorShift128 generation state.
*
* Returned Value:
* No
*
****************************************************************************/
void xorshift128_seed(uint32_t w, uint32_t x, uint32_t y, uint32_t z);
/****************************************************************************
* Name: xorshift128
*
* Description:
* Generate one 32-bit pseudo-random number
* Generate one 32-bit pseudo-random number.
*
* NOTE: Because the PRNG state is passed as a parameter, this function is
* fully re-entrant and may be called from an interrupt handler.
*
* The downside to this is that users of the PRNG might not get as much
* entropy as if it were a common state structure.
*
* Input Parameters:
* None
* state - The current XorShift128 state.
*
* Returned Value:
* The generated pseudo-random number
*
****************************************************************************/
uint32_t xorshift128(void);
uint32_t xorshift128(FAR struct xorshift128_state_s *state);
#undef EXTERN
#ifdef __cplusplus