summaryrefslogtreecommitdiff
path: root/test/utils-prng.h
blob: 285107f087265807d1e1f7ddf7cd078167f4a375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright © 2012 Siarhei Siamashka <siarhei.siamashka@gmail.com>
 *
 * Based on the public domain implementation of small noncryptographic PRNG
 * authored by Bob Jenkins: http://burtleburtle.net/bob/rand/smallprng.html
 *
 * 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 (including the next
 * paragraph) 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.
 */

#ifndef __UTILS_PRNG_H__
#define __UTILS_PRNG_H__

/*
 * This file provides a fast SIMD-optimized noncryptographic PRNG (pseudorandom
 * number generator), with the output good enough to pass "Big Crush" tests
 * from TestU01 (http://en.wikipedia.org/wiki/TestU01).
 *
 * SIMD code uses http://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
 * which is a GCC specific extension. There is also a slower alternative
 * code path, which should work with any C compiler.
 *
 * The "prng_t" structure keeps the internal state of the random number
 * generator. It is possible to have multiple instances of the random number
 * generator active at the same time, in this case each of them needs to have
 * its own "prng_t". All the functions take a pointer to "prng_t"
 * as the first argument.
 *
 * Functions:
 *
 * ----------------------------------------------------------------------------
 * void prng_srand_r (prng_t *prng, uint32_t seed);
 *
 * Initialize the pseudorandom number generator. The sequence of preudorandom
 * numbers is deterministic and only depends on "seed". Any two generators
 * initialized with the same seed will produce exactly the same sequence.
 *
 * ----------------------------------------------------------------------------
 * uint32_t prng_rand_r (prng_t *prng);
 *
 * Generate a single uniformly distributed 32-bit pseudorandom value.
 *
 * ----------------------------------------------------------------------------
 * void prng_randmemset_r (prng_t                  *prng,
 *                         void                    *buffer,
 *                         size_t                   size,
 *                         prng_randmemset_flags_t  flags);
 *
 * Fills the memory buffer "buffer" with "size" bytes of pseudorandom data.
 * The "flags" argument may be used to tweak some statistics properties:
 *    RANDMEMSET_MORE_00 - set ~25% of bytes to 0x00
 *    RANDMEMSET_MORE_FF - set ~25% of bytes to 0xFF
 * The flags can be combined. This allows a bit better simulation of typical
 * pixel data, which normally contains a lot of fully transparent or fully
 * opaque pixels.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "pixman-private.h"

/*****************************************************************************/

#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
#define GCC_VECTOR_EXTENSIONS_SUPPORTED
typedef uint32_t uint32x4 __attribute__ ((vector_size(16)));
typedef uint8_t  uint8x16 __attribute__ ((vector_size(16)));
#endif

typedef struct
{
    uint32_t a, b, c, d;
} smallprng_t;

typedef struct
{
#ifdef GCC_VECTOR_EXTENSIONS_SUPPORTED
    uint32x4 a, b, c, d;
#else
    smallprng_t p1, p2, p3, p4;
#endif
    smallprng_t p0;
} prng_t;

typedef union
{
    uint8_t  b[16];
    uint32_t w[4];
#ifdef GCC_VECTOR_EXTENSIONS_SUPPORTED
    uint8x16 vb;
    uint32x4 vw;
#endif
} prng_rand_128_data_t;

/*****************************************************************************/

static force_inline uint32_t
smallprng_rand_r (smallprng_t *x)
{
    uint32_t e = x->a - ((x->b << 27) + (x->b >> (32 - 27)));
    x->a = x->b ^ ((x->c << 17) ^ (x->c >> (32 - 17)));
    x->b = x->c + x->d;
    x->c = x->d + e;
    x->d = e + x->a;
    return x->d;
}

/* Generate 4 bytes (32-bits) of random data */
static force_inline uint32_t
prng_rand_r (prng_t *x)
{
    return smallprng_rand_r (&x->p0);
}

/* Generate 16 bytes (128-bits) of random data */
static force_inline void
prng_rand_128_r (prng_t *x, prng_rand_128_data_t *data)
{
#ifdef GCC_VECTOR_EXTENSIONS_SUPPORTED
    uint32x4 e = x->a - ((x->b << 27) + (x->b >> (32 - 27)));
    x->a = x->b ^ ((x->c << 17) ^ (x->c >> (32 - 17)));
    x->b = x->c + x->d;
    x->c = x->d + e;
    x->d = e + x->a;
    data->vw = x->d;
#else
    data->w[0] = smallprng_rand_r (&x->p1);
    data->w[1] = smallprng_rand_r (&x->p2);
    data->w[2] = smallprng_rand_r (&x->p3);
    data->w[3] = smallprng_rand_r (&x->p4);
#endif
}

typedef enum
{
    RANDMEMSET_MORE_00        = 1, /* ~25% chance for 0x00 bytes */
    RANDMEMSET_MORE_FF        = 2, /* ~25% chance for 0xFF bytes */
    RANDMEMSET_MORE_00_AND_FF = (RANDMEMSET_MORE_00 | RANDMEMSET_MORE_FF)
} prng_randmemset_flags_t;

/* Set the 32-bit seed for PRNG */
void prng_srand_r (prng_t *prng, uint32_t seed);

/* Fill memory buffer with random data */
void prng_randmemset_r (prng_t                  *prng,
                        void                    *buffer,
                        size_t                   size,
                        prng_randmemset_flags_t  flags);

#endif