[Mesa-dev] [PATCH 2/3] util/rand_xor: add function to seed rand

Nicolai Hähnle nhaehnle at gmail.com
Tue Mar 21 10:07:09 UTC 2017


Thanks, but this is now not thread-safe. I'm not sure how much we should 
care about that, but anyway, as I see it there are three options:

1) Add some kind of locking.
2) Make the caller provide a struct for the state (similar to rand_r).
3) Ignore the issue and add a fat warning comment about it.

Personally, I'd say (2) is the most robust option.

Cheers,
Nicolai

On 21.03.2017 09:57, Timothy Arceri wrote:
> ---
>  src/gallium/drivers/radeon/r600_test_dma.c |  3 +-
>  src/util/rand_xor.c                        | 62 ++++++++++++++++++++++++++++--
>  src/util/rand_xor.h                        |  6 ++-
>  3 files changed, 64 insertions(+), 7 deletions(-)
>
> diff --git a/src/gallium/drivers/radeon/r600_test_dma.c b/src/gallium/drivers/radeon/r600_test_dma.c
> index 7d636b9..abef8ed 100644
> --- a/src/gallium/drivers/radeon/r600_test_dma.c
> +++ b/src/gallium/drivers/radeon/r600_test_dma.c
> @@ -172,22 +172,21 @@ void r600_test_dma(struct r600_common_screen *rscreen)
>
>  	max_levels = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
>  	max_tex_side = 1 << (max_levels - 1);
>
>  	/* Max 128 MB allowed for both textures. */
>  	max_alloc_size = 128 * 1024 * 1024;
>
>  	/* the seed for random test parameters */
>  	srand(0x9b47d95b);
>  	/* the seed for random pixel data */
> -	seed_xorshift128plus[0] = 0x3bffb83978e24f88;
> -	seed_xorshift128plus[1] = 0x9238d5d56c71cd35;
> +	s_rand_xorshift128plus(false);
>
>  	iterations = 1000000000; /* just kill it when you are bored */
>  	num_partial_copies = 30;
>
>  	/* These parameters are randomly generated per test:
>  	 * - whether to do one whole-surface copy or N partial copies per test
>  	 * - which tiling modes to use (LINEAR_ALIGNED, 1D, 2D)
>  	 * - which texture dimensions to use
>  	 * - whether to use VRAM (all tiling modes) and GTT (staging, linear
>  	 *   only) allocations
> diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c
> index adb799d..cc834fa 100644
> --- a/src/util/rand_xor.c
> +++ b/src/util/rand_xor.c
> @@ -1,21 +1,77 @@
> -/* Super fast random number generator.
> +/*
> + * Copyright 2017 Timothy Arceri
> + *
> + * 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.
>   *
> - * This rand_xorshift128plus function by Sebastiano Vigna belongs
> - * to the public domain.
>   */
>
> +#if defined(__linux__)
> +#include <sys/file.h>
> +#include <unistd.h>
> +#endif
> +
>  #include "rand_xor.h"
>
> +static uint64_t seed_xorshift128plus[2];
> +
> +/* Super fast random number generator.
> + *
> + * This rand_xorshift128plus function by Sebastiano Vigna belongs
> + * to the public domain.
> + */
>  uint64_t
>  rand_xorshift128plus(void)
>  {
>     uint64_t *s = seed_xorshift128plus;
>
>     uint64_t s1 = s[0];
>     const uint64_t s0 = s[1];
>     s[0] = s0;
>     s1 ^= s1 << 23;
>     s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
>
>     return s[1] + s0;
>  }
> +
> +void
> +s_rand_xorshift128plus(bool randomised_seed)
> +{
> +#if defined(__linux__)
> +   if (!randomised_seed)
> +      goto fixed_seed;
> +
> +   int fd = open("/dev/urandom", O_RDONLY);
> +   if (fd < 0)
> +      goto fixed_seed;
> +
> +   size_t seed_size = sizeof(uint64_t) * 2;
> +   if (read(fd, &seed_xorshift128plus, seed_size) != seed_size)
> +      goto fixed_seed;
> +
> +   close(fd);
> +   return;
> +
> +fixed_seed:
> +#endif
> +
> +   /* Fallback to a fixed seed */
> +   seed_xorshift128plus[0] = 0x3bffb83978e24f88;
> +   seed_xorshift128plus[1] = 0x9238d5d56c71cd35;
> +}
> diff --git a/src/util/rand_xor.h b/src/util/rand_xor.h
> index c7667e4..8a48d63 100644
> --- a/src/util/rand_xor.h
> +++ b/src/util/rand_xor.h
> @@ -19,17 +19,19 @@
>   * 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 RAND_XOR_H
>  #define RAND_XOR_H
>
>  #include <stdint.h>
> -
> -static uint64_t seed_xorshift128plus[2];
> +#include <stdbool.h>
>
>  uint64_t
>  rand_xorshift128plus(void);
>
> +void
> +s_rand_xorshift128plus(bool randomised_seed);
> +
>  #endif /* RAND_XOR_H */
>



More information about the mesa-dev mailing list