forked from woodsbury/decimal128
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.go
More file actions
82 lines (73 loc) · 1.76 KB
/
binary.go
File metadata and controls
82 lines (73 loc) · 1.76 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
package decimal128
import "errors"
// AppendBinary implements the [encoding.BinaryAppender] interface. It
// marshals the Decimal into IEEE 754 format.
func (d Decimal) AppendBinary(buf []byte) ([]byte, error) {
buf = append(
buf,
byte(d.hi>>56),
byte(d.hi>>48),
byte(d.hi>>40),
byte(d.hi>>32),
byte(d.hi>>24),
byte(d.hi>>16),
byte(d.hi>>8),
byte(d.hi),
byte(d.lo>>56),
byte(d.lo>>48),
byte(d.lo>>40),
byte(d.lo>>32),
byte(d.lo>>24),
byte(d.lo>>16),
byte(d.lo>>8),
byte(d.lo),
)
return buf, nil
}
// MarshalBinary implements the [encoding.BinaryMarshaler] interface. It
// marshals the Decimal into IEEE 754 format.
func (d Decimal) MarshalBinary() ([]byte, error) {
return []byte{
byte(d.hi >> 56),
byte(d.hi >> 48),
byte(d.hi >> 40),
byte(d.hi >> 32),
byte(d.hi >> 24),
byte(d.hi >> 16),
byte(d.hi >> 8),
byte(d.hi),
byte(d.lo >> 56),
byte(d.lo >> 48),
byte(d.lo >> 40),
byte(d.lo >> 32),
byte(d.lo >> 24),
byte(d.lo >> 16),
byte(d.lo >> 8),
byte(d.lo),
}, nil
}
// UnmarshalBinary implements the [encoding.BinaryUnmarshaler] interface. It
// unmarshals a Decimal in IEEE 754 format.
func (d *Decimal) UnmarshalBinary(data []byte) error {
if len(data) != 16 {
return errors.New("Decimal.UnmarshalBinary: invalid length")
}
lo := uint64(data[15])
lo |= uint64(data[14]) << 8
lo |= uint64(data[13]) << 16
lo |= uint64(data[12]) << 24
lo |= uint64(data[11]) << 32
lo |= uint64(data[10]) << 40
lo |= uint64(data[9]) << 48
lo |= uint64(data[8]) << 56
hi := uint64(data[7])
hi |= uint64(data[6]) << 8
hi |= uint64(data[5]) << 16
hi |= uint64(data[4]) << 24
hi |= uint64(data[3]) << 32
hi |= uint64(data[2]) << 40
hi |= uint64(data[1]) << 48
hi |= uint64(data[0]) << 56
*d = Decimal{lo, hi}
return nil
}