[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gawk 3.1.6: rand.awk test case alignment problem
From: |
Duncan Moore |
Subject: |
gawk 3.1.6: rand.awk test case alignment problem |
Date: |
Mon, 11 Feb 2008 07:29:28 -0800 (PST) |
User-agent: |
G2/1.0 |
Hi,
Compiling gawk 3.1.6 on an ARM 710 platform and running the random
number test case gawk-3.1.6/test/rand.awk:
BEGIN {
srand(2)
for (i = 0; i < 19; i++)
printf "%3d ", (1 + int(100 * rand()))
print ""
}
with 'gawk -f rand.awk', gives:
30 5 39 81 93 31 71 45 82 43 99 97 100 52 86 91 68
65 60
As I understand it, this is wrong, and the output should be identical
on every platform, since the RNG is built in to gawk. This is what
should be output:
62 67 88 6 35 77 3 68 30 96 90 26 35 8 88 93 49
53 37
The reason for the error, is that char array 'state' in builtin.c is
not word aligned, and it is cast to an int*. This causes problems on
architectures that require ints to be word aligned. There is a comment
in random.c that this causes bus errors on the Sparc platform, but the
code does not appear to have been fixed. Other platforms could be
affected too.
A fix for this is:
diff -u -p gawk-3.1.6/builtin.c gawk-3.1.6.ro2/builtin.c
--- gawk-3.1.6/builtin.c 2007-09-30 19:57:05 +0000
+++ gawk-3.1.6.ro2/builtin.c 2008-01-21 19:38:12 +0000
@@ -2027,14 +2027,16 @@ do_cos(NODE *tree)
/* do_rand --- do the rand function */
static int firstrand = TRUE;
-static char state[256];
+#define SIZEOF_STATE 256
+static uint32_t istate[SIZEOF_STATE/sizeof(uint32_t)];
+static char *const state = (char *const) istate;
/* ARGSUSED */
NODE *
do_rand(NODE *tree ATTRIBUTE_UNUSED)
{
if (firstrand) {
- (void) initstate((unsigned) 1, state, sizeof state);
+ (void) initstate((unsigned) 1, state, SIZEOF_STATE);
/* don't need to srandom(1), initstate() does it for us. */
firstrand = FALSE;
setstate(state);
@@ -2057,7 +2059,7 @@ do_srand(NODE *tree)
long ret = save_seed; /* SVR4 awk srand returns previous seed */
if (firstrand) {
- (void) initstate((unsigned) 1, state, sizeof state);
+ (void) initstate((unsigned) 1, state, SIZEOF_STATE);
/* don't need to srandom(1), we're changing the seed below */
firstrand = FALSE;
(void) setstate(state);
Regards
Duncan Moore
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- gawk 3.1.6: rand.awk test case alignment problem,
Duncan Moore <=