forked from farbrausch/fr_public
-
Notifications
You must be signed in to change notification settings - Fork 2
/
model_asm.asm
800 lines (664 loc) · 15 KB
/
model_asm.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
; kkrunch_p7 model (asm version)
; fabian "ryg/farbrausch" giesen 2006
; I hereby place this code in the public domain.
; ---- uninitialized data
bits 32
section .bss
%define MEMSHIFT 23
%define MEM (1<<MEMSHIFT)
%define NMODEL 11
%define NINPUT 48
%define NWEIGHT (256+256+16+128)
%define MAXLEN 2047
%define USEAPM 1
%define APMSIZE 8192
struc ContextModel
.cpr resd 1
.cps resd 1
.ctx resd 1
.st resd 1
.size:
endstruc
struc Work
.c0 resd 1
.c0s resd 1
.bpos resd 1
.lentemp resd 1
.matchp resd 1
.matchl resd 1
.matchw resd 1
.pr resd 4
.ctx resd 3
.bit resd 1
.bitscaled resd 1
%if USEAPM
.APMi resd 1
%endif
.cm resb ContextModel.size * NMODEL
.match resd MEM/16
.modelMem resb MEM
.tx resw NINPUT
.wx resw NINPUT*NWEIGHT
.tx2 resw 4
.wx2 resw 8
.runTable resd 256
.stateCode resb 256*2 ; must follow immediately after runTable
.stateNext resb 256*2
.stateMap resd 256
.stretch resd 4096
%if USEAPM
.APM resd APMSIZE*33
%endif
.size:
endstruc
WorkData resb Work.size
; ---- code
section .text
; squash
; in: eax
; out: eax
; destroy: ecx,edx
squash:
cmp eax, -2047
jge .notsmall
xor eax, eax
ret
.notsmall:
cmp eax, 2047
jle .notbig
mov eax, 4095
ret
.notbig:
mov ecx, eax
sar ecx, 7
lea ecx, [squashTab+ecx*2+16*2]
and eax, byte 127
mov edx, [ecx+2]
sub edx, [ecx]
movzx edx, dx
imul eax, edx
add eax, byte 64
sar eax, 7
add ax, [ecx]
ret
; contextHash
; in: esi=model
; eax=i
; out: eax=p
; destroy: edx
contextHash:
mov edx, eax
shr edx, 24
and eax, ((MEM / 4) - 1) & ~1
lea eax, [ebp+Work.modelMem+eax*4]
cmp dl, [eax]
jne .notequal1
inc eax
ret
.notequal1:
add eax, byte 4
cmp dl, [eax]
jne .notequal2
inc eax
ret
.notequal2:
mov dh, [eax+1]
cmp dh, [eax-3]
jbe .noswap
sub eax, byte 4
.noswap:
movzx edx, dl
mov [eax], edx
inc eax
ret
; train
; in: eax=err
; esi=t
; edi=w
; ecx=n/4
; out: esi,edi advanced.
train:
movd mm0, eax
punpcklwd mm0, mm0
punpcklwd mm0, mm0
pcmpeqb mm1, mm1
psrlw mm1, 15
.lp:
movq mm3, [esi]
movq mm2, [edi]
paddsw mm3, mm3
pmulhw mm3, mm0
paddsw mm3, mm1
psraw mm3, 1
paddsw mm2, mm3
movq [edi], mm2
add esi, byte 8
add edi, byte 8
loop .lp
ret
; function exports
%ifndef BINARY
global @modelInitASM@0
global @modelASM@4
%endif
; modelInit
@modelInitASM@0:
pushad
mov ebp, WorkData
; assume everything is zero-initialized
; initialize variables
inc dword [ebp+Work.c0]
mov byte [ebp+Work.bpos], 8
mov eax, 2048
lea edi, [ebp+Work.pr]
stosd
stosd
stosd
stosd
; initialize run table via numerical integration of 1/x
mov ebx, 14155776
mov edi, WorkData+Work.runTable+4
xor ecx, ecx
inc ecx
.runTableLoop:
lea esi, [ecx*2+1]
mov eax, 774541002
cdq
div esi
add ebx, eax
mov eax, ebx
shr eax, 21
stosd
inc cl
jnz .runTableLoop
; build state table.
; note: edi already points to stateCode
; assumes ecx=0
mov esi, WorkData+Work.stateNext
xor ebx, ebx
mov cl, 4
.stateTableLoop:
xor ebx, byte 1
mov ah, [edi+ebx]
cmp ah, 2
jbe .log_underflow
movzx eax, ah
mov eax, [WorkData+Work.runTable+eax*4-4]
shl eax, 2
dec ah
.log_underflow:
xor ebx, byte 1
mov al, [edi+ebx]
inc eax
cmp al, 40
jbe .no_inc_overflow
mov al, 40
.no_inc_overflow:
test bl, 1
jz .bit_even
xchg al, ah
.bit_even:
push edi
mov edx, ecx
repne scasw
je .found
inc edx
scasw
.found:
not ecx
add ecx, edx
mov [edi-2], ax
mov [esi], cl
inc esi
mov ecx, edx
pop edi
inc ebx
cmp bh, 2
jne .stateTableLoop
; build initial state map.
; note that ecx=0x100 at this point
mov esi, edi
mov edi, WorkData+Work.stateMap
.stateMapLoop:
xor eax, eax
lodsb
mov ebx, eax
lodsb
inc eax
inc ebx
add ebx, eax
shl eax, 16
cdq
div ebx
stosd
loop .stateMapLoop
; transform state code table to and masks
mov ch, 2
mov esi, WorkData+Work.stateCode
mov edi, esi
.stateCodeLoop:
lodsb
sub al, 1
salc
stosb
loop .stateCodeLoop
; build stretch table (inverse of squash)
mov edi, WorkData+Work.stretch
or ebx, byte -1 ; =previous i
mov eax, -2047
.stretchLoop:
push eax
call squash
mov ecx, eax
sub ecx, ebx
mov ebx, eax
pop eax
rep stosd
inc eax
cmp eax, 2048
jle .stretchLoop
; initialize context model
; assumes ecx=0 here
lea esi, [ebp+Work.cm]
mov cl, NMODEL
.ctxModelLoop:
xor eax, eax
inc eax
call contextHash
mov [esi+ContextModel.cpr], eax
xor eax, eax
call contextHash
mov [esi+ContextModel.cps], eax
add esi, byte ContextModel.size
loop .ctxModelLoop
%if USEAPM
; initialize APM
lea edi, [ebp+Work.APM]
mov esi, edi
push byte -16
pop eax
.APMLoop:
push eax
shl eax, 7
call squash
shl eax, 4
stosd
pop eax
inc eax
cmp eax, byte 16
jle .APMLoop
; and clone it!
mov ecx, (APMSIZE-1)*33
rep movsd
%endif
popad
ret
; model
; in: ecx=bit
@modelASM@4:
push ebx
push esi
push edi
push ebp
mov ebp, WorkData
mov [ebp+Work.bit], ecx
shl ecx, 16
sub ecx, byte -128
mov [ebp+Work.bitscaled], ecx
; update mixers
xor edx, edx
.updmixer:
mov eax, [ebp+Work.bit]
shl eax, 12
sub eax, [ebp+Work.pr+edx*4]
imul eax, byte 7
cmp dl, 3
je .updmixerdone
mov edi, [ebp+Work.ctx+edx*4]
imul edi, byte (NINPUT*2)
lea edi, [ebp+Work.wx+edi]
mov esi, WorkData+Work.tx
push byte (NINPUT/4)
pop ecx
call train
inc edx
jmp short .updmixer
.updmixerdone:
; assumes ecx=0
mov edi, WorkData+Work.wx2
mov esi, WorkData+Work.tx2
inc ecx
call train
; update c0 and position in bit
mov eax, [ebp+Work.bit]
shl dword [ebp+Work.c0], 1
add [ebp+Work.c0], eax
dec dword [ebp+Work.bpos]
jnz near .nonewbyte
; assumes ecx=0
mov byte [ebp+Work.bpos], 8
inc ecx
mov [ebp+Work.c0], ecx
; update context models
lea esi, [ebp+Work.cm]
xor eax, eax
push byte NMODEL
pop ecx
.updcontext:
and eax, byte -2
mov [esi+ContextModel.ctx], eax
; update run context
mov ebx, [esi+ContextModel.cpr]
mov edi, [_bufPtr]
mov dh, [edi-1] ; bufPtr[-1]
cmp dh, [ebx+1] ; cpr[1]
je .norunflush
mov dl, 0
mov [ebx], dx
.norunflush:
inc byte [ebx]
inc eax
call contextHash
mov [esi+ContextModel.cpr], eax
; fnv hash function
; assumes edi=_bufPtr
mov eax, 0x811c9dc5
inc ecx
imul eax, ecx
dec ecx
mov bl, [masks+ecx-1]
mov dh, [bitm+ecx-1]
.hashnext:
dec edi
shr bl, 1
jnc .hashnotset
mov dl, [edi]
and dl, dh
xor al, dl
imul eax, 0x01000193
jmp short .hashnext
.hashnotset:
jnz .hashnext
add esi, byte ContextModel.size
loop .updcontext
; match processing
; assumes esi points to "match"
and eax, (MEM/16)-1
mov edi, [_bufPtr]
xchg edi, [esi+eax*4] ; now edi=Match[h]
mov esi, [_bufPtr]
mov ecx, [ebp+Work.matchl]
jecxz .searchmatch
inc ecx
inc dword [ebp+Work.matchp]
jmp short .updmatch
.searchmatch:
test edi, edi
jz .updmatch
mov [ebp+Work.matchp], edi
std
cmpsb
mov ecx, edi
sub ecx, [_startBufPtr]
mov [ebp+Work.lentemp], ecx
repe cmpsb
cld
je .allequal
inc ecx
.allequal:
neg ecx
add ecx, [ebp+Work.lentemp]
.updmatch:
cmp ecx, MAXLEN
jbe .noverylongmatch
mov ecx, MAXLEN
.noverylongmatch:
mov [ebp+Work.matchl], ecx
cmp ecx, byte 32
jbe .nolongmatch
push byte 32
pop ecx
.nolongmatch:
shl ecx, 6
mov [ebp+Work.matchw], ecx
.nonewbyte:
; start model mixing
mov ecx, [ebp+Work.c0]
shl ecx, 3
mov [ebp+Work.c0s], ecx
; constant model
mov edi, WorkData+Work.tx
push byte 127
pop eax
stosw
; match model
xor eax, eax
cmp eax, [ebp+Work.matchl]
je .nomatch
mov ebx, [ebp+Work.matchp]
movzx ebx, byte [ebx]
inc bh
mov cl, [ebp+Work.bpos]
shr ebx, cl
sbb edx, edx
cmp ebx, [ebp+Work.c0]
jne .nomatch
mov eax, [ebp+Work.matchw]
xor eax, edx
sub eax, edx
jmp short .matchdone
.nomatch:
mov [ebp+Work.matchl], eax
.matchdone:
stosw
mov eax, [ebp+Work.matchl]
cmp eax, 400
jbe .contextmodels
mov dword [ebp+Work.ctx], 512+14
jmp .mix
; context models
.contextmodels:
lea esi, [ebp+Work.cm]
xor eax, eax
mov ah, 2
mov [ebp+Work.ctx+8], eax
push byte NMODEL
pop ecx
.contextloop:
; run model
push ecx
mov eax, [esi+ContextModel.cpr]
movzx edx, byte [eax+1]
movzx eax, byte [eax]
mov cl, [ebp+Work.bpos]
inc dh
mov eax, [WorkData+Work.runTable+eax*4]
shr edx, cl
sbb ecx, ecx
xor eax, ecx
sub eax, ecx
cmp edx, [ebp+Work.c0]
je .run
xor eax, eax
.run:
stosw
pop ecx
; nonstationary context
mov eax, [esi+ContextModel.cps]
mov edx, [ebp+Work.bit]
movzx ebx, byte [eax]
add ebx, ebx
add ebx, edx
mov bl, [WorkData+Work.stateNext+ebx]
mov [eax], bl
mov eax, [esi+ContextModel.ctx]
xor eax, [ebp+Work.c0s]
call contextHash
.nsset:
mov [esi+ContextModel.cps], eax
; state mapping
mov ebx, [esi+ContextModel.st]
mov eax, [ebp+Work.bitscaled]
lea ebx, [WorkData+Work.stateMap+ebx*4]
sub eax, [ebx]
sar eax, 8
add [ebx], eax
mov eax, [esi+ContextModel.cps]
movzx ebx, byte [eax]
mov [esi+ContextModel.st], ebx
mov eax, [WorkData+Work.stateMap+ebx*4]
shr eax, 4
push eax
mov eax, [WorkData+Work.stretch+eax*4]
sar eax, 2
stosw
pop eax
shr eax, 4
mov edx, eax
not dl
push eax
sub eax, edx
stosw
pop eax
and al, [WorkData+Work.stateCode+ebx*2+0]
and dl, [WorkData+Work.stateCode+ebx*2+1]
sub eax, edx
stosw
cmp ebx, byte 1
sbb dword [ebp+Work.ctx+8], byte -1
add esi, byte ContextModel.size
dec ecx
jnz near .contextloop
; weighting contexts
mov ebx, [_bufPtr]
movzx eax, byte [ebx-1]
mov [ebp+Work.ctx+0], eax
mov al, [ebp+Work.c0]
inc ah
mov [ebp+Work.ctx+4], eax
xor ebx, ebx
mov eax, [ebp+Work.matchl]
dec eax
cmovs eax, ebx
not bl
cmp eax, ebx
cmova eax, ebx
mov eax, [ebp+Work.runTable+eax*4]
shr eax, 3
add [ebp+Work.ctx+8], eax
.mix:
; perform mixing
push byte 2
pop ebx
.mixloop:
mov eax, [ebp+Work.ctx+ebx*4]
imul eax, byte (NINPUT*2)
lea edi, [ebp+Work.wx+eax]
mov esi, WorkData+Work.tx
pxor mm0, mm0
push byte (NINPUT/4)
pop ecx
.mixdploop:
movq mm1, [esi]
pmaddwd mm1, [edi]
psrad mm1, 8
paddd mm0, mm1
add esi, byte 8
add edi, byte 8
loop .mixdploop
movq mm1, mm0
psrlq mm1, 32
paddd mm0, mm1
movd eax, mm0
sar eax, 3
mov [WorkData+Work.tx2+ebx*2], ax
call squash
mov [ebp+Work.pr+ebx*4], eax
dec ebx
jns .mixloop
; the final mix
mov esi, WorkData+Work.tx2
mov edi, WorkData+Work.wx2
xor eax, eax
push byte 3
pop ecx
.finalmix:
movsx ebx, word [edi]
movsx edx, word [esi]
imul ebx, edx
add eax, ebx
cmpsw
loop .finalmix
sar eax, 16
call squash
mov [ebp+Work.pr+12], eax
%if USEAPM
; APM stage
; assumes p still in eax
mov ecx, [ebp+Work.bit]
mov edx, [ebp+Work.APMi]
lea edi, [ebp+Work.APM+16*4+edx*4]
neg ecx
and ecx, 0x100fe ;=g
; update APM tables
push ecx
sub ecx, [edi]
sar ecx, 8
add [edi], ecx
add edi, byte 4
pop ecx
sub ecx, [edi]
sar ecx, 8
add [edi], ecx
; calc new APM index
mov ebx, [_bufPtr]
movzx ebx, byte [ebx-1]
mov ecx, ebx
shl ebx, 4
add ebx, ecx
add ebx, [ebp+Work.c0]
and ebx, APMSIZE-1
mov ecx, ebx
shl ebx, 5
add ebx, ecx
; calc pf/finish APMi
mov ecx, [ebp+Work.stretch+eax*4]
push ecx
sar ecx, 7
add ebx, ecx
mov [ebp+Work.APMi], ebx
pop ecx
; calc p
and ecx, byte 127
lea esi, [ebp+Work.APM+16*4+ebx*4]
lodsd
mov ebx, [esi]
sub ebx, eax
shl eax, 7
imul ebx, ecx
add eax, ebx
shr eax, 11
%endif
; return value
; assumes p still in eax
cmp ah, 8
adc eax, byte 0
pop ebp
pop edi
pop esi
pop ebx
ret
; ---- initialized data
section .data
%ifdef BINARY
_bufPtr dd 0
_startBufPtr dd 0
%else
extern _bufPtr
extern _startBufPtr
%endif
squashTab dw 1, 2, 4, 6, 10, 17, 27, 45, 74, 120, 194
dw 311, 488, 747,1102,1546,2048,2550,2994,3349,3608,3785
dw 3902,3976,4022,4051,4069,4079,4086,4090,4092,4094,4095
masks db 0x1f, 0x27, 0x88, 0x07, 0x0a, 0x09, 0x05, 0x03, 0x04, 0x02, 0x01
bitm db 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff