aboutsummaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/image/vector/acc_amd64.s
blob: 6a424bcddbda17c31945f2fed15be9cf03c06c82 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
// generated by go run gen.go; DO NOT EDIT

// +build !appengine
// +build gc
// +build go1.6
// +build !noasm

#include "textflag.h"

// fl is short for floating point math. fx is short for fixed point math.

DATA flAlmost65536<>+0x00(SB)/8, $0x477fffff477fffff
DATA flAlmost65536<>+0x08(SB)/8, $0x477fffff477fffff
DATA flOne<>+0x00(SB)/8, $0x3f8000003f800000
DATA flOne<>+0x08(SB)/8, $0x3f8000003f800000
DATA flSignMask<>+0x00(SB)/8, $0x7fffffff7fffffff
DATA flSignMask<>+0x08(SB)/8, $0x7fffffff7fffffff

// scatterAndMulBy0x101 is a PSHUFB mask that brings the low four bytes of an
// XMM register to the low byte of that register's four uint32 values. It
// duplicates those bytes, effectively multiplying each uint32 by 0x101.
//
// It transforms a little-endian 16-byte XMM value from
//	ijkl????????????
// to
//	ii00jj00kk00ll00
DATA scatterAndMulBy0x101<>+0x00(SB)/8, $0x8080010180800000
DATA scatterAndMulBy0x101<>+0x08(SB)/8, $0x8080030380800202

// gather is a PSHUFB mask that brings the second-lowest byte of the XMM
// register's four uint32 values to the low four bytes of that register.
//
// It transforms a little-endian 16-byte XMM value from
//	?i???j???k???l??
// to
//	ijkl000000000000
DATA gather<>+0x00(SB)/8, $0x808080800d090501
DATA gather<>+0x08(SB)/8, $0x8080808080808080

DATA fxAlmost65536<>+0x00(SB)/8, $0x0000ffff0000ffff
DATA fxAlmost65536<>+0x08(SB)/8, $0x0000ffff0000ffff
DATA inverseFFFF<>+0x00(SB)/8, $0x8000800180008001
DATA inverseFFFF<>+0x08(SB)/8, $0x8000800180008001

GLOBL flAlmost65536<>(SB), (NOPTR+RODATA), $16
GLOBL flOne<>(SB), (NOPTR+RODATA), $16
GLOBL flSignMask<>(SB), (NOPTR+RODATA), $16
GLOBL scatterAndMulBy0x101<>(SB), (NOPTR+RODATA), $16
GLOBL gather<>(SB), (NOPTR+RODATA), $16
GLOBL fxAlmost65536<>(SB), (NOPTR+RODATA), $16
GLOBL inverseFFFF<>(SB), (NOPTR+RODATA), $16

// func haveSSE4_1() bool
TEXT ·haveSSE4_1(SB), NOSPLIT, $0
	MOVQ $1, AX
	CPUID
	SHRQ $19, CX
	ANDQ $1, CX
	MOVB CX, ret+0(FP)
	RET

// ----------------------------------------------------------------------------

// func fixedAccumulateOpOverSIMD(dst []uint8, src []uint32)
//
// XMM registers. Variable names are per
// https://github.com/google/font-rs/blob/master/src/accumulate.c
//
//	xmm0	scratch
//	xmm1	x
//	xmm2	y, z
//	xmm3	-
//	xmm4	-
//	xmm5	fxAlmost65536
//	xmm6	gather
//	xmm7	offset
//	xmm8	scatterAndMulBy0x101
//	xmm9	fxAlmost65536
//	xmm10	inverseFFFF
TEXT ·fixedAccumulateOpOverSIMD(SB), NOSPLIT, $0-48

	MOVQ dst_base+0(FP), DI
	MOVQ dst_len+8(FP), BX
	MOVQ src_base+24(FP), SI
	MOVQ src_len+32(FP), R10

	// Sanity check that len(dst) >= len(src).
	CMPQ BX, R10
	JLT  fxAccOpOverEnd

	// R10 = len(src) &^ 3
	// R11 = len(src)
	MOVQ R10, R11
	ANDQ $-4, R10

	// fxAlmost65536 := XMM(0x0000ffff repeated four times) // Maximum of an uint16.
	MOVOU fxAlmost65536<>(SB), X5

	// gather               := XMM(see above)                      // PSHUFB shuffle mask.
	// scatterAndMulBy0x101 := XMM(see above)                      // PSHUFB shuffle mask.
	// fxAlmost65536        := XMM(0x0000ffff repeated four times) // 0xffff.
	// inverseFFFF          := XMM(0x80008001 repeated four times) // Magic constant for dividing by 0xffff.
	MOVOU gather<>(SB), X6
	MOVOU scatterAndMulBy0x101<>(SB), X8
	MOVOU fxAlmost65536<>(SB), X9
	MOVOU inverseFFFF<>(SB), X10

	// offset := XMM(0x00000000 repeated four times) // Cumulative sum.
	XORPS X7, X7

	// i := 0
	MOVQ $0, R9

