wavpack-sys 0.4.0

FFI bindings for WavPack
Documentation
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
############################################################################
##                           **** WAVPACK ****                            ##
##                  Hybrid Lossless Wavefile Compressor                   ##
##              Copyright (c) 1998 - 2015 Conifer Software.               ##
##                          All Rights Reserved.                          ##
##      Distributed under the BSD Software License (see license.txt)      ##
############################################################################

        .intel_syntax noprefix
        .text

        .globl  _unpack_decorr_stereo_pass_cont_x64win
        .globl  _unpack_decorr_mono_pass_cont_x64win

        .globl  unpack_decorr_stereo_pass_cont_x64win
        .globl  unpack_decorr_mono_pass_cont_x64win

        .globl  _unpack_decorr_stereo_pass_cont_x64
        .globl  _unpack_decorr_mono_pass_cont_x64

        .globl  unpack_decorr_stereo_pass_cont_x64
        .globl  unpack_decorr_mono_pass_cont_x64

# This is an assembly optimized version of the following WavPack function:
#
# void unpack_decorr_stereo_pass_cont (struct decorr_pass *dpp,
#                                      int32_t *buffer,
#                                      int32_t sample_count,
#                                      int32_t long_math;
#
# It performs a single pass of stereo decorrelation on the provided buffer.
# Note that this version of the function requires that up to 8 previous
# stereo samples are visible and correct. In other words, it ignores the
# "samples_*" fields in the decorr_pass structure and gets the history data
# directly from the buffer. It does, however, return the appropriate history
# samples to the decorr_pass structure before returning.
#
# The "long_math" argument is used to specify that a 32-bit multiply is
# not enough for the "apply_weight" operation (although in this case it
# would only apply to the -1 and -2 terms because the MMX code does not have
# this limitation) but we ignore the parameter and use the overflow detection
# of the "imul" instruction to switch automatically to the "long_math" loop.
#
# This is written to work on an X86-64 processor (also called the AMD64)
# running in 64-bit mode and generally uses the MMX extensions to improve
# the performance by processing both stereo channels together. Unfortunately
# this is not easily used for terms -1 and -2, so these terms are handled
# sequentially with regular assembler code.
#
# This version has entry points for both the System V ABI and the Windows
# X64 ABI. It does not use the "red zone" or the "shadow area"; it saves the
# non-volatile registers for both ABIs on the stack and allocates another
# 8 bytes on the stack to store the dpp pointer. Note that it does NOT
# provide unwind data for the Windows ABI (the unpack_x64.asm module for
# MSVC does). The arguments are passed in registers:
#
# System V  Windows
#   rdi       rcx      struct decorr_pass *dpp
#   rsi       rdx      int32_t *buffer
#   edx       r8       int32_t sample_count
#   ecx       r9       int32_t long_math
#
# registers after entry:
#
#   rdi         bptr
#   rsi         eptr
#
# stack usage:
#
# [rsp+0] = *dpp
#

_unpack_decorr_stereo_pass_cont_x64win:
unpack_decorr_stereo_pass_cont_x64win:
        push    rbp
        push    rbx
        push    rdi
        push    rsi
        sub     rsp, 8
        mov     rdi, rcx                    # copy params from win regs to Linux regs
        mov     rsi, rdx                    # so we can leave following code similar
        mov     rdx, r8
        mov     rcx, r9
        jmp     entry                       # jump into common portion

_unpack_decorr_stereo_pass_cont_x64:
unpack_decorr_stereo_pass_cont_x64:
        push    rbp
        push    rbx
        push    rdi
        push    rsi
        sub     rsp, 8

entry:  mov     [rsp], rdi                  # store dpp* at [rsp]
        and     edx, edx                    # if sample_count is zero, do nothing
        jz      done

        mov     rdi, rsi                    # rdi = bptr
        lea     rsi, [rdi+rdx*8]            # rsi = eptr

        mov     rax, [rsp]                  # get term from dpp struct & vector to handler
        mov     eax, [rax]
        cmp     al, 17
        je      term_17_entry
        cmp     al, 18
        je      term_18_entry
        cmp     al, -1
        je      term_minus_1_entry
        cmp     al, -2
        je      term_minus_2_entry
        cmp     al, -3
        je      term_minus_3_entry

