[Bf-blender-cvs] [bbfeb12] master: Code cleanup: use strict flags for BLI_rand

Campbell Barton noreply at git.blender.org
Sun Mar 30 06:04:57 CEST 2014


Commit: bbfeb120fcf7ac65e9b61599f85d562acd26bcbb
Author: Campbell Barton
Date:   Sun Mar 30 13:12:33 2014 +1100
https://developer.blender.org/rBbbfeb120fcf7ac65e9b61599f85d562acd26bcbb

Code cleanup: use strict flags for BLI_rand

===================================================================

M	source/blender/blenlib/BLI_rand.h
M	source/blender/blenlib/intern/rand.c
M	source/blender/modifiers/intern/MOD_explode.c

===================================================================

diff --git a/source/blender/blenlib/BLI_rand.h b/source/blender/blenlib/BLI_rand.h
index 20d5f8d..011b6ea 100644
--- a/source/blender/blenlib/BLI_rand.h
+++ b/source/blender/blenlib/BLI_rand.h
@@ -47,10 +47,11 @@ void        BLI_rng_free(struct RNG *rng);
 void        BLI_rng_seed(struct RNG *rng, unsigned int seed);
 void        BLI_rng_srandom(struct RNG *rng, unsigned int seed);
 int         BLI_rng_get_int(struct RNG *rng);
+unsigned int BLI_rng_get_uint(struct RNG *rng);
 double      BLI_rng_get_double(struct RNG *rng);
 float       BLI_rng_get_float(struct RNG *rng);
 void        BLI_rng_get_float_unit_v3(struct RNG *rng, float v[3]);
-void        BLI_rng_shuffle_array(struct RNG *rng, void *data, int elemSize, int numElems);
+void        BLI_rng_shuffle_array(struct RNG *rng, void *data, size_t elem_size, unsigned int elem_tot);
 
 /** Note that skipping is as slow as generating n numbers! */
 void        BLI_rng_skip(struct RNG *rng, int n);
@@ -72,7 +73,7 @@ float	BLI_hash_frand(unsigned int seed);
  * contents. This routine does not use nor modify
  * the state of the BLI random number generator.
  */
-void    BLI_array_randomize(void *data, int elemSize, int numElems, unsigned int seed);
+void    BLI_array_randomize(void *data, size_t elem_size, unsigned int elem_tot, unsigned int seed);
 
 
 /** Better seed for the random number generator, using noise.c hash[] */
diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c
index f6f7c6e..25a1a52 100644
--- a/source/blender/blenlib/intern/rand.c
+++ b/source/blender/blenlib/intern/rand.c
@@ -41,6 +41,8 @@
 #include "BLI_threads.h"
 #include "BLI_rand.h"
 
+#include "BLI_strict_flags.h"
+
 #ifdef _MSC_VER
 typedef unsigned __int64 r_uint64;
 
@@ -96,9 +98,9 @@ void BLI_rng_seed(RNG *rng, unsigned int seed)
 void BLI_rng_srandom(RNG *rng, unsigned int seed)
 {
 	BLI_rng_seed(rng, seed + hash[seed & 255]);
-	seed = BLI_rng_get_int(rng);
+	seed = BLI_rng_get_uint(rng);
 	BLI_rng_seed(rng, seed + hash[seed & 255]);
-	seed = BLI_rng_get_int(rng);
+	seed = BLI_rng_get_uint(rng);
 	BLI_rng_seed(rng, seed + hash[seed & 255]);
 }
 
@@ -108,6 +110,12 @@ int BLI_rng_get_int(RNG *rng)
 	return (int) (rng->X >> 17);
 }
 
+unsigned int BLI_rng_get_uint(RNG *rng)
+{
+	rng->X = (MULTIPLIER * rng->X + ADDEND) & MASK;
+	return (unsigned int) (rng->X >> 17);
+}
+
 double BLI_rng_get_double(RNG *rng)
 {
 	return (double) BLI_rng_get_int(rng) / 0x80000000;
@@ -133,27 +141,28 @@ void BLI_rng_get_float_unit_v3(RNG *rng, float v[3])
 	}
 }
 