fxAccOpOverLoop4:
	// for i < (len(src) &^ 3)
	CMPQ R9, R10
	JAE  fxAccOpOverLoop1

	// x = XMM(s0, s1, s2, s3)
	//
	// Where s0 is src[i+0], s1 is src[i+1], etc.
	MOVOU (SI), X1

	// scratch = XMM(0, s0, s1, s2)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s1+s2, s2+s3)
	MOVOU X1, X0
	PSLLO $4, X0
	PADDD X0, X1

	// scratch = XMM(0, 0, 0, 0)
	// scratch = XMM(scratch@0, scratch@0, x@0, x@1) // yields scratch == XMM(0, 0, s0, s0+s1)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s0+s1+s2, s0+s1+s2+s3)
	XORPS  X0, X0
	SHUFPS $0x40, X1, X0
	PADDD  X0, X1

	// x += offset
	PADDD X7, X1

	// y = abs(x)
	// y >>= 2 // Shift by 2*ϕ - 16.
	// y = min(y, fxAlmost65536)
	//
	// pabsd  %xmm1,%xmm2
	// psrld  $0x2,%xmm2
	// pminud %xmm5,%xmm2
	//
	// Hopefully we'll get these opcode mnemonics into the assembler for Go
	// 1.8. https://golang.org/issue/16007 isn't exactly the same thing, but
	// it's similar.
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x1e; BYTE $0xd1
	BYTE $0x66; BYTE $0x0f; BYTE $0x72; BYTE $0xd2; BYTE $0x02
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x3b; BYTE $0xd5

	// z = convertToInt32(y)
	// No-op.

	// Blend over the dst's prior value. SIMD for i in 0..3:
	//
	// dstA := uint32(dst[i]) * 0x101
	// maskA := z@i
	// outA := dstA*(0xffff-maskA)/0xffff + maskA
	// dst[i] = uint8(outA >> 8)
	//
	// First, set X0 to dstA*(0xfff-maskA).
	MOVL   (DI), X0
	PSHUFB X8, X0
	MOVOU  X9, X11
	PSUBL  X2, X11
	PMULLD X11, X0

	// We implement uint32 division by 0xffff as multiplication by a magic
	// constant (0x800080001) and then a shift by a magic constant (47).
	// See TestDivideByFFFF for a justification.
	//
	// That multiplication widens from uint32 to uint64, so we have to
	// duplicate and shift our four uint32s from one XMM register (X0) to
	// two XMM registers (X0 and X11).
	//
	// Move the second and fourth uint32s in X0 to be the first and third
	// uint32s in X11.
	MOVOU X0, X11
	PSRLQ $32, X11

	// Multiply by magic, shift by magic.
	//
	// pmuludq %xmm10,%xmm0
	// pmuludq %xmm10,%xmm11
	BYTE  $0x66; BYTE $0x41; BYTE $0x0f; BYTE $0xf4; BYTE $0xc2
	BYTE  $0x66; BYTE $0x45; BYTE $0x0f; BYTE $0xf4; BYTE $0xda
	PSRLQ $47, X0
	PSRLQ $47, X11

	// Merge the two registers back to one, X11, and add maskA.
	PSLLQ $32, X11
	XORPS X0, X11
	PADDD X11, X2

	// As per opSrcStore4, shuffle and copy the 4 second-lowest bytes.
	PSHUFB X6, X2
	MOVL   X2, (DI)

	// offset = XMM(x@3, x@3, x@3, x@3)
	MOVOU  X1, X7
	SHUFPS $0xff, X1, X7

	// i += 4
	// dst = dst[4:]
	// src = src[4:]
	ADDQ $4, R9
	ADDQ $4, DI
	ADDQ $16, SI
	JMP  fxAccOpOverLoop4

