zephyr/lib/libc/minimal/source/stdlib/rand.c
Stephanos Ioannidis 2bcb713371 libc: Define Z_LIBC_DATA macro globally
This commit globally defines the `Z_LIBC_DATA` macro, which is used to
place variables into the libc memory partition, so that it can be
re-used.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-06-01 11:03:38 +02:00

33 lines
605 B
C

/*
* Copyright (c) 2021 Space Cubics, LLC.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <zephyr/sys/libc-hooks.h>
#define OUTPUT_BITS (0x7fffffffU)
#define MULTIPLIER (1103515245U)
#define INCREMENT (12345U)
int rand_r(unsigned int *seed)
{
*seed = (MULTIPLIER * *seed + INCREMENT) & OUTPUT_BITS;
return *seed;
}
#ifdef CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS
static Z_LIBC_DATA unsigned int srand_seed = 1;
void srand(unsigned int s)
{
srand_seed = s;
}
int rand(void)
{
return rand_r(&srand_seed);
}
#endif /* CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS */