• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Siarhei Siamashka <siarhei.siamashka@gmail.com>
3  *
4  * Based on the public domain implementation of small noncryptographic PRNG
5  * authored by Bob Jenkins: http://burtleburtle.net/bob/rand/smallprng.html
6  *
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:
13  *
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
16  * Software.
17  *
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.
25  */
26 
27 #include <assert.h>
28 #include <stdlib.h>
29 #include "utils-prng.h"
30 #include "utils.h"
31 
32 /* The original code from http://www.burtleburtle.net/bob/rand/smallprng.html */
33 
34 typedef uint32_t u4;
35 typedef struct ranctx { u4 a; u4 b; u4 c; u4 d; } ranctx;
36 
37 #define rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
ranval(ranctx * x)38 u4 ranval( ranctx *x ) {
39     u4 e = x->a - rot(x->b, 27);
40     x->a = x->b ^ rot(x->c, 17);
41     x->b = x->c + x->d;
42     x->c = x->d + e;
43     x->d = e + x->a;
44     return x->d;
45 }
46 
raninit(ranctx * x,u4 seed)47 void raninit( ranctx *x, u4 seed ) {
48     u4 i;
49     x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
50     for (i=0; i<20; ++i) {
51         (void)ranval(x);
52     }
53 }
54 
55 /*****************************************************************************/
56 
57 #define BUFSIZE (8 * 1024 * 1024)
58 #define N 50
59 
bench(void)60 void bench (void)
61 {
62     double t1, t2;
63     int i;
64     prng_t prng;
65     uint8_t *buf = aligned_malloc (16, BUFSIZE + 1);
66 
67     prng_srand_r (&prng, 1234);
68     t1 = gettime();
69     for (i = 0; i < N; i++)
70         prng_randmemset_r (&prng, buf, BUFSIZE, 0);
71     t2 = gettime();
72     printf ("aligned randmemset                    : %.2f MB/s\n",
73             (double)BUFSIZE * N / 1000000. / (t2 - t1));
74 
75     t1 = gettime();
76     for (i = 0; i < N; i++)
77         prng_randmemset_r (&prng, buf + 1, BUFSIZE, 0);
78     t2 = gettime();
79     printf ("unaligned randmemset                  : %.2f MB/s\n",
80             (double)BUFSIZE * N / 1000000. / (t2 - t1));
81 
82     t1 = gettime();
83     for (i = 0; i < N; i++)
84     {
85         prng_randmemset_r (&prng, buf, BUFSIZE, RANDMEMSET_MORE_00_AND_FF);
86     }
87     t2 = gettime ();
88     printf ("aligned randmemset (more 00 and FF)   : %.2f MB/s\n",
89             (double)BUFSIZE * N / 1000000. / (t2 - t1));
90 
91     t1 = gettime();
92     for (i = 0; i < N; i++)
93     {
94         prng_randmemset_r (&prng, buf + 1, BUFSIZE, RANDMEMSET_MORE_00_AND_FF);
95     }
96     t2 = gettime ();
97     printf ("unaligned randmemset (more 00 and FF) : %.2f MB/s\n",
98             (double)BUFSIZE * N / 1000000. / (t2 - t1));
99 
100     free (buf);
101 }
102 
103 #define SMALLBUFSIZE 100
104 
main(int argc,char * argv[])105 int main (int argc, char *argv[])
106 {
107     const uint32_t ref_crc[RANDMEMSET_MORE_00_AND_FF + 1] =
108     {
109         0xBA06763D, 0x103FC550, 0x8B59ABA5, 0xD82A0F39,
110         0xD2321099, 0xFD8C5420, 0xD3B7C42A, 0xFC098093,
111         0x85E01DE0, 0x6680F8F7, 0x4D32DD3C, 0xAE52382B,
112         0x149E6CB5, 0x8B336987, 0x15DCB2B3, 0x8A71B781
113     };
114     uint32_t crc1, crc2;
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);
118     ranctx x;
119     prng_t prng;
120     prng_randmemset_flags_t flags;
121 
122     if (argc > 1 && strcmp(argv[1], "-bench") == 0)
123     {
124         bench ();
125         return 0;
126     }
127 
128     /* basic test */
129     raninit (&x, 0);
130     prng_srand_r (&prng, 0);
131     assert (ranval (&x) == prng_rand_r (&prng));
132 
133     /* test for simd code */
134     seed = 0;
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);
141 
142     raninit (&x, seed0);
143     ref = ranval (&x);
144     assert (ref == buf.w[0]);
145 
146     raninit (&x, seed1);
147     ref = ranval (&x);
148     assert (ref == buf.w[1]);
149 
150     raninit (&x, seed2);
151     ref = ranval (&x);
152     assert (ref == buf.w[2]);
153 
154     raninit (&x, seed3);
155     ref = ranval (&x);
156     assert (ref == buf.w[3]);
157 
158     /* test for randmemset */
159     for (flags = 0; flags <= RANDMEMSET_MORE_00_AND_FF; flags++)
160     {
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);
170     }
171 
172     free (bytebuf);
173 
174     return 0;
175 }
176