Debian Codegraph @

huff0/_generate/gen.go

raw
1 package main
2
3 //go:generate go run gen.go -out ../decompress.s -arch amd64,arm64 -pkg=huff0
4 //go:generate gofmt -w ../decompress_amd64.go
5
6 import (
7 "flag"
8 "fmt"
9 "strconv"
10
11 _ "github.com/klauspost/compress"
12
13 . "github.com/mmcloughlin/avo/build"
14 "github.com/mmcloughlin/avo/gotypes"
15 . "github.com/mmcloughlin/avo/operand"
16 "github.com/mmcloughlin/avo/reg"
17 )
18
19 func main() {
20 flag.Parse()
21
22 ConstraintExpr("!appengine,!noasm,gc")
23
24 {
25 decompress := decompress4x{}
26 decompress.generateProcedure("decompress4x_main_loop_amd64")
27 decompress.generateProcedure4x8bit("decompress4x_8b_main_loop_amd64")
28 }
29
30 {
31 decompress := decompress1x{}
32 decompress.generateProcedure("decompress1x_main_loop_amd64")
33
34 decompress.bmi2 = true
35 decompress.generateProcedure("decompress1x_main_loop_bmi2")
36 }
37
38 Generate()
39 }
40
41 type decompress4x struct {
42 }
43
44 func (d decompress4x) generateProcedure(name string) {
45 Package("github.com/klauspost/compress/huff0")
46 TEXT(name, 0, "func(ctx* decompress4xContext)")
47 Doc(name+" is an x86 assembler implementation of Decompress4X when tablelog > 8.decodes a sequence", "")
48 Pragma("noescape")
49
50 exhausted := GP8()
51 buffer := GP64()
52 limit := GP64()
53
54 peekBits := GP64()
55 dstEvery := GP64()
56 table := GP64()
57
58 br := GP64()
59
60 Comment("Preload values")
61 {
62 ctx := Dereference(Param("ctx"))
63 Load(ctx.Field("peekBits"), peekBits)
64 Load(ctx.Field("out"), buffer)
65 Load(ctx.Field("limit"), limit)
66 Load(ctx.Field("dstEvery"), dstEvery)
67 Load(ctx.Field("tbl"), table)
68 Load(ctx.Field("pbr"), br)
69 }
70
71 Comment("Main loop")
72 Label("main_loop")
73
74 // Check if we have space. We could zero exhausted outside the loop,
75 // but doing it here is a hint to the CPU that there's no dependency
76 // on the previous iteration's value.
77 XORL(exhausted.As32(), exhausted.As32())
78 CMPQ(buffer, limit)
79 SETGE(exhausted.As8())
80 d.decodeTwoValues(0, br, peekBits, table, buffer, dstEvery, exhausted)
81 d.decodeTwoValues(1, br, peekBits, table, buffer, dstEvery, exhausted)
82 d.decodeTwoValues(2, br, peekBits, table, buffer, dstEvery, exhausted)
83 d.decodeTwoValues(3, br, peekBits, table, buffer, dstEvery, exhausted)
84
85 ADDQ(U8(2), buffer) // off += 2
86
87 TESTB(exhausted, exhausted) // any br[i].ofs < 4?
88 JZ(LabelRef("main_loop"))
89
90 {
91 ctx := Dereference(Param("ctx"))
92 ctxout, _ := ctx.Field("out").Resolve()
93 decoded := buffer
94 SUBQ(ctxout.Addr, decoded)
95 SHLQ(U8(2), decoded) // decoded *= 4
96
97 Store(decoded, ctx.Field("decoded"))
98 }
99
100 RET()
101 }
102
103 // TODO [wmu]: I believe it's doable in avo, but can't figure out how to deal
104 // with arbitrary pointers to a given type
105 const bitReader_in = 0
106 const bitReader_off = bitReader_in + 3*8 // {ptr, len, cap}
107 const bitReader_value = bitReader_off + 8
108 const bitReader_bitsRead = bitReader_value + 8
109 const bitReader__size = bitReader_bitsRead + 8
110
111 func (d decompress4x) decodeTwoValues(id int, br, peekBits, table, buffer, dstEvery, exhausted reg.GPVirtual) {
112 brValue, brBitsRead := d.fillFast32(id, 32, br, exhausted)
113
114 val := GP64()
115 Commentf("val0 := br%d.peekTopBits(peekBits)", id)
116 CX := reg.CL
117 MOVQ(brValue, val.As64())
118 MOVQ(peekBits, CX.As64())
119 SHRQ(CX, val.As64()) // val = (value >> peek_bits) & mask
120
121 Comment("v0 := table[val0&mask]")
122 MOVWQZX(Mem{Base: table, Index: val.As64(), Scale: 2}, CX.As64())
123
124 Commentf("br%d.advance(uint8(v0.entry)", id)
125 out := reg.RAX // Fixed since we need 8H
126 // The first byte must be a zero-extending write: MOVB into AL would
127 // merge into RAX and carry a false dependency on the previous stream's
128 // value, serializing the four otherwise-independent streams through one
129 // register (on arm64 the lowered BFI chains the same way).
130 MOVBLZX(CX.As8H(), out.As32()) // AX = uint8(v0.entry >> 8)
131
132 SHLQ(CX, brValue) // value <<= n
133 ADDB(CX.As8(), brBitsRead.As8()) // bits_read += n
134
135 Commentf("val1 := br%d.peekTopBits(peekBits)", id)
136 MOVQ(peekBits, CX.As64())
137 MOVQ(brValue, val.As64())
138 SHRQ(CX, val.As64()) // val = (value >> peek_bits) & mask
139
140 Comment("v1 := table[val1&mask]")
141 MOVWQZX(Mem{Base: table, Index: val.As64(), Scale: 2}, CX.As64()) // tmp - v1
142
143 Commentf("br%d.advance(uint8(v1.entry))", id)
144 MOVB(CX.As8H(), out.As8H()) // AH = uint8(v0.entry >> 8)
145 SHLQ(CX, brValue) // value <<= n
146 ADDB(CX.As8(), brBitsRead.As8()) // bits_read += n
147
148 Comment("these two writes get coalesced")
149 Comment("out[id * dstEvery + 0] = uint8(v0.entry >> 8)")
150 Comment("out[id * dstEvery + 1] = uint8(v1.entry >> 8)")
151 MOVW(out.As16(), bufferIndex(id, buffer, dstEvery))
152
153 Comment("update the bitreader structure")
154 offset := id * bitReader__size
155 MOVQ(brValue, Mem{Base: br, Disp: offset + bitReader_value})
156 MOVB(brBitsRead.As8(), Mem{Base: br, Disp: offset + bitReader_bitsRead})
157 }
158
159 func (d decompress4x) generateProcedure4x8bit(name string) {
160 Package("github.com/klauspost/compress/huff0")
161 TEXT(name, 0, "func(ctx* decompress4xContext)")
162 Doc(name+" is an x86 assembler implementation of Decompress4X when tablelog > 8.decodes a sequence", "")
163 Pragma("noescape")
164
165 exhausted := GP8()
166 buffer := GP64()
167 limit := GP64()
168
169 peekBits := GP64()
170 dstEvery := GP64()
171 table := GP64()
172
173 br := GP64()
174
175 Comment("Preload values")
176 {
177 ctx := Dereference(Param("ctx"))
178 Load(ctx.Field("peekBits"), peekBits)
179 Load(ctx.Field("out"), buffer)
180 Load(ctx.Field("limit"), limit)
181 Load(ctx.Field("dstEvery"), dstEvery)
182 Load(ctx.Field("tbl"), table)
183 Load(ctx.Field("pbr"), br)
184 }
185
186 Comment("Main loop")
187 Label("main_loop")
188
189 // Check if we have space. We could zero exhausted outside the loop,
190 // but doing it here is a hint to the CPU that there's no dependency
191 // on the previous iteration's value.
192 XORL(exhausted.As32(), exhausted.As32())
193 CMPQ(buffer, limit)
194 SETGE(exhausted)
195 d.decodeFourValues(0, br, peekBits, table, buffer, dstEvery, exhausted)
196 d.decodeFourValues(1, br, peekBits, table, buffer, dstEvery, exhausted)
197 d.decodeFourValues(2, br, peekBits, table, buffer, dstEvery, exhausted)
198 d.decodeFourValues(3, br, peekBits, table, buffer, dstEvery, exhausted)
199
200 ADDQ(U8(4), buffer) // off += 4
201
202 TESTB(exhausted, exhausted) // any br[i].ofs < 4?
203 JZ(LabelRef("main_loop"))
204
205 {
206 ctx := Dereference(Param("ctx"))
207 ctxout, _ := ctx.Field("out").Resolve()
208 decoded := buffer
209 SUBQ(ctxout.Addr, decoded)
210 SHLQ(U8(2), decoded) // decoded *= 4
211
212 Store(decoded, ctx.Field("decoded"))
213 }
214 RET()
215 }
216
217 func (d decompress4x) decodeFourValues(id int, br, peekBits, table, buffer, dstEvery, exhausted reg.GPVirtual) {
218 brValue, brBitsRead := d.fillFast32(id, 32, br, exhausted)
219
220 decompress := func(valID int, outByte reg.Register, fresh bool) {
221 CX := reg.CL
222 val := GP64()
223 Commentf("val%d := br%d.peekTopBits(peekBits)", valID, id)
224 MOVQ(brValue, val.As64())
225 MOVQ(peekBits, CX.As64())
226 SHRQ(CX, val.As64()) // val = (value >> peek_bits) & mask
227
228 Commentf("v%d := table[val0&mask]", valID)
229 MOVWQZX(Mem{Base: table, Index: val.As64(), Scale: 2}, CX.As64())
230
231 Commentf("br%d.advance(uint8(v%d.entry)", id, valID)
232 if fresh {
233 // The first byte must be a zero-extending write: MOVB into AL
234 // would merge into RAX and carry a false dependency on the
235 // previous stream's value, serializing the four otherwise-
236 // independent streams through one register (on arm64 the
237 // lowered BFI chains the same way).
238 MOVBLZX(CX.As8H(), reg.EAX) // outByte = uint8(v0.entry >> 8)
239 } else {
240 MOVB(CX.As8H(), outByte) // outByte = uint8(v0.entry >> 8)
241 }
242
243 SHLQ(CX, brValue) // value <<= n
244 ADDB(CX, brBitsRead.As8()) // bits_read += n
245 }
246
247 out := reg.RAX // Fixed since we need 8H
248 decompress(0, out.As8L(), true)
249 decompress(1, out.As8H(), false)
250 BSWAPL(out.As32())
251 decompress(2, out.As8H(), false)
252 decompress(3, out.As8L(), false)
253 BSWAPL(out.As32())
254
255 Comment("these four writes get coalesced")
256 Comment("out[id * dstEvery + 0] = uint8(v0.entry >> 8)")
257 Comment("out[id * dstEvery + 1] = uint8(v1.entry >> 8)")
258 Comment("out[id * dstEvery + 3] = uint8(v2.entry >> 8)")
259 Comment("out[id * dstEvery + 4] = uint8(v3.entry >> 8)")
260 MOVL(out.As32(), bufferIndex(id, buffer, dstEvery))
261
262 Comment("update the bitreader structure")
263 offset := id * bitReader__size
264 MOVQ(brValue, Mem{Base: br, Disp: offset + bitReader_value})
265 MOVB(brBitsRead.As8(), Mem{Base: br, Disp: offset + bitReader_bitsRead})
266 }
267
268 func bufferIndex(id int, buffer, dstEvery reg.GPVirtual) Mem {
269 switch id {
270 case 0:
271 return Mem{Base: buffer}
272 case 1, 2:
273 return Mem{Base: buffer, Index: dstEvery, Scale: byte(id)}
274 case 3:
275 stride3 := GP64() // stride3 := 3*dstEvery
276 LEAQ(Mem{Base: dstEvery, Index: dstEvery, Scale: 2}, stride3)
277 return Mem{Base: buffer, Index: stride3, Scale: 1}
278 default:
279 panic("id must be >=0, <4")
280 }
281 }
282
283 func (d decompress4x) fillFast32(id, atLeast int, br, exhausted reg.GPVirtual) (brValue, brBitsRead reg.GPVirtual) {
284 if atLeast > 32 {
285 panic(fmt.Sprintf("at least (%d) cannot be >32", atLeast))
286 }
287 Commentf("br%d.fillFast32()", id)
288 brValue = GP64()
289 brBitsRead = GP64()
290 offset := bitReader__size * id
291 MOVQ(Mem{Base: br, Disp: offset + bitReader_value}, brValue)
292 MOVBQZX(Mem{Base: br, Disp: offset + bitReader_bitsRead}, brBitsRead)
293
294 // We must have at least 2 * max tablelog left
295 CMPQ(brBitsRead, U8(64-atLeast))
296 JBE(LabelRef("skip_fill" + strconv.Itoa(id)))
297 brOffset := GP64()
298 MOVQ(Mem{Base: br, Disp: offset + bitReader_off}, brOffset)
299
300 SUBQ(U8(32), brBitsRead) // b.bitsRead -= 32
301 SUBQ(U8(4), brOffset) // b.off -= 4
302
303 // v := b.in[b.off-4 : b.off]
304 // v = v[:4]
305 // low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
306 tmp := GP64()
307 MOVQ(Mem{Base: br, Disp: offset + bitReader_in}, tmp)
308
309 Comment("b.value |= uint64(low) << (b.bitsRead & 63)")
310 addr := Mem{Base: brOffset, Index: tmp.As64(), Scale: 1}
311 CX := reg.CL
312 MOVL(addr, tmp.As32()) // tmp = uint32(b.in[b.off:b.off+4])
313 MOVQ(brBitsRead, CX.As64())
314 SHLQ(CX, tmp.As64())
315
316 MOVQ(brOffset, Mem{Base: br, Disp: offset + bitReader_off})
317 ORQ(tmp.As64(), brValue)
318 {
319 Commentf("exhausted += (br%d.off < 4)", id)
320 CMPQ(brOffset, U8(4))
321 // Add carry from brOffset-4. We do this at most four times per iteration,
322 // and every iteration resets exhausted's lower byte, so it doesn't overflow.
323 ADCB(I8(0), exhausted)
324 }
325
326 Label("skip_fill" + strconv.Itoa(id))
327 return
328 }
329
330 type bitReader struct {
331 in reg.GPVirtual
332 off reg.GPVirtual
333 value reg.GPVirtual
334 bitsRead reg.GPVirtual
335 id int
336 bmi2 bool
337 }
338
339 func (b *bitReader) uniqId() string {
340 b.id += 1
341 return strconv.Itoa(b.id)
342 }
343
344 func (b *bitReader) load(pointer gotypes.Component) {
345 b.in = GP64()
346 b.off = GP64()
347 b.value = GP64()
348 b.bitsRead = GP64()
349
350 Load(pointer.Field("in").Base(), b.in)
351 Load(pointer.Field("off"), b.off)
352 Load(pointer.Field("value"), b.value)
353 Load(pointer.Field("bitsRead"), b.bitsRead)
354 }
355
356 func (b *bitReader) store(pointer gotypes.Component) {
357 Store(b.off, pointer.Field("off"))
358 Store(b.value, pointer.Field("value"))
359 // Note: explicit As8(), without this avo reports: "could not deduce mov instruction"
360 Store(b.bitsRead.As8(), pointer.Field("bitsRead"))
361 }
362
363 func (b *bitReader) fillFast() {
364 label := "bitReader_fillFast_" + b.uniqId() + "_end"
365 CMPQ(b.bitsRead, U8(32))
366 JL(LabelRef(label))
367
368 SUBQ(U8(32), b.bitsRead)
369 SUBQ(U8(4), b.off)
370
371 tmp := GP64()
372 MOVL(Mem{Base: b.in, Index: b.off, Scale: 1}, tmp.As32())
373 if b.bmi2 {
374 SHLXQ(b.bitsRead, tmp, tmp)
375 } else {
376 MOVQ(b.bitsRead, reg.RCX)
377 SHLQ(reg.CL, tmp)
378 }
379 ORQ(tmp, b.value)
380 Label(label)
381 }
382
383 func (b *bitReader) peekTopBits(n reg.GPVirtual) reg.GPVirtual {
384 res := GP64()
385 if b.bmi2 {
386 SHRXQ(n, b.value, res)
387 } else {
388 MOVQ(n, reg.RCX)
389 MOVQ(b.value, res)
390 SHRQ(reg.CL, res)
391 }
392
393 return res
394 }
395
396 func (b *bitReader) advance(n reg.Register) {
397 ADDQ(n, b.bitsRead)
398 if b.bmi2 {
399 SHLXQ(n, b.value, b.value)
400 } else {
401 MOVQ(n, reg.RCX)
402 SHLQ(reg.CL, b.value)
403 }
404 }
405
406 type decompress1x struct {
407 bmi2 bool
408 }
409
410 func (d decompress1x) generateProcedure(name string) {
411 Package("github.com/klauspost/compress/huff0")
412 TEXT(name, 0, "func(ctx* decompress1xContext)")
413 Doc(name+" is an x86 assembler implementation of Decompress1X", "")
414 Pragma("noescape")
415
416 br := bitReader{}
417 br.bmi2 = d.bmi2
418
419 buffer := GP64()
420 bufferEnd := GP64() // the past-end address of buffer
421 dt := GP64()
422 peekBits := GP64()
423
424 {
425 ctx := Dereference(Param("ctx"))
426 Load(ctx.Field("out"), buffer)
427
428 outCap := GP64()
429 Load(ctx.Field("outCap"), outCap)
430 CMPQ(outCap, U8(4))
431 JB(LabelRef("error_max_decoded_size_exceeded"))
432
433 LEAQ(Mem{Base: buffer, Index: outCap, Scale: 1}, bufferEnd)
434
435 // load bitReader struct
436 pbr := Dereference(ctx.Field("pbr"))
437 br.load(pbr)
438
439 Load(ctx.Field("tbl"), dt)
440 Load(ctx.Field("peekBits"), peekBits)
441 }
442
443 JMP(LabelRef("loop_condition"))
444
445 Label("main_loop")
446
447 out := reg.AX // Fixed, as we need an 8H part
448
449 Comment("Check if we have room for 4 bytes in the output buffer")
450 {
451 tmp := GP64()
452 LEAQ(Mem{Base: buffer, Disp: 4}, tmp)
453 CMPQ(tmp, bufferEnd)
454 JGE(LabelRef("error_max_decoded_size_exceeded"))
455 }
456
457 decompress := func(id int, out reg.Register) {
458 d.decompress(id, &br, peekBits, dt, out)
459 }
460
461 Comment("Decode 4 values")
462 br.fillFast()
463 decompress(0, out.As8L())
464 decompress(1, out.As8H())
465 BSWAPL(out.As32())
466
467 br.fillFast()
468 decompress(2, out.As8H())
469 decompress(3, out.As8L())
470 BSWAPL(out.As32())
471
472 Comment("Store the decoded values")
473 MOVL(out.As32(), Mem{Base: buffer})
474 ADDQ(U8(4), buffer)
475
476 Label("loop_condition")
477 CMPQ(br.off, U8(8))
478 JGE(LabelRef("main_loop"))
479
480 Comment("Update ctx structure")
481 {
482 // calculate decoded as current `out` - initial `out`
483 ctx := Dereference(Param("ctx"))
484 ctxout, _ := ctx.Field("out").Resolve()
485 decoded := buffer
486 SUBQ(ctxout.Addr, decoded)
487 Store(decoded, ctx.Field("decoded"))
488
489 pbr := Dereference(ctx.Field("pbr"))
490 br.store(pbr)
491 }
492
493 RET()
494
495 Comment("Report error")
496 Label("error_max_decoded_size_exceeded")
497 {
498 ctx := Dereference(Param("ctx"))
499 tmp := GP64()
500 MOVQ(I64(-1), tmp)
501 Store(tmp, ctx.Field("decoded"))
502 }
503
504 RET()
505 }
506
507 func (d decompress1x) decompress(id int, br *bitReader, peekBits, dt reg.GPVirtual, out reg.Register) {
508 // v := dt[br.peekBitsFast(d.actualTableLog)&tlMask]
509 k := br.peekTopBits(peekBits)
510 v := reg.RCX // Fixed, as we need 8H part
511 MOVWQZX(Mem{Base: dt, Index: k, Scale: 2}, v.As64())
512
513 // buf[id] = uint8(v.entry >> 8)
514 MOVB(v.As8H(), out)
515
516 // br.advance(uint8(v.entry))
517 MOVBQZX(v.As8L(), v)
518 br.advance(v)
519 }