ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_rand_stub.c
Go to the documentation of this file.
1
31
32#include <stdint.h>
33#include <stdlib.h>
34
36typedef enum : uint32_t {
41
43typedef enum : uint32_t {
44 k_ra8_rand_default_seed = 0x9E3779B9UL,
46
47static uint32_t s_state = (uint32_t)k_ra8_rand_default_seed;
48
68void srand(unsigned int seed)
69{
70 /* Avoid the all-zero state which would lock xorshift. */
71 s_state = (seed == 0U) ? (uint32_t)k_ra8_rand_default_seed : (uint32_t)seed;
72}
73
94int rand(void)
95{
96 /* Marsaglia xorshift32 with constants 13/17/5. */
97 uint32_t x = s_state;
98 const uint32_t k_mask = (uint32_t)RAND_MAX;
99 x ^= x << k_xorshift_a;
100 x ^= x >> k_xorshift_b;
101 x ^= x << k_xorshift_c;
102 s_state = x;
103 return (int)(x & k_mask);
104}
void srand(unsigned int seed)
Strong override for newlib srand() – xorshift32 seed.
static uint32_t s_state
xorshift32_param_t
Marsaglia xorshift32 shift constants.
@ k_xorshift_b
x ^= x >> 17.
@ k_xorshift_c
x ^= x << 5.
@ k_xorshift_a
x ^= x << 13.
int rand(void)
Strong override for newlib rand() – xorshift32 step.
ra8_rand_const_t
Xorshift32 default seed: arbitrary non-zero constant per Marsaglia 2003.
@ k_ra8_rand_default_seed
RA8 rand default seed.