Blame test/utils-prng.h

Packit 030a23
/*
Packit 030a23
 * Copyright © 2012 Siarhei Siamashka <siarhei.siamashka@gmail.com>
Packit 030a23
 *
Packit 030a23
 * Based on the public domain implementation of small noncryptographic PRNG
Packit 030a23
 * authored by Bob Jenkins: http://burtleburtle.net/bob/rand/smallprng.html
Packit 030a23
 *
Packit 030a23
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit 030a23
 * copy of this software and associated documentation files (the "Software"),
Packit 030a23
 * to deal in the Software without restriction, including without limitation
Packit 030a23
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit 030a23
 * and/or sell copies of the Software, and to permit persons to whom the
Packit 030a23
 * Software is furnished to do so, subject to the following conditions:
Packit 030a23
 *
Packit 030a23
 * The above copyright notice and this permission notice (including the next
Packit 030a23
 * paragraph) shall be included in all copies or substantial portions of the
Packit 030a23
 * Software.
Packit 030a23
 *
Packit 030a23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 030a23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 030a23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
Packit 030a23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit 030a23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit 030a23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit 030a23
 * DEALINGS IN THE SOFTWARE.
Packit 030a23
 */
Packit 030a23
Packit 030a23
#ifndef __UTILS_PRNG_H__
Packit 030a23
#define __UTILS_PRNG_H__
Packit 030a23
Packit 030a23
/*
Packit 030a23
 * This file provides a fast SIMD-optimized noncryptographic PRNG (pseudorandom
Packit 030a23
 * number generator), with the output good enough to pass "Big Crush" tests
Packit 030a23
 * from TestU01 (http://en.wikipedia.org/wiki/TestU01).
Packit 030a23
 *
Packit 030a23
 * SIMD code uses http://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
Packit 030a23
 * which is a GCC specific extension. There is also a slower alternative
Packit 030a23
 * code path, which should work with any C compiler.
Packit 030a23
 *
Packit 030a23
 * The "prng_t" structure keeps the internal state of the random number
Packit 030a23
 * generator. It is possible to have multiple instances of the random number
Packit 030a23
 * generator active at the same time, in this case each of them needs to have
Packit 030a23
 * its own "prng_t". All the functions take a pointer to "prng_t"
Packit 030a23
 * as the first argument.
Packit 030a23
 *
Packit 030a23
 * Functions:
Packit 030a23
 *
Packit 030a23
 * ----------------------------------------------------------------------------
Packit 030a23
 * void prng_srand_r (prng_t *prng, uint32_t seed);
Packit 030a23
 *
Packit 030a23
 * Initialize the pseudorandom number generator. The sequence of preudorandom
Packit 030a23
 * numbers is deterministic and only depends on "seed". Any two generators
Packit 030a23
 * initialized with the same seed will produce exactly the same sequence.
Packit 030a23
 *
Packit 030a23
 * ----------------------------------------------------------------------------
Packit 030a23
 * uint32_t prng_rand_r (prng_t *prng);
Packit 030a23
 *
Packit 030a23
 * Generate a single uniformly distributed 32-bit pseudorandom value.
Packit 030a23
 *
Packit 030a23
 * ----------------------------------------------------------------------------
Packit 030a23
 * void prng_randmemset_r (prng_t                  *prng,
Packit 030a23
 *                         void                    *buffer,
Packit 030a23
 *                         size_t                   size,
Packit 030a23
 *                         prng_randmemset_flags_t  flags);
Packit 030a23
 *
Packit 030a23
 * Fills the memory buffer "buffer" with "size" bytes of pseudorandom data.
Packit 030a23
 * The "flags" argument may be used to tweak some statistics properties:
Packit 030a23
 *    RANDMEMSET_MORE_00 - set ~25% of bytes to 0x00
Packit 030a23
 *    RANDMEMSET_MORE_FF - set ~25% of bytes to 0xFF
Packit 030a23
 * The flags can be combined. This allows a bit better simulation of typical
Packit 030a23
 * pixel data, which normally contains a lot of fully transparent or fully
Packit 030a23
 * opaque pixels.
Packit 030a23
 */
