diff options
author | OneOfOne <oneofone@gmail.com> | 2016-04-10 03:50:11 +0200 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2016-04-20 17:51:01 +0000 |
commit | d8c9dd604801958c649a32511deef373adeecfe0 (patch) | |
tree | 493c61ade210c32a50b5c0cc8346314232127295 /src/math/big/floatmarsh.go | |
parent | 7acb642e4f0b77763a13c99c756aa846b01a428c (diff) | |
download | go-d8c9dd604801958c649a32511deef373adeecfe0.tar.gz go-d8c9dd604801958c649a32511deef373adeecfe0.zip |
math/big: implement GobDecode/Encode for big.Float
Added GobEncode/Decode and a test for them.
Fixes #14593
Change-Id: Ic8d3efd24d0313a1a66f01da293c4c1fd39764a8
Reviewed-on: https://go-review.googlesource.com/21755
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/math/big/floatmarsh.go')
-rw-r--r-- | src/math/big/floatmarsh.go | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/src/math/big/floatmarsh.go b/src/math/big/floatmarsh.go index 44987ee03a..6127aa2e83 100644 --- a/src/math/big/floatmarsh.go +++ b/src/math/big/floatmarsh.go @@ -6,7 +6,70 @@ package big -import "fmt" +import ( + "encoding/binary" + "fmt" +) + +// Gob codec version. Permits backward-compatible changes to the encoding. +const floatGobVersion byte = 1 + +// GobEncode implements the gob.GobEncoder interface. +func (x *Float) GobEncode() ([]byte, error) { + if x == nil { + return nil, nil + } + sz := 1 + 1 + 4 // version + mode|acc|form|neg (3+2+2+1bit) + prec + if x.form == finite { + sz += 4 + int((x.prec+(_W-1))/_W)*_S // exp + mant + } + buf := make([]byte, sz) + + buf[0] = floatGobVersion + b := byte(x.mode&7)<<5 | byte((x.acc+1)&3)<<3 | byte(x.form&3)<<1 + if x.neg { + b |= 1 + } + buf[1] = b + binary.BigEndian.PutUint32(buf[2:], x.prec) + if x.form == finite { + binary.BigEndian.PutUint32(buf[6:], uint32(x.exp)) + x.mant.bytes(buf[10:]) + } + return buf, nil +} + +// GobDecode implements the gob.GobDecoder interface. +func (z *Float) GobDecode(buf []byte) error { + if len(buf) == 0 { + // Other side sent a nil or default value. + *z = Float{} + return nil + } + + if buf[0] != floatGobVersion { + return fmt.Errorf("Float.GobDecode: encoding version %d not supported", buf[0]) + } + + b := buf[1] + z.mode = RoundingMode((b >> 5) & 7) + z.acc = Accuracy((b>>3)&3) - 1 + z.form = form((b >> 1) & 3) + z.neg = b&1 != 0 + + oldPrec := uint(z.prec) + z.prec = binary.BigEndian.Uint32(buf[2:]) + + if z.form == finite { + z.exp = int32(binary.BigEndian.Uint32(buf[6:])) + z.mant = z.mant.setBytes(buf[10:]) + } + + if oldPrec != 0 { + z.SetPrec(oldPrec) + } + return nil +} // MarshalText implements the encoding.TextMarshaler interface. // Only the Float value is marshaled (in full precision), other |