fxAccOpOverLoop1:
	// for i < len(src)
	CMPQ R9, R11
	JAE  fxAccOpOverEnd

	// x = src[i] + offset
	MOVL  (SI), X1
	PADDD X7, X1

	// y = abs(x)
	// y >>= 2 // Shift by 2*ϕ - 16.
	// y = min(y, fxAlmost65536)
	//
	// pabsd  %xmm1,%xmm2
	// psrld  $0x2,%xmm2
	// pminud %xmm5,%xmm2
	//
	// Hopefully we'll get these opcode mnemonics into the assembler for Go
	// 1.8. https://golang.org/issue/16007 isn't exactly the same thing, but
	// it's similar.
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x1e; BYTE $0xd1
	BYTE $0x66; BYTE $0x0f; BYTE $0x72; BYTE $0xd2; BYTE $0x02
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x3b; BYTE $0xd5

	// z = convertToInt32(y)
	// No-op.

	// Blend over the dst's prior value.
	//
	// dstA := uint32(dst[0]) * 0x101
	// maskA := z
	// outA := dstA*(0xffff-maskA)/0xffff + maskA
	// dst[0] = uint8(outA >> 8)
	MOVBLZX (DI), R12
	IMULL   $0x101, R12
	MOVL    X2, R13
	MOVL    $0xffff, AX
	SUBL    R13, AX
	MULL    R12             // MULL's implicit arg is AX, and the result is stored in DX:AX.
	MOVL    $0x80008001, BX // Divide by 0xffff is to first multiply by a magic constant...
	MULL    BX              // MULL's implicit arg is AX, and the result is stored in DX:AX.
	SHRL    $15, DX         // ...and then shift by another magic constant (47 - 32 = 15).
	ADDL    DX, R13
	SHRL    $8, R13
	MOVB    R13, (DI)

	// offset = x
	MOVOU X1, X7

	// i += 1
	// dst = dst[1:]
	// src = src[1:]
	ADDQ $1, R9
	ADDQ $1, DI
	ADDQ $4, SI
	JMP  fxAccOpOverLoop1

fxAccOpOverEnd:
	RET

// ----------------------------------------------------------------------------

// func fixedAccumulateOpSrcSIMD(dst []uint8, src []uint32)
//
// XMM registers. Variable names are per
// https://github.com/google/font-rs/blob/master/src/accumulate.c
//
//	xmm0	scratch
//	xmm1	x
//	xmm2	y, z
//	xmm3	-
//	xmm4	-
//	xmm5	fxAlmost65536
//	xmm6	gather
//	xmm7	offset
//	xmm8	-
//	xmm9	-
//	xmm10	-
TEXT ·fixedAccumulateOpSrcSIMD(SB), NOSPLIT, $0-48

	MOVQ dst_base+0(FP), DI
	MOVQ dst_len+8(FP), BX
	MOVQ src_base+24(FP), SI
	MOVQ src_len+32(FP), R10

	// Sanity check that len(dst) >= len(src).
	CMPQ BX, R10
	JLT  fxAccOpSrcEnd

	// R10 = len(src) &^ 3
	// R11 = len(src)
	MOVQ R10, R11
	ANDQ $-4, R10

	// fxAlmost65536 := XMM(0x0000ffff repeated four times) // Maximum of an uint16.
	MOVOU fxAlmost65536<>(SB), X5

	// gather := XMM(see above) // PSHUFB shuffle mask.
	MOVOU gather<>(SB), X6

	// offset := XMM(0x00000000 repeated four times) // Cumulative sum.
	XORPS X7, X7

	// i := 0
	MOVQ $0, R9

fxAccOpSrcLoop4:
	// for i < (len(src) &^ 3)
	CMPQ R9, R10
	JAE  fxAccOpSrcLoop1

	// x = XMM(s0, s1, s2, s3)
	//
	// Where s0 is src[i+0], s1 is src[i+1], etc.
	MOVOU (SI), X1

	// scratch = XMM(0, s0, s1, s2)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s1+s2, s2+s3)
	MOVOU X1, X0
	PSLLO $4, X0
	PADDD X0, X1

	// scratch = XMM(0, 0, 0, 0)
	// scratch = XMM(scratch@0, scratch@0, x@0, x@1) // yields scratch == XMM(0, 0, s0, s0+s1)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s0+s1+s2, s0+s1+s2+s3)
	XORPS  X0, X0
	SHUFPS $0x40, X1, X0
	PADDD  X0, X1

	// x += offset
	PADDD X7, X1

	// y = abs(x)
	// y >>= 2 // Shift by 2*ϕ - 16.
	// y = min(y, fxAlmost65536)
	//
	// pabsd  %xmm1,%xmm2
	// psrld  $0x2,%xmm2
	// pminud %xmm5,%xmm2
	//
	// Hopefully we'll get these opcode mnemonics into the assembler for Go
	// 1.8. https://golang.org/issue/16007 isn't exactly the same thing, but
	// it's similar.
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x1e; BYTE $0xd1
	BYTE $0x66; BYTE $0x0f; BYTE $0x72; BYTE $0xd2; BYTE $0x02
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x3b; BYTE $0xd5

	// z = convertToInt32(y)
	// No-op.

	// z = shuffleTheSecondLowestBytesOfEach4ByteElement(z)
	// copy(dst[:4], low4BytesOf(z))
	PSHUFB X6, X2
	MOVL   X2, (DI)

	// offset = XMM(x@3, x@3, x@3, x@3)
	MOVOU  X1, X7
	SHUFPS $0xff, X1, X7

	// i += 4
	// dst = dst[4:]
	// src = src[4:]
	ADDQ $4, R9
	ADDQ $4, DI
	ADDQ $16, SI
	JMP  fxAccOpSrcLoop4

