Debian Codegraph @

build/slices/sums_asm.go

raw
1 // +build ignore
2
3 package main
4
5 import (
6 "fmt"
7
8 . "github.com/mmcloughlin/avo/build"
9 . "github.com/mmcloughlin/avo/operand"
10 . "github.com/segmentio/asm/build/internal/x86"
11
12 "github.com/mmcloughlin/avo/reg"
13 "github.com/segmentio/asm/cpu"
14 )
15
16 const unroll = 8
17
18 type Processor struct {
19 name string
20 typ string
21 scale uint8
22 avxOffset uint64
23 avxAdd func(...Op)
24 x86Mov func(imr, mr Op)
25 x86Add func(imr, amr Op)
26 x86Reg reg.GPVirtual
27 }
28
29 func init() {
30 ConstraintExpr("!purego")
31 }
32
33 func main() {
34 generate(Processor{
35 name: "sumUint64",
36 typ: "uint64",
37 scale: 8,
38 avxOffset: 2,
39 avxAdd: VPADDQ,
40 x86Mov: MOVQ,
41 x86Add: ADDQ,
42 x86Reg: GP64(),
43 })
44
45 generate(Processor{
46 name: "sumUint32",
47 typ: "uint32",
48 scale: 4,
49 avxOffset: 4,
50 avxAdd: VPADDD,
51 x86Mov: MOVL,
52 x86Add: ADDL,
53 x86Reg: GP32(),
54 })
55
56 generate(Processor{
57 name: "sumUint16",
58 typ: "uint16",
59 scale: 2,
60 avxOffset: 8,
61 avxAdd: VPADDW,
62 x86Mov: MOVW,
63 x86Add: ADDW,
64 x86Reg: GP16(),
65 })
66
67 generate(Processor{
68 name: "sumUint8",
69 typ: "uint8",
70 scale: 1,
71 avxOffset: 16,
72 avxAdd: VPADDB,
73 x86Mov: MOVB,
74 x86Add: ADDB,
75 x86Reg: GP8(),
76 })
77
78 Generate()
79 }
80
81 func generate(p Processor) {
82 TEXT(p.name, NOSPLIT, fmt.Sprintf("func(x, y []%s)", p.typ))
83 Doc(fmt.Sprintf("Sum %ss using avx2 instructions, results stored in x", p.typ))
84 idx := GP64()
85 XORQ(idx, idx)
86 xPtr := Mem{Base: Load(Param("x").Base(), GP64()), Index: idx, Scale: p.scale}
87 yPtr := Mem{Base: Load(Param("y").Base(), GP64()), Index: idx, Scale: p.scale}
88 len := Load(Param("x").Len(), GP64())
89 yLen := Load(Param("y").Len(), GP64())
90 // len = min(len(x), len(y))
91 CMPQ(yLen, len)
92 CMOVQLT(yLen, len)
93
94 JumpUnlessFeature("x86_loop", cpu.AVX2)
95
96 Label("avx2_loop")
97 next := GP64()
98 MOVQ(idx, next)
99 ADDQ(Imm(unroll*p.avxOffset), next)
100 CMPQ(next, len)
101 JAE(LabelRef("x86_loop"))
102
103 // Create unroll num vector registers
104 var vectors [unroll]reg.VecVirtual
105 for i := 0; i < unroll; i++ {
106 vectors[i] = YMM()
107 }
108 // So here essentially what we're doing is populating pairs
109 // of vector registers with 256 bits of integer data, so as an example
110 // for uint64s, it would look like...
111 // YMM0 [ x0, x1, x2, x3 ]
112 // YMM1 [ y0, y1, y2, y3 ]
113 // ...
114 // YMM(N) ...
115 //
116 // We then use VPADDQ to perform a SIMD addition operation
117 // on the pairs and the result is stored in even register (0,2,4...).
118 // Finally we copy the results back out to the slice pointed to by x
119 for offset, i := 0, 0; i < unroll/2; i++ {
120 VMOVDQU(xPtr.Offset(i*32), vectors[offset])
121 VMOVDQU(yPtr.Offset(i*32), vectors[offset+1])
122 offset += 2
123 }
124
125 // AVX intrinsics to sum 64 bit integers/quad words
126 for offset, i := 0, 0; i < unroll/2; i++ {
127 p.avxAdd(vectors[offset], vectors[offset+1], vectors[offset])
128 offset += 2
129 }
130
131 for offset, i := 0, 0; i < unroll/2; i++ {
132 VMOVDQU(vectors[offset], xPtr.Offset(i*32))
133 offset += 2
134 }
135 // Increment ptrs and loop.
136 MOVQ(next, idx)
137 JMP(LabelRef("avx2_loop"))
138
139 // Here's we're just going to manually bump our pointers
140 // and do a the addition on the remaining integers (if any)
141 Label("x86_loop")
142 CMPQ(idx, len)
143 JAE(LabelRef("return"))
144
145 // Delegate to specific computation
146 //calc()
147 p.x86Mov(yPtr, p.x86Reg)
148 p.x86Add(p.x86Reg, xPtr)
149
150 // Increment ptrs and loop.
151 ADDQ(Imm(1), idx)
152 JMP(LabelRef("x86_loop"))
153
154 Label("return")
155 RET()
156 }