]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/Reliance-Edge/tests/util/rand.c
5f05be7625c5b48c791e885b6de48b91b5195444
[freertos] / FreeRTOS-Plus / Source / Reliance-Edge / tests / util / rand.c
1 /*             ----> DO NOT REMOVE THE FOLLOWING NOTICE <----\r
2 \r
3                    Copyright (c) 2014-2015 Datalight, Inc.\r
4                        All Rights Reserved Worldwide.\r
5 \r
6     This program is free software; you can redistribute it and/or modify\r
7     it under the terms of the GNU General Public License as published by\r
8     the Free Software Foundation; use version 2 of the License.\r
9 \r
10     This program is distributed in the hope that it will be useful,\r
11     but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty\r
12     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13     GNU General Public License for more details.\r
14 \r
15     You should have received a copy of the GNU General Public License along\r
16     with this program; if not, write to the Free Software Foundation, Inc.,\r
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 */\r
19 /*  Businesses and individuals that for commercial or other reasons cannot\r
20     comply with the terms of the GPLv2 license may obtain a commercial license\r
21     before incorporating Reliance Edge into proprietary software for\r
22     distribution in any form.  Visit http://www.datalight.com/reliance-edge for\r
23     more information.\r
24 */\r
25 /** @file\r
26     @brief Implements a random number generator.\r
27 */\r
28 #include <redfs.h>\r
29 #include <redtestutils.h>\r
30 \r
31 \r
32 /*  This is the global seed used by the random number generator when the caller\r
33     has not provided a seed to either the RedRand32() or RedRand64() functions.\r
34 */\r
35 static uint64_t ullGlobalRandomNumberSeed;\r
36 \r
37 /*  Whether the above seed has been initialized.\r
38 */\r
39 static bool fGlobalSeedInited;\r
40 \r
41 \r
42 /** @brief Set the global seed used by the random number generator.\r
43 \r
44     The global seed gets used when RedRand64() or RedRand32() are called with\r
45     a NULL seed argument.\r
46 \r
47     @param ullSeed  The value to use as the global RNG seed.\r
48 */\r
49 void RedRandSeed(\r
50     uint64_t ullSeed)\r
51 {\r
52     ullGlobalRandomNumberSeed = ullSeed;\r
53     fGlobalSeedInited = true;\r
54 }\r
55 \r
56 \r
57 /** @brief Generate a 64-bit pseudo-random number.\r
58 \r
59     The period of this random number generator is 2^64 (1.8 x 1019).  These\r
60     parameters are the same as the default one-stream SPRNG lcg64 generator and\r
61     it satisfies the requirements for a maximal period.\r
62 \r
63     The tempering value is used and an AND mask and is specifically selected to\r
64     favor the distribution of lower bits.\r
65 \r
66     @param pullSeed A pointer to the seed to use.  Set this value to NULL to\r
67                     use the internal global seed value.\r
68 \r
69     @return A pseudo-random number in the range [0, UINT64_MAX].\r
70 */\r
71 uint64_t RedRand64(\r
72     uint64_t       *pullSeed)\r
73 {\r
74     const uint64_t  ullA = UINT64_SUFFIX(2862933555777941757);\r
75     const uint64_t  ullC = UINT64_SUFFIX(3037000493);\r
76     const uint64_t  ullT = UINT64_SUFFIX(4921441182957829599);\r
77     uint64_t        ullN;\r
78     uint64_t       *pullSeedPtr;\r
79     uint64_t        ullLocalSeed;\r
80 \r
81     if(pullSeed != NULL)\r
82     {\r
83         ullLocalSeed = *pullSeed;\r
84         pullSeedPtr = pullSeed;\r
85     }\r
86     else\r
87     {\r
88         if(!fGlobalSeedInited)\r
89         {\r
90             /*  Unfortunately, the Reliance Edge OS services don't give us much\r
91                 to work with to initialize the global seed.  There is no entropy\r
92                 abstraction, no tick count abstraction, and the timestamp\r
93                 abstraction uses an opaque type which is not guaranteed to be an\r
94                 integer.  The best we can do is use the RTC.\r
95 \r
96                 Tests using the RNG should be supplying a seed anyway, for\r
97                 reproducibility.\r
98             */\r
99             RedRandSeed((uint64_t)RedOsClockGetTime());\r
100         }\r
101 \r
102         ullLocalSeed = ullGlobalRandomNumberSeed;\r
103         pullSeedPtr = &ullGlobalRandomNumberSeed;\r
104     }\r
105 \r
106     ullN = (ullLocalSeed * ullA) + ullC;\r
107 \r
108     *pullSeedPtr = ullN;\r
109 \r
110     /*  The linear congruential generator used above produces good psuedo-random\r
111         64-bit number sequences, however, as with any LCG, the period of the\r
112         lower order bits is much shorter resulting in alternately odd/even pairs\r
113         in bit zero.\r
114 \r
115         The result of the LGC above is tempered below with a series of XOR and\r
116         shift operations to produce a more acceptable equidistribution of bits\r
117         throughout the 64-bit range.\r
118     */\r
119     ullN ^= (ullN >> 21U) & ullT;\r
120     ullN ^= (ullN >> 43U) & ullT;\r
121     ullN ^= (ullN << 23U) & ~ullT;\r
122     ullN ^= (ullN << 31U) & ~ullT;\r
123 \r
124     return ullN;\r
125 }\r
126 \r
127 \r
128 /** @brief Generate a 32-bit pseudo-random number.\r
129 \r
130     @note   The 32-bit random number generator internally uses the 64-bit random\r
131             number generator, returning the low 32-bits of the pseudo-random\r
132             64-bit value.\r
133 \r
134     @param pulSeed  A pointer to the seed to use.  Set this value to NULL to use\r
135                     the internal global seed value.\r
136 \r
137     @return A pseudo-random number in the range [0, UINT32_MAX].\r
138 */\r
139 uint32_t RedRand32(\r
140     uint32_t   *pulSeed)\r
141 {\r
142     uint64_t    ullN;\r
143 \r
144     if(pulSeed != NULL)\r
145     {\r
146         uint64_t ullLocalSeed;\r
147 \r
148         ullLocalSeed = *pulSeed;\r
149         ullN = RedRand64(&ullLocalSeed);\r
150         *pulSeed = (uint32_t)ullLocalSeed;\r
151     }\r
152     else\r
153     {\r
154         ullN = RedRand64(NULL);\r
155     }\r
156 \r
157     return (uint32_t)ullN;\r
158 }\r
159 \r