fxAccOpSrcLoop1:
	// for i < len(src)
	CMPQ R9, R11
	JAE  fxAccOpSrcEnd

	// x = src[i] + offset
	MOVL  (SI), X1
	PADDD X7, X1

	// y = abs(x)
	// y >>= 2 // Shift by 2*ϕ - 16.
	// y = min(y, fxAlmost65536)
	//
	// pabsd  %xmm1,%xmm2
	// psrld  $0x2,%xmm2
	// pminud %xmm5,%xmm2
	//
	// Hopefully we'll get these opcode mnemonics into the assembler for Go
	// 1.8. https://golang.org/issue/16007 isn't exactly the same thing, but
	// it's similar.
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x1e; BYTE $0xd1
	BYTE $0x66; BYTE $0x0f; BYTE $0x72; BYTE $0xd2; BYTE $0x02
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x3b; BYTE $0xd5

	// z = convertToInt32(y)
	// No-op.

	// dst[0] = uint8(z>>8)
	MOVL X2, BX
	SHRL $8, BX
	MOVB BX, (DI)

	// offset = x
	MOVOU X1, X7

	// i += 1
	// dst = dst[1:]
	// src = src[1:]
	ADDQ $1, R9
	ADDQ $1, DI
	ADDQ $4, SI
	JMP  fxAccOpSrcLoop1

fxAccOpSrcEnd:
	RET

// ----------------------------------------------------------------------------

// func fixedAccumulateMaskSIMD(buf []uint32)
//
// XMM registers. Variable names are per
// https://github.com/google/font-rs/blob/master/src/accumulate.c
//
//	xmm0	scratch
//	xmm1	x
//	xmm2	y, z
//	xmm3	-
//	xmm4	-
//	xmm5	fxAlmost65536
//	xmm6	-
//	xmm7	offset
//	xmm8	-
//	xmm9	-
//	xmm10	-
TEXT ·fixedAccumulateMaskSIMD(SB), NOSPLIT, $0-24

	MOVQ buf_base+0(FP), DI
	MOVQ buf_len+8(FP), BX
	MOVQ buf_base+0(FP), SI
	MOVQ buf_len+8(FP), R10

	// R10 = len(src) &^ 3
	// R11 = len(src)
	MOVQ R10, R11
	ANDQ $-4, R10

	// fxAlmost65536 := XMM(0x0000ffff repeated four times) // Maximum of an uint16.
	MOVOU fxAlmost65536<>(SB), X5

	// offset := XMM(0x00000000 repeated four times) // Cumulative sum.
	XORPS X7, X7

	// i := 0
	MOVQ $0, R9

fxAccMaskLoop4:
	// for i < (len(src) &^ 3)
	CMPQ R9, R10
	JAE  fxAccMaskLoop1

	// x = XMM(s0, s1, s2, s3)
	//
	// Where s0 is src[i+0], s1 is src[i+1], etc.
	MOVOU (SI), X1

	// scratch = XMM(0, s0, s1, s2)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s1+s2, s2+s3)
	MOVOU X1, X0
	PSLLO $4, X0
	PADDD X0, X1

	// scratch = XMM(0, 0, 0, 0)
	// scratch = XMM(scratch@0, scratch@0, x@0, x@1) // yields scratch == XMM(0, 0, s0, s0+s1)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s0+s1+s2, s0+s1+s2+s3)
	XORPS  X0, X0
	SHUFPS $0x40, X1, X0
	PADDD  X0, X1

	// x += offset
	PADDD X7, X1

	// y = abs(x)
	// y >>= 2 // Shift by 2*ϕ - 16.
	// y = min(y, fxAlmost65536)
	//
	// pabsd  %xmm1,%xmm2
	// psrld  $0x2,%xmm2
	// pminud %xmm5,%xmm2
	//
	// Hopefully we'll get these opcode mnemonics into the assembler for Go
	// 1.8. https://golang.org/issue/16007 isn't exactly the same thing, but
	// it's similar.
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x1e; BYTE $0xd1
	BYTE $0x66; BYTE $0x0f; BYTE $0x72; BYTE $0xd2; BYTE $0x02
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x3b; BYTE $0xd5

	// z = convertToInt32(y)
	// No-op.

	// copy(dst[:4], z)
	MOVOU X2, (DI)

	// offset = XMM(x@3, x@3, x@3, x@3)
	MOVOU  X1, X7
	SHUFPS $0xff, X1, X7

	// i += 4
	// dst = dst[4:]
	// src = src[4:]
	ADDQ $4, R9
	ADDQ $16, DI
	ADDQ $16, SI
	JMP  fxAccMaskLoop4

