| 1 |
// +build ignore |
| 2 |
// |
| 3 |
// This code is a go assembly implementation of: |
| 4 |
// |
| 5 |
// Muła, Wojciech, & Lemire, Daniel (Thu, 14 Jun 2018). |
| 6 |
// Faster Base64 Encoding and Decoding Using AVX2 Instructions. |
| 7 |
// [arXiv:1704.00605](https://arxiv.org/abs/1704.00605) |
| 8 |
// |
| 9 |
// ...with changes to support multiple encodings. |
| 10 |
package main |
| 11 |
|
| 12 |
import ( |
| 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 |
. "github.com/segmentio/asm/build/internal/asm" |
| 18 |
. "github.com/segmentio/asm/build/internal/x86" |
| 19 |
) |
| 20 |
|
| 21 |
var lutHi = ConstBytes("b64_dec_lut_hi", []byte{ |
| 22 |
16, 16, 1, 2, 4, 8, 4, 8, 16, 16, 16, 16, 16, 16, 16, 16, |
| 23 |
16, 16, 1, 2, 4, 8, 4, 8, 16, 16, 16, 16, 16, 16, 16, 16, |
| 24 |
}) |
| 25 |
|
| 26 |
var madd1 = ConstBytes("b64_dec_madd1", []byte{ |
| 27 |
64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, |
| 28 |
64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, 64, 1, |
| 29 |
}) |
| 30 |
|
| 31 |
var madd2 = ConstArray16("b64_dec_madd2", |
| 32 |
4096, 1, 4096, 1, 4096, 1, 4096, 1, |
| 33 |
4096, 1, 4096, 1, 4096, 1, 4096, 1, |
| 34 |
) |
| 35 |
|
| 36 |
var shufLo = ConstBytes("b64_dec_shuf_lo", []byte{ |
| 37 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 6, |
| 38 |
}) |
| 39 |
|
| 40 |
var shuf = ConstBytes("b64_dec_shuf", []byte{ |
| 41 |
2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12, 0, 0, 0, 0, |
| 42 |
5, 4, 10, 9, 8, 14, 13, 12, 0, 0, 0, 0, 0, 0, 0, 0, |
| 43 |
}) |
| 44 |
|
| 45 |
func init() { |
| 46 |
ConstraintExpr("!purego") |
| 47 |
} |
| 48 |
|
| 49 |
func main() { |
| 50 |
TEXT("decodeAVX2", NOSPLIT, "func(dst, src []byte, lut *int8) (int, int)") |
| 51 |
createDecode(Param("dst"), Param("src"), Param("lut"), func(m Mem, r VecVirtual) { |
| 52 |
VMOVDQU(m, r) |
| 53 |
}) |
| 54 |
|
| 55 |
TEXT("decodeAVX2URI", NOSPLIT, "func(dst, src []byte, lut *int8) (int, int)") |
| 56 |
slash := VecBroadcast(U8('/'), YMM()) |
| 57 |
underscore := VecBroadcast(U8('_'), YMM()) |
| 58 |
createDecode(Param("dst"), Param("src"), Param("lut"), func(m Mem, r VecVirtual) { |
| 59 |
eq := YMM() |
| 60 |
VMOVDQU(m, r) |
| 61 |
VPCMPEQB(r, underscore, eq) |
| 62 |
VPBLENDVB(eq, slash, r, r) |
| 63 |
}) |
| 64 |
|
| 65 |
Generate() |
| 66 |
} |
| 67 |
|
| 68 |
func createDecode(pdst, psrc, plut Component, load func(m Mem, r VecVirtual)) { |
| 69 |
dst := Mem{Base: Load(pdst.Base(), GP64()), Index: GP64(), Scale: 1} |
| 70 |
src := Mem{Base: Load(psrc.Base(), GP64()), Index: GP64(), Scale: 1} |
| 71 |
lut := Mem{Base: Load(plut, GP64())} |
| 72 |
rem := Load(psrc.Len(), GP64()) |
| 73 |
|
| 74 |
rsrc := YMM() |
| 75 |
rdst := YMM() |
| 76 |
nibh := YMM() |
| 77 |
nibl := YMM() |
| 78 |
emsk := YMM() |
| 79 |
roll := YMM() |
| 80 |
shfl := YMM() |
| 81 |
lutl := YMM() |
| 82 |
luth := YMM() |
| 83 |
lutr := YMM() |
| 84 |
zero := YMM() |
| 85 |
lo := YMM() |
| 86 |
hi := YMM() |
| 87 |
mask := VecBroadcast(U8(0x2f), YMM()) |
| 88 |
|
| 89 |
XORQ(dst.Index, dst.Index) |
| 90 |
XORQ(src.Index, src.Index) |
| 91 |
VPXOR(zero, zero, zero) |
| 92 |
|
| 93 |
VPERMQ(Imm(1<<6|1<<2), lut, lutr) |
| 94 |
VPERMQ(Imm(1<<6|1<<2), lut.Offset(16), lutl) |
| 95 |
VMOVDQA(lutHi, luth) |
| 96 |
|
| 97 |
Label("loop") |
| 98 |
|
| 99 |
load(src, rsrc) |
| 100 |
|
| 101 |
VPSRLD(Imm(4), rsrc, nibh) |
| 102 |
VPAND(mask, rsrc, nibl) |
| 103 |
VPSHUFB(nibl, lutl, lo) |
| 104 |
VPAND(mask, nibh, nibh) |
| 105 |
VPSHUFB(nibh, luth, hi) |
| 106 |
|
| 107 |
VPTEST(hi, lo) |
| 108 |
JNE(LabelRef("done")) |
| 109 |
|
| 110 |
VPCMPEQB(mask, rsrc, emsk) |
| 111 |
VPADDB(emsk, nibh, roll) |
| 112 |
|
| 113 |
VPSHUFB(roll, lutr, roll) |
| 114 |
|
| 115 |
VPADDB(rsrc, roll, shfl) |
| 116 |
VPMADDUBSW(madd1, shfl, shfl) |
| 117 |
VPMADDWD(madd2, shfl, shfl) |
| 118 |
|
| 119 |
VEXTRACTI128(Imm(1), shfl, rdst.AsX()) |
| 120 |
VPSHUFB(shufLo, rdst.AsX(), rdst.AsX()) |
| 121 |
VPSHUFB(shuf, shfl, shfl) |
| 122 |
|
| 123 |
VPBLENDD(Imm(8), rdst, shfl, rdst) |
| 124 |
VPBLENDD(Imm(192), zero, rdst, rdst) |
| 125 |
VMOVDQU(rdst, dst) |
| 126 |
|
| 127 |
ADDQ(Imm(24), dst.Index) |
| 128 |
ADDQ(Imm(32), src.Index) |
| 129 |
SUBQ(Imm(32), rem) |
| 130 |
|
| 131 |
CMPQ(rem, Imm(45)) |
| 132 |
JB(LabelRef("done")) |
| 133 |
JMP(LabelRef("loop")) |
| 134 |
|
| 135 |
Label("done") |
| 136 |
Store(dst.Index, ReturnIndex(0)) |
| 137 |
Store(src.Index, ReturnIndex(1)) |
| 138 |
VZEROUPPER() |
| 139 |
RET() |
| 140 |
} |