summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/klauspost/compress/huff0/decompress_8b_amd64.s
blob: 0d6cb1a962b3fc95360ccbb1e0ef6ec68918c16f (plain) (blame)
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
// +build !appengine
// +build gc
// +build !noasm

#include "textflag.h"
#include "funcdata.h"
#include "go_asm.h"

#define bufoff      256 // see decompress.go, we're using [4][256]byte table

// func decompress4x_main_loop_x86(pbr0, pbr1, pbr2, pbr3 *bitReaderShifted,
//	peekBits uint8, buf *byte, tbl *dEntrySingle) (int, bool)
TEXT ·decompress4x_8b_loop_x86(SB), NOSPLIT, $8
#define off             R8
#define buffer          DI
#define table           SI

#define br_bits_read    R9
#define br_value        R10
#define br_offset       R11
#define peek_bits       R12
#define exhausted       DX

#define br0             R13
#define br1             R14
#define br2             R15
#define br3             BP

	MOVQ BP, 0(SP)

	XORQ exhausted, exhausted // exhausted = false
	XORQ off, off             // off = 0

	MOVBQZX peekBits+32(FP), peek_bits
	MOVQ    buf+40(FP), buffer
	MOVQ    tbl+48(FP), table

	MOVQ pbr0+0(FP), br0
	MOVQ pbr1+8(FP), br1
	MOVQ pbr2+16(FP), br2
	MOVQ pbr3+24(FP), br3

main_loop:

	// const stream = 0
	// br0.fillFast()
	MOVBQZX bitReaderShifted_bitsRead(br0), br_bits_read
	MOVQ    bitReaderShifted_value(br0), br_value
	MOVQ    bitReaderShifted_off(br0), br_offset

	// if b.bitsRead >= 32 {
	CMPQ br_bits_read, $32
	JB   skip_fill0

	SUBQ $32, br_bits_read // b.bitsRead -= 32
	SUBQ $4, br_offset     // b.off -= 4

	// v := b.in[b.off-4 : b.off]
	// v = v[:4]
	// low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
	MOVQ bitReaderShifted_in(br0), AX
	MOVL 0(br_offset)(AX*1), AX       // AX = uint32(b.in[b.off:b.off+4])

	// b.value |= uint64(low) << (b.bitsRead & 63)
	MOVQ br_bits_read, CX
	SHLQ CL, AX
	ORQ  AX, br_value

	// exhausted = exhausted || (br0.off < 4)
	CMPQ  br_offset, $4
	SETLT DL
	ORB   DL, DH

	// }
skip_fill0:

	// val0 := br0.peekTopBits(peekBits)
	MOVQ br_value, AX
	MOVQ peek_bits, CX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v0 := table[val0&mask]
	MOVW 0(table)(AX*2), AX // AX - v0

	// br0.advance(uint8(v0.entry))
	MOVB    AH, BL           // BL = uint8(v0.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CL, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// val1 := br0.peekTopBits(peekBits)
	MOVQ peek_bits, CX
	MOVQ br_value, AX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v1 := table[val1&mask]
	MOVW 0(table)(AX*2), AX // AX - v1

	// br0.advance(uint8(v1.entry))
	MOVB    AH, BH           // BH = uint8(v1.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CX, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// these two writes get coalesced
	// buf[stream][off] = uint8(v0.entry >> 8)
	// buf[stream][off+1] = uint8(v1.entry >> 8)
	MOVW BX, 0(buffer)(off*1)

	// SECOND PART:
	// val2 := br0.peekTopBits(peekBits)
	MOVQ br_value, AX
	MOVQ peek_bits, CX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v2 := table[val0&mask]
	MOVW 0(table)(AX*2), AX // AX - v0

	// br0.advance(uint8(v0.entry))
	MOVB    AH, BL           // BL = uint8(v0.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CL, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// val3 := br0.peekTopBits(peekBits)
	MOVQ peek_bits, CX
	MOVQ br_value, AX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v3 := table[val1&mask]
	MOVW 0(table)(AX*2), AX // AX - v1

	// br0.advance(uint8(v1.entry))
	MOVB    AH, BH           // BH = uint8(v1.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CX, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// these two writes get coalesced
	// buf[stream][off+2] = uint8(v2.entry >> 8)
	// buf[stream][off+3] = uint8(v3.entry >> 8)
	MOVW BX, 0+2(buffer)(off*1)

	// update the bitrader reader structure
	MOVB br_bits_read, bitReaderShifted_bitsRead(br0)
	MOVQ br_value, bitReaderShifted_value(br0)
	MOVQ br_offset, bitReaderShifted_off(br0)

	// const stream = 1
	// br1.fillFast()
	MOVBQZX bitReaderShifted_bitsRead(br1), br_bits_read
	MOVQ    bitReaderShifted_value(br1), br_value
	MOVQ    bitReaderShifted_off(br1), br_offset

	// if b.bitsRead >= 32 {
	CMPQ br_bits_read, $32
	JB   skip_fill1

	SUBQ $32, br_bits_read // b.bitsRead -= 32
	SUBQ $4, br_offset     // b.off -= 4

	// v := b.in[b.off-4 : b.off]
	// v = v[:4]
	// low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
	MOVQ bitReaderShifted_in(br1), AX
	MOVL 0(br_offset)(AX*1), AX       // AX = uint32(b.in[b.off:b.off+4])

	// b.value |= uint64(low) << (b.bitsRead & 63)
	MOVQ br_bits_read, CX
	SHLQ CL, AX
	ORQ  AX, br_value

	// exhausted = exhausted || (br1.off < 4)
	CMPQ  br_offset, $4
	SETLT DL
	ORB   DL, DH

	// }