fxAccMaskLoop1:
	// for i < len(src)
	CMPQ R9, R11
	JAE  fxAccMaskEnd

	// x = src[i] + offset
	MOVL  (SI), X1
	PADDD X7, X1

	// y = abs(x)
	// y >>= 2 // Shift by 2*ϕ - 16.
	// y = min(y, fxAlmost65536)
	//
	// pabsd  %xmm1,%xmm2
	// psrld  $0x2,%xmm2
	// pminud %xmm5,%xmm2
	//
	// Hopefully we'll get these opcode mnemonics into the assembler for Go
	// 1.8. https://golang.org/issue/16007 isn't exactly the same thing, but
	// it's similar.
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x1e; BYTE $0xd1
	BYTE $0x66; BYTE $0x0f; BYTE $0x72; BYTE $0xd2; BYTE $0x02
	BYTE $0x66; BYTE $0x0f; BYTE $0x38; BYTE $0x3b; BYTE $0xd5

	// z = convertToInt32(y)
	// No-op.

	// dst[0] = uint32(z)
	MOVL X2, (DI)

	// offset = x
	MOVOU X1, X7

	// i += 1
	// dst = dst[1:]
	// src = src[1:]
	ADDQ $1, R9
	ADDQ $4, DI
	ADDQ $4, SI
	JMP  fxAccMaskLoop1

fxAccMaskEnd:
	RET

// ----------------------------------------------------------------------------

// func floatingAccumulateOpOverSIMD(dst []uint8, src []float32)
//
// XMM registers. Variable names are per
// https://github.com/google/font-rs/blob/master/src/accumulate.c
//
//	xmm0	scratch
//	xmm1	x
//	xmm2	y, z
//	xmm3	flSignMask
//	xmm4	flOne
//	xmm5	flAlmost65536
//	xmm6	gather
//	xmm7	offset
//	xmm8	scatterAndMulBy0x101
//	xmm9	fxAlmost65536
//	xmm10	inverseFFFF
TEXT ·floatingAccumulateOpOverSIMD(SB), NOSPLIT, $8-48

	MOVQ dst_base+0(FP), DI
	MOVQ dst_len+8(FP), BX
	MOVQ src_base+24(FP), SI
	MOVQ src_len+32(FP), R10

	// Sanity check that len(dst) >= len(src).
	CMPQ BX, R10
	JLT  flAccOpOverEnd

	// R10 = len(src) &^ 3
	// R11 = len(src)
	MOVQ R10, R11
	ANDQ $-4, R10

	// Prepare to set MXCSR bits 13 and 14, so that the CVTPS2PL below is
	// "Round To Zero".
	STMXCSR mxcsrOrig-8(SP)
	MOVL    mxcsrOrig-8(SP), AX
	ORL     $0x6000, AX
	MOVL    AX, mxcsrNew-4(SP)

	// flSignMask    := XMM(0x7fffffff repeated four times) // All but the sign bit of a float32.
	// flOne         := XMM(0x3f800000 repeated four times) // 1 as a float32.
	// flAlmost65536 := XMM(0x477fffff repeated four times) // 255.99998 * 256 as a float32.
	MOVOU flSignMask<>(SB), X3
	MOVOU flOne<>(SB), X4
	MOVOU flAlmost65536<>(SB), X5

	// gather               := XMM(see above)                      // PSHUFB shuffle mask.
	// scatterAndMulBy0x101 := XMM(see above)                      // PSHUFB shuffle mask.
	// fxAlmost65536        := XMM(0x0000ffff repeated four times) // 0xffff.
	// inverseFFFF          := XMM(0x80008001 repeated four times) // Magic constant for dividing by 0xffff.
	MOVOU gather<>(SB), X6
	MOVOU scatterAndMulBy0x101<>(SB), X8
	MOVOU fxAlmost65536<>(SB), X9
	MOVOU inverseFFFF<>(SB), X10

	// offset := XMM(0x00000000 repeated four times) // Cumulative sum.
	XORPS X7, X7

	// i := 0
	MOVQ $0, R9

