rand() and srand() are pseudo-random number generator functions
defined in ISO C. This implementation uses the Linear Congruential
Generator (LCG) algorithm with the following parameters, which are the
same as used in GNU Libc "TYPE_0" algorithm.
Modulus 2^31
Multiplier 1103515245
Increment 12345
Output Bits 30..0
Note that the default algorithm used by GNU Libc is not TYPE_0, and
TYPE_0 should be selected first by an initstate() call as shown below.
All global variables in a C library must be routed to a memory
partition in order to be used by user-mode applications when
CONFIG_USERSPACE is enabled. Thus, srand_seed is marked as
such. z_libc_partition is originally used by the Newlib C library but
it's generic enough to be used by either the minimal libc or the
newlib.
All other functions in the Minimal C library, however, don't require
global variables/states. Unconditionally using z_libc_partition with
the minimal libc might be a problem for applications utilizing many
custom memory partitions on platforms with a limited number of MPU
regions (eg. Cortex M0/M3). This commit introduces a kconfig option
CONFIG_MINIMAL_LIBC_RAND so that applications can enable the
functions if needed. The option is disabled by default.
Because this commit _does_ implement rand() and srand(), our coding
guideline check on GitHub Action finds it as a violation.
Error: lib/libc/minimal/include/stdlib.h:45:WARNING: Violation to
rule 21.2 (Should not used a reserved identifier) - srand
But this is false positive.
The following is a simple test program for LCG with GNU Libc.
#include <stdio.h>
#include <stdlib.h>
int main()
{
static char state[8];
/* Switch GLIBC to use LCG/TYPE_0 generator type. */
initstate(0, state, sizeof(state));
srand(1); /* Or any other value. */
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}
See initstate(3p) for more detail about how to use LCG in GLIBC.
Signed-off-by: Yasushi SHOJI <yashi@spacecubics.com>
181 lines
5.2 KiB
Plaintext
181 lines
5.2 KiB
Plaintext
# C library
|
|
|
|
# Copyright (c) 2016 Intel Corporation
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
menu "C Library"
|
|
|
|
config REQUIRES_FULL_LIBC
|
|
bool
|
|
help
|
|
Helper symbol to indicate some feature requires a C library implementation
|
|
with more functionality than what MINIMAL_LIBC provides
|
|
|
|
choice LIBC_IMPLEMENTATION
|
|
prompt "C Library Implementation"
|
|
default EXTERNAL_LIBC if NATIVE_APPLICATION
|
|
default NEWLIB_LIBC if REQUIRES_FULL_LIBC
|
|
default MINIMAL_LIBC
|
|
|
|
config MINIMAL_LIBC
|
|
bool "Minimal C library"
|
|
depends on !NATIVE_APPLICATION
|
|
depends on !REQUIRES_FULL_LIBC
|
|
help
|
|
Build with minimal C library.
|
|
|
|
config NEWLIB_LIBC
|
|
bool "Newlib C library"
|
|
depends on !NATIVE_APPLICATION
|
|
help
|
|
Build with newlib library. The newlib library is expected to be
|
|
part of the SDK in this case.
|
|
|
|
config EXTERNAL_LIBC
|
|
bool "External C library"
|
|
help
|
|
Build with external/user provided C library.
|
|
|
|
endchoice # LIBC_IMPLEMENTATION
|
|
|
|
config HAS_NEWLIB_LIBC_NANO
|
|
bool
|
|
|
|
if NEWLIB_LIBC
|
|
|
|
config NEWLIB_LIBC_NANO
|
|
bool "Build with newlib-nano C library"
|
|
depends on HAS_NEWLIB_LIBC_NANO
|
|
default y
|
|
help
|
|
Build with newlib-nano library, for small embedded apps.
|
|
The newlib-nano library for ARM embedded processors is a part of the
|
|
GNU Tools for ARM Embedded Processors.
|
|
|
|
config NEWLIB_LIBC_MAX_MAPPED_REGION_SIZE
|
|
int "Maximum memory mapped for newlib heap"
|
|
depends on MMU
|
|
default 1048576
|
|
help
|
|
On MMU-based systems, indicates the maximum amount of memory which
|
|
will be used for the newlib malloc() heap. The actual amount of
|
|
memory used will be the minimum of this value and the amount of
|
|
free physical memory at kernel boot.
|
|
|
|
config NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE
|
|
int "Newlib minimum required heap size"
|
|
default 2048 if NEWLIB_LIBC_NANO
|
|
default 8192 if !NEWLIB_LIBC_NANO
|
|
help
|
|
Specifies the amount of memory space that must be available for the
|
|
newlib heap. An assertion failure message will be displayed during
|
|
initialization if the memory space available for the newlib heap is
|
|
smaller than this value.
|
|
|
|
config NEWLIB_LIBC_ALIGNED_HEAP_SIZE
|
|
int "Newlib aligned heap size"
|
|
depends on MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT
|
|
depends on USERSPACE
|
|
default 0
|
|
help
|
|
If user mode is enabled, and MPU hardware has requirements that
|
|
regions be sized to a power of two and aligned to their size,
|
|
and user mode threads need to access this heap, then this is necessary
|
|
to properly define an MPU region for the heap.
|
|
|
|
If this is left at 0, then remaining system RAM will be used for this
|
|
area and it may not be possible to program it as an MPU region.
|
|
|
|
config NEWLIB_LIBC_FLOAT_PRINTF
|
|
bool "Build with newlib float printf"
|
|
help
|
|
Build with floating point printf enabled. This will increase the size of
|
|
the image.
|
|
|
|
config NEWLIB_LIBC_FLOAT_SCANF
|
|
bool "Build with newlib float scanf"
|
|
help
|
|
Build with floating point scanf enabled. This will increase the size of
|
|
the image.
|
|
|
|
endif # NEWLIB_LIBC
|
|
|
|
if MINIMAL_LIBC
|
|
|
|
config MINIMAL_LIBC_MALLOC
|
|
bool "Enable minimal libc malloc implementation"
|
|
default y
|
|
help
|
|
Enable the minimal libc's implementation of malloc, free, and realloc.
|
|
Disable if you wish to provide your own implementations of these functions.
|
|
|
|
config MINIMAL_LIBC_MALLOC_ARENA_SIZE
|
|
int "Size of the minimal libc malloc arena"
|
|
default 0
|
|
depends on MINIMAL_LIBC_MALLOC
|
|
help
|
|
Indicate the size in bytes of the memory arena used for
|
|
minimal libc's malloc() implementation.
|
|
|
|
config MINIMAL_LIBC_CALLOC
|
|
bool "Enable minimal libc trivial calloc implementation"
|
|
default y
|
|
help
|
|
Enable the minimal libc's trivial implementation of calloc, which
|
|
forwards to malloc and memset.
|
|
|
|
config MINIMAL_LIBC_REALLOCARRAY
|
|
bool "Enable minimal libc trivial reallocarray implementation"
|
|
default y
|
|
help
|
|
Enable the minimal libc's trivial implementation of reallocarray, which
|
|
forwards to realloc.
|
|
|
|
config MINIMAL_LIBC_LL_PRINTF
|
|
bool "Build with minimal libc long long printf" if !64BIT
|
|
default y if 64BIT
|
|
help
|
|
Build with long long printf enabled. This will increase the size of
|
|
the image.
|
|
|
|
config MINIMAL_LIBC_OPTIMIZE_STRING_FOR_SIZE
|
|
bool "Use size optimized string functions"
|
|
default y if SIZE_OPTIMIZATIONS
|
|
help
|
|
Enable smaller but potentially slower implementations of memcpy and
|
|
memset. On the Cortex-M0+ this reduces the total code size by 120 bytes.
|
|
|
|
config MINIMAL_LIBC_RAND
|
|
bool "Enables rand and srand functions"
|
|
select NEED_LIBC_MEM_PARTITION
|
|
help
|
|
Enable rand() and srand() for the minimal libc. The
|
|
functions implicitly access global/static data. Such data
|
|
must be put into a memory partition if CONFIG_USERSPACE=y,
|
|
and disabling this option may save an entry for application
|
|
defining many custom partitions.
|
|
|
|
Say 'Y' here if you need rand() and srand(). This might require
|
|
an additional memory partition.
|
|
|
|
endif # MINIMAL_LIBC
|
|
|
|
config STDOUT_CONSOLE
|
|
bool "Send stdout to console"
|
|
depends on CONSOLE_HAS_DRIVER
|
|
depends on !NATIVE_APPLICATION
|
|
default y
|
|
help
|
|
This option directs standard output (e.g. printf) to the console
|
|
device, rather than suppressing it entirely. See also EARLY_CONSOLE
|
|
option.
|
|
|
|
config NEED_LIBC_MEM_PARTITION
|
|
bool
|
|
help
|
|
Hidden option to signal that a memory partition is needed for
|
|
the C libraray even though it would not have been enabled
|
|
otherwise.
|
|
|
|
endmenu
|