skip_fill1:

	// val0 := br1.peekTopBits(peekBits)
	MOVQ br_value, AX
	MOVQ peek_bits, CX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v0 := table[val0&mask]
	MOVW 0(table)(AX*2), AX // AX - v0

	// br1.advance(uint8(v0.entry))
	MOVB    AH, BL           // BL = uint8(v0.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CL, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// val1 := br1.peekTopBits(peekBits)
	MOVQ peek_bits, CX
	MOVQ br_value, AX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v1 := table[val1&mask]
	MOVW 0(table)(AX*2), AX // AX - v1

	// br1.advance(uint8(v1.entry))
	MOVB    AH, BH           // BH = uint8(v1.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CX, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// these two writes get coalesced
	// buf[stream][off] = uint8(v0.entry >> 8)
	// buf[stream][off+1] = uint8(v1.entry >> 8)
	MOVW BX, 256(buffer)(off*1)

	// SECOND PART:
	// val2 := br1.peekTopBits(peekBits)
	MOVQ br_value, AX
	MOVQ peek_bits, CX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v2 := table[val0&mask]
	MOVW 0(table)(AX*2), AX // AX - v0

	// br1.advance(uint8(v0.entry))
	MOVB    AH, BL           // BL = uint8(v0.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CL, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// val3 := br1.peekTopBits(peekBits)
	MOVQ peek_bits, CX
	MOVQ br_value, AX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v3 := table[val1&mask]
	MOVW 0(table)(AX*2), AX // AX - v1

	// br1.advance(uint8(v1.entry))
	MOVB    AH, BH           // BH = uint8(v1.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CX, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// these two writes get coalesced
	// buf[stream][off+2] = uint8(v2.entry >> 8)
	// buf[stream][off+3] = uint8(v3.entry >> 8)
	MOVW BX, 256+2(buffer)(off*1)

	// update the bitrader reader structure
	MOVB br_bits_read, bitReaderShifted_bitsRead(br1)
	MOVQ br_value, bitReaderShifted_value(br1)
	MOVQ br_offset, bitReaderShifted_off(br1)

	// const stream = 2
	// br2.fillFast()
	MOVBQZX bitReaderShifted_bitsRead(br2), br_bits_read
	MOVQ    bitReaderShifted_value(br2), br_value
	MOVQ    bitReaderShifted_off(br2), br_offset

	// if b.bitsRead >= 32 {
	CMPQ br_bits_read, $32
	JB   skip_fill2

	SUBQ $32, br_bits_read // b.bitsRead -= 32
	SUBQ $4, br_offset     // b.off -= 4

	// v := b.in[b.off-4 : b.off]
	// v = v[:4]
	// low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
	MOVQ bitReaderShifted_in(br2), AX
	MOVL 0(br_offset)(AX*1), AX       // AX = uint32(b.in[b.off:b.off+4])

	// b.value |= uint64(low) << (b.bitsRead & 63)
	MOVQ br_bits_read, CX
	SHLQ CL, AX
	ORQ  AX, br_value

	// exhausted = exhausted || (br2.off < 4)
	CMPQ  br_offset, $4
	SETLT DL
	ORB   DL, DH

	// }
