-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.go
346 lines (295 loc) · 7.98 KB
/
complex.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package schemer
import (
"encoding/json"
"errors"
"fmt"
"io"
"math"
"reflect"
"strconv"
)
type ComplexSchema struct {
SchemaOptions
Bits int // must be 64 or 128
}
func (s *ComplexSchema) GoType() reflect.Type {
var c1 complex64
var c2 complex128
var retval reflect.Type
if s.Bits == 64 {
retval = reflect.TypeOf(c1)
} else {
retval = reflect.TypeOf(c2)
}
if s.Nullable() {
retval = reflect.PtrTo(retval)
}
return retval
}
func (s *ComplexSchema) Valid() bool {
return s.Bits == 64 || s.Bits == 128
}
func (s *ComplexSchema) MarshalJSON() ([]byte, error) {
if !s.Valid() {
return nil, fmt.Errorf("invalid ComplexSchema")
}
return json.Marshal(map[string]interface{}{
"type": "complex",
"nullable": s.Nullable(),
"bits": s.Bits,
})
}
// Bytes encodes the schema in a portable binary format
func (s *ComplexSchema) MarshalSchemer() ([]byte, error) {
// floating point schemas are 1 byte long
var schema []byte = []byte{ComplexByte}
// bit 8 indicates whether or not the type is nullable
if s.Nullable() {
schema[0] |= NullMask
}
// bit 1 = complex number size in (64 << n) bits
if s.Bits == 64 {
// do nothing; third bit should be 0
} else if s.Bits == 128 {
// third bit should be one; indicating 128 bit complex
schema[0] |= 1
}
return schema, nil
}
// Encode uses the schema to write the encoded value of i to the output stream
func (s *ComplexSchema) Encode(w io.Writer, i interface{}) error {
return s.EncodeValue(w, reflect.ValueOf(i))
}
// EncodeValue uses the schema to write the encoded value of v to the output stream
func (s *ComplexSchema) EncodeValue(w io.Writer, v reflect.Value) error {
// just double check the schema they are using
if !s.Valid() {
return fmt.Errorf("cannot encode using invalid ComplexNumber schema")
}
done, err := PreEncode(w, &v, s.Nullable())
if err != nil || done {
return err
}
t := v.Type()
k := t.Kind()
if k != reflect.Complex64 && k != reflect.Complex128 {
return fmt.Errorf("ComplexSchema only supports encoding Complex64 and Complex128 values")
}
complex := v.Complex()
switch s.Bits {
case 64:
if k == reflect.Float64 {
return fmt.Errorf("32bit FloatSchema schema cannot encode 64 bit values")
}
r := math.Float32bits(float32(real(complex)))
imaginary := math.Float32bits(float32(imag(complex)))
n, err := w.Write([]byte{
byte(r),
byte(r >> 8),
byte(r >> 16),
byte(r >> 24),
byte(imaginary),
byte(imaginary >> 8),
byte(imaginary >> 16),
byte(imaginary >> 24),
})
if err == nil && n != 8 {
err = errors.New("unexpected number of bytes written")
}
return err
case 128:
r := math.Float64bits(real(complex))
imaginary := math.Float64bits(imag(complex))
n, err := w.Write([]byte{
byte(r),
byte(r >> 8),
byte(r >> 16),
byte(r >> 24),
byte(r >> 32),
byte(r >> 40),
byte(r >> 48),
byte(r >> 56),
byte(imaginary),
byte(imaginary >> 8),
byte(imaginary >> 16),
byte(imaginary >> 24),
byte(imaginary >> 32),
byte(imaginary >> 40),
byte(imaginary >> 48),
byte(imaginary >> 56),
})
if err == nil && n != 16 {
err = errors.New("unexpected number of bytes written")
}
return err
default:
// error
}
return nil
}
// Decode uses the schema to read the next encoded value from the input stream and store it in i
func (s *ComplexSchema) Decode(r io.Reader, i interface{}) error {
if i == nil {
return fmt.Errorf("cannot decode to nil destination")
}
return s.DecodeValue(r, reflect.ValueOf(i))
}
// DecodeValue uses the schema to read the next encoded value from the input stream and store it in v
func (s *ComplexSchema) DecodeValue(r io.Reader, v reflect.Value) error {
// just double check the schema they are using
if !s.Valid() {
return fmt.Errorf("cannot decode using invalid ComplexNumber schema")
}
done, err := PreDecode(r, &v, s.Nullable())
if err != nil || done {
return err
}
t := v.Type()
k := t.Kind()
if k == reflect.Interface {
v.Set(reflect.New(s.GoType()))
v = v.Elem().Elem()
t = v.Type()
k = t.Kind()
}
var realPart float64
var imagPart float64
// take a look at the schema..
switch s.Bits {
case 64:
buf := make([]byte, 8)
_, err := io.ReadAtLeast(r, buf, 8)
if err != nil {
return err
}
realPart = float64(math.Float32frombits(
uint32(buf[0]) |
uint32(buf[1])<<8 |
uint32(buf[2])<<16 |
uint32(buf[3])<<24))
imagPart = float64(math.Float32frombits(
uint32(buf[4]) |
uint32(buf[5])<<8 |
uint32(buf[6])<<16 |
uint32(buf[7])<<24))
case 128:
buf := make([]byte, 16)
_, err := io.ReadAtLeast(r, buf, 16)
if err != nil {
return err
}
realPart = math.Float64frombits(
uint64(buf[0]) |
uint64(buf[1])<<8 |
uint64(buf[2])<<16 |
uint64(buf[3])<<24 |
uint64(buf[4])<<32 |
uint64(buf[5])<<40 |
uint64(buf[6])<<48 |
uint64(buf[7])<<56)
imagPart = math.Float64frombits(
uint64(buf[8]) |
uint64(buf[9])<<8 |
uint64(buf[10])<<16 |
uint64(buf[11])<<24 |
uint64(buf[12])<<32 |
uint64(buf[13])<<40 |
uint64(buf[14])<<48 |
uint64(buf[15])<<56)
}
var complexToWrite complex128 = complex(realPart, imagPart)
// Ensure v is settable
if !v.CanSet() {
return fmt.Errorf("decode destination is not settable")
}
switch k {
case reflect.Complex64:
fallthrough
case reflect.Complex128:
if v.OverflowComplex(complexToWrite) {
return fmt.Errorf("decoded complex overflows destination %v", k)
}
v.SetComplex(complexToWrite)
case reflect.Float32:
fallthrough
case reflect.Float64:
// make sure there is no imaginary component
if imagPart != 0 {
return fmt.Errorf("cannot decode ComplexNumber to non complex type when imaginary component is present")
}
if v.OverflowFloat(realPart) {
return fmt.Errorf("decoded value overflows destination %v", k)
}
v.SetFloat(realPart)
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
// make sure there is no imaginary component
if imagPart != 0 {
return fmt.Errorf("cannot decode ComplexNumber to non complex type when imaginary component is present")
}
if v.OverflowInt(int64(realPart)) {
return fmt.Errorf("decoded value overflows destination %v", k)
}
if realPart == math.Trunc(realPart) {
v.SetInt(int64(realPart))
} else {
return fmt.Errorf("loss of floating point precision not allowed w/o WeakDecoding")
}
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
// make sure there is no imaginary component
if imagPart != 0 {
return fmt.Errorf("cannot decode ComplexNumber to non complex type when imaginary component is present")
}
if v.OverflowUint(uint64(realPart)) {
return fmt.Errorf("decoded value overflows destination %v", k)
}
if realPart < 0 {
return fmt.Errorf("cannot decode negative ComplexNumber to unsigned int")
}
if realPart == math.Trunc(realPart) {
v.SetUint(uint64(realPart))
} else {
return fmt.Errorf("loss of floating point precision not allowed w/o WeakDecoding")
}
case reflect.String:
if !s.WeakDecoding() {
return fmt.Errorf("weak decoding not enabled; cannot decode to string")
}
tmp := complex128(complex(realPart, imagPart))
v.SetString(strconv.FormatComplex(tmp, 'E', -1, int(s.Bits)))
case reflect.Slice:
fallthrough
case reflect.Array:
if !s.WeakDecoding() {
return fmt.Errorf("weak decoding not enabled; cannot decode complex to array/slice")
}
if v.Len() != 2 {
return fmt.Errorf("complex numbers must be decoded into array/slice of exactly length 2")
}
elemK := t.Elem().Kind()
if elemK != reflect.Float32 && elemK != reflect.Float64 {
return fmt.Errorf("complex numbers must be decoded into array/slice of type float32 or float64")
}
// check overflow for each float
v.Index(0).SetFloat(realPart)
v.Index(1).SetFloat(imagPart)
default:
return fmt.Errorf("invalid destination %v", k)
}
return nil
}