#
# registers in default term loop:
#
#   rbx         term * -8 (for indexing correlation sample)
#   rdi         bptr
#   rsi         eptr
#
#   mm0, mm1    scratch
#   mm2         original sample values
#   mm3         correlation sample
#   mm4         zero (for pcmpeqd)
#   mm5         weights
#   mm6         delta
#   mm7         512 (for rounding)
#

default_term_entry:
        imul    rbx, rax, -8                # set RBX to term * -8
        mov     eax, 512
        movd    mm7, eax
        punpckldq mm7, mm7                  # mm7 = round (512)
        mov     rdx, [rsp]                  # set RDX to *dpp
        mov     eax, [rdx+4]
        movd    mm6, eax
        punpckldq mm6, mm6                  # mm6 = delta (0-7)
        mov     eax, 0xFFFF                 # mask high weights to zero for PMADDWD
        movd    mm5, eax
        punpckldq mm5, mm5                  # mm5 = weight mask 0x0000FFFF0000FFFF
        pand    mm5, [rdx+8]                # mm5 = weight_AB masked to 16 bits
        pxor    mm4, mm4                    # mm4 = zero (for pcmpeqd)
        jmp     default_term_loop

        .balign  64
default_term_loop:
        movq    mm3, [rdi+rbx]              # mm3 = sam_AB
        movq    mm1, mm3
        movq    mm0, mm3
        paddd   mm1, mm1
        psrld   mm0, 15
        psrlw   mm1, 1
        pmaddwd mm0, mm5
        pmaddwd mm1, mm5
        movq    mm2, [rdi]                  # mm2 = left_right
        pslld   mm0, 5
        paddd   mm1, mm7                    # add 512 for rounding
        psrad   mm1, 10
        paddd   mm0, mm2
        paddd   mm0, mm1                    # add shifted sums
        movq    [rdi], mm0                  # store result
        movq    mm0, mm3
        pxor    mm0, mm2
        psrad   mm0, 31                     # mm0 = sign (sam_AB ^ left_right)
        add     rdi, 8
        pcmpeqd mm2, mm4                    # mm2 = 1s if left_right was zero
        pcmpeqd mm3, mm4                    # mm3 = 1s if sam_AB was zero
        por     mm2, mm3                    # mm2 = 1s if either was zero
        pandn   mm2, mm6                    # mask delta with zeros check
        pxor    mm5, mm0
        paddw   mm5, mm2                    # and add to weight_AB
        pxor    mm5, mm0
        cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      default_term_loop

        pslld   mm5, 16                     # sign-extend 16-bit weights back to dwords
        psrad   mm5, 16
        mov     rdx, [rsp]                  # point to dpp
        movq    [rdx+8], mm5                # put weight_AB back
        emms

        mov     ecx, [rdx]                  # ecx = dpp->term

default_store_samples:
        dec     ecx
        sub     rdi, 8                      # back up one full sample
        mov     eax, [rdi+4]
        mov     [rdx+rcx*4+48], eax         # store samples_B [ecx]
        mov     eax, [rdi]
        mov     [rdx+rcx*4+16], eax         # store samples_A [ecx]
        test    ecx, ecx
        jnz     default_store_samples
        jmp     done

#
# registers in term 17 & 18 loops:
#
#   rdi         bptr
#   rsi         eptr
#
#   mm0, mm1    scratch
#   mm2         original sample values
#   mm3         correlation samples
#   mm4         last calculated values (so we don't need to reload)
#   mm5         weights
#   mm6         delta
#   mm7         512 (for rounding)
#