-void BLI_rng_shuffle_array(RNG *rng, void *data, int elemSize, int numElems)
+void BLI_rng_shuffle_array(RNG *rng, void *data, size_t elem_size, unsigned int elem_tot)
 {
-	int i = numElems;
+	const unsigned int elem_size_i = (unsigned int)elem_size;
+	unsigned int i = elem_tot;
 	void *temp;
 
-	if (numElems <= 0) {
+	if (elem_tot == 0) {
 		return;
 	}
 
-	temp = malloc(elemSize);
+	temp = malloc(elem_size);
 
 	/* XXX Shouldn't it rather be "while (i--) {" ?
 	 *     Else we have no guaranty first (0) element has a chance to be shuffled... --mont29 */
 	while (--i) {
-		int j = BLI_rng_get_int(rng) % numElems;
+		unsigned int j = BLI_rng_get_uint(rng) % elem_tot;
 		if (i != j) {
-			void *iElem = (unsigned char *)data + i * elemSize;
-			void *jElem = (unsigned char *)data + j * elemSize;
-			memcpy(temp, iElem, elemSize);
-			memcpy(iElem, jElem, elemSize);
-			memcpy(jElem, temp, elemSize);
+			void *iElem = (unsigned char *)data + i * elem_size_i;
+			void *jElem = (unsigned char *)data + j * elem_size_i;
+			memcpy(temp, iElem, elem_size);
+			memcpy(iElem, jElem, elem_size);
+			memcpy(jElem, temp, elem_size);
 		}
 	}
 
@@ -202,12 +211,12 @@ float BLI_hash_frand(unsigned int seed)
 	return BLI_rng_get_float(&rng);
 }
 
-void BLI_array_randomize(void *data, int elemSize, int numElems, unsigned int seed)
+void BLI_array_randomize(void *data, size_t elem_size, unsigned int elem_tot, unsigned int seed)
 {
 	RNG rng;
 
 	BLI_rng_seed(&rng, seed);
-	BLI_rng_shuffle_array(&rng, data, elemSize, numElems);
+	BLI_rng_shuffle_array(&rng, data, elem_size, elem_tot);
 }
 
 /* ********* for threaded random ************** */
@@ -220,9 +229,9 @@ void BLI_thread_srandom(int thread, unsigned int seed)
 		thread = 0;
 	
 	BLI_rng_seed(&rng_tab[thread], seed + hash[seed & 255]);
-	seed = BLI_rng_get_int(&rng_tab[thread]);
+	seed = BLI_rng_get_uint(&rng_tab[thread]);
 	BLI_rng_seed(&rng_tab[thread], seed + hash[seed & 255]);
-	seed = BLI_rng_get_int(&rng_tab[thread]);
+	seed = BLI_rng_get_uint(&rng_tab[thread]);
 	BLI_rng_seed(&rng_tab[thread], seed + hash[seed & 255]);
 }
 
diff --git a/source/blender/modifiers/intern/MOD_explode.c b/source/blender/modifiers/intern/MOD_explode.c
index e26cf6a..139a3db 100644
--- a/source/blender/modifiers/intern/MOD_explode.c
+++ b/source/blender/modifiers/intern/MOD_explode.c
@@ -37,11 +37,11 @@
 #include "DNA_scene_types.h"
 #include "DNA_object_types.h"
 
+#include "BLI_utildefines.h"
 #include "BLI_kdtree.h"
 #include "BLI_rand.h"
 #include "BLI_math.h"
 #include "BLI_edgehash.h"
-#include "BLI_utildefines.h"
 
 #include "BKE_cdderivedmesh.h"
 #include "BKE_deform.h"




More information about the Bf-blender-cvs mailing list