-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.go
224 lines (189 loc) · 5.08 KB
/
binary.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package dec128
import (
"encoding/binary"
"io"
"github.com/jokruger/dec128/state"
)
// BinarySize returns the number of bytes required to encode this instance of Dec128 in binary form.
func (self Dec128) BinarySize() int {
sz := 1
if self.state <= state.Error {
if self.coef.Hi > 0 {
sz += 8
}
if self.coef.Lo > 0 {
sz += 8
}
if self.exp > 0 {
sz++
}
}
return sz
}
// EncodeBinary encodes the binary representation of Dec128 into buf. It returns an error if buf is too small, otherwise the number of bytes written into buf.
func (self Dec128) EncodeBinary(buf []byte) (int, error) {
sz := len(buf)
// Fast path for error state or zero coefficient
if self.state >= state.Error || self.coef.IsZero() {
if sz == 0 {
return 0, io.ErrShortBuffer
}
buf[0] = byte(self.state)
return 1, nil
}
flags := byte(self.state)
pos := 1
if self.coef.Hi > 0 {
if pos+8 > sz {
return pos, io.ErrShortBuffer
}
flags |= 0b10000000
binary.LittleEndian.PutUint64(buf[pos:], self.coef.Hi)
pos += 8
}
if self.coef.Lo > 0 {
if pos+8 > sz {
return pos, io.ErrShortBuffer
}
flags |= 0b01000000
binary.LittleEndian.PutUint64(buf[pos:], self.coef.Lo)
pos += 8
}
if self.exp > 0 {
if pos+1 > sz {
return pos, io.ErrShortBuffer
}
flags |= 0b00100000
buf[pos] = self.exp
pos++
}
// Write flag byte at the beginning
buf[0] = flags
return pos, nil
}
// DecodeBinary decodes binary representation of Dec128 from buf. It returns an error if buf is too small, otherwise the number of bytes consumed from buf.
func (self *Dec128) DecodeBinary(buf []byte) (int, error) {
sz := len(buf)
if sz == 0 {
return 0, io.ErrShortBuffer
}
flags := buf[0]
// Determine how many extra bytes to read.
hiPresent := int((flags >> 7) & 1) // 1 if coef.Hi is present, else 0
loPresent := int((flags >> 6) & 1) // 1 if coef.Lo is present, else 0
expPresent := int((flags >> 5) & 1) // 1 if exponent is present, else 0
extraLen := hiPresent*8 + loPresent*8 + expPresent
if extraLen+1 > sz {
return 1, io.ErrShortBuffer
}
// Parse the extra bytes.
idx := 1
var h, l uint64
var e uint8
if hiPresent > 0 {
h = binary.LittleEndian.Uint64(buf[idx : idx+8])
idx += 8
}
if loPresent > 0 {
l = binary.LittleEndian.Uint64(buf[idx : idx+8])
idx += 8
}
if expPresent > 0 {
e = buf[idx]
idx++
}
self.state = state.State(flags & 0b00011111)
self.coef.Hi = h
self.coef.Lo = l
self.exp = e
return idx, nil
}
// MarshalBinary implements the encoding.BinaryMarshaler interface. It encodes Dec128 into a binary form and returns the result.
func (self Dec128) MarshalBinary() ([]byte, error) {
var buf [MaxBytes]byte
n, err := self.EncodeBinary(buf[:])
if err != nil {
return nil, err
}
return buf[:n], nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (self *Dec128) UnmarshalBinary(data []byte) error {
n, err := self.DecodeBinary(data)
if err != nil {
return err
}
if n != len(data) {
return io.ErrShortBuffer
}
return nil
}
// GobEncode implements the gob.GobEncoder interface for gob serialization.
func (self Dec128) GobEncode() ([]byte, error) {
return self.MarshalBinary()
}
// GobDecode implements the gob.GobDecoder interface for gob serialization.
func (self *Dec128) GobDecode(data []byte) error {
return self.UnmarshalBinary(data)
}
// AppendBinary appends the binary representation of Dec128 to the end of b (allocating a larger slice if necessary) and returns the updated slice.
func (self Dec128) AppendBinary(buf []byte) ([]byte, error) {
var tmp [MaxBytes]byte
n, err := self.EncodeBinary(tmp[:])
if err != nil {
return buf, err
}
return append(buf, tmp[:n]...), nil
}
// WriteBinary writes the binary representation of Dec128 to w.
func (self Dec128) WriteBinary(w io.Writer) error {
var buf [MaxBytes]byte
n, err := self.EncodeBinary(buf[:])
if err != nil {
return err
}
_, err = w.Write(buf[:n])
return err
}
// ReadBinary reads the binary representation of Dec128 from r.
func (self *Dec128) ReadBinary(r io.Reader) error {
// Use one fixed buffer of 18 bytes.
var buf [18]byte
// First, read the flag byte.
if _, err := io.ReadFull(r, buf[:1]); err != nil {
return err
}
flags := buf[0]
// Determine how many extra bytes to read.
hiPresent := int((flags >> 7) & 1) // 1 if coef.Hi is present, else 0
loPresent := int((flags >> 6) & 1) // 1 if coef.Lo is present, else 0
expPresent := int((flags >> 5) & 1) // 1 if exponent is present, else 0
extraLen := hiPresent*8 + loPresent*8 + expPresent
// Read the extra bytes directly into the remaining portion of buf.
if extraLen > 0 {
if _, err := io.ReadFull(r, buf[1:1+extraLen]); err != nil {
return err
}
}
// Parse the extra bytes.
idx := 1
var h, l uint64
var e uint8
if hiPresent > 0 {
h = binary.LittleEndian.Uint64(buf[idx : idx+8])
idx += 8
}
if loPresent > 0 {
l = binary.LittleEndian.Uint64(buf[idx : idx+8])
idx += 8
}
if expPresent > 0 {
e = buf[idx]
// idx++ is not needed since no further byte is used.
}
self.state = state.State(flags & 0b00011111)
self.coef.Hi = h
self.coef.Lo = l
self.exp = e
return nil
}