term_17_entry:
        mov     eax, 512
        movd    mm7, eax
        punpckldq mm7, mm7                  # mm7 = round (512)
        mov     rdx, [rsp]                  # set RDX to *dpp
        mov     eax, [rdx+4]
        movd    mm6, eax
        punpckldq mm6, mm6                  # mm6 = delta (0-7)
        mov     eax, 0xFFFF                 # mask high weights to zero for PMADDWD
        movd    mm5, eax
        punpckldq mm5, mm5                  # mm5 = weight mask 0x0000FFFF0000FFFF
        pand    mm5, [rdx+8]                # mm5 = weight_AB masked to 16 bits
        movq    mm4, [rdi-8]                # preload last calculated values in mm4
        jmp     term_17_loop

        .balign  64
term_17_loop:
        paddd   mm4, mm4
        psubd   mm4, [rdi-16]               # mm3 = sam_AB
        movq    mm3, mm4
        movq    mm1, mm3
        paddd   mm1, mm1
        psrld   mm4, 15
        psrlw   mm1, 1
        pmaddwd mm4, mm5
        pmaddwd mm1, mm5
        movq    mm2, [rdi]                  # mm2 = left_right
        pslld   mm4, 5
        paddd   mm1, mm7                    # add 512 for rounding
        psrad   mm1, 10
        paddd   mm4, mm2
        paddd   mm4, mm1                    # add shifted sums
        movq    mm0, mm3
        movq    [rdi], mm4                  # store result
        pxor    mm0, mm2
        psrad   mm0, 31                     # mm0 = sign (sam_AB ^ left_right)
        add     rdi, 8
        pxor    mm1, mm1                    # mm1 = zero
        pcmpeqd mm2, mm1                    # mm2 = 1s if left_right was zero
        pcmpeqd mm3, mm1                    # mm3 = 1s if sam_AB was zero
        por     mm2, mm3                    # mm2 = 1s if either was zero
        pandn   mm2, mm6                    # mask delta with zeros check
        pxor    mm5, mm0
        paddw   mm5, mm2                    # and add to weight_AB
        pxor    mm5, mm0
        cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      term_17_loop
        jmp     term_1718_exit              # terms 17 & 18 treat samples_AB[] the same

term_18_entry:
        mov     eax, 512
        movd    mm7, eax
        punpckldq mm7, mm7                  # mm7 = round (512)
        mov     rdx, [rsp]                  # set RDX to *dpp
        mov     eax, [rdx+4]
        movd    mm6, eax
        punpckldq mm6, mm6                  # mm6 = delta (0-7)
        mov     eax, 0xFFFF                 # mask high weights to zero for PMADDWD
        movd    mm5, eax
        punpckldq mm5, mm5                  # mm5 = weight mask 0x0000FFFF0000FFFF
        pand    mm5, [rdx+8]                # mm5 = weight_AB masked to 16 bits
        movq    mm4, [rdi-8]                # preload last calculated values in mm4
        jmp     term_18_loop

        .balign  64
term_18_loop:
        movq    mm3, mm4
        psubd   mm3, [rdi-16]
        psrad   mm3, 1
        paddd   mm3, mm4                    # mm3 = sam_AB
        movq    mm1, mm3
        movq    mm4, mm3
        paddd   mm1, mm1
        psrld   mm4, 15
        psrlw   mm1, 1
        pmaddwd mm4, mm5
        pmaddwd mm1, mm5
        movq    mm2, [rdi]                  # mm2 = left_right
        pslld   mm4, 5
        paddd   mm1, mm7                    # add 512 for rounding
        psrad   mm1, 10
        paddd   mm4, mm2
        paddd   mm4, mm1                    # add shifted sums
        movq    mm0, mm3
        movq    [rdi], mm4                  # store result
        pxor    mm0, mm2
        psrad   mm0, 31                     # mm0 = sign (sam_AB ^ left_right)
        add     rdi, 8
        pxor    mm1, mm1                    # mm1 = zero
        pcmpeqd mm2, mm1                    # mm2 = 1s if left_right was zero
        pcmpeqd mm3, mm1                    # mm3 = 1s if sam_AB was zero
        por     mm2, mm3                    # mm2 = 1s if either was zero
        pandn   mm2, mm6                    # mask delta with zeros check
        pxor    mm5, mm0
        paddw   mm5, mm2                    # and add to weight_AB
        pxor    mm5, mm0
        cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      term_18_loop

