From dccf5afbef67ffd0201da26cc6603c1f42d3668f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 12 Jun 2016 00:38:49 +1000 Subject: BLI_rand: add BLI_rng_get_char_n Use to fill an array of bytes to random values. --- source/blender/blenlib/intern/rand.c | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'source/blender/blenlib/intern/rand.c') diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c index 66c568a7ff3..40d9a3da3d9 100644 --- a/source/blender/blenlib/intern/rand.c +++ b/source/blender/blenlib/intern/rand.c @@ -46,6 +46,7 @@ #define MULTIPLIER 0x5DEECE66Dll #define MASK 0x0000FFFFFFFFFFFFll +#define MASK_BYTES 2 #define ADDEND 0xB #define LOWSEED 0x330E @@ -107,6 +108,45 @@ BLI_INLINE void rng_step(RNG *rng) rng->X = (MULTIPLIER * rng->X + ADDEND) & MASK; } +void BLI_rng_get_char_n(RNG *rng, char *bytes, size_t bytes_len) +{ + size_t last_len = 0; + size_t trim_len = bytes_len; + +#define RAND_STRIDE (sizeof(rng->X) - MASK_BYTES) + + if (trim_len > RAND_STRIDE) { + last_len = trim_len % RAND_STRIDE; + trim_len = trim_len - last_len; + } + else { + trim_len = 0; + last_len = bytes_len; + } + + const char *data_src = (void *)&(rng->X); + size_t i = 0; + while (i != trim_len) { + BLI_assert(i < trim_len); +#ifdef __BIG_ENDIAN__ + for (size_t j = (RAND_STRIDE + MASK_BYTES) - 1; j != MASK_BYTES - 1; j--) +#else + for (size_t j = 0; j != RAND_STRIDE; j++) +#endif + { + bytes[i++] = data_src[j]; + } + rng_step(rng); + } + if (last_len) { + for (size_t j = 0; j != last_len; j++) { + bytes[i++] = data_src[j]; + } + } + +#undef RAND_STRIDE +} + int BLI_rng_get_int(RNG *rng) { rng_step(rng); -- cgit v1.2.3