-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLFIB4.vim
117 lines (111 loc) · 5.64 KB
/
LFIB4.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
" http://www.cse.yorku.ca/~oz/marsaglia-rng.html
if 0x7fffffff <= 0 || 0x7fffffff+1 >= 0
echoerr 'TODO check full period if 64 bits'
endif
" #define UC (unsigned char) /*a cast operation*/
" #define LFIB4 (c++,t[c]=t[c]+t[UC(c+58)]+t[UC(c+119)]+t[UC(c+178)])
" The cast is needed because in C 1 + $uint8 does integer arithmetic,
" so the macro without the cast would overflow 256 elements.
" Using '+' rather than '-' is just a perversion.
" Mostly interesting because of speed -- it's 3x slower than xkcd221
let s:t = range(256)
let s:c = 0
function! LFIB4()
let s:c = (s:c + 1) % 256
let t = s:t
let c = s:c
let t[c] = t[c] + t[(c+58)%256] + t[(c+119)%256] + t[(c+178)%256]
return t[c]
endfunction
" At least an element must be odd
function! LFIB4_srand(seed)
let x = a:seed
for i in range(256)
let s:t[i] = x
let x = x * 69069 + 1234567
endfor
endfunction
call LFIB4_srand(localtime())
let s:t[42] = getpid()
if len($DEBUG) > 0
" the test code requires porting KISS or copying the initialized array...
let s:t = [
\ 2406566837, 3945488823, 3217501373, 1294085848,
\ 1685356791, 1879998683, 354155033, 1162810413,
\ 2655137063, 226356688, 3472744942, 543082922,
\ 3762916352, 1247145318, 1960484516, 2834937135,
\ 3556413474, 4015588494, 2433866612, 1542681415,
\ 837890377, 2156031093, 641728713, 2071218145,
\ 3128597539, 2092573851, 1548155322, 3718870319,
\ 4167822943, 2912646175, 2911643863, 3362985712,
\ 1886528418, 592629127, 1204737837, 1592327664,
\ 3126732962, 1554786, 1906632076, 2492476552,
\ 111002753, 4108454543, 3793230690, 4226138624,
\ 422644314, 2797766512, 847647967, 1289123979,
\ 1969535464, 1902370438, 974706952, 4222545502,
\ 1130793588, 3094143516, 86711091, 1749721364,
\ 278301801, 2154688974, 2866252662, 1487174760,
\ 119646572, 3549236133, 302009453, 162239274,
\ 1776703449, 1318650385, 3594113676, 4020467310,
\ 941062378, 1823539007, 3848290724, 1275281653,
\ 4013456607, 2787872658, 770103194, 2457950900,
\ 3336084503, 657199110, 1723679164, 798896722,
\ 1705823163, 1383115390, 2041707688, 3804770597,
\ 1136092689, 2827291170, 1227628224, 221271644,
\ 1977255824, 1852155101, 2301247803, 3595646145,
\ 3709922751, 2732837192, 529978376, 1840394256,
\ 850198089, 2335427956, 3449558694, 3023230479,
\ 868614440, 1099611446, 3334890752, 4190589169,
\ 3993307947, 3680421264, 3143497825, 930612474,
\ 3805940850, 2814955303, 2609714583, 3412337804,
\ 1947870458, 1173915461, 1573012014, 927925469,
\ 1957387227, 2823082061, 3013874876, 1144690729,
\ 321153706, 3505341913, 2118050097, 1431882057,
\ 3398744687, 2232621844, 3712718609, 2146053627,
\ 159722353, 3930544785, 1289237494, 3622337738,
\ 132620258, 3395020004, 3971404583, 2672497632,
\ 418556265, 375722238, 4083798982, 3832998408,
\ 4013463868, 1778758890, 3900234414, 3865137622,
\ 7986325, 3745270548, 1264572959, 1918227621,
\ 2293215037, 4013539679, 1728047317, 1329424085,
\ 3875557031, 3025492221, 424037964, 3308379748,
\ 463014440, 2869036490, 3540996349, 378750900,
\ 1776914604, 2246544134, 1500424097, 1031318071,
\ 653625805, 3003062951, 950363772, 1878098560,
\ 1705309242, 3066444961, 2718370657, 2326258461,
\ 3630708903, 987445312, 187170730, 958728662,
\ 4026066647, 4126946950, 1809706436, 2404651296,
\ 2208247683, 3582265286, 2071240021, 2116044308,
\ 2716786358, 2335209520, 2975196056, 245850675,
\ 2274606232, 801725457, 4131433647, 291882028,
\ 173368002, 211292240, 1606934101, 86222388,
\ 2744096089, 2594669165, 2583936776, 267153027,
\ 1270120814, 3848285866, 3106929809, 3457673136,
\ 4059379692, 2694564428, 3420145872, 4000069744,
\ 186853964, 1294043757, 445382178, 1508441456,
\ 975087012, 1239667053, 827019081, 2586195652,
\ 2163669318, 1252742899, 221573984, 9342999,
\ 3375791150, 813492546, 881353819, 1507495170,
\ 4208776213, 3297654913, 3373356384, 2245647143,
\ 3413689312, 2468442767, 3233866900, 2119481006,
\ 3323181420, 275426611, 267195229, 3023950404,
\ 2970782157, 3409642083, 1313209854, 3473951044,
\ 3633718954, 3189450566, 1981092696, 708163582,
\ 488758822, 2882757362, 3437433893, 1453523484,
\ 3414277213, 1339141620, 3946001167, 103943150,
\ 1689000206, 2735316516, 916302334, 823403292,
\ ]
if len(s:t) != 256
throw 'ops'
endif
echomsg 'testing should take less than a minute on any 21st century PC...'
" The original code prints a zero for each generator:
"for(i=1;i<1000001;i++){k=LFIB4;} printf("%u\n", k-1064612766U);
for i in range(1000001-1)
let k = LFIB4()
endfor
if k != 1064612766
throw printf("k = %d // should be %s", k, 1099611446)
endif
echomsg 'tested ok'
endif