term_1718_exit:
        pslld   mm5, 16                     # sign-extend 16-bit weights back to dwords
        psrad   mm5, 16
        mov     rdx, [rsp]                  # point to dpp
        movq    [rdx+8], mm5                # put weight_AB back
        emms

        mov     eax, [rdi-4]                # dpp->samples_B [0] = bptr [-1];
        mov     [rdx+48], eax
        mov     eax, [rdi-8]                # dpp->samples_A [0] = bptr [-2];
        mov     [rdx+16], eax
        mov     eax, [rdi-12]               # dpp->samples_B [1] = bptr [-3];
        mov     [rdx+52], eax
        mov     eax, [rdi-16]               # dpp->samples_A [1] = bptr [-4];
        mov     [rdx+20], eax
        jmp     done

#
# registers in term -1 & -2 loops:
#
#   eax,ebx,edx scratch
#   ecx         weight_A
#   ebp         weight_B
#   rdi         bptr
#   rsi         eptr
#   r8d         delta
#

term_minus_1_entry:
        cld
        mov     rdx, [rsp]                  # point to dpp
        mov     ecx, [rdx+8]                # ecx = weight_A
        mov     ebp, [rdx+12]               # ebp = weight_B
        mov     r8d, [rdx+4]                # r8d = delta
        mov     eax, [rdi-4]
        jmp     term_minus_1_loop

        .balign  64
term_minus_1_loop:
        mov     ebx, eax
        imul    eax, ecx
        mov     edx, [rdi]
        jo      OV11
        sar     eax, 10
        adc     eax, edx
        stosd
        test    ebx, ebx
        je      L182
        test    edx, edx
        je      L182
        xor     ebx, edx
        sar     ebx, 31
        xor     ecx, ebx
        add     ecx, r8d
        mov     edx, 1024
        add     edx, ebx
        cmp     ecx, edx
        jle     L183
        mov     ecx, edx
L183:   xor     ecx, ebx
L182:   mov     ebx, eax
        imul    eax, ebp
        mov     edx, [rdi]
        jo      OV12
        sar     eax, 10
        adc     eax, edx
        stosd
        test    ebx, ebx
        je      L187
        test    edx, edx
        je      L187
        xor     ebx, edx
        sar     ebx, 31
        xor     ebp, ebx
        add     ebp, r8d
        mov     edx, 1024
        add     edx, ebx
        cmp     ebp, edx
        jle     L188
        mov     ebp, edx
L188:   xor     ebp, ebx
L187:   cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      term_minus_1_loop
        jmp     term_minus_1_done

OV11:   mov     eax, ebx                    # restore previous sample into eax
        jmp     long_term_minus_1_loop

OV12:   mov     eax, ebx                    # restore previous sample into eax
        jmp     L282

        .balign  64
long_term_minus_1_loop:
        mov     ebx, eax
        imul    ecx
        shl     edx, 22
        shr     eax, 10
        adc     eax, edx
        mov     edx, [rdi]
        add     eax, edx
        stosd
        test    ebx, ebx
        je      L282
        test    edx, edx
        je      L282
        xor     ebx, edx
        sar     ebx, 31
        xor     ecx, ebx
        add     ecx, r8d
        mov     edx, 1024
        add     edx, ebx
        cmp     ecx, edx
        jle     L283
        mov     ecx, edx
L283:   xor     ecx, ebx
L282:   mov     ebx, eax
        imul    ebp
        shl     edx, 22
        shr     eax, 10
        adc     eax, edx
        mov     edx, [rdi]
        add     eax, edx
        stosd
        test    ebx, ebx
        je      L287
        test    edx, edx
        je      L287
        xor     ebx, edx
        sar     ebx, 31
        xor     ebp, ebx
        add     ebp, r8d
        mov     edx, 1024
        add     edx, ebx
        cmp     ebp, edx
        jle     L288
        mov     ebp, edx
