guile-commits
[Top][All Lists]
Advanced

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

[Guile-commits] GNU Guile branch, branch_release-1-8, updated. release_1


From: Andy Wingo
Subject: [Guile-commits] GNU Guile branch, branch_release-1-8, updated. release_1-8-7-27-g7e6f4be
Date: Wed, 04 Aug 2010 18:10:25 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU Guile".

http://git.savannah.gnu.org/cgit/guile.git/commit/?id=7e6f4be53e56319a1b78d426e5b2a4815b022fc4

The branch, branch_release-1-8 has been updated
       via  7e6f4be53e56319a1b78d426e5b2a4815b022fc4 (commit)
      from  cfbccaf49ca248a1c6076a21c063e5b2e925dbea (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 7e6f4be53e56319a1b78d426e5b2a4815b022fc4
Author: Andy Wingo <address@hidden>
Date:   Wed Aug 4 20:13:04 2010 +0200

    Fix the range of `random' on 64-bit platforms
    
    For > 32 bit integers still in the fixnum range, scm_random() would
    return random numbers with a lower range than specified.
    
    * libguile/random.c (scm_i_mask32): New static inline function.
      (scm_c_random): Use `scm_i_mask32'.
      (scm_c_random64): New function, 64-bit variant of scm_c_random.
      (scm_random): Use `scm_c_random64' instead of forming the 64-bit random
      number in a bogus way.
    * libguile/random.h: Added `scm_c_random64'.
    
    Conflicts:
    
        libguile/random.c
        libguile/random.h

-----------------------------------------------------------------------

Summary of changes:
 libguile/random.c |   50 ++++++++++++++++++++++++++++++++++----------------
 libguile/random.h |    2 ++
 2 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/libguile/random.c b/libguile/random.c
index 1c2f668..693ed4a 100644
--- a/libguile/random.c
+++ b/libguile/random.c
@@ -178,22 +178,49 @@ scm_c_exp1 (scm_t_rstate *state)
 
 unsigned char scm_masktab[256];
 
-/* Returns 32 random bits. */
-unsigned long
-scm_c_random (scm_t_rstate *state, unsigned long m)
+static inline scm_t_uint32
+scm_i_mask32 (scm_t_uint32 m)
 {
-  scm_t_uint32 r, mask;
-  mask = (m < 0x100
+  return (m < 0x100
          ? scm_masktab[m]
          : (m < 0x10000
             ? scm_masktab[m >> 8] << 8 | 0xff
             : (m < 0x1000000
                ? scm_masktab[m >> 16] << 16 | 0xffff
                : scm_masktab[m >> 24] << 24 | 0xffffff)));
+}
+
+static scm_t_uint32
+scm_c_random32 (scm_t_rstate *state, scm_t_uint32 m)
+{
+  scm_t_uint32 r, mask = scm_i_mask32 (m);
   while ((r = scm_the_rng.random_bits (state) & mask) >= m);
   return r;
 }
 
+/* Returns 32 random bits. */
+unsigned long
+scm_c_random (scm_t_rstate *state, unsigned long m)
+{
+  return scm_c_random32 (state, (scm_t_uint32)m);
+}
+
+scm_t_uint64
+scm_c_random64 (scm_t_rstate *state, scm_t_uint64 m)
+{
+  scm_t_uint64 r;
+  scm_t_uint32 mask;
+
+  if (m <= SCM_T_UINT32_MAX)
+    return scm_c_random32 (state, (scm_t_uint32) m);
+  
+  mask = scm_i_mask32 (m >> 32);
+  while ((r = ((scm_t_uint64) (scm_the_rng.random_bits (state) & mask) << 32)
+          | scm_the_rng.random_bits (state)) >= m)
+    ;
+  return r;
+}
+
 /*
   SCM scm_c_random_bignum (scm_t_rstate *state, SCM m)
 
@@ -321,17 +348,8 @@ SCM_DEFINE (scm_random, "random", 1, 1, 0,
       return scm_from_uint32 (scm_c_random (SCM_RSTATE (state),
                                             (scm_t_uint32) m));
 #elif SCM_SIZEOF_UNSIGNED_LONG <= 8
-      if (m <= SCM_T_UINT32_MAX)
-        return scm_from_uint32 (scm_c_random (SCM_RSTATE (state),
-                                              (scm_t_uint32) m));
-      else
-        {
-          scm_t_uint64 upper, lower;
-          
-          upper = scm_c_random (SCM_RSTATE (state), (scm_t_uint32) (m >> 32));
-          lower = scm_c_random (SCM_RSTATE (state), SCM_T_UINT32_MAX);
-          return scm_from_uint64 ((upper << 32) | lower);
-        }
+      return scm_from_uint64 (scm_c_random64 (SCM_RSTATE (state),
+                                              (scm_t_uint64) m));
 #else
 #error "Cannot deal with this platform's unsigned long size"
 #endif
diff --git a/libguile/random.h b/libguile/random.h
index c16fa0e..0690b59 100644
--- a/libguile/random.h
+++ b/libguile/random.h
@@ -80,6 +80,8 @@ SCM_API double scm_c_normal01 (scm_t_rstate *);
 SCM_API double scm_c_exp1 (scm_t_rstate *);
 /* Though this returns an unsigned long, it's only 32 bits of randomness. */
 SCM_API unsigned long scm_c_random (scm_t_rstate *, unsigned long m);
+/* This one returns 64 bits of randomness. */
+SCM_API scm_t_uint64 scm_c_random64 (scm_t_rstate *state, scm_t_uint64 m);
 SCM_API SCM scm_c_random_bignum (scm_t_rstate *, SCM m);
 
 


hooks/post-receive
-- 
GNU Guile



reply via email to

[Prev in Thread] Current Thread [Next in Thread]