1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys: random: add simple LCG PRNGs

This commit is contained in:
Kaspar Schleiser 2015-10-12 16:05:17 +02:00
parent fab1681735
commit 9c93e72d81
4 changed files with 134 additions and 0 deletions

View File

@ -23,3 +23,6 @@ PSEUDOMODULES += netif
# include variants of the AT86RF2xx drivers as pseudo modules
PSEUDOMODULES += at86rf23%
PSEUDOMODULES += at86rf21%
# add all pseudo random number generator variants as pseudomodules
PSEUDOMODULES += prng_%

View File

@ -1 +1,14 @@
ifeq (,$(filter prng_%,$(USEMODULE)))
USEMODULE += prng_mersenne
endif
ifneq (,$(filter prng_mersenne,$(USEMODULE)))
SRC += mersenne.c
endif
ifneq (,$(filter prng_minstd,$(USEMODULE)))
SRC += minstd.c
endif
ifneq (,$(filter prng_musl_lcg,$(USEMODULE)))
SRC += musl_lcg.c
endif
include $(RIOTBASE)/Makefile.base

73
sys/random/minstd.c Normal file
View File

@ -0,0 +1,73 @@
/**
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
* Code taken from C FAQ (http://c-faq.com/lib/rand.html).
*/
/**
* @ingroup sys_random
* @{
* @file
*
* @brief Simple Park & Miller "minimal standard" PRNG
*
* This file contains a simple Park-Miller pseudo random number generator.
*
* While not very random when considering crypto requirements, this is probably
* random enough anywhere where pseudo-randomness is sufficient, e.g., when
* provided with a sensible seed source, for MAC algorithms.
*
* The implementation is taken from the C FAQ, but modified to use magic number
* division and adapted to RIOT's coding conventions..
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @}
*/
#include <stdint.h>
#include "div.h"
#define a 48271
#define m 2147483647
#define q (m / a) /* 44488 */
#define r (m % a) /* 3399 */
static uint32_t _seed = 1;
int rand_minstd(void)
{
uint32_t hi = div_u32_by_44488(_seed);
uint32_t lo = div_u32_mod_44488(_seed);
uint32_t test = (a * lo) - (r * hi);
if(test > 0) {
_seed = test;
}
else {
_seed = test + m;
}
return _seed;
}
uint32_t genrand_uint32(void)
{
/* minstd as implemented returns only values from 1 to 2147483647,
* so run it two times to get 32bits */
uint16_t A = (rand_minstd() >> 15);
uint16_t B = (rand_minstd() >> 15);
return (((uint32_t)A) << 16) | B;
}
void genrand_init(uint32_t val)
{
if (!val) {
val = 1;
}
_seed = val;
}

45
sys/random/musl_lcg.c Normal file
View File

@ -0,0 +1,45 @@
/**
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
* Code taken from Musl C library. Original copyright notice:
*
* Copyright © 2005-2014 Rich Felker, et al.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdint.h>
static uint64_t _seed;
void genrand_init(uint32_t s)
{
_seed = s-1;
}
uint32_t genrand_uint32(void)
{
_seed = 6364136223846793005ULL*_seed + 1;
return _seed>>32;
}