L288:   xor     ebp, ebx
L287:   cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      long_term_minus_1_loop

term_minus_1_done:
        mov     rdx, [rsp]                  # point to dpp
        mov     [rdx+8], ecx                # store weights back
        mov     [rdx+12], ebp
        mov     eax, [rdi-4]                # dpp->samples_A [0] = bptr [-1];
        mov     [rdx+16], eax
        jmp     done

term_minus_2_entry:
        mov     rdx, [rsp]                  # point to dpp
        mov     ecx, [rdx+8]                # ecx = weight_A
        mov     ebp, [rdx+12]               # ebp = weight_B
        mov     r8d, [rdx+4]                # r8d = delta
        mov     eax, [rdi-8]
        jmp     term_minus_2_loop

        .balign  64
term_minus_2_loop:
        mov     ebx, eax
        imul    eax, ebp
        mov     edx, [rdi+4]
        jo      OV21
        sar     eax, 10
        adc     eax, edx
        mov     [rdi+4], eax
        test    ebx, ebx
        je      L194
        test    edx, edx
        je      L194
        xor     ebx, edx
        sar     ebx, 31
        xor     ebp, ebx
        add     ebp, r8d
        mov     edx, 1024
        add     edx, ebx
        cmp     ebp, edx
        jle     L195
        mov     ebp, edx
L195:   xor     ebp, ebx
L194:   mov     ebx, eax
        imul    eax, ecx
        mov     edx, [rdi]
        jo      OV22
        sar     eax, 10
        adc     eax, edx
        mov     [rdi], eax
        test    ebx, ebx
        je      L199
        test    edx, edx
        je      L199
        xor     ebx, edx
        sar     ebx, 31
        xor     ecx, ebx
        add     ecx, r8d
        mov     edx, 1024
        add     edx, ebx
        cmp     ecx, edx
        jle     L200
        mov     ecx, edx
L200:   xor     ecx, ebx
L199:   add     rdi, 8
        cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      term_minus_2_loop
        jmp     term_minus_2_done

OV21:   mov     eax, ebx                    # restore previous sample into eax
        jmp     long_term_minus_2_loop

OV22:   mov     eax, ebx                    # restore previous sample into eax
        jmp     L294

        .balign  64
long_term_minus_2_loop:
        mov     ebx, eax
        imul    ebp
        shl     edx, 22
        shr     eax, 10
        adc     eax, edx
        mov     edx, [rdi+4]
        add     eax, edx
        mov     [rdi+4], eax
        test    ebx, ebx
        je      L294
        test    edx, edx
        je      L294
        xor     ebx, edx
        sar     ebx, 31
        xor     ebp, ebx
        add     ebp, r8d
        mov     edx, 1024
        add     edx, ebx
        cmp     ebp, edx
        jle     L295
        mov     ebp, edx
L295:   xor     ebp, ebx
L294:   mov     ebx, eax
        imul    ecx
        shl     edx, 22
        shr     eax, 10
        adc     eax, edx
        mov     edx, [rdi]
        add     eax, edx
        mov     [rdi], eax
        test    ebx, ebx
        je      L299
        test    edx, edx
        je      L299
        xor     ebx, edx
        sar     ebx, 31
        xor     ecx, ebx
        add     ecx, r8d
        mov     edx, 1024
        add     edx, ebx
        cmp     ecx, edx
        jle     L300
        mov     ecx, edx
L300:   xor     ecx, ebx
L299:   add     rdi, 8
        cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      long_term_minus_2_loop

term_minus_2_done:
        mov     rdx, [rsp]                  # point to dpp
        mov     [rdx+8], ecx                # store weights back
        mov     [rdx+12], ebp
        mov     eax, [rdi-8]                # dpp->samples_B [0] = bptr [-2];
        mov     [rdx+48], eax
        jmp     done