flAccOpOverLoop4:
	// for i < (len(src) &^ 3)
	CMPQ R9, R10
	JAE  flAccOpOverLoop1

	// x = XMM(s0, s1, s2, s3)
	//
	// Where s0 is src[i+0], s1 is src[i+1], etc.
	MOVOU (SI), X1

	// scratch = XMM(0, s0, s1, s2)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s1+s2, s2+s3)
	MOVOU X1, X0
	PSLLO $4, X0
	ADDPS X0, X1

	// scratch = XMM(0, 0, 0, 0)
	// scratch = XMM(scratch@0, scratch@0, x@0, x@1) // yields scratch == XMM(0, 0, s0, s0+s1)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s0+s1+s2, s0+s1+s2+s3)
	XORPS  X0, X0
	SHUFPS $0x40, X1, X0
	ADDPS  X0, X1

	// x += offset
	ADDPS X7, X1

	// y = x & flSignMask
	// y = min(y, flOne)
	// y = mul(y, flAlmost65536)
	MOVOU X3, X2
	ANDPS X1, X2
	MINPS X4, X2
	MULPS X5, X2

	// z = convertToInt32(y)
	LDMXCSR  mxcsrNew-4(SP)
	CVTPS2PL X2, X2
	LDMXCSR  mxcsrOrig-8(SP)

	// Blend over the dst's prior value. SIMD for i in 0..3:
	//
	// dstA := uint32(dst[i]) * 0x101
	// maskA := z@i
	// outA := dstA*(0xffff-maskA)/0xffff + maskA
	// dst[i] = uint8(outA >> 8)
	//
	// First, set X0 to dstA*(0xfff-maskA).
	MOVL   (DI), X0
	PSHUFB X8, X0
	MOVOU  X9, X11
	PSUBL  X2, X11
	PMULLD X11, X0

	// We implement uint32 division by 0xffff as multiplication by a magic
	// constant (0x800080001) and then a shift by a magic constant (47).
	// See TestDivideByFFFF for a justification.
	//
	// That multiplication widens from uint32 to uint64, so we have to
	// duplicate and shift our four uint32s from one XMM register (X0) to
	// two XMM registers (X0 and X11).
	//
	// Move the second and fourth uint32s in X0 to be the first and third
	// uint32s in X11.
	MOVOU X0, X11
	PSRLQ $32, X11

	// Multiply by magic, shift by magic.
	//
	// pmuludq %xmm10,%xmm0
	// pmuludq %xmm10,%xmm11
	BYTE  $0x66; BYTE $0x41; BYTE $0x0f; BYTE $0xf4; BYTE $0xc2
	BYTE  $0x66; BYTE $0x45; BYTE $0x0f; BYTE $0xf4; BYTE $0xda
	PSRLQ $47, X0
	PSRLQ $47, X11

	// Merge the two registers back to one, X11, and add maskA.
	PSLLQ $32, X11
	XORPS X0, X11
	PADDD X11, X2

	// As per opSrcStore4, shuffle and copy the 4 second-lowest bytes.
	PSHUFB X6, X2
	MOVL   X2, (DI)

	// offset = XMM(x@3, x@3, x@3, x@3)
	MOVOU  X1, X7
	SHUFPS $0xff, X1, X7

	// i += 4
	// dst = dst[4:]
	// src = src[4:]
	ADDQ $4, R9
	ADDQ $4, DI
	ADDQ $16, SI
	JMP  flAccOpOverLoop4

flAccOpOverLoop1:
	// for i < len(src)
	CMPQ R9, R11
	JAE  flAccOpOverEnd

	// x = src[i] + offset
	MOVL  (SI), X1
	ADDPS X7, X1

	// y = x & flSignMask
	// y = min(y, flOne)
	// y = mul(y, flAlmost65536)
	MOVOU X3, X2
	ANDPS X1, X2
	MINPS X4, X2
	MULPS X5, X2

	// z = convertToInt32(y)
	LDMXCSR  mxcsrNew-4(SP)
	CVTPS2PL X2, X2
	LDMXCSR  mxcsrOrig-8(SP)

	// Blend over the dst's prior value.
	//
	// dstA := uint32(dst[0]) * 0x101
	// maskA := z
	// outA := dstA*(0xffff-maskA)/0xffff + maskA
	// dst[0] = uint8(outA >> 8)
	MOVBLZX (DI), R12
	IMULL   $0x101, R12
	MOVL    X2, R13
	MOVL    $0xffff, AX
	SUBL    R13, AX
	MULL    R12             // MULL's implicit arg is AX, and the result is stored in DX:AX.
	MOVL    $0x80008001, BX // Divide by 0xffff is to first multiply by a magic constant...
	MULL    BX              // MULL's implicit arg is AX, and the result is stored in DX:AX.
	SHRL    $15, DX         // ...and then shift by another magic constant (47 - 32 = 15).
	ADDL    DX, R13
	SHRL    $8, R13
	MOVB    R13, (DI)

	// offset = x
	MOVOU X1, X7

	// i += 1
	// dst = dst[1:]
	// src = src[1:]
	ADDQ $1, R9
	ADDQ $1, DI
	ADDQ $4, SI
	JMP  flAccOpOverLoop1

flAccOpOverEnd:
	RET

// ----------------------------------------------------------------------------

