arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/float16.go
common/float16.goRaw
  1// Package common Float16 provides methods to work with Hermez custom half float
  2// precision, 16 bits, codification internally called Float16 has been adopted
  3// to encode large integers. This is done in order to save bits when L2
  4// transactions are published.
  5//nolint:gomnd
  6package common
  7
  8import (
  9	"encoding/binary"
 10	"errors"
 11	"math/big"
 12
 13	"github.com/hermeznetwork/tracerr"
 14)
 15
 16var (
 17	// ErrRoundingLoss is used when converted big.Int to Float16 causes rounding loss
 18	ErrRoundingLoss = errors.New("input value causes rounding loss")
 19)
 20
 21// Float16 represents a float in a 16 bit format
 22type Float16 uint16
 23
 24// Bytes return a byte array of length 2 with the Float16 value encoded in BigEndian
 25func (f16 Float16) Bytes() []byte {
 26	var b [2]byte
 27	binary.BigEndian.PutUint16(b[:], uint16(f16))
 28	return b[:]
 29}
 30
 31// Float16FromBytes returns a Float16 from a byte array of 2 bytes.
 32func Float16FromBytes(b []byte) *Float16 {
 33	f16 := Float16(binary.BigEndian.Uint16(b[:2]))
 34	return &f16
 35}
 36
 37// BigInt converts the Float16 to a *big.Int integer
 38func (f16 *Float16) BigInt() *big.Int {
 39	fl := int64(*f16)
 40
 41	m := big.NewInt(fl & 0x3FF)
 42	e := big.NewInt(fl >> 11)
 43	e5 := (fl >> 10) & 0x01
 44
 45	exp := big.NewInt(0).Exp(big.NewInt(10), e, nil)
 46	res := m.Mul(m, exp)
 47
 48	if e5 != 0 && e.Cmp(big.NewInt(0)) != 0 {
 49		res.Add(res, exp.Div(exp, big.NewInt(2)))
 50	}
 51	return res
 52}
 53
 54// floorFix2Float converts a fix to a float, always rounding down
 55func floorFix2Float(_f *big.Int) Float16 {
 56	zero := big.NewInt(0)
 57	ten := big.NewInt(10)
 58	e := int64(0)
 59
 60	m := big.NewInt(0)
 61	m.Set(_f)
 62
 63	if m.Cmp(zero) == 0 {
 64		return 0
 65	}
 66
 67	s := big.NewInt(0).Rsh(m, 10)
 68
 69	for s.Cmp(zero) != 0 {
 70		m.Div(m, ten)
 71		s.Rsh(m, 10)
 72		e++
 73	}
 74
 75	return Float16(m.Int64() | e<<11)
 76}
 77
 78// NewFloat16 encodes a *big.Int integer as a Float16, returning error in
 79// case of loss during the encoding.
 80func NewFloat16(f *big.Int) (Float16, error) {
 81	fl1 := floorFix2Float(f)
 82	fi1 := fl1.BigInt()
 83	fl2 := fl1 | 0x400
 84	fi2 := fl2.BigInt()
 85
 86	m3 := (fl1 & 0x3FF) + 1
 87	e3 := fl1 >> 11
 88
 89	if m3&0x400 == 0 {
 90		m3 = 0x66
 91		e3++
 92	}
 93
 94	fl3 := m3 + e3<<11
 95	fi3 := fl3.BigInt()
 96
 97	res := fl1
 98
 99	d := big.NewInt(0).Abs(fi1.Sub(fi1, f))
100	d2 := big.NewInt(0).Abs(fi2.Sub(fi2, f))
101
102	if d.Cmp(d2) == 1 {
103		res = fl2
104		d = d2
105	}
106
107	d3 := big.NewInt(0).Abs(fi3.Sub(fi3, f))
108
109	if d.Cmp(d3) == 1 {
110		res = fl3
111	}
112
113	// Do rounding check
114	if res.BigInt().Cmp(f) == 0 {
115		return res, nil
116	}
117	return res, tracerr.Wrap(ErrRoundingLoss)
118}
119
120// NewFloat16Floor encodes a big.Int integer as a Float16, rounding down in
121// case of loss during the encoding.
122func NewFloat16Floor(f *big.Int) Float16 {
123	fl1 := floorFix2Float(f)
124	fl2 := fl1 | 0x400
125	fi2 := fl2.BigInt()
126
127	if fi2.Cmp(f) < 1 {
128		return fl2
129	}
130	return fl1
131}