Debian Codegraph @

huff0/_generate/gen.go

raw
1 package main
2
3 //go:generate go run gen.go -out ../decompress_amd64.s -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("amd64,!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 MOVW(Mem{Base: table, Index: val.As64(), Scale: 2}, CX.As16())
123
124 Commentf("br%d.advance(uint8(v0.entry)", id)
125 out := reg.RAX // Fixed since we need 8H
126 MOVB(CX.As8H(), out.As8()) // AL = uint8(v0.entry >> 8)
127
128 SHLQ(CX, brValue) // value <<= n
129 ADDB(CX.As8(), brBitsRead.As8()) // bits_read += n
130
131 Commentf("val1 := br%d.peekTopBits(peekBits)", id)
132 MOVQ(peekBits, CX.As64())
133 MOVQ(brValue, val.As64())
134 SHRQ(CX, val.As64()) // val = (value >> peek_bits) & mask
135
136 Comment("v1 := table[val1&mask]")
137 MOVW(Mem{Base: table, Index: val.As64(), Scale: 2}, CX.As16()) // tmp - v1
138
139 Commentf("br%d.advance(uint8(v1.entry))", id)
140 MOVB(CX.As8H(), out.As8H()) // AH = uint8(v0.entry >> 8)
141 SHLQ(CX, brValue) // value <<= n
142 ADDB(CX.As8(), brBitsRead.As8()) // bits_read += n
143
144 Comment("these two writes get coalesced")
145 Comment("out[id * dstEvery + 0] = uint8(v0.entry >> 8)")
146 Comment("out[id * dstEvery + 1] = uint8(v1.entry >> 8)")
147 MOVW(out.As16(), bufferIndex(id, buffer, dstEvery))
148
149 Comment("update the bitreader structure")
150 offset := id * bitReader__size
151 MOVQ(brValue, Mem{Base: br, Disp: offset + bitReader_value})
152 MOVB(brBitsRead.As8(), Mem{Base: br, Disp: offset + bitReader_bitsRead})
153 }
154
155 func (d decompress4x) generateProcedure4x8bit(name string) {
156 Package("github.com/klauspost/compress/huff0")
157 TEXT(name, 0, "func(ctx* decompress4xContext)")
158 Doc(name+" is an x86 assembler implementation of Decompress4X when tablelog > 8.decodes a sequence", "")
159 Pragma("noescape")
160
161 exhausted := GP8()
162 buffer := GP64()
163 limit := GP64()
164
165 peekBits := GP64()
166 dstEvery := GP64()
167 table := GP64()
168
169 br := GP64()
170
171 Comment("Preload values")
172 {
173 ctx := Dereference(Param("ctx"))
174 Load(ctx.Field("peekBits"), peekBits)
175 Load(ctx.Field("out"), buffer)
176 Load(ctx.Field("limit"), limit)
177 Load(ctx.Field("dstEvery"), dstEvery)
178 Load(ctx.Field("tbl"), table)
179 Load(ctx.Field("pbr"), br)
180 }
181
182 Comment("Main loop")
183 Label("main_loop")
184
185 // Check if we have space. We could zero exhausted outside the loop,
186 // but doing it here is a hint to the CPU that there's no dependency
187 // on the previous iteration's value.
188 XORL(exhausted.As32(), exhausted.As32())
189 CMPQ(buffer, limit)
190 SETGE(exhausted)
191 d.decodeFourValues(0, br, peekBits, table, buffer, dstEvery, exhausted)
192 d.decodeFourValues(1, br, peekBits, table, buffer, dstEvery, exhausted)
193 d.decodeFourValues(2, br, peekBits, table, buffer, dstEvery, exhausted)
194 d.decodeFourValues(3, br, peekBits, table, buffer, dstEvery, exhausted)
195
196 ADDQ(U8(4), buffer) // off += 4
197
198 TESTB(exhausted, exhausted) // any br[i].ofs < 4?
199 JZ(LabelRef("main_loop"))
200
201 {
202 ctx := Dereference(Param("ctx"))
203 ctxout, _ := ctx.Field("out").Resolve()
204 decoded := buffer
205 SUBQ(ctxout.Addr, decoded)
206 SHLQ(U8(2), decoded) // decoded *= 4
207
208 Store(decoded, ctx.Field("decoded"))
209 }
210 RET()
211 }
212
213 func (d decompress4x) decodeFourValues(id int, br, peekBits, table, buffer, dstEvery, exhausted reg.GPVirtual) {
214 brValue, brBitsRead := d.fillFast32(id, 32, br, exhausted)
215
216 decompress := func(valID int, outByte reg.Register) {
217 CX := reg.CL
218 val := GP64()
219 Commentf("val%d := br%d.peekTopBits(peekBits)", valID, id)
220 MOVQ(brValue, val.As64())
221 MOVQ(peekBits, CX.As64())
222 SHRQ(CX, val.As64()) // val = (value >> peek_bits) & mask
223
224 Commentf("v%d := table[val0&mask]", valID)
225 MOVW(Mem{Base: table, Index: val.As64(), Scale: 2}, CX.As16())
226
227 Commentf("br%d.advance(uint8(v%d.entry)", id, valID)
228 MOVB(CX.As8H(), outByte) // outByte = uint8(v0.entry >> 8)
229
230 SHLQ(CX, brValue) // value <<= n
231 ADDB(CX, brBitsRead.As8()) // bits_read += n
232 }
233
234 out := reg.RAX // Fixed since we need 8H
235 decompress(0, out.As8L())
236 decompress(1, out.As8H())
237 BSWAPL(out.As32())
238 decompress(2, out.As8H())
239 decompress(3, out.As8L())
240 BSWAPL(out.As32())
241
242 Comment("these four writes get coalesced")
243 Comment("out[id * dstEvery + 0] = uint8(v0.entry >> 8)")
244 Comment("out[id * dstEvery + 1] = uint8(v1.entry >> 8)")
245 Comment("out[id * dstEvery + 3] = uint8(v2.entry >> 8)")
246 Comment("out[id * dstEvery + 4] = uint8(v3.entry >> 8)")
247 MOVL(out.As32(), bufferIndex(id, buffer, dstEvery))
248
249 Comment("update the bitreader structure")
250 offset := id * bitReader__size
251 MOVQ(brValue, Mem{Base: br, Disp: offset + bitReader_value})
252 MOVB(brBitsRead.As8(), Mem{Base: br, Disp: offset + bitReader_bitsRead})
253 }
254
255 func bufferIndex(id int, buffer, dstEvery reg.GPVirtual) Mem {
256 switch id {
257 case 0:
258 return Mem{Base: buffer}
259 case 1, 2:
260 return Mem{Base: buffer, Index: dstEvery, Scale: byte(id)}
261 case 3:
262 stride3 := GP64() // stride3 := 3*dstEvery
263 LEAQ(Mem{Base: dstEvery, Index: dstEvery, Scale: 2}, stride3)
264 return Mem{Base: buffer, Index: stride3, Scale: 1}
265 default:
266 panic("id must be >=0, <4")
267 }
268 }
269
270 func (d decompress4x) fillFast32(id, atLeast int, br, exhausted reg.GPVirtual) (brValue, brBitsRead reg.GPVirtual) {
271 if atLeast > 32 {
272 panic(fmt.Sprintf("at least (%d) cannot be >32", atLeast))
273 }
274 Commentf("br%d.fillFast32()", id)
275 brValue = GP64()
276 brBitsRead = GP64()
277 offset := bitReader__size * id
278 MOVQ(Mem{Base: br, Disp: offset + bitReader_value}, brValue)
279 MOVBQZX(Mem{Base: br, Disp: offset + bitReader_bitsRead}, brBitsRead)
280
281 // We must have at least 2 * max tablelog left
282 CMPQ(brBitsRead, U8(64-atLeast))
283 JBE(LabelRef("skip_fill" + strconv.Itoa(id)))
284 brOffset := GP64()
285 MOVQ(Mem{Base: br, Disp: offset + bitReader_off}, brOffset)
286
287 SUBQ(U8(32), brBitsRead) // b.bitsRead -= 32
288 SUBQ(U8(4), brOffset) // b.off -= 4
289
290 // v := b.in[b.off-4 : b.off]
291 // v = v[:4]
292 // low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
293 tmp := GP64()
294 MOVQ(Mem{Base: br, Disp: offset + bitReader_in}, tmp)
295
296 Comment("b.value |= uint64(low) << (b.bitsRead & 63)")
297 addr := Mem{Base: brOffset, Index: tmp.As64(), Scale: 1}
298 CX := reg.CL
299 MOVL(addr, tmp.As32()) // tmp = uint32(b.in[b.off:b.off+4])
300 MOVQ(brBitsRead, CX.As64())
301 SHLQ(CX, tmp.As64())
302
303 MOVQ(brOffset, Mem{Base: br, Disp: offset + bitReader_off})
304 ORQ(tmp.As64(), brValue)
305 {
306 Commentf("exhausted += (br%d.off < 4)", id)
307 CMPQ(brOffset, U8(4))
308 // Add carry from brOffset-4. We do this at most four times per iteration,
309 // and every iteration resets exhausted's lower byte, so it doesn't overflow.
310 ADCB(I8(0), exhausted)
311 }
312
313 Label("skip_fill" + strconv.Itoa(id))
314 return
315 }
316
317 type bitReader struct {
318 in reg.GPVirtual
319 off reg.GPVirtual
320 value reg.GPVirtual
321 bitsRead reg.GPVirtual
322 id int
323 bmi2 bool
324 }
325
326 func (b *bitReader) uniqId() string {
327 b.id += 1
328 return strconv.Itoa(b.id)
329 }
330
331 func (b *bitReader) load(pointer gotypes.Component) {
332 b.in = GP64()
333 b.off = GP64()
334 b.value = GP64()
335 b.bitsRead = GP64()
336
337 Load(pointer.Field("in").Base(), b.in)
338 Load(pointer.Field("off"), b.off)
339 Load(pointer.Field("value"), b.value)
340 Load(pointer.Field("bitsRead"), b.bitsRead)
341 }
342
343 func (b *bitReader) store(pointer gotypes.Component) {
344 Store(b.off, pointer.Field("off"))
345 Store(b.value, pointer.Field("value"))
346 // Note: explicit As8(), without this avo reports: "could not deduce mov instruction"
347 Store(b.bitsRead.As8(), pointer.Field("bitsRead"))
348 }
349
350 func (b *bitReader) fillFast() {
351 label := "bitReader_fillFast_" + b.uniqId() + "_end"
352 CMPQ(b.bitsRead, U8(32))
353 JL(LabelRef(label))
354
355 SUBQ(U8(32), b.bitsRead)
356 SUBQ(U8(4), b.off)
357
358 tmp := GP64()
359 MOVL(Mem{Base: b.in, Index: b.off, Scale: 1}, tmp.As32())
360 if b.bmi2 {
361 SHLXQ(b.bitsRead, tmp, tmp)
362 } else {
363 MOVQ(b.bitsRead, reg.RCX)
364 SHLQ(reg.CL, tmp)
365 }
366 ORQ(tmp, b.value)
367 Label(label)
368 }
369
370 func (b *bitReader) peekTopBits(n reg.GPVirtual) reg.GPVirtual {
371 res := GP64()
372 if b.bmi2 {
373 SHRXQ(n, b.value, res)
374 } else {
375 MOVQ(n, reg.RCX)
376 MOVQ(b.value, res)
377 SHRQ(reg.CL, res)
378 }
379
380 return res
381 }
382
383 func (b *bitReader) advance(n reg.Register) {
384 ADDQ(n, b.bitsRead)
385 if b.bmi2 {
386 SHLXQ(n, b.value, b.value)
387 } else {
388 MOVQ(n, reg.RCX)
389 SHLQ(reg.CL, b.value)
390 }
391 }
392
393 type decompress1x struct {
394 bmi2 bool
395 }
396
397 func (d decompress1x) generateProcedure(name string) {
398 Package("github.com/klauspost/compress/huff0")
399 TEXT(name, 0, "func(ctx* decompress1xContext)")
400 Doc(name+" is an x86 assembler implementation of Decompress1X", "")
401 Pragma("noescape")
402
403 br := bitReader{}
404 br.bmi2 = d.bmi2
405
406 buffer := GP64()
407 bufferEnd := GP64() // the past-end address of buffer
408 dt := GP64()
409 peekBits := GP64()
410
411 {
412 ctx := Dereference(Param("ctx"))
413 Load(ctx.Field("out"), buffer)
414
415 outCap := GP64()
416 Load(ctx.Field("outCap"), outCap)
417 CMPQ(outCap, U8(4))
418 JB(LabelRef("error_max_decoded_size_exceeded"))
419
420 LEAQ(Mem{Base: buffer, Index: outCap, Scale: 1}, bufferEnd)
421
422 // load bitReader struct
423 pbr := Dereference(ctx.Field("pbr"))
424 br.load(pbr)
425
426 Load(ctx.Field("tbl"), dt)
427 Load(ctx.Field("peekBits"), peekBits)
428 }
429
430 JMP(LabelRef("loop_condition"))
431
432 Label("main_loop")
433
434 out := reg.AX // Fixed, as we need an 8H part
435
436 Comment("Check if we have room for 4 bytes in the output buffer")
437 {
438 tmp := GP64()
439 LEAQ(Mem{Base: buffer, Disp: 4}, tmp)
440 CMPQ(tmp, bufferEnd)
441 JGE(LabelRef("error_max_decoded_size_exceeded"))
442 }
443
444 decompress := func(id int, out reg.Register) {
445 d.decompress(id, &br, peekBits, dt, out)
446 }
447
448 Comment("Decode 4 values")
449 br.fillFast()
450 decompress(0, out.As8L())
451 decompress(1, out.As8H())
452 BSWAPL(out.As32())
453
454 br.fillFast()
455 decompress(2, out.As8H())
456 decompress(3, out.As8L())
457 BSWAPL(out.As32())
458
459 Comment("Store the decoded values")
460 MOVL(out.As32(), Mem{Base: buffer})
461 ADDQ(U8(4), buffer)
462
463 Label("loop_condition")
464 CMPQ(br.off, U8(8))
465 JGE(LabelRef("main_loop"))
466
467 Comment("Update ctx structure")
468 {
469 // calculate decoded as current `out` - initial `out`
470 ctx := Dereference(Param("ctx"))
471 ctxout, _ := ctx.Field("out").Resolve()
472 decoded := buffer
473 SUBQ(ctxout.Addr, decoded)
474 Store(decoded, ctx.Field("decoded"))
475
476 pbr := Dereference(ctx.Field("pbr"))
477 br.store(pbr)
478 }
479
480 RET()
481
482 Comment("Report error")
483 Label("error_max_decoded_size_exceeded")
484 {
485 ctx := Dereference(Param("ctx"))
486 tmp := GP64()
487 MOVQ(I64(-1), tmp)
488 Store(tmp, ctx.Field("decoded"))
489 }
490
491 RET()
492 }
493
494 func (d decompress1x) decompress(id int, br *bitReader, peekBits, dt reg.GPVirtual, out reg.Register) {
495 // v := dt[br.peekBitsFast(d.actualTableLog)&tlMask]
496 k := br.peekTopBits(peekBits)
497 v := reg.RCX // Fixed, as we need 8H part
498 MOVW(Mem{Base: dt, Index: k, Scale: 2}, v.As16())
499
500 // buf[id] = uint8(v.entry >> 8)
501 MOVB(v.As8H(), out)
502
503 // br.advance(uint8(v.entry))
504 MOVBQZX(v.As8L(), v)
505 br.advance(v)
506 }