xref: /rk3399_rockchip-uboot/lib/rand.c (revision 9acf1ca50d8b031511d146f6ffd73201fedce28c)
1*9acf1ca5SMichael Walle /*
2*9acf1ca5SMichael Walle  * Simple xorshift PRNG
3*9acf1ca5SMichael Walle  *   see http://www.jstatsoft.org/v08/i14/paper
4*9acf1ca5SMichael Walle  *
5*9acf1ca5SMichael Walle  * Copyright (c) 2012 Michael Walle
6*9acf1ca5SMichael Walle  * Michael Walle <michael@walle.cc>
7*9acf1ca5SMichael Walle  *
8*9acf1ca5SMichael Walle  * See file CREDITS for list of people who contributed to this
9*9acf1ca5SMichael Walle  * project.
10*9acf1ca5SMichael Walle  *
11*9acf1ca5SMichael Walle  * This program is free software; you can redistribute it and/or
12*9acf1ca5SMichael Walle  * modify it under the terms of the GNU General Public License as
13*9acf1ca5SMichael Walle  * published by the Free Software Foundation; either version 2 of
14*9acf1ca5SMichael Walle  * the License, or (at your option) any later version.
15*9acf1ca5SMichael Walle  *
16*9acf1ca5SMichael Walle  * This program is distributed in the hope that it will be useful,
17*9acf1ca5SMichael Walle  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18*9acf1ca5SMichael Walle  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*9acf1ca5SMichael Walle  * GNU General Public License for more details.
20*9acf1ca5SMichael Walle  *
21*9acf1ca5SMichael Walle  * You should have received a copy of the GNU General Public License
22*9acf1ca5SMichael Walle  * along with this program; if not, write to the Free Software
23*9acf1ca5SMichael Walle  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24*9acf1ca5SMichael Walle  * MA 02111-1307 USA
25*9acf1ca5SMichael Walle  */
26*9acf1ca5SMichael Walle 
27*9acf1ca5SMichael Walle #include <common.h>
28*9acf1ca5SMichael Walle 
29*9acf1ca5SMichael Walle static unsigned int y = 1U;
30*9acf1ca5SMichael Walle 
31*9acf1ca5SMichael Walle unsigned int rand_r(unsigned int *seedp)
32*9acf1ca5SMichael Walle {
33*9acf1ca5SMichael Walle 	*seedp ^= (*seedp << 13);
34*9acf1ca5SMichael Walle 	*seedp ^= (*seedp >> 17);
35*9acf1ca5SMichael Walle 	*seedp ^= (*seedp << 5);
36*9acf1ca5SMichael Walle 
37*9acf1ca5SMichael Walle 	return *seedp;
38*9acf1ca5SMichael Walle }
39*9acf1ca5SMichael Walle 
40*9acf1ca5SMichael Walle unsigned int rand(void)
41*9acf1ca5SMichael Walle {
42*9acf1ca5SMichael Walle 	return rand_r(&y);
43*9acf1ca5SMichael Walle }
44*9acf1ca5SMichael Walle 
45*9acf1ca5SMichael Walle void srand(unsigned int seed)
46*9acf1ca5SMichael Walle {
47*9acf1ca5SMichael Walle 	y = seed;
48*9acf1ca5SMichael Walle }
49