Packit 030a23
Packit 030a23
#ifdef HAVE_CONFIG_H
Packit 030a23
#include <config.h>
Packit 030a23
#endif
Packit 030a23
Packit 030a23
#include "pixman-private.h"
Packit 030a23
Packit 030a23
/*****************************************************************************/
Packit 030a23
Packit 030a23
#ifdef HAVE_GCC_VECTOR_EXTENSIONS
Packit 030a23
typedef uint32_t uint32x4 __attribute__ ((vector_size(16)));
Packit 030a23
typedef uint8_t  uint8x16 __attribute__ ((vector_size(16)));
Packit 030a23
#endif
Packit 030a23
Packit 030a23
typedef struct
Packit 030a23
{
Packit 030a23
    uint32_t a, b, c, d;
Packit 030a23
} smallprng_t;
Packit 030a23
Packit 030a23
typedef struct
Packit 030a23
{
Packit 030a23
#ifdef HAVE_GCC_VECTOR_EXTENSIONS
Packit 030a23
    uint32x4 a, b, c, d;
Packit 030a23
#else
Packit 030a23
    smallprng_t p1, p2, p3, p4;
Packit 030a23
#endif
Packit 030a23
    smallprng_t p0;
Packit 030a23
} prng_t;
Packit 030a23
Packit 030a23
typedef union
Packit 030a23
{
Packit 030a23
    uint8_t  b[16];
Packit 030a23
    uint32_t w[4];
Packit 030a23
#ifdef HAVE_GCC_VECTOR_EXTENSIONS
Packit 030a23
    uint8x16 vb;
Packit 030a23
    uint32x4 vw;
Packit 030a23
#endif
Packit 030a23
} prng_rand_128_data_t;
Packit 030a23
Packit 030a23
/*****************************************************************************/
Packit 030a23
Packit 030a23
static force_inline uint32_t
Packit 030a23
smallprng_rand_r (smallprng_t *x)
Packit 030a23
{
Packit 030a23
    uint32_t e = x->a - ((x->b << 27) + (x->b >> (32 - 27)));
Packit 030a23
    x->a = x->b ^ ((x->c << 17) ^ (x->c >> (32 - 17)));
Packit 030a23
    x->b = x->c + x->d;
Packit 030a23
    x->c = x->d + e;
Packit 030a23
    x->d = e + x->a;
Packit 030a23
    return x->d;
Packit 030a23
}
Packit 030a23
Packit 030a23
/* Generate 4 bytes (32-bits) of random data */
Packit 030a23
static force_inline uint32_t
Packit 030a23
prng_rand_r (prng_t *x)
Packit 030a23
{
Packit 030a23
    return smallprng_rand_r (&x->p0);
Packit 030a23
}
Packit 030a23
Packit 030a23
/* Generate 16 bytes (128-bits) of random data */
Packit 030a23
static force_inline void
Packit 030a23
prng_rand_128_r (prng_t *x, prng_rand_128_data_t *data)
Packit 030a23
{
Packit 030a23
#ifdef HAVE_GCC_VECTOR_EXTENSIONS
Packit 030a23
    uint32x4 e = x->a - ((x->b << 27) + (x->b >> (32 - 27)));
Packit 030a23
    x->a = x->b ^ ((x->c << 17) ^ (x->c >> (32 - 17)));
Packit 030a23
    x->b = x->c + x->d;
Packit 030a23
    x->c = x->d + e;
Packit 030a23
    x->d = e + x->a;
Packit 030a23
    data->vw = x->d;
Packit 030a23
#else
Packit 030a23
    data->w[0] = smallprng_rand_r (&x->p1);
Packit 030a23
    data->w[1] = smallprng_rand_r (&x->p2);
Packit 030a23
    data->w[2] = smallprng_rand_r (&x->p3);
Packit 030a23
    data->w[3] = smallprng_rand_r (&x->p4);
Packit 030a23
#endif
Packit 030a23
}
Packit 030a23
Packit 030a23
typedef enum
Packit 030a23
{
Packit 030a23
    RANDMEMSET_MORE_00        = 1, /* ~25% chance for 0x00 bytes */
Packit 030a23
    RANDMEMSET_MORE_FF        = 2, /* ~25% chance for 0xFF bytes */
Packit 030a23
    RANDMEMSET_MORE_00000000  = 4, /* ~25% chance for 0x00000000 clusters */
Packit 030a23
    RANDMEMSET_MORE_FFFFFFFF  = 8, /* ~25% chance for 0xFFFFFFFF clusters */
Packit 030a23
    RANDMEMSET_MORE_00_AND_FF = (RANDMEMSET_MORE_00 | RANDMEMSET_MORE_00000000 |
Packit 030a23
                                 RANDMEMSET_MORE_FF | RANDMEMSET_MORE_FFFFFFFF)
Packit 030a23
} prng_randmemset_flags_t;
Packit 030a23
Packit 030a23
/* Set the 32-bit seed for PRNG */
Packit 030a23
void prng_srand_r (prng_t *prng, uint32_t seed);
Packit 030a23
Packit 030a23
/* Fill memory buffer with random data */
Packit 030a23
void prng_randmemset_r (prng_t                  *prng,
Packit 030a23
                        void                    *buffer,
Packit 030a23
                        size_t                   size,
Packit 030a23
                        prng_randmemset_flags_t  flags);
Packit 030a23
Packit 030a23
#endif