-
Notifications
You must be signed in to change notification settings - Fork 53
/
bitpack_gen.go
165 lines (140 loc) · 3.64 KB
/
bitpack_gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//go:build ignore
// +build ignore
package main
// This file is based on the code from https://github.com/kostya-sh/parquet-go
// Copyright (c) 2015 Konstantin Shaposhnikov
import (
"bytes"
"fmt"
"go/format"
"io"
"io/ioutil"
"log"
"strings"
)
func genExpr(maxWidth int, bw int, i int, startBit int) (expr string, newStartBit int) {
byteShift := 0
firstCurByteBit := startBit - startBit%8
for bw != 0 {
curByte := startBit / 8
bitsInCurByte := bw
if bitsLeft := startBit - firstCurByteBit + 1; bitsInCurByte > bitsLeft {
bitsInCurByte = bitsLeft
}
shiftSize := 7 - startBit%8
mask := 1<<uint(bitsInCurByte) - 1
if len(expr) != 0 {
expr += " | "
}
expr += fmt.Sprintf("uint%d((data[%d] >> %d) & %d) << %d",
maxWidth, curByte, shiftSize, mask, byteShift)
bw -= bitsInCurByte
startBit -= bitsInCurByte
if startBit < firstCurByteBit {
startBit = firstCurByteBit + 15
firstCurByteBit += 8
}
byteShift += bitsInCurByte
}
return expr, startBit
}
func genUnpackFunc(out io.Writer, maxWidth int, bw int) {
fmt.Fprintf(out, "func unpack8int%d_%d(data []byte) (a [8]int%d) {\n", maxWidth, bw, maxWidth)
fmt.Fprintf(out, "\t_ = data[%d]\n", bw-1)
startBit := 7
var expr string
for i := 0; i < 8; i++ {
expr, startBit = genExpr(maxWidth, bw, i, startBit)
fmt.Fprintf(out, "\ta[%d] = int%d(%s)\n", i, maxWidth, expr)
}
fmt.Fprintf(out, "\treturn\n")
fmt.Fprintf(out, "}\n\n")
}
func getBits(idx int, bitSize, size, left, pos int, rev bool) string {
op := "<<"
if rev {
op = ">>"
}
return fmt.Sprintf("uint%d(data[%d])%s%d", bitSize, idx, op, size-left+pos)
}
func genPackFunc(w io.Writer, bitSize, size int) {
var (
left = size
indx int
rev bool
)
fmt.Fprintf(w, "func pack8int%[1]d_%[2]d(data [8]int%[1]d) []byte {", bitSize, size)
fmt.Fprintln(w, "\n\treturn []byte{")
for i := 0; i < size; i++ {
var fields []string
for right := 0; right < 8; {
if left == 0 {
indx++
left = size
rev = false
}
fields = append(fields, getBits(indx, bitSize, size, left, right, rev))
if left >= 8-right {
left -= (8 - right)
right = 8
rev = true
} else {
right += left
left = 0
}
}
fmt.Fprintf(w, "\t\tbyte(%s),\n", strings.Join(fields, " | "))
}
fmt.Fprintln(w, "\t}\n}\n")
}
func funcSlice(bitSize int) string {
buf := &bytes.Buffer{}
fmt.Fprintf(buf, `var unpack8Int%[1]dFuncByWidth = [%[2]d]unpack8int%[1]dFunc{`, bitSize, bitSize+1)
for i := 0; i <= bitSize; i++ {
fmt.Fprintf(buf, "\n\tunpack8int%d_%d,", bitSize, i)
}
fmt.Fprintf(buf, "\n}\n")
fmt.Fprintf(buf, `var pack8Int%[1]dFuncByWidth = [%[2]d]pack8int%[1]dFunc{`, bitSize, bitSize+1)
for i := 0; i <= bitSize; i++ {
fmt.Fprintf(buf, "\n\tpack8int%d_%d,", bitSize, i)
}
fmt.Fprintf(buf, "\n}\n")
return buf.String()
}
func zeroFuncs(w io.Writer, bitSize int) {
fmt.Fprintf(w, `
type (
unpack8int%[1]dFunc func([]byte) [8]int%[1]d
pack8int%[1]dFunc func([8]int%[1]d) []byte
)
%[2]s
func unpack8int%[1]d_0(_ []byte) (a [8]int%[1]d) {
return a
}
func pack8int%[1]d_0(_ [8]int%[1]d) []byte {
return []byte{}
}
`, bitSize, funcSlice(bitSize))
}
func genPackage(fn string, maxWidth int) {
buf := new(bytes.Buffer)
fmt.Fprint(buf, "// Code generated by \"bitpacking_gen.go\"; DO NOT EDIT.\n\n")
fmt.Fprintf(buf, "package goparquet\n\n")
zeroFuncs(buf, maxWidth)
for i := 1; i <= maxWidth; i++ {
genUnpackFunc(buf, maxWidth, i)
genPackFunc(buf, maxWidth, i)
}
src, err := format.Source(buf.Bytes())
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(fn, src, 0644)
if err != nil {
log.Fatal(err)
}
}
func main() {
genPackage("bitbacking32.go", 32)
genPackage("bitpacking64.go", 64)
}