Skip to content

Commit 8804b13

Browse files
committed
auto merge of #6058 : huonw/rust/rt-isaac-update, r=graydon
The "unsigned 4 byte" `ub4`s are actually 8 bytes on 64-bit platforms which mean that some bits > 2**32 were retained in calculations, these would then "reappear" after a right shift and so the stream of random numbers would differ on 32 bit vs 64 bit platforms. http://burtleburtle.net/bob/c/randport.c
2 parents 7c1696b + 1fc8a2f commit 8804b13

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/rt/isaac/randport.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ rand.c: By Bob Jenkins. My random number generator, ISAAC. Public Domain
66
970719: use context, not global variables, for internal state
77
980324: make a portable version
88
010626: Note this is public domain
9+
100725: Mask on use of >32 bits, not on assignment: from Paul Eggert
910
------------------------------------------------------------------------------
1011
*/
1112
#ifndef STANDARD
@@ -27,37 +28,37 @@ rand.c: By Bob Jenkins. My random number generator, ISAAC. Public Domain
2728

2829
void isaac(randctx *ctx)
2930
{
30-
register ub4 a,b,x,y,*m,*mm,*m2,*r,*mend;
31+
ub4 a,b,x,y,*m,*mm,*m2,*r,*mend;
3132
mm=ctx->randmem; r=ctx->randrsl;
32-
a = ctx->randa; b = (ctx->randb + (++ctx->randc)) & 0xffffffff;
33+
a = ctx->randa; b = ctx->randb + (++ctx->randc);
3334
for (m = mm, mend = m2 = m+(RANDSIZ/2); m<mend; )
3435
{
3536
rngstep( a<<13, a, b, mm, m, m2, r, x);
36-
rngstep( a>>6 , a, b, mm, m, m2, r, x);
37+
rngstep( (a & 0xffffffff) >>6 , a, b, mm, m, m2, r, x);
3738
rngstep( a<<2 , a, b, mm, m, m2, r, x);
38-
rngstep( a>>16, a, b, mm, m, m2, r, x);
39+
rngstep( (a & 0xffffffff) >>16, a, b, mm, m, m2, r, x);
3940
}
4041
for (m2 = mm; m2<mend; )
4142
{
4243
rngstep( a<<13, a, b, mm, m, m2, r, x);
43-
rngstep( a>>6 , a, b, mm, m, m2, r, x);
44+
rngstep( (a & 0xffffffff) >>6 , a, b, mm, m, m2, r, x);
4445
rngstep( a<<2 , a, b, mm, m, m2, r, x);
45-
rngstep( a>>16, a, b, mm, m, m2, r, x);
46+
rngstep( (a & 0xffffffff) >>16, a, b, mm, m, m2, r, x);
4647
}
4748
ctx->randb = b; ctx->randa = a;
4849
}
4950

5051

5152
#define mix(a,b,c,d,e,f,g,h) \
5253
{ \
53-
a^=b<<11; d+=a; b+=c; \
54-
b^=c>>2; e+=b; c+=d; \
55-
c^=d<<8; f+=c; d+=e; \
56-
d^=e>>16; g+=d; e+=f; \
57-
e^=f<<10; h+=e; f+=g; \
58-
f^=g>>4; a+=f; g+=h; \
59-
g^=h<<8; b+=g; h+=a; \
60-
h^=a>>9; c+=h; a+=b; \
54+
a^=b<<11; d+=a; b+=c; \
55+
b^=(c&0xffffffff)>>2; e+=b; c+=d; \
56+
c^=d<<8; f+=c; d+=e; \
57+
d^=(e&0xffffffff)>>16; g+=d; e+=f; \
58+
e^=f<<10; h+=e; f+=g; \
59+
f^=(g&0xffffffff)>>4; a+=f; g+=h; \
60+
g^=h<<8; b+=g; h+=a; \
61+
h^=(a&0xffffffff)>>9; c+=h; a+=b; \
6162
}
6263

6364
/* if (flag==TRUE), then use the contents of randrsl[] to initialize mm[]. */
@@ -81,17 +82,21 @@ void randinit(randctx *ctx, word flag)
8182
/* initialize using the contents of r[] as the seed */
8283
for (i=0; i<RANDSIZ; i+=8)
8384
{
84-
a+=r[i ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3];
85-
e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7];
85+
a+=r[i ]; b+=r[i+1];
86+
c+=r[i+2]; d+=r[i+3];
87+
e+=r[i+4]; f+=r[i+5];
88+
g+=r[i+6]; h+=r[i+7];
8689
mix(a,b,c,d,e,f,g,h);
8790
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
8891
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
8992
}
9093
/* do a second pass to make all of the seed affect all of m */
9194
for (i=0; i<RANDSIZ; i+=8)
9295
{
93-
a+=m[i ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3];
94-
e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7];
96+
a+=m[i ]; b+=m[i+1];
97+
c+=m[i+2]; d+=m[i+3];
98+
e+=m[i+4]; f+=m[i+5];
99+
g+=m[i+6]; h+=m[i+7];
95100
mix(a,b,c,d,e,f,g,h);
96101
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
97102
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;

src/rt/isaac/standard.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,32 @@ Standard definitions and types, Bob Jenkins
1313
# include <stddef.h>
1414
# define STDDEF
1515
# endif
16-
typedef unsigned long long ub8;
16+
# ifndef STDINT
17+
# include <stdint.h>
18+
# define STDINT
19+
# endif
20+
21+
typedef uint64_t ub8;
1722
#define UB8MAXVAL 0xffffffffffffffffLL
1823
#define UB8BITS 64
19-
typedef signed long long sb8;
24+
typedef int64_t sb8;
2025
#define SB8MAXVAL 0x7fffffffffffffffLL
21-
typedef unsigned long int ub4; /* unsigned 4-byte quantities */
26+
typedef uint32_t ub4; /* unsigned 4-byte quantities */
2227
#define UB4MAXVAL 0xffffffff
23-
typedef signed long int sb4;
28+
typedef int32_t sb4;
2429
#define UB4BITS 32
2530
#define SB4MAXVAL 0x7fffffff
26-
typedef unsigned short int ub2;
31+
typedef uint16_t ub2;
2732
#define UB2MAXVAL 0xffff
2833
#define UB2BITS 16
29-
typedef signed short int sb2;
34+
typedef int16_t sb2;
3035
#define SB2MAXVAL 0x7fff
31-
typedef unsigned char ub1;
36+
typedef uint8_t ub1;
3237
#define UB1MAXVAL 0xff
3338
#define UB1BITS 8
34-
typedef signed char sb1; /* signed 1-byte quantities */
39+
typedef int8_t sb1; /* signed 1-byte quantities */
3540
#define SB1MAXVAL 0x7f
36-
typedef int word; /* fastest type available */
41+
typedef int word; /* fastest type available */
3742

3843
#define bis(target,mask) ((target) |= (mask))
3944
#define bic(target,mask) ((target) &= ~(mask))

0 commit comments

Comments
 (0)