random-state: use portable solution
authorDaniel Kochmański <daniel@turtleware.eu>
Tue, 22 Sep 2015 18:36:20 +0000 (20:36 +0200)
committerDaniel Kochmański <daniel@turtleware.eu>
Tue, 22 Sep 2015 18:36:20 +0000 (20:36 +0200)
This results in twice as big array as we could use if uint32_t was
granted with C99 - half of the bytes on 64 bit platforms are filled
with 0. Despite that it's clean and portable solution without
immediate imposing dependency on C99.

Signed-off-by: Daniel Kochmański <daniel@turtleware.eu>
src/c/hash.d
src/c/num_rand.d

index ed76513..3d34d74 100644 (file)
@@ -120,8 +120,8 @@ _hash_equal(int depth, cl_hashkey h, cl_object x)
         case t_random: {
                 cl_object array = x->random.value;
                 return hash_string
-                        (h, (unsigned char*)array->vector.self.b32,
-                         array->vector.fillp * sizeof(ecl_uint32_t));
+                        (h, (unsigned char*)array->vector.self.b8,
+                         array->vector.fillp);
         }
 #ifdef ECL_SIGNED_ZERO
         case t_singlefloat: {
index 93355af..ab13ea1 100644 (file)
@@ -45,16 +45,16 @@ n*/
 
 /* INV: for 64 bit implementation modify accordingly _hash_equal in
    hash.d file for case t_random */
-#define ulong ecl_uint32_t
+#define ulong unsigned long
 
 cl_object
 init_genrand(ulong seed)
 {
         cl_object array =
                 ecl_alloc_simple_vector
-                ((MT_N + 1),
-                 ecl_symbol_to_elttype(@'ext::byte32'));
-        ulong *mt = (ulong*)(array->vector.self.b32);
+                ((MT_N + 1) * sizeof(ulong),
+                 ecl_symbol_to_elttype(@'ext::byte8'));
+        ulong *mt = (ulong*)(array->vector.self.b8);
         int j = 0;
         mt[0] = seed & 0xffffffffUL;
         for (j=1; j < MT_N; j++)
@@ -92,7 +92,7 @@ generate_int32(cl_object state)
 {
         static ulong mag01[2]={0x0UL, MATRIX_A};
         ulong y;
-        ulong *mt = (ulong*)state->vector.self.b32;
+        ulong *mt = (ulong*)state->vector.self.b8;
         if (mt[MT_N] >= MT_N){
                 /* refresh data */
                 int kk;
@@ -223,8 +223,7 @@ ecl_make_random_state(cl_object rs)
                    with ecl_to_ulong_long from number.d, which takes
                    widest available type (32 or 64 bit)
                    automatically. */
-                z->random.value = init_genrand
-                        (ecl_to_uint32_t(rs));
+                z->random.value = init_genrand(ecl_fixnum(rs));
                 break;
         default: {
                 const char *type