-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_decode_sse3.go
427 lines (336 loc) · 12.2 KB
/
gen_decode_sse3.go
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
// +build ignore
/*
Copyright (c) 2020 Brian M. Kessler
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import (
"encoding/binary"
. "github.com/mmcloughlin/avo/build"
. "github.com/mmcloughlin/avo/operand"
. "github.com/mmcloughlin/avo/reg"
)
// preamble loads the input data and returns variables referencing those values
func preamble(dataByteCount, dataByteMask Mem) (encoded Mem, encodedCap Register, data Mem, dataLen Register, dataTail GPVirtual, ci GPVirtual, di GPVirtual, n GPVirtual, byteCountPtr Mem, byteMaskptr Mem) {
encoded = Mem{Base: Load(Param("encoded").Base(), GP64())}
encodedCap = Load(Param("encoded").Cap(), GP64())
Comment("Revert to scalar processing if we are within 16 bytes of the end.")
SUBQ(Imm(16), encodedCap)
data = Mem{Base: Load(Param("data").Base(), GP64())}
dataLen = Load(Param("data").Len(), GP64())
dataTail = GP64()
Comment("Revert to scalar processing if we have less than 4 values to process.")
MOVQ(dataLen, dataTail)
SUBQ(Imm(4), dataTail)
Comment("Initialize the control index.")
ci = GP64()
XORQ(ci, ci)
Comment("Initialize the data index. (len(data) + 3) >> 2")
di = GP64()
MOVQ(dataLen, di)
ADDQ(Imm(3), di)
SHRQ(Imm(2), di)
Comment("Initialize the output index.")
n = GP64()
XORQ(n, n)
Comment("The byte count lookup table.")
byteCountPtr = Mem{Base: GP64()}
LEAQ(dataByteCount, byteCountPtr.Base)
Comment("The byte mask lookup table.")
byteMaskptr = Mem{Base: GP64()}
LEAQ(dataByteMask, byteMaskptr.Base)
return encoded, encodedCap, data, dataLen, dataTail, ci, di, n, byteCountPtr, byteMaskptr
}
// decodeSIMDUint32 reads control byte and 4 uint32 from data bytes and returns the count of bytes read aong with the dataBytes
func decodeSIMDUint32(encoded Mem, ci, di GPVirtual, byteCountPtr, byteMaskptr Mem) (VecVirtual, GPVirtual) {
Comment("Load control byte.")
cb := GP64()
MOVBQZX(encoded.Idx(ci, 1), cb)
INCQ(ci)
Comment("Load 16 data bytes into XMM.")
dataBytes := XMM()
MOVOU(encoded.Idx(di, 1), dataBytes)
Comment("Lookup count to increment data index.")
byteCount := GP64()
MOVBQZX(byteCountPtr.Idx(cb, 1), byteCount)
Comment("Lookup the PSHUFB mask.")
SHLQ(Imm(4), cb)
Comment("Use mask to shuffle the relevant bytes into place.")
PSHUFB(byteMaskptr.Idx(cb, 1), dataBytes)
return dataBytes, byteCount
}
// decodeScalarUint32 reads control byte and returns the decoded uint32 value
func decodeScalarUint32(n, ci, di GPVirtual, encoded, data Mem) (val GPVirtual) {
Comment("Determine if we need to load a new control byte.")
TESTQ(U32(3), n)
JNE(LabelRef("loadBytes"))
Comment("Load control byte.")
cb := GP64()
MOVBQZX(encoded.Idx(ci, 1), cb)
INCQ(ci)
Label("loadBytes")
Comment("Switch on the low two bits of the control byte.")
switchVal := GP64()
MOVQ(cb, switchVal)
ANDQ(Imm(3), switchVal)
JE(LabelRef("oneByte"))
CMPQ(switchVal, Imm(1))
JE(LabelRef("twoByte"))
CMPQ(switchVal, Imm(2))
JE(LabelRef("threeByte"))
val = GP32()
Label("fourByte")
MOVL(encoded.Idx(di, 1), val) // val = binary.LittleEndian.Uint32(encoded[di:])
ADDQ(Imm(4), di) // di += 4
JMP(LabelRef("shiftControl"))
Label("threeByte")
hi := GP32()
MOVWLZX(encoded.Idx(di, 1), val) // val = uint32(binary.LittleEndian.Uint16(encoded[di:]))
MOVBLZX(encoded.Idx(di, 1).Offset(2), hi) // hi = uint32(encoded[di+2])
SHLL(Imm(16), hi) // hi <<= 16
ORL(hi, val) // val = (hi | val)
ADDQ(Imm(3), di) // di +=3
JMP(LabelRef("shiftControl"))
Label("twoByte")
MOVWLZX(encoded.Idx(di, 1), val) // val = uint32(binary.LittleEndian.Uint16(encoded[di:]))
ADDQ(Imm(2), di) // di += 2
JMP(LabelRef("shiftControl"))
Label("oneByte")
MOVBLZX(encoded.Idx(di, 1), val) // val = uint32(encoded[di])
INCQ(di) // di++
Label("shiftControl")
Comment("Shift control byte to get next value.")
SHRQ(Imm(2), cb)
return val
}
func prefixSumSIMD(dataBytes, previousX VecVirtual) {
shifted := XMM()
Comment("Calculate prefix sum.")
//copy dataBytes to shifted
MOVOU(dataBytes, shifted)
Comment("(0, 0, delta_0, delta_1)")
PSLLDQ(Imm(8), shifted)
Comment("(delta_0, delta_1, delta_2 + delta_0, delta_3 + delta_1)")
PADDD(shifted, dataBytes)
// copy dataBytes to shifted
MOVOU(dataBytes, shifted)
Comment("(0, delta_0, delta_1, delta_2 + delta_0)")
PSLLDQ(Imm(4), shifted)
Comment("(delta_0, delta_0 + delta_1, delta_0 + delta_1 + delta_2, delta_0 + delta_1 + delta_2 + delta_delta_3)")
PADDD(shifted, dataBytes)
Comment("Add the previous last decoded value to all lanes.")
PADDD(previousX, dataBytes)
Comment("Propagate last decoded value to all lanes of previous.")
PSHUFD(Imm(0b_11_11_11_11), dataBytes, previousX)
}
func zigzagDecodeScalar(val GPVirtual) {
Comment("Zigzag decode.")
tmp := GP32()
MOVL(val, tmp)
SHRL(Imm(1), tmp)
ANDL(Imm(1), val)
NEGL(val)
XORL(tmp, val)
}
func zigzagDecodeSIMD(dataBytes VecVirtual) {
Comment("Zigzag decode.")
tmpX := XMM()
MOVOU(dataBytes, tmpX)
Comment("(x >> 1)")
PSRLL(Imm(1), tmpX)
oneX := XMM()
Comment("Set to all ones.")
PCMPEQL(oneX, oneX)
Comment("Shift to one in each lane.")
PSRLL(Imm(31), oneX)
Comment("(x & 1)")
PAND(dataBytes, oneX)
Comment("Set to all zeroes.")
PXOR(dataBytes, dataBytes)
Comment("-(x & 1)")
PSUBL(oneX, dataBytes)
Comment("(x >> 1) ^ - (x & 1)")
PXOR(tmpX, dataBytes)
}
func main() {
// Lookup table of the count of data bytes (4 to 16) referenced by a control byte.
dataByteCount := GLOBL("dataByteCount", RODATA|NOPTR)
for i := 0; i < 256; i++ {
count := byte(i&3) + byte((i>>2)&3) + byte((i>>4)&3) + byte((i>>6)&3) + 4
DATA(i, U8(count))
}
// Lookup table of the PSUFB mask referenced by a control byte to move data bytes
// into the correct location.
dataByteMask := GLOBL("dataByteMask", RODATA|NOPTR)
for i := 0; i < 256; i++ {
curIndex, controlByte := byte(0), byte(i)
mask := [16]byte{}
for j := 0; j < 4; j++ {
byteCount := controlByte & 3
for k := 0; k < 4; k++ {
if k <= int(byteCount) {
mask[4*j+k] = curIndex
curIndex++
} else {
mask[4*j+k] = 0xFF
}
}
controlByte >>= 2
}
lowerHalf := binary.LittleEndian.Uint64(mask[0:8])
upperHalf := binary.LittleEndian.Uint64(mask[8:16])
DATA(16*i, U64(lowerHalf))
DATA(16*i+8, U64(upperHalf))
}
TEXT("decodeUint32SSE3", NOSPLIT, "func (data []uint32, encoded []byte)")
Doc("decodeUint32SSE3 decodes 4 uint32 at a time using SSE3 instructions (PSHUFB)")
{
encoded, encodedCap, data, dataLen, dataTail, ci, di, n, byteCountPtr, byteMaskptr := preamble(dataByteCount, dataByteMask)
Label("simd")
Comment("Check if less than 16 encoded bytes remain and jump to scalar.")
CMPQ(di, encodedCap)
JGT(LabelRef("scalar"))
Comment("Check if less than 4 values remain and jump to scalar.")
CMPQ(n, dataTail)
JGT(LabelRef("scalar"))
dataBytes, bytecount := decodeSIMDUint32(encoded, ci, di, byteCountPtr, byteMaskptr)
Comment("Store 4 uint32.")
MOVOU(dataBytes, data.Idx(n, 4))
Comment("Increment the indices.")
ADDQ(Imm(4), n)
ADDQ(bytecount, di)
JMP(LabelRef("simd"))
Label("scalar")
Comment("Process a single value at a time.")
CMPQ(n, dataLen)
JE(LabelRef("done"))
val := decodeScalarUint32(n, ci, di, encoded, data)
MOVL(val, data.Idx(n, 4)) // data[i] = val
INCQ(n)
JMP(LabelRef("scalar"))
Label("done")
RET()
}
TEXT("decodeDeltaUint32SSE3", NOSPLIT, "func (data []uint32, encoded []byte, previous uint32)")
Doc("decodeDeltaUint32SSE3 decodes 4 uint32 at a time using SSE3 instructions (PSHUFB)")
{
encoded, encodedCap, data, dataLen, dataTail, ci, di, n, byteCountPtr, byteMaskptr := preamble(dataByteCount, dataByteMask)
previous := Load(Param("previous"), GP32())
previousX := XMM()
MOVD(previous, previousX)
PSHUFD(Imm(0b_00_00_00_00), previousX, previousX)
Label("simd")
Comment("Check if less than 16 encoded bytes remain and jump to scalar.")
CMPQ(di, encodedCap)
JGT(LabelRef("scalar"))
Comment("Check if less than 4 values remain and jump to scalar.")
CMPQ(n, dataTail)
JGT(LabelRef("scalar"))
dataBytes, bytecount := decodeSIMDUint32(encoded, ci, di, byteCountPtr, byteMaskptr)
prefixSumSIMD(dataBytes, previousX)
MOVD(previousX, previous)
Comment("Store 4 uint32.")
MOVOU(dataBytes, data.Idx(n, 4))
Comment("Increment the indices.")
ADDQ(Imm(4), n)
ADDQ(bytecount, di)
JMP(LabelRef("simd"))
Label("scalar")
Comment("Process a single value at a time.")
CMPQ(n, dataLen)
JE(LabelRef("done"))
val := decodeScalarUint32(n, ci, di, encoded, data)
Comment("Add the previous decoded value to the delta.")
ADDL(val, previous) // previous += val
MOVL(previous, data.Idx(n, 4)) // data[i] = val
INCQ(n)
JMP(LabelRef("scalar"))
Label("done")
RET()
}
TEXT("decodeInt32SSE3", NOSPLIT, "func (data []int32, encoded []byte)")
Doc("decodeInt32SSE3 decodes 4 int32 at a time using SSE3 instructions (PSHUFB)")
{
encoded, encodedCap, data, dataLen, dataTail, ci, di, n, byteCountPtr, byteMaskptr := preamble(dataByteCount, dataByteMask)
Label("simd")
Comment("Check if less than 16 encoded bytes remain and jump to scalar.")
CMPQ(di, encodedCap)
JGT(LabelRef("scalar"))
Comment("Check if less than 4 values remain and jump to scalar.")
CMPQ(n, dataTail)
JGT(LabelRef("scalar"))
dataBytes, bytecount := decodeSIMDUint32(encoded, ci, di, byteCountPtr, byteMaskptr)
zigzagDecodeSIMD(dataBytes)
Comment("Store 4 uint32.")
MOVOU(dataBytes, data.Idx(n, 4))
Comment("Increment the indices.")
ADDQ(Imm(4), n)
ADDQ(bytecount, di)
JMP(LabelRef("simd"))
Label("scalar")
Comment("Process a single value at a time.")
CMPQ(n, dataLen)
JE(LabelRef("done"))
val := decodeScalarUint32(n, ci, di, encoded, data)
zigzagDecodeScalar(val)
MOVL(val, data.Idx(n, 4)) // data[i] = val
INCQ(n)
JMP(LabelRef("scalar"))
Label("done")
RET()
}
TEXT("decodeDeltaInt32SSE3", NOSPLIT, "func (data []int32, encoded []byte, previous int32)")
Doc("decodeDeltaInt32SSE3 decodes 4 int32 at a time using SSE3 instructions (PSHUFB)")
{
encoded, encodedCap, data, dataLen, dataTail, ci, di, n, byteCountPtr, byteMaskptr := preamble(dataByteCount, dataByteMask)
previous := Load(Param("previous"), GP32())
previousX := XMM()
MOVD(previous, previousX)
PSHUFD(Imm(0b_00_00_00_00), previousX, previousX)
Label("simd")
Comment("Check if less than 16 encoded bytes remain and jump to scalar.")
CMPQ(di, encodedCap)
JGT(LabelRef("scalar"))
Comment("Check if less than 4 values remain and jump to scalar.")
CMPQ(n, dataTail)
JGT(LabelRef("scalar"))
dataBytes, bytecount := decodeSIMDUint32(encoded, ci, di, byteCountPtr, byteMaskptr)
zigzagDecodeSIMD(dataBytes)
prefixSumSIMD(dataBytes, previousX)
MOVD(previousX, previous)
Comment("Store 4 uint32.")
MOVOU(dataBytes, data.Idx(n, 4))
Comment("Increment the indices.")
ADDQ(Imm(4), n)
ADDQ(bytecount, di)
JMP(LabelRef("simd"))
Label("scalar")
Comment("Process a single value at a time.")
CMPQ(n, dataLen)
JE(LabelRef("done"))
val := decodeScalarUint32(n, ci, di, encoded, data)
zigzagDecodeScalar(val)
Comment("Add the previous decoded value to the delta.")
ADDL(val, previous) // previous += val
MOVL(previous, data.Idx(n, 4)) // data[i] = previous
INCQ(n)
JMP(LabelRef("scalar"))
Label("done")
RET()
}
Generate()
}