2 * Copyright © 2012 Siarhei Siamashka <siarhei.siamashka@gmail.com>
4 * Based on the public domain implementation of small noncryptographic PRNG
5 * authored by Bob Jenkins: http://burtleburtle.net/bob/rand/smallprng.html
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
29 #include "utils-prng.h"
32 /* The original code from http://www.burtleburtle.net/bob/rand/smallprng.html */
35 typedef struct ranctx { u4 a; u4 b; u4 c; u4 d; } ranctx;
37 #define rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
38 u4 ranval( ranctx *x ) {
39 u4 e = x->a - rot(x->b, 27);
40 x->a = x->b ^ rot(x->c, 17);
47 void raninit( ranctx *x, u4 seed ) {
49 x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
50 for (i=0; i<20; ++i) {
55 /*****************************************************************************/
57 #define BUFSIZE (8 * 1024 * 1024)
65 uint8_t *buf = aligned_malloc (16, BUFSIZE + 1);
67 prng_srand_r (&prng, 1234);
69 for (i = 0; i < N; i++)
70 prng_randmemset_r (&prng, buf, BUFSIZE, 0);
72 printf ("aligned randmemset : %.2f MB/s\n",
73 (double)BUFSIZE * N / 1000000. / (t2 - t1));
76 for (i = 0; i < N; i++)
77 prng_randmemset_r (&prng, buf + 1, BUFSIZE, 0);
79 printf ("unaligned randmemset : %.2f MB/s\n",
80 (double)BUFSIZE * N / 1000000. / (t2 - t1));
83 for (i = 0; i < N; i++)
85 prng_randmemset_r (&prng, buf, BUFSIZE, RANDMEMSET_MORE_00_AND_FF);
88 printf ("aligned randmemset (more 00 and FF) : %.2f MB/s\n",
89 (double)BUFSIZE * N / 1000000. / (t2 - t1));
92 for (i = 0; i < N; i++)
94 prng_randmemset_r (&prng, buf + 1, BUFSIZE, RANDMEMSET_MORE_00_AND_FF);
97 printf ("unaligned randmemset (more 00 and FF) : %.2f MB/s\n",
98 (double)BUFSIZE * N / 1000000. / (t2 - t1));
103 #define SMALLBUFSIZE 100
105 int main (int argc, char *argv[])
107 const uint32_t ref_crc[RANDMEMSET_MORE_00_AND_FF + 1] =
109 0xBA06763D, 0x103FC550, 0x8B59ABA5, 0xD82A0F39,
110 0xD2321099, 0xFD8C5420, 0xD3B7C42A, 0xFC098093,
111 0x85E01DE0, 0x6680F8F7, 0x4D32DD3C, 0xAE52382B,
112 0x149E6CB5, 0x8B336987, 0x15DCB2B3, 0x8A71B781
115 uint32_t ref, seed, seed0, seed1, seed2, seed3;
116 prng_rand_128_data_t buf;
117 uint8_t *bytebuf = aligned_malloc(16, SMALLBUFSIZE + 1);
120 prng_randmemset_flags_t flags;
122 if (argc > 1 && strcmp(argv[1], "-bench") == 0)
130 prng_srand_r (&prng, 0);
131 assert (ranval (&x) == prng_rand_r (&prng));
133 /* test for simd code */
135 prng_srand_r (&prng, seed);
136 seed0 = (seed = seed * 1103515245 + 12345);
137 seed1 = (seed = seed * 1103515245 + 12345);
138 seed2 = (seed = seed * 1103515245 + 12345);
139 seed3 = (seed = seed * 1103515245 + 12345);
140 prng_rand_128_r (&prng, &buf);
144 assert (ref == buf.w[0]);
148 assert (ref == buf.w[1]);
152 assert (ref == buf.w[2]);
156 assert (ref == buf.w[3]);
158 /* test for randmemset */
159 for (flags = 0; flags <= RANDMEMSET_MORE_00_AND_FF; flags++)
161 prng_srand_r (&prng, 1234);
162 prng_randmemset_r (&prng, bytebuf, 16, flags);
163 prng_randmemset_r (&prng, bytebuf + 16, SMALLBUFSIZE - 17, flags);
164 crc1 = compute_crc32 (0, bytebuf, SMALLBUFSIZE - 1);
165 prng_srand_r (&prng, 1234);
166 prng_randmemset_r (&prng, bytebuf + 1, SMALLBUFSIZE - 1, flags);
167 crc2 = compute_crc32 (0, bytebuf + 1, SMALLBUFSIZE - 1);
168 assert (ref_crc[flags] == crc1);
169 assert (ref_crc[flags] == crc2);