skip_fill2:

	// val0 := br2.peekTopBits(peekBits)
	MOVQ br_value, AX
	MOVQ peek_bits, CX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v0 := table[val0&mask]
	MOVW 0(table)(AX*2), AX // AX - v0

	// br2.advance(uint8(v0.entry))
	MOVB    AH, BL           // BL = uint8(v0.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CL, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// val1 := br2.peekTopBits(peekBits)
	MOVQ peek_bits, CX
	MOVQ br_value, AX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v1 := table[val1&mask]
	MOVW 0(table)(AX*2), AX // AX - v1

	// br2.advance(uint8(v1.entry))
	MOVB    AH, BH           // BH = uint8(v1.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CX, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// these two writes get coalesced
	// buf[stream][off] = uint8(v0.entry >> 8)
	// buf[stream][off+1] = uint8(v1.entry >> 8)
	MOVW BX, 512(buffer)(off*1)

	// SECOND PART:
	// val2 := br2.peekTopBits(peekBits)
	MOVQ br_value, AX
	MOVQ peek_bits, CX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v2 := table[val0&mask]
	MOVW 0(table)(AX*2), AX // AX - v0

	// br2.advance(uint8(v0.entry))
	MOVB    AH, BL           // BL = uint8(v0.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CL, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// val3 := br2.peekTopBits(peekBits)
	MOVQ peek_bits, CX
	MOVQ br_value, AX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v3 := table[val1&mask]
	MOVW 0(table)(AX*2), AX // AX - v1

	// br2.advance(uint8(v1.entry))
	MOVB    AH, BH           // BH = uint8(v1.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CX, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// these two writes get coalesced
	// buf[stream][off+2] = uint8(v2.entry >> 8)
	// buf[stream][off+3] = uint8(v3.entry >> 8)
	MOVW BX, 512+2(buffer)(off*1)

	// update the bitrader reader structure
	MOVB br_bits_read, bitReaderShifted_bitsRead(br2)
	MOVQ br_value, bitReaderShifted_value(br2)
	MOVQ br_offset, bitReaderShifted_off(br2)

	// const stream = 3
	// br3.fillFast()
	MOVBQZX bitReaderShifted_bitsRead(br3), br_bits_read
	MOVQ    bitReaderShifted_value(br3), br_value
	MOVQ    bitReaderShifted_off(br3), br_offset

	// if b.bitsRead >= 32 {
	CMPQ br_bits_read, $32
	JB   skip_fill3

	SUBQ $32, br_bits_read // b.bitsRead -= 32
	SUBQ $4, br_offset     // b.off -= 4

	// v := b.in[b.off-4 : b.off]
	// v = v[:4]
	// low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
	MOVQ bitReaderShifted_in(br3), AX
	MOVL 0(br_offset)(AX*1), AX       // AX = uint32(b.in[b.off:b.off+4])

	// b.value |= uint64(low) << (b.bitsRead & 63)
	MOVQ br_bits_read, CX
	SHLQ CL, AX
	ORQ  AX, br_value

	// exhausted = exhausted || (br3.off < 4)
	CMPQ  br_offset, $4
	SETLT DL
	ORB   DL, DH

	// }
skip_fill3:

	// val0 := br3.peekTopBits(peekBits)
	MOVQ br_value, AX
	MOVQ peek_bits, CX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v0 := table[val0&mask]
	MOVW 0(table)(AX*2), AX // AX - v0

	// br3.advance(uint8(v0.entry))
	MOVB    AH, BL           // BL = uint8(v0.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CL, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// val1 := br3.peekTopBits(peekBits)
	MOVQ peek_bits, CX
	MOVQ br_value, AX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v1 := table[val1&mask]
	MOVW 0(table)(AX*2), AX // AX - v1

	// br3.advance(uint8(v1.entry))
	MOVB    AH, BH           // BH = uint8(v1.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CX, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// these two writes get coalesced
	// buf[stream][off] = uint8(v0.entry >> 8)
	// buf[stream][off+1] = uint8(v1.entry >> 8)
	MOVW BX, 768(buffer)(off*1)

	// SECOND PART:
	// val2 := br3.peekTopBits(peekBits)
	MOVQ br_value, AX
	MOVQ peek_bits, CX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v2 := table[val0&mask]
	MOVW 0(table)(AX*2), AX // AX - v0

	// br3.advance(uint8(v0.entry))
	MOVB    AH, BL           // BL = uint8(v0.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CL, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// val3 := br3.peekTopBits(peekBits)
	MOVQ peek_bits, CX
	MOVQ br_value, AX
	SHRQ CL, AX        // AX = (value >> peek_bits) & mask

	// v3 := table[val1&mask]
	MOVW 0(table)(AX*2), AX // AX - v1

	// br3.advance(uint8(v1.entry))
	MOVB    AH, BH           // BH = uint8(v1.entry >> 8)
	MOVBQZX AL, CX
	SHLQ    CX, br_value     // value <<= n
	ADDQ    CX, br_bits_read // bits_read += n

	// these two writes get coalesced
	// buf[stream][off+2] = uint8(v2.entry >> 8)
	// buf[stream][off+3] = uint8(v3.entry >> 8)
	MOVW BX, 768+2(buffer)(off*1)

	// update the bitrader reader structure
	MOVB br_bits_read, bitReaderShifted_bitsRead(br3)
	MOVQ br_value, bitReaderShifted_value(br3)
	MOVQ br_offset, bitReaderShifted_off(br3)

	ADDQ $4, off // off += 2

	TESTB DH, DH // any br[i].ofs < 4?
	JNZ   end

	CMPQ off, $bufoff
	JL   main_loop

end:
	MOVQ 0(SP), BP

	MOVB off, ret+56(FP)
	RET

#undef off
#undef buffer
#undef table

#undef br_bits_read
#undef br_value
#undef br_offset
#undef peek_bits
#undef exhausted

#undef br0
#undef br1
#undef br2
#undef br3