func (w *Writer) Index() int {
	return w.idx<<3 + int(w.fill)
}
Index returns the current writer position in bits.
package iobit
import (
	"encoding/binary"
	"errors"
)
type Writer struct {
	dst   []byte
	cache uint64
	fill  uint
	idx   int
}
var (
	ErrOverflow = errors.New("bit overflow")
	ErrUnderflow = errors.New("bit underflow")
)
func NewWriter(dst []byte) Writer {
	return Writer{dst: dst}
}
func (w *Writer) PutUint32(bits uint, val uint32) {
	u := uint64(val) << (64 - bits)
	if w.fill > 64-bits {
		if w.idx+4 <= len(w.dst) {
			binary.BigEndian.PutUint32(w.dst[w.idx:], uint32(w.cache>>32))
		}
		w.idx += 4
		w.fill -= 32
		w.cache <<= 32
	}
	u >>= w.fill
	w.fill += bits
	w.cache |= u
}
func (w *Writer) PutUint64(bits uint, val uint64) {
	if bits > 32 {
		bits -= 32
		w.PutBe32(uint32(val >> bits))
	}
	w.PutUint32(bits, uint32(val))
}
func (w *Writer) PutBit(val bool) {
	v := uint32(0)
	if val {
		v = 1
	}
	w.PutUint32(1, v)
}
func (w *Writer) PutByte(val byte) {
	w.PutUint32(8, uint32(val))
}
func (w *Writer) PutLe16(val uint16) {
	w.PutUint32(16, uint32(bswap16(val)))
}
func (w *Writer) PutBe16(val uint16) {
	w.PutUint32(16, uint32(val))
}
func (w *Writer) PutLe32(val uint32) {
	w.PutUint32(32, bswap32(val))
}
func (w *Writer) PutBe32(val uint32) {
	w.PutUint32(32, val)
}
func (w *Writer) PutLe64(val uint64) {
	w.PutBe32(bswap32(uint32(val)))
	w.PutBe32(bswap32(uint32(val >> 32)))
}
func (w *Writer) PutBe64(val uint64) {
	w.PutBe32(uint32(val >> 32))
	w.PutBe32(uint32(val))
}
func (w *Writer) PutUint8(bits uint, val byte) {
	w.PutUint32(bits, uint32(val))
}
func (w *Writer) PutInt8(bits uint, val int8) {
	w.PutUint32(bits, uint32(val))
}
func (w *Writer) PutUint16(bits uint, val uint16) {
	w.PutUint32(bits, uint32(val))
}
func (w *Writer) PutInt16(bits uint, val int16) {
	w.PutUint32(bits, uint32(val))
}
func (w *Writer) PutInt32(bits uint, val int32) {
	w.PutUint32(bits, uint32(val))
}
func (w *Writer) PutInt64(bits uint, val int64) {
	w.PutUint64(bits, uint64(val))
}
func (w *Writer) Flush() error {
	for w.fill >= 8 && w.idx < len(w.dst) {
		w.dst[w.idx] = byte(w.cache >> 56)
		w.idx++
		w.cache <<= 8
		w.fill -= 8
	}
	if w.idx+int(w.fill) > len(w.dst) {
		return ErrOverflow
	}
	if w.fill != 0 {
		return ErrUnderflow
	}
	return nil
}
func (w *Writer) Write(p []byte) (int, error) {
	err := w.Flush()
	if err != nil {
		return 0, err
	}
	n := 0
	if w.idx < len(w.dst) {
		n = copy(w.dst[w.idx:], p)
	}
	w.idx += len(p)
	if n != len(p) {
		return n, ErrOverflow
	}
	return n, nil
}