// func floatingAccumulateOpSrcSIMD(dst []uint8, src []float32)
//
// XMM registers. Variable names are per
// https://github.com/google/font-rs/blob/master/src/accumulate.c
//
//	xmm0	scratch
//	xmm1	x
//	xmm2	y, z
//	xmm3	flSignMask
//	xmm4	flOne
//	xmm5	flAlmost65536
//	xmm6	gather
//	xmm7	offset
//	xmm8	-
//	xmm9	-
//	xmm10	-
TEXT ·floatingAccumulateOpSrcSIMD(SB), NOSPLIT, $8-48

	MOVQ dst_base+0(FP), DI
	MOVQ dst_len+8(FP), BX
	MOVQ src_base+24(FP), SI
	MOVQ src_len+32(FP), R10

	// Sanity check that len(dst) >= len(src).
	CMPQ BX, R10
	JLT  flAccOpSrcEnd

	// R10 = len(src) &^ 3
	// R11 = len(src)
	MOVQ R10, R11
	ANDQ $-4, R10

	// Prepare to set MXCSR bits 13 and 14, so that the CVTPS2PL below is
	// "Round To Zero".
	STMXCSR mxcsrOrig-8(SP)
	MOVL    mxcsrOrig-8(SP), AX
	ORL     $0x6000, AX
	MOVL    AX, mxcsrNew-4(SP)

	// flSignMask    := XMM(0x7fffffff repeated four times) // All but the sign bit of a float32.
	// flOne         := XMM(0x3f800000 repeated four times) // 1 as a float32.
	// flAlmost65536 := XMM(0x477fffff repeated four times) // 255.99998 * 256 as a float32.
	MOVOU flSignMask<>(SB), X3
	MOVOU flOne<>(SB), X4
	MOVOU flAlmost65536<>(SB), X5

	// gather := XMM(see above) // PSHUFB shuffle mask.
	MOVOU gather<>(SB), X6

	// offset := XMM(0x00000000 repeated four times) // Cumulative sum.
	XORPS X7, X7

	// i := 0
	MOVQ $0, R9

flAccOpSrcLoop4:
	// for i < (len(src) &^ 3)
	CMPQ R9, R10
	JAE  flAccOpSrcLoop1

	// x = XMM(s0, s1, s2, s3)
	//
	// Where s0 is src[i+0], s1 is src[i+1], etc.
	MOVOU (SI), X1

	// scratch = XMM(0, s0, s1, s2)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s1+s2, s2+s3)
	MOVOU X1, X0
	PSLLO $4, X0
	ADDPS X0, X1

	// scratch = XMM(0, 0, 0, 0)
	// scratch = XMM(scratch@0, scratch@0, x@0, x@1) // yields scratch == XMM(0, 0, s0, s0+s1)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s0+s1+s2, s0+s1+s2+s3)
	XORPS  X0, X0
	SHUFPS $0x40, X1, X0
	ADDPS  X0, X1

	// x += offset
	ADDPS X7, X1

	// y = x & flSignMask
	// y = min(y, flOne)
	// y = mul(y, flAlmost65536)
	MOVOU X3, X2
	ANDPS X1, X2
	MINPS X4, X2
	MULPS X5, X2

	// z = convertToInt32(y)
	LDMXCSR  mxcsrNew-4(SP)
	CVTPS2PL X2, X2
	LDMXCSR  mxcsrOrig-8(SP)

	// z = shuffleTheSecondLowestBytesOfEach4ByteElement(z)
	// copy(dst[:4], low4BytesOf(z))
	PSHUFB X6, X2
	MOVL   X2, (DI)

	// offset = XMM(x@3, x@3, x@3, x@3)
	MOVOU  X1, X7
	SHUFPS $0xff, X1, X7

	// i += 4
	// dst = dst[4:]
	// src = src[4:]
	ADDQ $4, R9
	ADDQ $4, DI
	ADDQ $16, SI
	JMP  flAccOpSrcLoop4

flAccOpSrcLoop1:
	// for i < len(src)
	CMPQ R9, R11
	JAE  flAccOpSrcEnd

	// x = src[i] + offset
	MOVL  (SI), X1
	ADDPS X7, X1

	// y = x & flSignMask
	// y = min(y, flOne)
	// y = mul(y, flAlmost65536)
	MOVOU X3, X2
	ANDPS X1, X2
	MINPS X4, X2
	MULPS X5, X2

	// z = convertToInt32(y)
	LDMXCSR  mxcsrNew-4(SP)
	CVTPS2PL X2, X2
	LDMXCSR  mxcsrOrig-8(SP)

	// dst[0] = uint8(z>>8)
	MOVL X2, BX
	SHRL $8, BX
	MOVB BX, (DI)

	// offset = x
	MOVOU X1, X7

	// i += 1
	// dst = dst[1:]
	// src = src[1:]
	ADDQ $1, R9
	ADDQ $1, DI
	ADDQ $4, SI
	JMP  flAccOpSrcLoop1

flAccOpSrcEnd:
	RET

