Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2016-06-11 17:38:49 +0300
committerCampbell Barton <ideasman42@gmail.com>2016-06-11 17:41:02 +0300
commitdccf5afbef67ffd0201da26cc6603c1f42d3668f (patch)
treeeaeecaa7d7220874b479c1d376ef34133ae5ea88 /source/blender/blenlib/intern/rand.c
parent4ec1c76afce1aa94a5a31e6b4bf446995f98ff5a (diff)
BLI_rand: add BLI_rng_get_char_n
Use to fill an array of bytes to random values.
Diffstat (limited to 'source/blender/blenlib/intern/rand.c')
-rw-r--r--source/blender/blenlib/intern/rand.c40
1 files changed, 40 insertions, 0 deletions
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);