Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: RNGs: A Super KISS
Replies: 14   Last Post: Feb 26, 2013 10:53 AM

 Messages: [ Previous | Next ]
 adrian dogsbody Posts: 110 Registered: 12/13/04
Re: A Super KISS
Posted: Nov 3, 2009 7:38 PM

"geo" <gmarsaglia@gmail.com> ha scritto nel messaggio
> #include <stdio.h>
> static unsigned long Q
> [41790],indx=41790,carry=362436,xcng=1236789,xs=521288629;
>
> #define CNG ( xcng=69609*xcng+123 ) /*Congruential*/
> #define XS ( xs^=xs<<13, xs^=(unsigned)xs>>17, xs^=xs>>5 ) /
> *Xorshift*/
> #define SUPR ( indx<41790 ? Q[indx++] : refill() )
> #define KISS SUPR+CNG+XS
>
> int refill( )
> { int i; unsigned long long t;
> for(i=0;i<41790;i++) { t=7010176LL*Q[i]+carry; carry=(t>>32); Q[i]=~
> (t);}
> indx=1; return (Q[0]);
> }
>
> int main()
> {unsigned long i,x;
> for(i=0;i<41790;i++) Q[i]=CNG+XS;
> for(i=0;i<1000000000;i++) x=KISS;
> printf(" x=%d.\nDoes x=-872412446?\n",x);
> }

the asm version

; compile with
; nasmw -fobj rndasm.asm
; bcc32 -v rndasm.obj
section _DATA use32 public class=DATA
global _main
extern _printf

indx dd 41790
carry dd 362436
xcng dd 1236789
xs dd 521288629
IIIIIIxIIdIIn db " x=%d." , 13, 10, 0
IDoesIxII872412446IIn db "Does x=-872412446?" , 13, 10, 0
section _BSS use32 public class=BSS

vettoreQ resd 41792

section _TEXT use32 public class=CODE

align 4
initialize:
push esi
push edi
push ebp
mov ecx, 41790
mov esi, vettoreQ
mov edi, [xcng]
mov ebp, [xs]
.0: mov eax, 69609
mul edi
xchg eax, edi
mov eax, ebp
shl eax, 13
xor ebp, eax
mov eax, ebp
shr eax, 17
xor ebp, eax
mov eax, ebp
shr eax, 5
xor ebp, eax
lea eax, [ebp+edi]
mov [esi], eax
loop .0
mov [xcng], edi
mov [xs], ebp
pop ebp
pop edi
pop esi
ret

align 4
rnd:
push esi
push edi
mov ecx, [indx]
mov esi, vettoreQ
cmp ecx, 41790
jne .1
push esi
.0: mov eax, 7010176
mul dword[esi]
mov [carry], edx
not eax
mov dword[esi], eax
loop .0
pop esi
.1: mov edi, [esi+4*ecx]
inc ecx
mov [indx], ecx
mov eax, 69609
mov ecx, [xcng]
mul ecx
mov [xcng], eax
mov edx, [xs]
mov eax, edx
shl eax, 13
xor edx, eax
mov eax, edx
shr eax, 17
xor edx, eax
mov eax, edx
shr eax, 5
xor edx, eax
mov [xs], edx
xchg edi, eax
pop edi
pop esi
ret

align 4
_main:
call initialize
mov esi, 0
.0: call rnd
inc esi
cmp esi, 1000000000
jb .0
mov edi, eax
push eax
push IIIIIIxIIdIIn
call _printf
push IDoesIxII872412446IIn
call _printf
xor eax, eax
ret

Date Subject Author
11/3/09 geo
11/3/09 Dann Corbit
11/3/09 tom@iahu.ca
11/3/09 Dann Corbit