#
# registers in term -3 loop:
#
#   rdi         bptr
#   rsi         eptr
#
#   mm0, mm1    scratch
#   mm2         original sample values
#   mm3         correlation samples
#   mm4         last calculated values (so we don't need to reload)
#   mm5         weights
#   mm6         delta
#   mm7         512 (for rounding)
#

term_minus_3_entry:
        mov     eax, 512
        movd    mm7, eax
        punpckldq mm7, mm7                  # mm7 = round (512)
        mov     rdx, [rsp]                  # set RDX to *dpp
        mov     eax, [rdx+4]
        movd    mm6, eax
        punpckldq mm6, mm6                  # mm6 = delta (0-7)
        mov     eax, 0xFFFF                 # mask high weights to zero for PMADDWD
        movd    mm5, eax
        punpckldq mm5, mm5                  # mm5 = weight mask 0x0000FFFF0000FFFF
        pand    mm5, [rdx+8]                # mm5 = weight_AB masked to 16 bits
        movq    mm4, [rdi-8]
        jmp     term_minus_3_loop

        .balign  64
term_minus_3_loop:
        movq    mm3, mm4
        psrlq   mm3, 32
        punpckldq mm3, mm4                  # mm3 = sam_AB
        movq    mm1, mm3
        movq    mm4, mm3
        pslld   mm1, 1
        psrld   mm4, 15
        psrlw   mm1, 1
        pmaddwd mm4, mm5
        pmaddwd mm1, mm5
        movq    mm2, [rdi]                  # mm2 = left_right
        pslld   mm4, 5
        paddd   mm1, mm7                    # add 512 for rounding
        psrad   mm1, 10
        paddd   mm4, mm2
        paddd   mm4, mm1                    # add shifted sums
        movq    [rdi], mm4                  # store result
        movq    mm0, mm3
        pxor    mm0, mm2
        psrad   mm0, 31                     # mm0 = sign (sam_AB ^ left_right)
        add     rdi, 8
        pxor    mm1, mm1                    # mm1 = zero
        pcmpeqd mm2, mm1                    # mm2 = 1s if left_right was zero
        pcmpeqd mm3, mm1                    # mm3 = 1s if sam_AB was zero
        por     mm2, mm3                    # mm2 = 1s if either was zero
        pandn   mm2, mm6                    # mask delta with zeros check
        pcmpeqd mm1, mm1
        psubd   mm1, mm7
        psubd   mm1, mm7
        psubd   mm1, mm0
        pxor    mm5, mm0
        paddw   mm5, mm1
        paddusw mm5, mm2                    # and add to weight_AB
        psubw   mm5, mm1
        pxor    mm5, mm0
        cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      term_minus_3_loop

        pslld   mm5, 16                     # sign-extend 16-bit weights back to dwords
        psrad   mm5, 16
        mov     rdx, [rsp]                  # point to dpp
        movq    [rdx+8], mm5                # put weight_AB back
        emms

        mov     edx, [rdi-4]                # dpp->samples_A [0] = bptr [-1];
        mov     rax, [rsp]
        mov     [rax+16], edx
        mov     edx, [rdi-8]                # dpp->samples_B [0] = bptr [-2];
        mov     [rax+48], edx

done:   add     rsp, 8
        pop     rsi
        pop     rdi
        pop     rbx
        pop     rbp
        ret

#######################################################################################################################
#
# This is the mono version of the above function. It does not use MMX and does not handle negative terms.
#
# void unpack_decorr_mono_pass_cont (struct decorr_pass *dpp,
#                                    int32_t *buffer,
#                                    int32_t sample_count,
#                                    int32_t long_math;
# arguments on entry:
#
# System V  Windows
#   rdi       rcx      struct decorr_pass *dpp
#   rsi       rdx      int32_t *buffer
#   edx       r8       int32_t sample_count
#   ecx       r9       int32_t long_math
#
# registers after entry:
#
#   rdi         bptr
#   rsi         eptr
#
# stack usage:
#
# [rsp+0] = *dpp
#

