zephyr/include
Yasushi SHOJI b2fde24c4c libc: minimal: Add rand() and srand()
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>
2021-07-20 13:32:36 -04:00
..
app_memory arm/arm64: Make ARM64 a standalone architecture 2021-03-31 10:34:33 -05:00
arch arch: arm64: Unify the initialization of MMU and MPU 2021-07-13 09:30:29 -04:00
audio
bluetooth bluetooth: uuid: add Running Speed and Cadence uuids 2021-07-20 13:44:45 +02:00
canbus modules: canopennode: move glue code to modules directory 2021-07-01 13:32:18 -04:00
console
crypto
data
debug kernel: remove object tracing 2021-05-07 22:10:21 -04:00
devicetree reserved-memory: Introduce support for reserved-memory node 2021-07-15 18:12:51 -05:00
dfu dfu: flash_img: Removing conditional compilation from header 2021-06-29 08:38:36 -04:00
disk include: disk_access: Add a compat warning 2021-03-26 08:59:47 -04:00
display
drivers drivers: sensor: add const qualifier to conversion 2021-07-19 18:16:52 +03:00
dt-bindings esp32: drivers: interrupt_controller: add interrupt allocation support 2021-07-16 07:19:28 -04:00
fs doc: doxygen: replace option alias with kconfig 2021-06-29 10:26:28 -04:00
ipc ipc: Added IPC Service to support different transport backends 2021-07-16 21:43:39 -04:00
kernel kernel: mempool_heap: fix header guard 2021-06-16 20:38:55 -04:00
linker reserved-memory: Introduce support for reserved-memory node 2021-07-15 18:12:51 -05:00
logging logging: Eclipse CDT parser workaround 2021-06-02 06:41:35 -05:00
lorawan lorawan: port oriented downlink callbacks 2021-04-30 13:05:10 -05:00
mgmt subsys/mgmt/mcumgr: Add SMP Zephyr specific storage erase command 2021-07-13 11:56:39 +02:00
modbus
net net: pkt: remove ieee802154_frame_retry flag 2021-07-19 18:15:56 +03:00
pm pm: remove callback from control function 2021-07-13 09:36:45 -04:00
portability portability: move cmsis api headers 2021-04-20 08:45:05 -04:00
posix sys: eventfd: Add locking to eventfd_read/write functions 2021-04-15 07:16:51 -05:00
power pm: deprecate power/power.h header 2021-05-05 18:35:49 -04:00
random
settings
shell shell: Add option to bypass shell 2021-07-02 13:35:18 -04:00
stats
storage storage/stream: Add persistent write progress to stream_flash 2021-04-23 15:01:32 -05:00
sys libc: minimal: Add rand() and srand() 2021-07-20 13:32:36 -04:00
task_wdt
timing timing: guard the header with CONFIG_TIMING_FUNCTIONS 2021-04-06 16:43:55 -04:00
toolchain toolchain: Add macro for warning for unused return values 2021-06-16 10:42:17 +02:00
tracing Tracing: TRACING_NONE Compilation Fix 2021-05-27 14:39:40 -05:00
usb usb: hid: remove get_protocol/set_protocol from USB HID class API 2021-05-07 13:14:47 +02:00
zephyr
cache.h cache: Do not warn on unused parameters 2021-05-25 07:05:22 -05:00
device.h device: add DEVICE_DT_GET_ONE macro 2021-07-15 11:47:11 -04:00
devicetree.h dts: Introduce DT_STRING_TOKEN and DT_STRING_UPPER_TOKEN 2021-07-15 18:12:51 -05:00
ec_host_cmd.h include: Move ec_host_cmd.h to mgmt/ec_host_cmd.h 2021-04-22 12:51:16 -04:00
emul.h include: Move emul.h to drivers/emul.h 2021-04-22 12:51:01 -04:00
exc_handle.h
fatal.h
generated_dts_board.h
init.h init: tweak Zephyr .init* section name conflicting with toolchain 2021-04-26 14:15:43 -04:00
irq_nextlevel.h
irq_offload.h irq_offload: Remove ifdef from the api 2021-04-29 14:50:35 +02:00
irq.h doc: fix display of the ISR_DIRECT_DECLARE example usage 2021-05-07 20:52:02 -04:00
kernel_includes.h kernel: move internal headers into include/kernel 2021-06-16 20:38:55 -04:00
kernel_structs.h kernel: Add extern "C" to kernel_struct.h 2021-06-18 08:11:22 -05:00
kernel_version.h
kernel.h doc: doxygen: replace option alias with kconfig 2021-06-29 10:26:28 -04:00
ptp_clock.h include: Move ptp_clock.h to drivers/ptp_clock.h 2021-03-25 10:09:33 +02:00
shared_irq.h drivers: intc: shared_irq: remove internal structs from header 2021-04-13 13:03:33 -04:00
spinlock.h
sw_isr_table.h
sys_clock.h
syscall_handler.h
syscall.h userspace: rename _is_user_context -> k_is_user_context 2021-04-01 05:34:17 -04:00
timeout_q.h kernel/arch: cleanup function definitions 2021-04-01 05:34:17 -04:00
toolchain.h toolchain: add llvm abstraction 2021-04-22 07:42:58 -04:00
wait_q.h kernel: move internal headers into include/kernel 2021-06-16 20:38:55 -04:00
zephyr.h