-
Notifications
You must be signed in to change notification settings - Fork 6
/
sfmt32.asm
1265 lines (1071 loc) · 46.7 KB
/
sfmt32.asm
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; ----------------------------- SFMT32.ASM ---------------------------
; Author: Agner Fog
; Date created: 2008-11-01
; Last modified: 2013-09-13
; Source URL: www.agner.org/optimize
; Project: asmlib.zip
; Language: assembly, NASM/YASM syntax, 32 bit
; Description:
; Random number generator of type SIMD-oriented Fast Mersenne Twister (SFMT)
; (Mutsuo Saito and Makoto Matsumoto: "SIMD-oriented Fast Mersenne Twister:
; a 128-bit Pseudorandom Number Generator", Monte Carlo and Quasi-Monte
; Carlo Methods 2006, Springer, 2008, pp. 607-622).
;
; 32-bit mode version for x86 compatible microprocessors.
; Copyright (c) 2008-2013 GNU General Public License www.gnu.org/licenses
; ----------------------------------------------------------------------
; structure definition and constants:
%INCLUDE "randomah.asi"
global _SFMTRandomInit, _SFMTRandomInitByArray, _SFMTBRandom, _SFMTRandom
global _SFMTRandomL, _SFMTIRandom, _SFMTIRandomX, _SFMTgenRandomInit
global _SFMTgenRandomInitByArray, _SFMTgenRandom, _SFMTgenRandomL
global _SFMTgenIRandom, _SFMTgenIRandomX, _SFMTgenBRandom
%ifdef WINDOWS
global _SFMTgenRandomInitD@8, _SFMTgenRandomInitByArrayD@12, _SFMTgenRandomD@0
global _SFMTgenRandomLD@0, _SFMTgenIRandomD@8, _SFMTgenIRandomXD@8, _SFMTgenIRandomDX@8
global _SFMTgenBRandomD@0
%endif
extern _InstructionSet
section .data
align 16
; Data for single instance of random number generator
SFMTInstance: ISTRUC CRandomSFMTA
; Size of structure
IEND
SFMTSize equ $-SFMTInstance
align 16
; Initialization constants for Mother-Of-All:
InitMother DD 2111111111, 0, 1492, 0, 1776, 0, 5115, 0
; Initialization Mask for SFMT:
InitMask DD SFMT_MASK
; Period certification vector for SFMT:
InitParity DD SFMT_PARITY
SECTION .CODE align=16 ; code segment
; ---------------------------------------------------------------
; Thread-safe static link versions for SFMT
; ---------------------------------------------------------------
; extern "C" void SFMTRandomInit(void * Pthis, int ThisSize, int seed, int IncludeMother = 0);
; Parameters:
; [esp+4] = Pthis
; [esp+8] = ThisSize
; [esp+12] = seed
; [esp+16] = IncludeMother
_SFMTRandomInit:
mov ecx, [esp+4] ; Pthis
cmp dword [esp+8], SFMTSize
jb Error ; Error exit if buffer too small
push edi
; Align by 16. Will overlap part of Fill if Pthis unaligned
and ecx, -16
xor eax, eax
cmp dword [esp+16+4], eax ; IncludeMother
setnz al ; convert any nonzero value to 1
; Store USEMOTHER
mov [ecx+CRandomSFMTA.USEMOTHER], eax
mov eax, [esp+12+4] ; seed
xor edi, edi ; loop counter i
jmp L002 ; go into seeding loop
L001: ; seeding loop for SFMT
; y = factor * (y ^ (y >> 30)) + (++i);
call InitSubf0 ; randomization subfunction
L002: mov [ecx+edi*4+CRandomSFMTA.STATE],eax ; initialize state
cmp edi, SFMT_N*4 - 1
jb L001
; Put 5 more values into Mother-Of-All generator
call InitSubf0
mov [ecx+CRandomSFMTA.M0], eax
call InitSubf0
mov [ecx+CRandomSFMTA.M1], eax
call InitSubf0
mov [ecx+CRandomSFMTA.M2], eax
call InitSubf0
mov [ecx+CRandomSFMTA.M3], eax
call InitSubf0
mov [ecx+CRandomSFMTA.MC], eax
; more initialization and period certification
call InitAndPeriod
pop edi
ret
;_SFMTRandomInit ENDP
Error: ; Error exit
xor eax, eax
div eax ; Divide by 0
ret
; Subfunction used by _SFMTRandomInit
InitSubf0: ; private
; y = 1812433253 * (y ^ (y >> 30)) + (++i);
; input parameters:
; eax = y
; edi = i
; output:
; eax = new y
; edi = i+1
; edx modified
mov edx, eax
shr eax, 30
xor eax, edx
imul eax, 1812433253
inc edi
add eax, edi
ret
;InitSubf0 endp
; Subfunction used by _SFMTRandomInitByArray
InitSubf1: ; private
; r = 1664525U * (r ^ (r >> 27));
; input parameters:
; eax = r
; output:
; eax = new r
; edx modified
mov edx, eax
shr eax, 27
xor eax, edx
imul eax, 1664525
ret
;InitSubf1 endp
; Subfunction used by _SFMTRandomInitByArray
InitSubf2: ; private
; r = 1566083941U * (r ^ (r >> 27));
; input parameters:
; eax = r
; output:
; eax = new r
; edx modified
mov edx, eax
shr eax, 27
xor eax, edx
imul eax, 1566083941
ret
;InitSubf2 endp
; Subfunciton for initialization and period certification, except seeding
; ecx = aligned pointer to CRandomSFMTA
InitAndPeriod: ; private
push ebx
push edi
; initialize constants for Mother-Of-All and SFMT
LOADOFFSET2EDI InitMother ; edi points to InitMother
xor edx, edx
L101: ; Loop fills MF3 - MF0
mov eax, [edi+edx] ; load from InitMother
mov [ecx+edx+CRandomSFMTA.MF3], eax
add edx, 4
cmp edx, 32
jb L101
xor edx, edx
L102: ; Loop fills AMASK
mov eax, [edi+edx+32] ; load from InitMask
mov [ecx+edx+CRandomSFMTA.AMASK], eax
add edx, 4
cmp edx, 4*4
jb L102
; get instruction set
push ecx
call _InstructionSet
pop ecx
mov [ecx+CRandomSFMTA.Instset], eax
xor eax, eax
mov dword [ecx+CRandomSFMTA.one], eax
mov dword [ecx+4+CRandomSFMTA.one], 3FF00000H
; Period certification
; Compute parity of STATE[0-4] & InitParity
xor edx, edx ; parity
xor ebx, ebx ; loop counter
L104: mov eax, [ecx+ebx*4+CRandomSFMTA.STATE]
and eax, [edi+(InitParity-InitMother)+ebx*4] ; and InitParity[i]
xor edx, eax
inc ebx
cmp ebx, 4
jb L104
; get parity of edx
mov eax, edx
shr edx, 16
xor eax, edx
xor al, ah
jpo L108 ; parity odd: period OK
; parity even: period not OK
; Find a nonzero dword in period certification vector
xor ebx, ebx ; loop counter
L105: mov eax, [edi+(InitParity-InitMother)+ebx*4] ; InitParity[i]
test eax, eax
jnz L106
inc ebx
; assume that there is a nonzero dword in InitParity
jmp L105 ; loop until nonzero found
L106: ; find first nonzero bit in eax
bsf edx, eax
; flip the corresponding bit in STATE
btc [ecx+ebx*4+CRandomSFMTA.STATE], edx
L108: cmp dword [ecx+CRandomSFMTA.USEMOTHER], 0
je L109
call Mother_Next ; Make first random number ready
L109: ; Generate first random numbers and set IX = 0
call SFMT_Generate
pop edi
pop ebx
ret
;InitAndPeriod endp
; extern "C" void SFMTRandomInitByArray
; (void * Pthis, int ThisSize, int const seeds[], int NumSeeds, int IncludeMother = 0);
; // Seed by more than 32 bits
_SFMTRandomInitByArray:
; Parameters
; [esp+ 4] = Pthis
; [esp+ 8] = ThisSize
; [esp+12] = seeds
; [esp+16] = NumSeeds
; [esp+20] = IncludeMother
; define constants:
SFMT_SIZE equ SFMT_N*4 ; number of 32-bit integers in state
%IF SFMT_SIZE >= 623
SFMT_LAG equ 11
%ELIF SFMT_SIZE >= 68
SFMT_LAG equ 7
%ELIF SFMT_SIZE >= 39
SFMT_LAG equ 5
%ELSE
SFMT_LAG equ 3
%ENDIF
SFMT_MID equ ((SFMT_SIZE - SFMT_LAG) / 2)
push ebx
push esi
push edi
push ebp
cmp dword [esp+8+16], SFMTSize
jb Error ; Error exit if buffer too small
mov ecx, [esp+4+16] ; Pthis
mov ebx, [esp+12+16] ; seeds
mov ebp, [esp+16+16] ; NumSeeds
; Align by 16. Will overlap part of Fill if Pthis unaligned
and ecx, -16
xor eax, eax
cmp dword [esp+20+16], eax ; IncludeMother
setnz al ; convert any nonzero value to 1
; Store USEMOTHER
mov [ecx+CRandomSFMTA.USEMOTHER], eax
; 1. loop: Fill state vector with random numbers from NumSeeds
; r = NumSeeds;
; for (i = 0; i < SFMT_N*4; i++) {
; r = factor * (r ^ (r >> 30)) + i;
; sta[i] = r;}
mov eax, ebp ; r = NumSeeds
xor esi, esi ; i
L200: mov edx, eax
shr eax, 30
xor eax, edx
imul eax, 1812433253
add eax, esi
mov [ecx+esi*4+CRandomSFMTA.STATE], eax
inc esi
cmp esi, SFMT_SIZE
jb L200
; count = max(NumSeeds,size-1)
mov eax, SFMT_SIZE - 1
cmp ebp, eax
cmovb ebp, eax
push ebp ; save count as local variable
; 2. loop: Fill state vector with random numbers from seeds[]
; for (i = 1, j = 0; j < count; j++) {
; r = func1(sta[i] ^ sta[(i + mid) % size] ^ sta[(i + size - 1) % size]);
; sta[(i + mid) % size] += r;
; if (j < NumSeeds) r += seeds[j]
; r += i;
; sta[(i + mid + lag) % size] += r;
; sta[i] = r;
; i = (i + 1) % size;
; }
xor edi, edi
lea esi, [edi+1]
; ecx = Pthis
; ebx = seeds
; esi = i
; edi = j
; eax = r
; [esp] = count
; [esp+20+16] = NumSeeds
L201: ; r = sta[i] ^ sta[(i + mid) % size] ^ sta[(i + size - 1) % size];
mov eax, [ecx+esi*4+CRandomSFMTA.STATE] ; sta[i]
lea ebp, [esi+SFMT_MID]
cmp ebp, SFMT_SIZE
jb L202
sub ebp, SFMT_SIZE
L202: xor eax, [ecx+ebp*4+CRandomSFMTA.STATE] ; sta[(i + mid) % size]
lea edx, [esi+SFMT_SIZE-1]
cmp edx, SFMT_SIZE
jb L203
sub edx, SFMT_SIZE
L203: xor eax, [ecx+edx*4+CRandomSFMTA.STATE] ; sta[(i + size - 1) % size]
; r = func1(r) = (r ^ (r >> 27)) * 1664525U;
call InitSubf1
; sta[(i + mid) % size] += r;
add [ecx+ebp*4+CRandomSFMTA.STATE], eax
; if (j < NumSeeds) r += seeds[j]
cmp edi, [esp+20+16]
jnb L204
add eax, [ebx+edi*4]
L204:
; r += i;
add eax, esi
; sta[(i + mid + lag) % size] += r;
lea edx, [esi+SFMT_MID+SFMT_LAG]
cmp edx, SFMT_SIZE
jb L205
sub edx, SFMT_SIZE
L205: add [ecx+edx*4+CRandomSFMTA.STATE], eax
;sta[i] = r;
mov [ecx+esi*4+CRandomSFMTA.STATE], eax
; i = (i + 1) % size;
inc esi
cmp esi, SFMT_SIZE
jb L206
sub esi, SFMT_SIZE
L206:
; j++, loop while j < count
inc edi
cmp edi, [esp]
jb L201
; 3. loop: Randomize some more
; for (j = 0; j < size; j++) {
; r = func2(sta[i] + sta[(i + mid) % size] + sta[(i + size - 1) % size]);
; sta[(i + mid) % size] ^= r;
; r -= i;
; sta[(i + mid + lag) % size] ^= r;
; sta[i] = r;
; i = (i + 1) % size;
; }
; j = 0
xor edi, edi
L210: ; r = sta[i] + sta[(i + mid) % size] + sta[(i + size - 1) % size]
mov eax, [ecx+esi*4+CRandomSFMTA.STATE] ; sta[i]
lea ebp, [esi+SFMT_MID]
cmp ebp, SFMT_SIZE
jb L211
sub ebp, SFMT_SIZE
L211: add eax, [ecx+ebp*4+CRandomSFMTA.STATE] ; sta[(i + mid) % size]
lea edx, [esi+SFMT_SIZE-1]
cmp edx, SFMT_SIZE
jb L212
sub edx, SFMT_SIZE
L212: add eax, [ecx+edx*4+CRandomSFMTA.STATE] ; sta[(i + size - 1) % size]
; r = func2(r) = (x ^ (x >> 27)) * 1566083941U;
call InitSubf2
; sta[(i + mid) % size] ^= r;
xor [ecx+ebp*4+CRandomSFMTA.STATE], eax
; r -= i;
sub eax, esi
; sta[(i + mid + lag) % size] ^= r;
lea edx, [esi+SFMT_MID+SFMT_LAG]
cmp edx, SFMT_SIZE
jb L213
sub edx, SFMT_SIZE
L213: xor [ecx+edx*4+CRandomSFMTA.STATE], eax
; sta[i] = r;
mov [ecx+esi*4+CRandomSFMTA.STATE], eax
; i = (i + 1) % size;
inc esi
cmp esi, SFMT_SIZE
jb L214
sub esi, SFMT_SIZE
L214:
; j++, loop while j < size
inc edi
cmp edi, SFMT_SIZE
jb L210
pop ebp ; remove local variable count
; if (UseMother) {
cmp dword [ecx+CRandomSFMTA.USEMOTHER], 0
jz L220
; 4. loop: Initialize MotherState
; for (j = 0; j < 5; j++) {
; r = func2(r) + j;
; MotherState[j] = r + sta[2*j];
; }
call InitSubf2
mov edx, [ecx+CRandomSFMTA.STATE]
add edx, eax
mov [ecx+CRandomSFMTA.M0], edx
call InitSubf2
inc eax
mov edx, [ecx+8+CRandomSFMTA.STATE]
add edx, eax
mov [ecx+CRandomSFMTA.M1], edx
call InitSubf2
add eax, 2
mov edx, [ecx+16+CRandomSFMTA.STATE]
add edx, eax
mov [ecx+CRandomSFMTA.M2], edx
call InitSubf2
add eax, 3
mov edx, [ecx+24+CRandomSFMTA.STATE]
add edx, eax
mov [ecx+CRandomSFMTA.M3], edx
call InitSubf2
add eax, 4
mov edx, [ecx+32+CRandomSFMTA.STATE]
add edx, eax
mov [ecx+CRandomSFMTA.MC], edx
L220: ; More initialization and period certification
call InitAndPeriod
pop ebp
pop edi
pop esi
pop ebx
ret
;_SFMTRandomInitByArray ENDP
align 16
Mother_Next: ; private
; Internal procedure: advance Mother-Of-All generator
; The random value is in M0
; ecx = pointer to structure CRandomSFMTA
; eax, ecx, xmm0 unchanged
cmp dword [ecx+CRandomSFMTA.Instset], 4
jb Mother_Next_386
movdqa xmm1, oword [ecx+CRandomSFMTA.M3] ; load M3,M2
movdqa xmm2, oword [ecx+CRandomSFMTA.M1] ; load M1,M0
movhps qword [ecx+CRandomSFMTA.M3], xmm1 ; M3=M2
movq qword [ecx+CRandomSFMTA.M2], xmm2 ; M2=M1
movhps qword [ecx+CRandomSFMTA.M1], xmm2 ; M1=M0
pmuludq xmm1, oword [ecx+CRandomSFMTA.MF3] ; M3*MF3, M2*MF2
pmuludq xmm2, oword [ecx+CRandomSFMTA.MF1] ; M1*MF1, M0*MF0
paddq xmm1, xmm2 ; P3+P1, P2+P0
movhlps xmm2, xmm1 ; Get high qword
movq xmm3, qword [ecx+CRandomSFMTA.MC] ; +carry
paddq xmm1, xmm3
paddq xmm1, xmm2 ; P0+P1+P2+P3
movq qword [ecx+CRandomSFMTA.M0], xmm1 ; Store new M0 and carry
ret
Mother_Next_386: ; same, no SSE2
push eax
push esi
push edi
; prepare new random number
mov eax, [ecx+CRandomSFMTA.MF3]
mul dword [ecx+CRandomSFMTA.M3] ; x[n-4]
mov esi, eax
mov eax, [ecx+CRandomSFMTA.M2] ; x[n-3]
mov edi, edx
mov [ecx+CRandomSFMTA.M3], eax
mul dword [ecx+CRandomSFMTA.MF2]
add esi, eax
mov eax, [ecx+CRandomSFMTA.M1] ; x[n-2]
adc edi, edx
mov [ecx+CRandomSFMTA.M2], eax
mul dword [ecx+CRandomSFMTA.MF1]
add esi, eax
mov eax,[ecx+CRandomSFMTA.M0] ; x[n-1]
adc edi, edx
mov [ecx+CRandomSFMTA.M1], eax
mul dword [ecx+CRandomSFMTA.MF0]
add eax, esi
adc edx, edi
add eax, [ecx+CRandomSFMTA.MC]
adc edx, 0
; store next random number and carry
mov [ecx+CRandomSFMTA.M0], eax
mov [ecx+CRandomSFMTA.MC], edx
pop edi
pop esi
pop eax
ret
;Mother_Next endp
align 16
SFMT_Generate: ; private
; void CRandomSFMT::Generate() {
; Fill state array with new random numbers
; check if SSE2 instruction set supported
cmp dword [ecx+CRandomSFMTA.Instset], 4
jb SFMT_Generate_386
push ebx
; register use
; ecx = Pthis
; edx = i*16 + offset state
; eax, ebx = loop end
; xmm1 = r1
; xmm2 = r2 = r
; xmm0, xmm3 = scratch
; r1 = state[SFMT_N*16 - 2];
; r2 = state[SFMT_N*16 - 1];
movdqa xmm1, oword [ecx+(SFMT_N-2)*16+CRandomSFMTA.STATE]
movdqa xmm2, oword [ecx+(SFMT_N-1)*16+CRandomSFMTA.STATE]
mov edx, CRandomSFMTA.STATE
;static inline __m128i sfmt_recursion(__m128i const &a, __m128i const &b,
;__m128i const &c, __m128i const &d, __m128i const &mask) {
; __m128i a1, b1, c1, d1, z1, z2;
; b1 = _mm_srli_epi32(b, SFMT_SR1);
; a1 = _mm_slli_si128(a, SFMT_SL2);
; c1 = _mm_srli_si128(c, SFMT_SR2);
; d1 = _mm_slli_epi32(d, SFMT_SL1);
; b1 = _mm_and_si128(b1, mask);
; z1 = _mm_xor_si128(a, a1);
; z2 = _mm_xor_si128(b1, d1);
; z1 = _mm_xor_si128(z1, c1);
; z2 = _mm_xor_si128(z1, z2);
; return z2;}
; for (i = 0; i < SFMT_N - SFMT_M; i++) {
; r = sfmt_recursion(state[i], state[i + SFMT_M], r1, r2, mask);
; state[i] = r;
; r1 = r2;
; r2 = r;
; }
mov eax, (SFMT_N-SFMT_M)*16 + CRandomSFMTA.STATE ; first loop end
mov ebx, SFMT_N*16 + CRandomSFMTA.STATE ; second loop end
; first i loop from 0 to SFMT_N - SFMT_M
align 8
L301: movdqa xmm0, oword [ecx+edx+SFMT_M*16] ; b
psrld xmm0, SFMT_SR1 ; b1
pand xmm0, oword [ecx+CRandomSFMTA.AMASK] ; b1
movdqa xmm3, oword [ecx+edx] ; a
pxor xmm0, xmm3
pslldq xmm3, SFMT_SL2 ; a1
psrldq xmm1, SFMT_SR2 ; c1, c = r1
pxor xmm0, xmm3
pxor xmm0, xmm1
movdqa xmm1, xmm2 ; r1 = r2
pslld xmm2, SFMT_SL1 ; d1, d = r2
pxor xmm2, xmm0 ; r2 = r
; state[i] = r;
movdqa oword [ecx+edx], xmm2
; i++ while i < SFMT_N - SFMT_M
add edx, 16
cmp edx, eax
jb L301
;align 16
L302: ; second i loop from SFMT_N - SFMT_M + 1 to SFMT_N
movdqa xmm0, oword [ecx+edx+(SFMT_M-SFMT_N)*16] ; b
psrld xmm0, SFMT_SR1 ; b1
pand xmm0, oword [ecx+CRandomSFMTA.AMASK ] ; b1
movdqa xmm3, oword [ecx+edx] ; a
pxor xmm0, xmm3
pslldq xmm3, SFMT_SL2 ; a1
psrldq xmm1, SFMT_SR2 ; c1, c = r1
pxor xmm0, xmm3
pxor xmm0, xmm1
movdqa xmm1, xmm2 ; r1 = r2
pslld xmm2, SFMT_SL1 ; d1, d = r2
pxor xmm2, xmm0 ; r2 = r
; state[i] = r;
movdqa oword [ecx+edx], xmm2
; i++ while i < SFMT_N
add edx, 16
cmp edx, ebx
jb L302
; Check if initialized
L308: cmp dword [ecx+CRandomSFMTA.AMASK], SFMT_MASK1
jne Error ; Make error if not initialized
; ix = 0;
mov dword [ecx+CRandomSFMTA.IX], 0 ; point to start of STATE buffer
pop ebx
ret
; Same, SSE2 instruction set not supported:
SFMT_Generate_386:
push ebx
push esi
push edi
push ebp
sub esp, 32
; register use
; ecx = Pthis
; edx = i*16
; ebx = ((i+SFMT_M) mod SFMT_N) * 16
; ebp = accumulator
; eax = temporary
; esi, edi = previous state[i]
%define RR1 esp ; r1
%define RR2 esp+16 ; r2 = r
; r1 = state[SFMT_N - 2];
; r2 = state[SFMT_N - 1];
lea esi, [ecx+(SFMT_N-2)*16+CRandomSFMTA.STATE]
mov edi, esp
push ecx
mov ecx, 8
rep movsd
pop ecx
; The two loops from i = 0 to SFMT_N - SFMT_M - 1 and
; from SFMT_N - SFMT_M to SFMT_N - 1 are joined together here:
; for (i = 0; i < SFMT_N; i++) {
; r = sfmt_recursion(state[i], state[(i+SFMT_M)%SFMT_N], r1, r2, mask);
; state[i] = r;
; r1 = r2;
; r2 = r;
xor edx, edx ; i = 0
mov ebx, SFMT_M * 16 ; j = ((i+SFMT_M)%SFMT_N)*16
M1: ; loop start
; 1. dword:
mov ebp, [ecx+ebx+CRandomSFMTA.STATE+0]
shr ebp, SFMT_SR1 ; 32-bit shifts right
and ebp, [ecx+CRandomSFMTA.AMASK+0]
mov eax, [ecx+edx+CRandomSFMTA.STATE+0]
xor ebp, eax
mov esi, eax ; save for 2. dword
shl eax, SFMT_SL2*8 ; 128-bit shift left
xor ebp, eax
mov eax, [RR1+0]
mov edi, [RR1+4]
shrd eax, edi, SFMT_SR2*8 ; 128-bit shift right
xor ebp, eax
mov eax, [RR2+0]
mov [RR1+0], eax ; r1 = r2
shl eax, SFMT_SL1 ; 32-bit shifts left
xor ebp, eax
mov [RR2+0], ebp ; r2 = r
mov [ecx+edx+CRandomSFMTA.STATE+0], ebp ; state[i] = r
; 2. dword:
mov ebp, [ecx+ebx+CRandomSFMTA.STATE+4]
shr ebp, SFMT_SR1 ; 32-bit shifts right
and ebp, [ecx+CRandomSFMTA.AMASK+4]
mov eax, [ecx+edx+CRandomSFMTA.STATE+4]
xor ebp, eax
mov edi, eax ; save for 3. dword
; esi = [ecx+edx].STATE[0] before change
shld eax, esi, SFMT_SL2*8 ; 128-bit shift left
xor ebp, eax
mov eax, [RR1+4]
mov esi, [RR1+8]
shrd eax, esi, SFMT_SR2*8 ; 128-bit shift right
xor ebp, eax
mov eax, [RR2+4]
mov [RR1+4], eax ; r1 = r2
shl eax, SFMT_SL1 ; 32-bit shifts left
xor ebp, eax
mov [RR2+4], ebp ; r2 = r
mov [ecx+edx+CRandomSFMTA.STATE+4], ebp ; state[i] = r
; 3. dword:
mov ebp, [ecx+ebx+CRandomSFMTA.STATE+8]
shr ebp, SFMT_SR1 ; 32-bit shifts right
and ebp, [ecx+CRandomSFMTA.AMASK+8]
mov eax, [ecx+edx+CRandomSFMTA.STATE+8]
mov esi, eax ; save for 4. dword
xor ebp, eax
; edi = [ecx+edx+CRandomSFMTA.STATE+4] before change
shld eax, edi, SFMT_SL2*8 ; 128-bit shift left
xor ebp, eax
mov eax, [RR1+8]
mov edi, [RR1+12]
shrd eax, edi, SFMT_SR2*8 ; 128-bit shift right
xor ebp, eax
mov eax, [RR2+8]
mov [RR1+8], eax ; r1 = r2
shl eax, SFMT_SL1 ; 32-bit shifts left
xor ebp, eax
mov [RR2+8], ebp ; r2 = r
mov [ecx+edx+CRandomSFMTA.STATE+8], ebp ; state[i] = r
; 4. dword:
mov ebp, [ecx+ebx+CRandomSFMTA.STATE+12]
shr ebp, SFMT_SR1 ; 32-bit shifts right
and ebp, [ecx+CRandomSFMTA.AMASK+12]
mov eax, [ecx+edx+CRandomSFMTA.STATE+12]
xor ebp, eax
; esi = [ecx+edx+CRandomSFMTA.STATE+8] before change
shld eax, esi, SFMT_SL2*8 ; 128-bit shift left
xor ebp, eax
mov eax, [RR1+12]
shr eax, SFMT_SR2*8 ; 128-bit shift right
xor ebp, eax
mov eax, [RR2+12]
mov [RR1+12], eax ; r1 = r2
shl eax, SFMT_SL1 ; 32-bit shifts left
xor ebp, eax
mov [RR2+12], ebp ; r2 = r
mov [ecx+edx+CRandomSFMTA.STATE+12], ebp ; state[i] = r
; increment i, j
add ebx, 16
cmp ebx, SFMT_N*16
jb M4
sub ebx, SFMT_N*16 ; modulo SFMT_N
M4: add edx, 16
cmp edx, SFMT_N*16
jb M1
; free r1, r2 from stack
add esp, 32
pop ebp
pop edi
pop esi
; pop ebx
jmp L308
;SFMT_Generate endp
; extern "C" unsigned int SFMTBRandom(void * Pthis); // Output random bits
_SFMTBRandom: ; generate random bits
mov ecx, [esp+4] ; Pthis
; Align by 16. Will overlap part of Fill1 if Pthis unaligned
and ecx, -16
SFMTBRandom_reg: ; Entry for register parameters, used internally
; if (ix >= SFMT_N*4) Generate();
mov edx, [ecx+CRandomSFMTA.IX]
cmp edx, SFMT_N*16
jnb NeedGenerate
; y = ((uint32_t*)state)[ix++];
mov eax, dword [ecx+edx+CRandomSFMTA.STATE]
add edx, 4
mov [ecx+CRandomSFMTA.IX], edx
AfterGenerate:
; if (UseMother) y += MotherBits();
cmp dword [ecx+CRandomSFMTA.USEMOTHER], 0
jz NoMother
; add mother bits
add eax, [ecx+CRandomSFMTA.M0] ; Add Mother random number
call Mother_Next ; Make next Mother random number ready
NoMother: ; return y;
ret
NeedGenerate:
call SFMT_Generate ; generate SFMT_N*4 random dwords
mov eax, [ecx+CRandomSFMTA.STATE]
mov dword [ecx+CRandomSFMTA.IX], 4
jmp AfterGenerate
;_SFMTBRandom ENDP
; extern "C" double SFMTRandom (void * Pthis); // Output random float
_SFMTRandom: ; generate random float with 52 bits resolution
mov ecx, [esp+4] ; Pthis
; Align by 16. Will overlap part of Fill1 if Pthis unaligned
and ecx, -16
SFMTRandom_reg: ; internal entry point
; check if there are at least 64 random bits in state buffer
; if (ix >= SFMT_N*4-1) Generate();
mov edx, [ecx+CRandomSFMTA.IX]
cmp edx, SFMT_N*16-4
jnb L403
; check instruction set
L401: cmp dword [ecx+CRandomSFMTA.Instset], 4
jb L404
; read 64 random bits
movq xmm0, qword [ecx+edx+CRandomSFMTA.STATE]
add edx, 8
mov [ecx+CRandomSFMTA.IX], edx
; combine with Mother-Of-All generator?
cmp dword [ecx+CRandomSFMTA.USEMOTHER], 0
jz L402
; add mother bits
movq xmm1, qword [ecx+CRandomSFMTA.M0] ; Mother random number MC and M0
pshuflw xmm1, xmm1, 01001011B ; Put M0 before MC, and swap the words in MC
paddq xmm0, xmm1 ; Add SFMT and Mother outputs
call Mother_Next ; Make next Mother random number ready
L402: ; ConvertToFloat
psrlq xmm0, 12 ; align with mantissa field of double precision float
movsd xmm1, [ecx+CRandomSFMTA.one] ; 1.0 double precision
por xmm0, xmm1 ; insert exponent to get 1.0 <= x < 2.0
subsd xmm0, xmm1 ; subtract 1.0 to get 0.0 <= x < 1.0
movsd [ecx+CRandomSFMTA.TempRan], xmm0
fld qword [ecx+CRandomSFMTA.TempRan] ; transfer to st(0) register
ret ; return value
L403: ;NeedGenerateR
call SFMT_Generate ; generate SFMT_N*4 random dwords
xor edx, edx
jmp L401
L404: ;NoSSE2 ; Use old 386 instruction set:
push ebx
; read 64 random bits
mov eax, [ecx+edx+CRandomSFMTA.STATE]
mov ebx, [ecx+edx+4+CRandomSFMTA.STATE]
add edx, 8
mov [ecx+CRandomSFMTA.IX], edx
; combine with Mother-Of-All generator?
cmp dword [ecx+CRandomSFMTA.USEMOTHER], 0
jz L405
; add mother bits
mov edx, [ecx+CRandomSFMTA.MC] ; Mother random number MC
ror edx, 16 ; rotate
add eax, edx ; 64 bit add
adc ebx, [ecx+CRandomSFMTA.M0] ; Mother random number M0
call Mother_Next ; next Mother. eax, ebx unchanged
L405: ;ToFloatNoSSE2
shrd eax, ebx, 12 ; align with mantissa field of double precision float
shr ebx, 12
or ebx, 3FF00000H ; insert exponent to get 1.0 <= x < 2.0
mov dword [ecx+CRandomSFMTA.TempRan], eax
mov dword [ecx+4+CRandomSFMTA.TempRan], ebx
fld qword [ecx+CRandomSFMTA.TempRan] ; transfer to st(0) register
fsub qword [ecx+CRandomSFMTA.one] ; subtract 1.0 to get 0.0 <= x < 1.0
pop ebx
ret ; return value
;_SFMTRandom ENDP
; extern "C" long double SFMTRandomL (void * Pthis);
_SFMTRandomL: ; generate random float with 63 bits resolution
mov ecx, [esp+4] ; Pthis
; Align by 16. Will overlap part of Fill1 if Pthis unaligned
and ecx, -16
SFMTRandomL_reg: ; internal entry point
; check if there are at least 64 random bits in state buffer
; if (ix >= SFMT_N*4-1) Generate();
mov edx, [ecx+CRandomSFMTA.IX]
cmp edx, SFMT_N*16-4
jnb L505
; check instruction set
L501: cmp dword [ecx+CRandomSFMTA.Instset], 4
jb L506
; read 64 random bits
movq xmm0, qword [ecx+edx+CRandomSFMTA.STATE]
add edx, 8
mov [ecx+CRandomSFMTA.IX], edx
; combine with Mother-Of-All generator?
cmp dword [ecx+CRandomSFMTA.USEMOTHER], 0
jz L502
; add mother bits
movq xmm1, qword [ecx+CRandomSFMTA.M0] ; Mother random number MC and M0
pshuflw xmm1, xmm1, 01001011B ; Put M0 before MC, and swap the words in MC
paddq xmm0, xmm1 ; Add SFMT and Mother outputs
call Mother_Next ; Make next Mother random number ready
L502: ;ConvertToFloat
sub esp, 16 ; make space for long double
psrlq xmm0, 1 ; align with mantissa field of long double
pcmpeqw xmm1, xmm1 ; all 1's
psllq xmm1, 63 ; create a 1 in bit 63
por xmm0, xmm1 ; bit 63 is always 1 in long double
movq qword [esp], xmm0 ; store mantissa
L504: mov dword [esp+8], 3FFFH ; exponent
fld tword [esp] ; load long double
fsub qword [ecx+CRandomSFMTA.one] ; subtract 1.0 to get 0.0 <= x < 1.0
add esp, 16
ret ; return value
L505: ; NeedGenerateR
call SFMT_Generate ; generate SFMT_N*4 random dwords
xor edx, edx
jmp L501
L506: ;NoSSE2 ; Use old 386 instruction set:
push ebx
; read 64 random bits
mov eax, [ecx+edx+CRandomSFMTA.STATE]
mov ebx, [ecx+edx+4+CRandomSFMTA.STATE]
add edx, 8
mov [ecx+CRandomSFMTA.IX], edx
; combine with Mother-Of-All generator?
cmp dword [ecx+CRandomSFMTA.USEMOTHER], 0
jz L507
; add mother bits
mov edx, [ecx+CRandomSFMTA.MC] ; Mother random number MC
ror edx, 16 ; rotate
add eax, edx ; 64 bit add
adc ebx, [ecx+CRandomSFMTA.M0] ; Mother random number M0
call Mother_Next ; next Mother. eax, ebx unchanged
L507: ;ToFloatNoSSE2
mov edx, ebx ; now random bits are in edx:eax
pop ebx ; clean stack
sub esp, 16 ; make room for long double
shrd eax, edx, 1 ; align with mantissa field of long double
stc
rcr edx, 1 ; bit 63 is always 1
mov [esp], eax
mov [esp+4], edx
jmp L504 ; the rest is the same as above
;_SFMTRandomL ENDP
; extern "C" int SFMTIRandom (void * Pthis, int min, int max); // Output random integer
_SFMTIRandom:
mov ecx, [esp+4] ; Pthis
; Align by 16. Will overlap part of Fill if Pthis unaligned
and ecx, -16
call SFMTBRandom_reg ; random bits
mov edx, [esp+12] ; max
mov ecx, [esp+8] ; min