From 918b0a410f43db03608caff9514aaf2650526da0 Mon Sep 17 00:00:00 2001 From: Cary R Date: Mon, 8 Dec 2008 18:38:20 -0800 Subject: [PATCH] V0.8: backport $random fix. Back port a $random fix from development. --- vpi/sys_random.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/vpi/sys_random.c b/vpi/sys_random.c index c859bef62..e4dc08660 100644 --- a/vpi/sys_random.c +++ b/vpi/sys_random.c @@ -79,7 +79,7 @@ long rtl_dist_uniform(long*seed, long start, long end) } else { - i = (unsigned long) (r-1); + i = - ( (unsigned long) (-(r - 1)) ); } if (i=end) i = end-1; @@ -94,7 +94,7 @@ long rtl_dist_uniform(long*seed, long start, long end) } else { - i = (unsigned long) (r-1); + i = - ( (unsigned long) (-(r - 1)) ); } if (i<=start) i = start+1; if (i>end) i = end; @@ -110,7 +110,13 @@ long rtl_dist_uniform(long*seed, long start, long end) } else { - i = (unsigned long) (r-1); + /* At least some compilers will notice that (r-1) + is <0 when castling to unsigned long and + replace the result with a zero. This causes + much wrongness, so do the casting to the + positive version and invert it back. */ + i = - ( (unsigned long) (-(r - 1)) ); + } }