Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

huff0: Pass a single bitReader pointer to asm #634

Merged
merged 1 commit into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 46 additions & 62 deletions huff0/_generate/gen.go
Expand Up @@ -49,49 +49,41 @@ func (d decompress4x) generateProcedure(name string) {
exhausted := GP64()
XORQ(exhausted.As64(), exhausted.As64()) // exhausted = false

limitPtr := AllocLocal(8)
limit := GP64()

bufferOrigin := GP64()
peekBits := GP64()
buffer := GP64()
dstEvery := GP64()
table := GP64()

br0 := GP64()
br1 := GP64()
br2 := GP64()
br3 := GP64()
br := GP64()

Comment("Preload values")
{
ctx := Dereference(Param("ctx"))
Load(ctx.Field("peekBits"), peekBits)
Load(ctx.Field("out"), buffer)
MOVQ(buffer, bufferOrigin)
limit := Load(ctx.Field("limit"), GP64())
MOVQ(limit, limitPtr)
Load(ctx.Field("out"), bufferOrigin)
Load(ctx.Field("limit"), limit)
Load(ctx.Field("dstEvery"), dstEvery)
Load(ctx.Field("tbl"), table)
Load(ctx.Field("pbr0"), br0)
Load(ctx.Field("pbr1"), br1)
Load(ctx.Field("pbr2"), br2)
Load(ctx.Field("pbr3"), br3)
Load(ctx.Field("pbr"), br)
}

Comment("Main loop")
Label("main_loop")

MOVQ(bufferOrigin, buffer)
// Check if we have space
CMPQ(buffer, limitPtr)
CMPQ(buffer, limit)
SETGE(exhausted.As8())
d.decodeTwoValues(0, br0, peekBits, table, buffer, exhausted)
d.decodeTwoValues(0, br, peekBits, table, buffer, exhausted)
ADDQ(dstEvery, buffer)
d.decodeTwoValues(1, br1, peekBits, table, buffer, exhausted)
d.decodeTwoValues(1, br, peekBits, table, buffer, exhausted)
ADDQ(dstEvery, buffer)
d.decodeTwoValues(2, br2, peekBits, table, buffer, exhausted)
d.decodeTwoValues(2, br, peekBits, table, buffer, exhausted)
ADDQ(dstEvery, buffer)
d.decodeTwoValues(3, br3, peekBits, table, buffer, exhausted)
d.decodeTwoValues(3, br, peekBits, table, buffer, exhausted)

ADDQ(U8(2), bufferOrigin) // off += 2

Expand All @@ -100,10 +92,9 @@ func (d decompress4x) generateProcedure(name string) {

{
ctx := Dereference(Param("ctx"))
tmp := Load(ctx.Field("out"), GP64())
decoded := GP64()
MOVQ(bufferOrigin, decoded)
SUBQ(tmp, decoded)
ctxout, _ := ctx.Field("out").Resolve()
decoded := bufferOrigin
SUBQ(ctxout.Addr, decoded)
SHLQ(U8(2), decoded) // decoded *= 4

Store(decoded, ctx.Field("decoded"))
Expand All @@ -118,6 +109,7 @@ const bitReader_in = 0
const bitReader_off = bitReader_in + 3*8 // {ptr, len, cap}
const bitReader_value = bitReader_off + 8
const bitReader_bitsRead = bitReader_value + 8
const bitReader__size = bitReader_bitsRead + 8

func (d decompress4x) decodeTwoValues(id int, br, peekBits, table, buffer, exhausted reg.GPVirtual) {
brValue, brBitsRead := d.fillFast32(id, 32, br, exhausted)
Expand Down Expand Up @@ -157,9 +149,10 @@ func (d decompress4x) decodeTwoValues(id int, br, peekBits, table, buffer, exhau
Comment("out[id * dstEvery + 1] = uint8(v1.entry >> 8)")
MOVW(out.As16(), Mem{Base: buffer})

Comment("update the bitrader reader structure")
MOVQ(brValue, Mem{Base: br, Disp: bitReader_value})
MOVB(brBitsRead.As8(), Mem{Base: br, Disp: bitReader_bitsRead})
Comment("update the bitreader structure")
offset := id * bitReader__size
MOVQ(brValue, Mem{Base: br, Disp: offset + bitReader_value})
MOVB(brBitsRead.As8(), Mem{Base: br, Disp: offset + bitReader_bitsRead})
}

func (d decompress4x) generateProcedure4x8bit(name string) {
Expand All @@ -171,49 +164,41 @@ func (d decompress4x) generateProcedure4x8bit(name string) {
exhausted := GP64() // Fixed since we need 8H
XORQ(exhausted.As64(), exhausted.As64()) // exhausted = false

bufferOrigin := AllocLocal(8)
limitPtr := AllocLocal(8)
bufferOrigin := GP64()
limit := GP64()

peekBits := GP64()
buffer := GP64()
dstEvery := GP64()
table := GP64()

br0 := GP64()
br1 := GP64()
br2 := GP64()
br3 := GP64()
br := GP64()

Comment("Preload values")
{
ctx := Dereference(Param("ctx"))
Load(ctx.Field("peekBits"), peekBits)
Load(ctx.Field("out"), buffer)
MOVQ(buffer, bufferOrigin)
limit := Load(ctx.Field("limit"), GP64())
MOVQ(limit, limitPtr)
Load(ctx.Field("out"), bufferOrigin)
Load(ctx.Field("limit"), limit)
Load(ctx.Field("dstEvery"), dstEvery)
Load(ctx.Field("tbl"), table)
Load(ctx.Field("pbr0"), br0)
Load(ctx.Field("pbr1"), br1)
Load(ctx.Field("pbr2"), br2)
Load(ctx.Field("pbr3"), br3)
Load(ctx.Field("pbr"), br)
}

Comment("Main loop")
Label("main_loop")

MOVQ(bufferOrigin, buffer)
// Check if we have space
CMPQ(buffer, limitPtr)
CMPQ(buffer, limit)
SETGE(exhausted.As8())
d.decodeFourValues(0, br0, peekBits, table, buffer, exhausted)
d.decodeFourValues(0, br, peekBits, table, buffer, exhausted)
ADDQ(dstEvery, buffer)
d.decodeFourValues(1, br1, peekBits, table, buffer, exhausted)
d.decodeFourValues(1, br, peekBits, table, buffer, exhausted)
ADDQ(dstEvery, buffer)
d.decodeFourValues(2, br2, peekBits, table, buffer, exhausted)
d.decodeFourValues(2, br, peekBits, table, buffer, exhausted)
ADDQ(dstEvery, buffer)
d.decodeFourValues(3, br3, peekBits, table, buffer, exhausted)
d.decodeFourValues(3, br, peekBits, table, buffer, exhausted)

ADDQ(U8(4), bufferOrigin) // off += 4

Expand All @@ -222,10 +207,9 @@ func (d decompress4x) generateProcedure4x8bit(name string) {

{
ctx := Dereference(Param("ctx"))
tmp := Load(ctx.Field("out"), GP64())
decoded := GP64()
MOVQ(bufferOrigin, decoded)
SUBQ(tmp, decoded)
ctxout, _ := ctx.Field("out").Resolve()
decoded := bufferOrigin
SUBQ(ctxout.Addr, decoded)
SHLQ(U8(2), decoded) // decoded *= 4

Store(decoded, ctx.Field("decoded"))
Expand All @@ -234,7 +218,7 @@ func (d decompress4x) generateProcedure4x8bit(name string) {
}

func (d decompress4x) decodeFourValues(id int, br, peekBits, table, buffer, exhausted reg.GPVirtual) {
brValue, brBitsRead := d.fillFast32(id+1000, 32, br, exhausted)
brValue, brBitsRead := d.fillFast32(id, 32, br, exhausted)

decompress := func(valID int, outByte reg.Register) {
CX := reg.CL
Expand Down Expand Up @@ -269,9 +253,10 @@ func (d decompress4x) decodeFourValues(id int, br, peekBits, table, buffer, exha
Comment("out[id * dstEvery + 4] = uint8(v3.entry >> 8)")
MOVL(out.As32(), Mem{Base: buffer})

Comment("update the bitreader reader structure")
MOVQ(brValue, Mem{Base: br, Disp: bitReader_value})
MOVB(brBitsRead.As8(), Mem{Base: br, Disp: bitReader_bitsRead})
Comment("update the bitreader structure")
offset := id * bitReader__size
MOVQ(brValue, Mem{Base: br, Disp: offset + bitReader_value})
MOVB(brBitsRead.As8(), Mem{Base: br, Disp: offset + bitReader_bitsRead})
}

func (d decompress4x) fillFast32(id, atLeast int, br, exhausted reg.GPVirtual) (brValue, brBitsRead reg.GPVirtual) {
Expand All @@ -281,14 +266,15 @@ func (d decompress4x) fillFast32(id, atLeast int, br, exhausted reg.GPVirtual) (
Commentf("br%d.fillFast32()", id)
brValue = GP64()
brBitsRead = GP64()
MOVQ(Mem{Base: br, Disp: bitReader_value}, brValue)
MOVBQZX(Mem{Base: br, Disp: bitReader_bitsRead}, brBitsRead)
offset := bitReader__size * id
MOVQ(Mem{Base: br, Disp: offset + bitReader_value}, brValue)
MOVBQZX(Mem{Base: br, Disp: offset + bitReader_bitsRead}, brBitsRead)

// We must have at least 2 * max tablelog left
CMPQ(brBitsRead, U8(64-atLeast))
JBE(LabelRef("skip_fill" + strconv.Itoa(id)))
brOffset := GP64()
MOVQ(Mem{Base: br, Disp: bitReader_off}, brOffset)
MOVQ(Mem{Base: br, Disp: offset + bitReader_off}, brOffset)

SUBQ(U8(32), brBitsRead) // b.bitsRead -= 32
SUBQ(U8(4), brOffset) // b.off -= 4
Expand All @@ -297,7 +283,7 @@ func (d decompress4x) fillFast32(id, atLeast int, br, exhausted reg.GPVirtual) (
// v = v[:4]
// low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
tmp := GP64()
MOVQ(Mem{Base: br, Disp: bitReader_in}, tmp)
MOVQ(Mem{Base: br, Disp: offset + bitReader_in}, tmp)

Comment("b.value |= uint64(low) << (b.bitsRead & 63)")
addr := Mem{Base: brOffset, Index: tmp.As64(), Scale: 1}
Expand All @@ -306,7 +292,7 @@ func (d decompress4x) fillFast32(id, atLeast int, br, exhausted reg.GPVirtual) (
MOVQ(brBitsRead, CX.As64())
SHLQ(CX, tmp.As64())

MOVQ(brOffset, Mem{Base: br, Disp: bitReader_off})
MOVQ(brOffset, Mem{Base: br, Disp: offset + bitReader_off})
ORQ(tmp.As64(), brValue)
{
Commentf("exhausted = exhausted || (br%d.off < 4)", id)
Expand Down Expand Up @@ -474,11 +460,9 @@ func (d decompress1x) generateProcedure(name string) {
{
// calculate decoded as current `out` - initial `out`
ctx := Dereference(Param("ctx"))
decoded := GP64()
tmp := GP64()
MOVQ(buffer, decoded)
Load(ctx.Field("out"), tmp)
SUBQ(tmp, decoded)
ctxout, _ := ctx.Field("out").Resolve()
decoded := buffer
SUBQ(ctxout.Addr, decoded)
Store(decoded, ctx.Field("decoded"))

pbr := Dereference(ctx.Field("pbr"))
Expand Down
10 changes: 2 additions & 8 deletions huff0/decompress_amd64.go
Expand Up @@ -27,10 +27,7 @@ func decompress4x_8b_main_loop_amd64(ctx *decompress4xContext)
const fallback8BitSize = 800

type decompress4xContext struct {
pbr0 *bitReaderShifted
pbr1 *bitReaderShifted
pbr2 *bitReaderShifted
pbr3 *bitReaderShifted
pbr *[4]bitReaderShifted
peekBits uint8
out *byte
dstEvery int
Expand Down Expand Up @@ -89,10 +86,7 @@ func (d *Decoder) Decompress4X(dst, src []byte) ([]byte, error) {

if len(out) > 4*4 && !(br[0].off < 4 || br[1].off < 4 || br[2].off < 4 || br[3].off < 4) {
ctx := decompress4xContext{
pbr0: &br[0],
pbr1: &br[1],
pbr2: &br[2],
pbr3: &br[3],
pbr: &br,
peekBits: uint8((64 - d.actualTableLog) & 63), // see: bitReaderShifted.peekBitsFast()
out: &out[0],
dstEvery: dstEvery,
Expand Down