_unpack_decorr_mono_pass_cont_x64win:
unpack_decorr_mono_pass_cont_x64win:
        push    rbp
        push    rbx
        push    rdi
        push    rsi
        sub     rsp, 8

        mov     rdi, rcx                    # copy params from win regs to Linux regs
        mov     rsi, rdx                    # so we can leave following code similar
        mov     rdx, r8
        mov     rcx, r9
        jmp     mentry                      # jump into common portion

_unpack_decorr_mono_pass_cont_x64:
unpack_decorr_mono_pass_cont_x64:
        push    rbp
        push    rbx
        push    rdi
        push    rsi
        sub     rsp, 8

mentry: mov     [rsp], rdi                  # store dpp* into [rsp]
        and     edx, edx                    # if sample_count is zero, do nothing
        jz      mono_done

        cld                                 # we use stosd
        mov     rdi, rsi                    # rdi = bptr
        lea     rsi, [rdi+rdx*4]            # rsi = eptr

        mov     rax, [rsp]                  # get term from dpp struct & vector to handler
        mov     eax, [rax]
        cmp     al, 17
        je      mono_17_entry
        cmp     al, 18
        je      mono_18_entry

#
# registers during default term processing loop:
#   rdi         active buffer pointer
#   rsi         end of buffer pointer
#   r8d         delta
#   ecx         weight_A
#   ebx         term * -4
#   eax,edx     scratch
#

default_mono_entry:
        imul    rbx, rax, -4                # set rbx to term * -4 for decorrelation index
        mov     rdx, [rsp]
        mov     ecx, [rdx+8]                # ecx = weight, r8d = delta
        mov     r8d, [rdx+4]
        jmp     default_mono_loop

#
# registers during processing loop for terms 17 & 18:
#   rdi         active buffer pointer
#   rsi         end of buffer pointer
#   r8d         delta
#   ecx         weight_A
#   ebp         previously calculated value
#   ebx         calculated correlation sample
#   eax,edx     scratch
#

mono_17_entry:
        mov     rdx, [rsp]                  # rdx = dpp*
        mov     ecx, [rdx+8]                # ecx = weight, r8d = delta
        mov     r8d, [rdx+4]
        mov     ebp, [rdi-4]
        jmp     mono_17_loop

mono_18_entry:
        mov     rdx, [rsp]                  # rdx = dpp*
        mov     ecx, [rdx+8]                # ecx = weight, r8d = delta
        mov     r8d, [rdx+4]
        mov     ebp, [rdi-4]
        jmp     mono_18_loop

        .balign  64
default_mono_loop:
        mov     eax, [rdi+rbx]
        imul    eax, ecx
        mov     edx, [rdi]
        jo      long_default_mono_loop
        sar     eax, 10
        adc     eax, edx
        mov     [rdi], eax
        mov     eax, [rdi+rbx]
        add     rdi, 4
        test    edx, edx
        je      L100
        test    eax, eax
        je      L100
        xor     eax, edx
        cdq
        xor     ecx, edx
        add     ecx, r8d
        xor     ecx, edx
L100:   cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      default_mono_loop
        jmp     default_mono_done

        .balign  64
long_default_mono_loop:
        mov     eax, [rdi+rbx]
        imul    ecx
        shl     edx, 22
        shr     eax, 10
        adc     eax, edx
        mov     edx, [rdi]
        add     eax, edx
        mov     [rdi], eax
        mov     eax, [rdi+rbx]
        add     rdi, 4
        test    edx, edx
        je      L101
        test    eax, eax
        je      L101
        xor     eax, edx
        cdq
        xor     ecx, edx
        add     ecx, r8d
        xor     ecx, edx
L101:   cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      long_default_mono_loop

default_mono_done:
        mov     rdx, [rsp]                  # edx = dpp*
        mov     [rdx+8], ecx                # store weight_A back
        mov     ecx, [rdx]                  # ecx = dpp->term

default_mono_store_samples:
        dec     ecx
        sub     rdi, 4                      # back up one full sample
        mov     eax, [rdi]
        mov     [rdx+rcx*4+16], eax         # store samples_A [ecx]
        test    ecx, ecx
        jnz     default_mono_store_samples
        jmp     mono_done

        .balign  64