// ----------------------------------------------------------------------------

// func floatingAccumulateMaskSIMD(dst []uint32, src []float32)
//
// XMM registers. Variable names are per
// https://github.com/google/font-rs/blob/master/src/accumulate.c
//
//	xmm0	scratch
//	xmm1	x
//	xmm2	y, z
//	xmm3	flSignMask
//	xmm4	flOne
//	xmm5	flAlmost65536
//	xmm6	-
//	xmm7	offset
//	xmm8	-
//	xmm9	-
//	xmm10	-
TEXT ·floatingAccumulateMaskSIMD(SB), NOSPLIT, $8-48

	MOVQ dst_base+0(FP), DI
	MOVQ dst_len+8(FP), BX
	MOVQ src_base+24(FP), SI
	MOVQ src_len+32(FP), R10

	// Sanity check that len(dst) >= len(src).
	CMPQ BX, R10
	JLT  flAccMaskEnd

	// R10 = len(src) &^ 3
	// R11 = len(src)
	MOVQ R10, R11
	ANDQ $-4, R10

	// Prepare to set MXCSR bits 13 and 14, so that the CVTPS2PL below is
	// "Round To Zero".
	STMXCSR mxcsrOrig-8(SP)
	MOVL    mxcsrOrig-8(SP), AX
	ORL     $0x6000, AX
	MOVL    AX, mxcsrNew-4(SP)

	// flSignMask    := XMM(0x7fffffff repeated four times) // All but the sign bit of a float32.
	// flOne         := XMM(0x3f800000 repeated four times) // 1 as a float32.
	// flAlmost65536 := XMM(0x477fffff repeated four times) // 255.99998 * 256 as a float32.
	MOVOU flSignMask<>(SB), X3
	MOVOU flOne<>(SB), X4
	MOVOU flAlmost65536<>(SB), X5

	// offset := XMM(0x00000000 repeated four times) // Cumulative sum.
	XORPS X7, X7

	// i := 0
	MOVQ $0, R9

flAccMaskLoop4:
	// for i < (len(src) &^ 3)
	CMPQ R9, R10
	JAE  flAccMaskLoop1

	// x = XMM(s0, s1, s2, s3)
	//
	// Where s0 is src[i+0], s1 is src[i+1], etc.
	MOVOU (SI), X1

	// scratch = XMM(0, s0, s1, s2)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s1+s2, s2+s3)
	MOVOU X1, X0
	PSLLO $4, X0
	ADDPS X0, X1

	// scratch = XMM(0, 0, 0, 0)
	// scratch = XMM(scratch@0, scratch@0, x@0, x@1) // yields scratch == XMM(0, 0, s0, s0+s1)
	// x += scratch                                  // yields x == XMM(s0, s0+s1, s0+s1+s2, s0+s1+s2+s3)
	XORPS  X0, X0
	SHUFPS $0x40, X1, X0
	ADDPS  X0, X1

	// x += offset
	ADDPS X7, X1

	// y = x & flSignMask
	// y = min(y, flOne)
	// y = mul(y, flAlmost65536)
	MOVOU X3, X2
	ANDPS X1, X2
	MINPS X4, X2
	MULPS X5, X2

	// z = convertToInt32(y)
	LDMXCSR  mxcsrNew-4(SP)
	CVTPS2PL X2, X2
	LDMXCSR  mxcsrOrig-8(SP)

	// copy(dst[:4], z)
	MOVOU X2, (DI)

	// offset = XMM(x@3, x@3, x@3, x@3)
	MOVOU  X1, X7
	SHUFPS $0xff, X1, X7

	// i += 4
	// dst = dst[4:]
	// src = src[4:]
	ADDQ $4, R9
	ADDQ $16, DI
	ADDQ $16, SI
	JMP  flAccMaskLoop4

flAccMaskLoop1:
	// for i < len(src)
	CMPQ R9, R11
	JAE  flAccMaskEnd

	// x = src[i] + offset
	MOVL  (SI), X1
	ADDPS X7, X1

	// y = x & flSignMask
	// y = min(y, flOne)
	// y = mul(y, flAlmost65536)
	MOVOU X3, X2
	ANDPS X1, X2
	MINPS X4, X2
	MULPS X5, X2

	// z = convertToInt32(y)
	LDMXCSR  mxcsrNew-4(SP)
	CVTPS2PL X2, X2
	LDMXCSR  mxcsrOrig-8(SP)

	// dst[0] = uint32(z)
	MOVL X2, (DI)

	// offset = x
	MOVOU X1, X7

	// i += 1
	// dst = dst[1:]
	// src = src[1:]
	ADDQ $1, R9
	ADDQ $4, DI
	ADDQ $4, SI
	JMP  flAccMaskLoop1

flAccMaskEnd:
	RET