Debian Codegraph @

build/base64/encode_asm.go

raw
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/operand"
15 . "github.com/segmentio/asm/build/internal/asm"
16 . "github.com/segmentio/asm/build/internal/x86"
17 )
18
19 func init() {
20 ConstraintExpr("!purego")
21 }
22
23 func main() {
24 TEXT("encodeAVX2", NOSPLIT, "func(dst, src []byte, lut *int8) (int, int)")
25
26 dst := Mem{Base: Load(Param("dst").Base(), GP64()), Index: GP64(), Scale: 1}
27 src := Mem{Base: Load(Param("src").Base(), GP64()), Index: GP64(), Scale: 1}
28 lut := Mem{Base: Load(Param("lut"), GP64())}
29 rem := Load(Param("src").Len(), GP64())
30
31 rsrc := YMM()
32 rdst := YMM()
33 msrc := YMM()
34 shl4 := YMM()
35 shl8 := YMM()
36 blnd := YMM()
37 mult := YMM()
38 shfl := YMM()
39 subs := YMM()
40 cmps := YMM()
41 xlat := YMM()
42 xtab := YMM()
43 xsub := VecBroadcast(U8(51), YMM())
44 xcmp := VecBroadcast(U8(25), YMM())
45
46 XORQ(dst.Index, dst.Index)
47 XORQ(src.Index, src.Index)
48
49 Comment("Load the 16-byte LUT into both lanes of the register")
50 VPERMQ(Imm(1<<6|1<<2), lut, xtab)
51
52 Comment("Load the first block using a mask to avoid potential fault")
53 VMOVDQU(ConstLoadMask32("b64_enc_load",
54 0, 1, 1, 1,
55 1, 1, 1, 1,
56 ), rsrc)
57 VPMASKMOVD(src.Offset(-4), rsrc, rsrc)
58
59 Label("loop")
60
61 VPSHUFB(ConstBytes("b64_enc_shuf", []byte{
62 5, 4, 6, 5, 8, 7, 9, 8, 11, 10, 12, 11, 14, 13, 15, 14,
63 1, 0, 2, 1, 4, 3, 5, 4, 7, 6, 8, 7, 10, 9, 11, 10,
64 }), rsrc, rsrc)
65
66 VPAND(ConstArray16("b64_enc_mask1",
67 0x03f0, 0x003f, 0x03f0, 0x003f, 0x03f0, 0x003f, 0x03f0, 0x003f,
68 0x03f0, 0x003f, 0x03f0, 0x003f, 0x03f0, 0x003f, 0x03f0, 0x003f,
69 ), rsrc, msrc)
70 VPSLLW(Imm(8), msrc, shl8)
71 VPSLLW(Imm(4), msrc, shl4)
72 VPBLENDW(Imm(170), shl8, shl4, blnd)
73
74 VPAND(ConstArray16("b64_enc_mask2",
75 0xfc00, 0x0fc0, 0xfc00, 0x0fc0, 0xfc00, 0x0fc0, 0xfc00, 0x0fc0,
76 0xfc00, 0x0fc0, 0xfc00, 0x0fc0, 0xfc00, 0x0fc0, 0xfc00, 0x0fc0,
77 ), rsrc, msrc)
78 VPMULHUW(ConstArray16("b64_enc_mult",
79 0x0040, 0x0400, 0x0040, 0x0400, 0x0040, 0x0400, 0x0040, 0x0400,
80 0x0040, 0x0400, 0x0040, 0x0400, 0x0040, 0x0400, 0x0040, 0x0400,
81 ), msrc, mult)
82
83 VPOR(mult, blnd, shfl)
84
85 VPSUBUSB(xsub, shfl, subs)
86 VPCMPGTB(xcmp, shfl, cmps)
87 VPSUBB(cmps, subs, subs)
88 VPSHUFB(subs, xtab, xlat)
89 VPADDB(shfl, xlat, rdst)
90 VMOVDQU(rdst, dst)
91
92 ADDQ(Imm(32), dst.Index)
93 ADDQ(Imm(24), src.Index)
94 SUBQ(Imm(24), rem)
95
96 CMPQ(rem, Imm(32))
97 JB(LabelRef("done"))
98
99 VMOVDQU(src.Offset(-4), rsrc)
100 JMP(LabelRef("loop"))
101
102 Label("done")
103 Store(dst.Index, ReturnIndex(0))
104 Store(src.Index, ReturnIndex(1))
105 VZEROUPPER()
106 RET()
107
108 Generate()
109 }