[gegl] gegl_random: make algorithm be 32bit, for better gpu portability



commit aa35c61240ae17a2be85477dbe371090f7937f37
Author: Øyvind Kolås <pippin gimp org>
Date:   Sat Jul 20 16:29:17 2013 +0100

    gegl_random: make algorithm be 32bit, for better gpu portability

 gegl/gegl-random.c |   46 ++++++++++++++++++++++++++++++++++++++--------
 gegl/gegl.h        |   38 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 74 insertions(+), 10 deletions(-)
---
diff --git a/gegl/gegl-random.c b/gegl/gegl-random.c
index 11360ff..408547c 100644
--- a/gegl/gegl-random.c
+++ b/gegl/gegl-random.c
@@ -93,7 +93,7 @@ static long primes[]={
 
 #define RANDOM_DATA_SIZE (15083+15091+15101)
 
-static gint64   random_data[RANDOM_DATA_SIZE];
+static gint32   random_data[RANDOM_DATA_SIZE];
 static gboolean random_data_inited = FALSE;
 
 static inline void random_init (void)
@@ -105,14 +105,14 @@ static inline void random_init (void)
     GRand *gr = g_rand_new_with_seed (42);
     int i;
     for (i = 0; i < RANDOM_DATA_SIZE; i++)
-      random_data[i] = (((gint64) g_rand_int (gr)) << 32) + g_rand_int (gr);
+      random_data[i] = g_rand_int (gr);
     g_rand_free (gr);
     random_data_inited = TRUE;
   }
 }
 
 
-static inline guint64
+static inline guint32
 _gegl_random_int (int seed,
                  int x,
                  int y,
@@ -138,7 +138,7 @@ _gegl_random_int (int seed,
   return ret;
 }
 
-guint64
+guint32
 gegl_random_int (int seed,
                  int x,
                  int y,
@@ -148,7 +148,7 @@ gegl_random_int (int seed,
   return _gegl_random_int (seed, x, y, z, n);
 }
 
-gint64
+gint32
 gegl_random_int_range (int seed,
                        int x,
                        int y,
@@ -161,8 +161,7 @@ gegl_random_int_range (int seed,
   return (ret % (max-min)) + min;
 }
 
-/* transform [0..2^32] -> [0..1] */
-#define G_RAND_DOUBLE_TRANSFORM 2.3283064365386962890625e-10
+#define G_RAND_FLOAT_TRANSFORM  0.00001525902189669642175
 
 double
 gegl_random_double (int seed,
@@ -171,7 +170,17 @@ gegl_random_double (int seed,
                     int z,
                     int n)
 {
-  return (_gegl_random_int (seed, x, y, z, n) & 0xffffffff) * G_RAND_DOUBLE_TRANSFORM;
+  return gegl_random_float (seed, x, y, z, n);
+}
+
+float
+gegl_random_float (int seed,
+                   int x,
+                   int y,
+                   int z,
+                   int n)
+{
+  return (_gegl_random_int (seed, x, y, z, n) & 0xffff) * G_RAND_FLOAT_TRANSFORM;
 }
 
 double
@@ -185,3 +194,24 @@ gegl_random_double_range (int seed,
 {
   return gegl_random_double (seed, x, y, z, n) * (max - min) + min;
 }
+
+float
+gegl_random_float_range (int seed,
+                         int x,
+                         int y,
+                         int z,
+                         int n,
+                         float min,
+                         float max);
+
+float
+gegl_random_float_range (int seed,
+                         int x,
+                         int y,
+                         int z,
+                         int n,
+                         float min,
+                         float max)
+{
+  return gegl_random_float (seed, x, y, z, n) * (max - min) + min;
+}
diff --git a/gegl/gegl.h b/gegl/gegl.h
index dd7dc7a..ab2a7c6 100644
--- a/gegl/gegl.h
+++ b/gegl/gegl.h
@@ -999,6 +999,25 @@ GeglNode *gegl_graph (GeglNode *node);
  */
 double gegl_random_double_range (int seed, int x, int y, int z, int n, double min, double max);
 
+
+/**
+ * gegl_random_float_range:
+ * @seed: an integer seed, change for different permutation
+ * @x: x coordinate
+ * @y: y coordinate
+ * @z: z coordinate (mipmap level)
+ * @n: number no (each x,y coordinate provides its own sequence of
+ * numbers
+ * @min: minimum value
+ * @max: maxmimum value
+ *
+ * Return a random floating point number in the range specified,
+ * for the given x,y coordinates and seed provided, if multiple different
+ * numbers are needed pass in incrementing n's.
+ */
+float gegl_random_float_range (int seed, int x, int y, int z, int n, float min, float max);
+
+
 /**
  * gegl_random_int_range:
  * @seed: an integer seed, change for different permutation
@@ -1014,7 +1033,7 @@ double gegl_random_double_range (int seed, int x, int y, int z, int n, double mi
  * for the given x,y coordinates and seed provided, if multiple different
  * numbers are needed pass in incrementing n's.
  */
-gint64 gegl_random_int_range    (int seed, int x, int y, int z, int n, int min, int max);
+gint32 gegl_random_int_range    (int seed, int x, int y, int z, int n, int min, int max);
 
 /**
  * gegl_random_int:
@@ -1027,7 +1046,7 @@ gint64 gegl_random_int_range    (int seed, int x, int y, int z, int n, int min,
  *
  * Return a random integer number in range 0 .. MAX_UINT
  */
-guint64 gegl_random_int         (int seed, int x, int y, int z, int n);
+guint32 gegl_random_int         (int seed, int x, int y, int z, int n);
 
 /**
  * gegl_random_double:
@@ -1042,6 +1061,21 @@ guint64 gegl_random_int         (int seed, int x, int y, int z, int n);
  */
 double gegl_random_double       (int seed, int x, int y, int z, int n);
 
+
+
+/**
+ * gegl_random_float:
+ * @seed: an integer seed, change for different permutation
+ * @x: x coordinate
+ * @y: y coordinate
+ * @z: z coordinate (mipmap level)
+ * @n: number no (each x,y coordinate provides its own sequence of
+ * numbers
+ *
+ * Return a random floating point number in range 0.0 .. 1.0.
+ */
+float  gegl_random_float        (int seed, int x, int y, int z, int n);
+
 #define GEGL_ALIGNED __restrict__ __attribute__((__aligned__ (16)))
 
 G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]