Skip to content

Commit

Permalink
decode major7 floats
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws committed Jan 12, 2024
1 parent 698102b commit 73d400f
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions encoding/cbor/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cbor
import (
"encoding/binary"
"fmt"
"math"
)

const minorIndefinite = 31
Expand Down Expand Up @@ -254,11 +255,23 @@ func decodeMajor7(p []byte) (Value, int, error) {
case major7Undefined:
return &Major7Undefined{}, 1, nil
case major7Float16:
return nil, 0, fmt.Errorf("TODO")
if len(p) < 3 {
return nil, 0, fmt.Errorf("encoded float16 at end of buffer")
}
b := binary.BigEndian.Uint16(p[1:])
return Major7Float32(math.Float32frombits(float16to32(b))), 3, nil
case major7Float32:
return nil, 0, fmt.Errorf("TODO")
if len(p) < 5 {
return nil, 0, fmt.Errorf("encoded float32 at end of buffer")
}
b := binary.BigEndian.Uint32(p[1:])
return Major7Float32(math.Float32frombits(b)), 5, nil
case major7Float64:
return nil, 0, fmt.Errorf("TODO")
if len(p) < 9 {
return nil, 0, fmt.Errorf("encoded float64 at end of buffer")
}
b := binary.BigEndian.Uint64(p[1:])
return Major7Float64(math.Float64frombits(b)), 9, nil
default:
return nil, 0, fmt.Errorf("unexpected minor value %d", m)
}
Expand Down

0 comments on commit 73d400f

Please sign in to comment.