aboutsummaryrefslogtreecommitdiffstats
path: root/src/sysdep.c
diff options
context:
space:
mode:
authorPaul Eggert <[email protected]>2011-04-28 01:18:53 -0700
committerPaul Eggert <[email protected]>2011-04-28 01:18:53 -0700
commitede49d7153ed628078bcbc2473f898904b5250ea (patch)
treeee55ad2109712fbc72489489490439ef6c31c039 /src/sysdep.c
parent2f30ecd05f7e5b9f78f256f75677530c501e5a6d (diff)
* sysdep.c (get_random): Don't assume EMACS_INT is no wider than long.
Also, don't assume VALBITS / RAND_BITS is less than 5, and don't rely on undefined behavior when shifting a 1 left into the sign bit. * lisp.h (get_random): Change signature to match.
Diffstat (limited to 'src/sysdep.c')
-rw-r--r--src/sysdep.c21
1 files changed, 6 insertions, 15 deletions
diff --git a/src/sysdep.c b/src/sysdep.c
index ca7de4f54b..43f50cdb0a 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -1760,23 +1760,14 @@ seed_random (long int arg)
* Build a full Emacs-sized word out of whatever we've got.
* This suffices even for a 64-bit architecture with a 15-bit rand.
*/
-long
+EMACS_INT
get_random (void)
{
- long val = random ();
-#if VALBITS > RAND_BITS
- val = (val << RAND_BITS) ^ random ();
-#if VALBITS > 2*RAND_BITS
- val = (val << RAND_BITS) ^ random ();
-#if VALBITS > 3*RAND_BITS
- val = (val << RAND_BITS) ^ random ();
-#if VALBITS > 4*RAND_BITS
- val = (val << RAND_BITS) ^ random ();
-#endif /* need at least 5 */
-#endif /* need at least 4 */
-#endif /* need at least 3 */
-#endif /* need at least 2 */
- return val & ((1L << VALBITS) - 1);
+ EMACS_UINT val = 0;
+ int i;
+ for (i = 0; i < (VALBITS + RAND_BITS - 1) / RAND_BITS; i++)
+ val = (val << RAND_BITS) ^ random ();
+ return val & (((EMACS_INT) 1 << VALBITS) - 1);
}
#ifndef HAVE_STRERROR