Uno wrote:
> Gib, can you post your entire fortran version?
OK, here's the Fortran. I've removed almost all the comments to save space.
If you uncomment the line in MWC():
c = c_this_works
you'll see how to reproduce the C code results.
Gib
module kiss_mod
implicit none
integer :: xs = 521288629
integer :: xcng = 362436069
integer :: Q(0:4690)
contains
integer function MWC()
integer :: m, x, i
integer, save :: c = 0, j = 4691
integer :: t, tLTx, c_this_works
if (j < 4690) then
j = j + 1
else
j = 0
endif
x = Q(j)
m = SHIFTL(x,13) + c
Q(j) = m + x
t = Q(j)
! This is the laborious determination of c
if ((t >= 0 .and. x >= 0) .or. (t < 0 .and. x < 0)) then
if (t < x) then
tLTx = 1
else
tLTx = 0
endif
elseif (x < 0) then
tLTx = 1
elseif (t < 0) then
tLTx = 0
endif
c_this_works = tLTx + SHIFTR(x,19)
! This c gives results inconsistent with the C code
if (sign(1,m) == sign(1,x)) then
c = sign(1,x) + SHIFTR(x,19)
else
c = 1 - sign(1,m) + SHIFTR(x,19)
endif
! c = c_this_works
MWC = Q(j)
end function
subroutine test_RNG4691
integer :: i, x, unsigned_result
integer :: n = 1000000000
do i = 0, 4690
xcng = 69069 * xcng + 123
xs = XOR(xs,SHIFTL(xs,13))
xs = XOR(xs,SHIFTR(xs,17))
xs = XOR(xs,SHIFTL(xs,5))
Q(i) = xcng + xs
enddo
do i = 1, n
x = MWC()
enddo
unsigned_result = 3740121002
write(*,*) "MWC result=3740121002 ?: ",unsigned_result,x
do i = 1,n
xcng = 69069 * xcng + 123
xs = XOR(xs,SHIFTL(xs,13))
xs = XOR(xs,SHIFTR(xs,17))
xs = XOR(xs,SHIFTL(xs,5))
x = MWC() + xcng + xs
enddo
unsigned_result = 2224631993
write(*,*) "KISS result=2224631993 ?: ",unsigned_result,x
end subroutine
end module
program main
use kiss_mod
call test_RNG4691
end program
|