Debian Codegraph @

gcc/testsuite/gfortran.dg/unsigned_kiss.f90

raw
1 !
2 ! { dg-do run }
3 ! { dg-options "-funsigned" }
4 !
5 ! Modern Fortran rewrite of Marsaglia's 64-bit KISS PRNG.
6 ! https://www.thecodingforums.com/threads/64-bit-kiss-rngs.673657/
7 !
8 module kissm
9
10 implicit none
11 private
12 public uk, kseed, kiss
13
14 integer, parameter :: uk = kind(1u_8) ! Check kind() works.
15
16 ! Default seeds. Checks unsigned with parameter attribute.
17 unsigned(uk), parameter :: seed(4) = [ &
18 & 1234567890987654321u_uk, 362436362436362436u_uk, &
19 & 1066149217761810u_uk, 123456123456123456u_uk ]
20
21 ! Seeds used during generation
22 unsigned(uk), save :: sd(4) = seed
23
24 contains
25
26 ! Tests unsigned in an internal function.
27 function s(x)
28 unsigned(uk) s
29 unsigned(uk), intent(in) :: x
30 s = ishft(x, -63) ! Tests ishft
31 end function
32
33 ! Poor seeding routine. Need to check v for entropy!
34 ! Tests intent(in) and optional attributes.
35 ! Tests ishftc() and array constructors.
36 subroutine kseed(v)
37 unsigned(uk), intent(in), optional :: v
38 if (present(v)) then
39 sd = seed + [ishftc(v,1), ishftc(v,15), ishftc(v,31), ishftc(v,44)]
40 else
41 sd = seed
42 end if
43 end subroutine kseed
44
45 function kiss()
46 unsigned(uk) kiss
47 unsigned(uk) m, t
48 integer k
49
50 ! Test unsigned in a statement function
51 m(t, k) = ieor(t, ishft(t, k))
52
53 t = ishft(sd(1), 58) + sd(4)
54 if (s(sd(1)) == s(t)) then
55 sd(4) = ishft(sd(1), -6) + s(sd(1))
56 else
57 sd(4) = ishft(sd(1), -6) + 1u_uk - s(sd(1) + t)
58 endif
59
60 sd(1) = t + sd(1)
61 sd(2) = m(m(m(sd(2), 13), -17), 43)
62 sd(3) = 6906969069u_uk * sd(3) + 1234567u_uk
63 kiss = sd(1) + sd(2) + sd(3)
64 end function kiss
65
66 end module kissm
67
68 program testkiss
69 use kissm
70 integer, parameter :: n = 4
71 unsigned(uk) prn(4)
72
73 ! Default sequence
74 unsigned(uk), parameter :: a(4) = [8932985056925012148u_uk, &
75 & 5710300428094272059u_uk, 18342510866933518593u_uk, &
76 & 14303636270573868250u_uk]
77
78 ! Sequence with the seed 123412341234u_uk
79 unsigned(uk), parameter :: b(4) = [4002508872477953753u_uk, &
80 & 18025327658415290923u_uk, 16058856976144281263u_uk, &
81 & 11842224026193909403u_uk]
82
83 do i = 1, n
84 prn(i) = kiss()
85 end do
86 if (any(prn /= a)) stop 1
87
88 call kseed(123412341234u_uk)
89 do i = 1, n
90 prn(i) = kiss()
91 end do
92 if (any(prn /= b)) stop 2
93
94 call kseed()
95 do i = 1, n
96 prn(i) = kiss()
97 end do
98 if (any(prn /= a)) stop 3
99
100 end program testkiss