-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.go
More file actions
197 lines (179 loc) · 5.2 KB
/
binary.go
File metadata and controls
197 lines (179 loc) · 5.2 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package numfmt
import (
"encoding/binary"
"fmt"
"math"
)
const (
binaryMagic = "NMF1"
binaryVersion = uint8(1)
headerSize = 40
)
const (
headerFlagRange = 1 << 0
)
// BinaryHeader stores codec metadata persisted in a binary payload header.
type BinaryHeader struct {
Version uint8
TotalBits uint8
ExpBits uint8
Base float64
RangeMin float64
RangeMax float64
UseRange bool
Count uint64
}
// EncodeValuesBinary encodes values into a compact binary payload.
//
// Format:
// - 4 bytes magic: "NMF1"
// - 1 byte version
// - 1 byte total bits
// - 1 byte exponent bits
// - 1 byte flags (bit0: range mode)
// - 8 bytes base (float64 little-endian bits)
// - 8 bytes min (float64 little-endian bits)
// - 8 bytes max (float64 little-endian bits)
// - 8 bytes count (uint64 little-endian)
// - bit-packed codes (little-endian bit order, width=total bits)
func EncodeValuesBinary(c Codec, values []float64) ([]byte, error) {
if _, err := New(c.TotalBits, c.ExpBits, c.Base); err != nil {
return nil, err
}
if c.useRange {
if _, err := c.WithRange(c.RangeMin, c.RangeMax); err != nil {
return nil, err
}
}
codes := make([]uint64, len(values))
for i, v := range values {
codes[i] = c.Encode(v)
}
payload, err := packCodes(c.TotalBits, codes)
if err != nil {
return nil, err
}
out := make([]byte, headerSize+len(payload))
copy(out[0:4], []byte(binaryMagic))
out[4] = binaryVersion
out[5] = c.TotalBits
out[6] = c.ExpBits
if c.useRange {
out[7] = headerFlagRange
}
binary.LittleEndian.PutUint64(out[8:16], math.Float64bits(c.Base))
binary.LittleEndian.PutUint64(out[16:24], math.Float64bits(c.RangeMin))
binary.LittleEndian.PutUint64(out[24:32], math.Float64bits(c.RangeMax))
binary.LittleEndian.PutUint64(out[32:40], uint64(len(values)))
copy(out[headerSize:], payload)
return out, nil
}
// DecodeValuesBinary decodes a binary payload produced by EncodeValuesBinary.
func DecodeValuesBinary(data []byte) (Codec, []float64, error) {
h, err := DecodeBinaryHeader(data)
if err != nil {
return Codec{}, nil, err
}
codec, err := New(h.TotalBits, h.ExpBits, h.Base)
if err != nil {
return Codec{}, nil, err
}
if h.UseRange {
codec, err = codec.WithRange(h.RangeMin, h.RangeMax)
if err != nil {
return Codec{}, nil, err
}
}
payloadBits := h.Count * uint64(h.TotalBits)
payloadBytes := int((payloadBits + 7) / 8)
if len(data) != headerSize+payloadBytes {
return Codec{}, nil, fmt.Errorf("invalid payload size: got %d, want %d", len(data), headerSize+payloadBytes)
}
codes, err := unpackCodes(h.TotalBits, h.Count, data[headerSize:])
if err != nil {
return Codec{}, nil, err
}
values := make([]float64, len(codes))
for i, code := range codes {
values[i] = codec.Decode(code)
}
return codec, values, nil
}
// DecodeBinaryHeader parses only the binary header.
func DecodeBinaryHeader(data []byte) (BinaryHeader, error) {
if len(data) < headerSize {
return BinaryHeader{}, fmt.Errorf("binary payload too small: got %d, need at least %d", len(data), headerSize)
}
if string(data[0:4]) != binaryMagic {
return BinaryHeader{}, fmt.Errorf("invalid magic")
}
if data[4] != binaryVersion {
return BinaryHeader{}, fmt.Errorf("unsupported version: %d", data[4])
}
h := BinaryHeader{
Version: data[4],
TotalBits: data[5],
ExpBits: data[6],
UseRange: data[7]&headerFlagRange != 0,
Base: math.Float64frombits(binary.LittleEndian.Uint64(data[8:16])),
RangeMin: math.Float64frombits(binary.LittleEndian.Uint64(data[16:24])),
RangeMax: math.Float64frombits(binary.LittleEndian.Uint64(data[24:32])),
Count: binary.LittleEndian.Uint64(data[32:40]),
}
if _, err := New(h.TotalBits, h.ExpBits, h.Base); err != nil {
return BinaryHeader{}, err
}
if h.UseRange {
c := Codec{TotalBits: h.TotalBits, ExpBits: h.ExpBits, Base: h.Base}
if _, err := c.WithRange(h.RangeMin, h.RangeMax); err != nil {
return BinaryHeader{}, err
}
}
return h, nil
}
func packCodes(totalBits uint8, codes []uint64) ([]byte, error) {
if !isValidTotalBits(totalBits) {
return nil, fmt.Errorf("totalBits must be one of [8,16,32], got %d", totalBits)
}
mask := uint64(1<<totalBits) - 1
totalBitsLen := uint64(len(codes)) * uint64(totalBits)
out := make([]byte, (totalBitsLen+7)/8)
bitPos := uint64(0)
for _, code := range codes {
code &= mask
for b := uint8(0); b < totalBits; b++ {
if (code & (1 << b)) != 0 {
byteIdx := bitPos / 8
bitIdx := bitPos % 8
out[byteIdx] |= byte(1 << bitIdx)
}
bitPos++
}
}
return out, nil
}
func unpackCodes(totalBits uint8, count uint64, payload []byte) ([]uint64, error) {
if !isValidTotalBits(totalBits) {
return nil, fmt.Errorf("totalBits must be one of [8,16,32], got %d", totalBits)
}
totalBitsLen := count * uint64(totalBits)
wantBytes := (totalBitsLen + 7) / 8
if uint64(len(payload)) != wantBytes {
return nil, fmt.Errorf("invalid packed code size: got %d, want %d", len(payload), wantBytes)
}
codes := make([]uint64, count)
bitPos := uint64(0)
for i := uint64(0); i < count; i++ {
var code uint64
for b := uint8(0); b < totalBits; b++ {
byteIdx := bitPos / 8
bitIdx := bitPos % 8
if payload[byteIdx]&(1<<bitIdx) != 0 {
code |= (1 << b)
}
bitPos++
}
codes[i] = code
}
return codes, nil
}