package decode import ( "fmt" "fq/pkg/bitio" "math" "strconv" ) //go:generate sh -c "cat numbers.go.tmpl | go run ../../dev/tmpl.go numbers.go.json | gofmt > numbers_gen.go" // UE reads a nBits bits unsigned integer with byte order endian // MSB first func (d *D) TryUE(nBits int, endian Endian) (uint64, error) { n, err := d.Bits(nBits) if err != nil { return 0, err } if endian == LittleEndian { n = bitio.Uint64ReverseBytes(nBits, n) } return n, nil } func (d *D) UE(nBits int, endian Endian) uint64 { n, err := d.TryUE(nBits, endian) if err != nil { panic(IOError{Err: err, Op: "UE", Size: int64(nBits), Pos: d.Pos()}) } return n } func (d *D) TrySE(nBits int, endian Endian) (int64, error) { n, err := d.Bits(nBits) if err != nil { return 0, err } if endian == LittleEndian { n = bitio.Uint64ReverseBytes(nBits, n) } var s int64 if n&(1<<(nBits-1)) > 0 { // two's complement s = -int64((^n & ((1 << nBits) - 1)) + 1) } else { s = int64(n) } return s, nil } func (d *D) SE(nBits int, endian Endian) int64 { n, err := d.TrySE(nBits, endian) if err != nil { panic(IOError{Err: err, Op: "SE", Size: int64(nBits), Pos: d.Pos()}) } return n } func (d *D) TryUnary(s uint64) (uint64, error) { var n uint64 for { b, err := d.TryU1() if err != nil { return 0, err } if b != s { break } n++ } return n, nil } func (d *D) Unary(s uint64) uint64 { n, err := d.TryUnary(s) if err != nil { panic(IOError{Err: err, Op: "Unary", Size: 1, Pos: d.Pos()}) } return n } func (d *D) TryFPE(nBits int, fBits int64, endian Endian) (float64, error) { n, err := d.Bits(nBits) if err != nil { return 0, err } if endian == LittleEndian { n = bitio.Uint64ReverseBytes(nBits, n) } return float64(n) / float64(uint64(1<