mono_17_loop:
        lea     ebx, [ebp+ebp]
        sub     ebx, [rdi-8]
        mov     eax, ecx
        imul    eax, ebx
        mov     edx, [rdi]
        jo      long_mono_17_loop
        sar     eax, 10
        adc     eax, edx
        stosd
        test    ebx, ebx
        mov     ebp, eax
        je      L117
        test    edx, edx
        je      L117
        xor     ebx, edx
        sar     ebx, 31
        xor     ecx, ebx
        add     ecx, r8d
        xor     ecx, ebx
L117:   cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      mono_17_loop
        jmp     mono_1718_exit

        .balign  64
long_mono_17_loop:
        lea     ebx, [ebp+ebp]
        sub     ebx, [rdi-8]
        mov     eax, ecx
        imul    ebx
        shl     edx, 22
        shr     eax, 10
        adc     eax, edx
        mov     edx, [rdi]
        add     eax, edx
        stosd
        test    ebx, ebx
        mov     ebp, eax
        je      L217
        test    edx, edx
        je      L217
        xor     ebx, edx
        sar     ebx, 31
        xor     ecx, ebx
        add     ecx, r8d
        xor     ecx, ebx
L217:   cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      long_mono_17_loop
        jmp     mono_1718_exit

        .balign  64
mono_18_loop:
        lea     ebx, [ebp+ebp*2]
        sub     ebx, [rdi-8]
        sar     ebx, 1
        mov     eax, ecx
        imul    eax, ebx
        mov     edx, [rdi]
        jo      long_mono_18_loop
        sar     eax, 10
        adc     eax, edx
        stosd
        test    ebx, ebx
        mov     ebp, eax
        je      L118
        test    edx, edx
        je      L118
        xor     ebx, edx
        sar     ebx, 31
        xor     ecx, ebx
        add     ecx, r8d
        xor     ecx, ebx
L118:   cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      mono_18_loop
        jmp     mono_1718_exit

        .balign  64
long_mono_18_loop:
        lea     ebx, [ebp+ebp*2]
        sub     ebx, [rdi-8]
        sar     ebx, 1
        mov     eax, ecx
        imul    ebx
        shl     edx, 22
        shr     eax, 10
        adc     eax, edx
        mov     edx, [rdi]
        add     eax, edx
        stosd
        test    ebx, ebx
        mov     ebp, eax
        je      L218
        test    edx, edx
        je      L218
        xor     ebx, edx
        sar     ebx, 31
        xor     ecx, ebx
        add     ecx, r8d
        xor     ecx, ebx
L218:   cmp     rdi, rsi                    # compare bptr and eptr to see if we're done
        jb      long_mono_18_loop

mono_1718_exit:
        mov     rdx, [rsp]                  # edx = dpp*
        mov     [rdx+8], ecx                # store weight_A back
        mov     eax, [rdi-4]                # dpp->samples_A [0] = bptr [-1];
        mov     [rdx+16], eax
        mov     eax, [rdi-8]                # dpp->samples_A [1] = bptr [-2];
        mov     [rdx+20], eax

mono_done:
        add     rsp, 8
        pop     rsi
        pop     rdi
        pop     rbx
        pop     rbp
        ret

#ifdef __ELF__
        .section .note.GNU-stack,"",@progbits
#endif

#ifdef __midipix__
	.section .got$unpack_decorr_mono_pass_cont_x64win,"r"
	.global __imp_unpack_decorr_mono_pass_cont_x64win
__imp_unpack_decorr_mono_pass_cont_x64win:
	.quad	unpack_decorr_mono_pass_cont_x64win
	.linkonce discard

	.section .got$unpack_decorr_stereo_pass_cont_x64win,"r"
	.global __imp_unpack_decorr_stereo_pass_cont_x64win
__imp_unpack_decorr_stereo_pass_cont_x64win:
	.quad	unpack_decorr_stereo_pass_cont_x64win
	.linkonce discard
#endif