From 509bbeb407f58d06a8680b48a7f02f530d67f088 Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sat, 11 May 2024 06:59:46 +0000 Subject: [PATCH] math/rand/v2, math/big: use internal/byteorder Change-Id: Id07f16d14133ee539bc2880b39641c42418fa6e2 GitHub-Last-Rev: 7b327d508f677f2476d24f046d25921f4599dd9a GitHub-Pull-Request: golang/go#67319 Reviewed-on: https://go-review.googlesource.com/c/go/+/585016 Auto-Submit: Ian Lance Taylor Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor --- src/go/build/deps_test.go | 2 +- src/math/big/floatmarsh.go | 10 +++++----- src/math/big/nat.go | 6 +++--- src/math/big/ratmarsh.go | 6 +++--- src/math/rand/v2/pcg.go | 29 +++++------------------------ 5 files changed, 17 insertions(+), 36 deletions(-) diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go index 44976c7356..537de94a75 100644 --- a/src/go/build/deps_test.go +++ b/src/go/build/deps_test.go @@ -249,7 +249,7 @@ var depsRules = ` < hash/adler32, hash/crc32, hash/crc64, hash/fnv; # math/big - FMT, encoding/binary, math/rand + FMT, math/rand < math/big; # compression diff --git a/src/math/big/floatmarsh.go b/src/math/big/floatmarsh.go index 8a908cef28..16be946971 100644 --- a/src/math/big/floatmarsh.go +++ b/src/math/big/floatmarsh.go @@ -7,9 +7,9 @@ package big import ( - "encoding/binary" "errors" "fmt" + "internal/byteorder" ) // Gob codec version. Permits backward-compatible changes to the encoding. @@ -48,10 +48,10 @@ func (x *Float) GobEncode() ([]byte, error) { b |= 1 } buf[1] = b - binary.BigEndian.PutUint32(buf[2:], x.prec) + byteorder.BePutUint32(buf[2:], x.prec) if x.form == finite { - binary.BigEndian.PutUint32(buf[6:], uint32(x.exp)) + byteorder.BePutUint32(buf[6:], uint32(x.exp)) x.mant[len(x.mant)-n:].bytes(buf[10:]) // cut off unused trailing words } @@ -84,13 +84,13 @@ func (z *Float) GobDecode(buf []byte) error { z.acc = Accuracy((b>>3)&3) - 1 z.form = form((b >> 1) & 3) z.neg = b&1 != 0 - z.prec = binary.BigEndian.Uint32(buf[2:]) + z.prec = byteorder.BeUint32(buf[2:]) if z.form == finite { if len(buf) < 10 { return errors.New("Float.GobDecode: buffer too small for finite form float") } - z.exp = int32(binary.BigEndian.Uint32(buf[6:])) + z.exp = int32(byteorder.BeUint32(buf[6:])) z.mant = z.mant.setBytes(buf[10:]) } diff --git a/src/math/big/nat.go b/src/math/big/nat.go index 1d702c7726..23b2a0b8dd 100644 --- a/src/math/big/nat.go +++ b/src/math/big/nat.go @@ -14,7 +14,7 @@ package big import ( - "encoding/binary" + "internal/byteorder" "math/bits" "math/rand" "sync" @@ -1321,9 +1321,9 @@ func (z nat) bytes(buf []byte) (i int) { // bigEndianWord returns the contents of buf interpreted as a big-endian encoded Word value. func bigEndianWord(buf []byte) Word { if _W == 64 { - return Word(binary.BigEndian.Uint64(buf)) + return Word(byteorder.BeUint64(buf)) } - return Word(binary.BigEndian.Uint32(buf)) + return Word(byteorder.BeUint32(buf)) } // setBytes interprets buf as the bytes of a big-endian unsigned diff --git a/src/math/big/ratmarsh.go b/src/math/big/ratmarsh.go index 033fb4459d..6962829453 100644 --- a/src/math/big/ratmarsh.go +++ b/src/math/big/ratmarsh.go @@ -7,9 +7,9 @@ package big import ( - "encoding/binary" "errors" "fmt" + "internal/byteorder" "math" ) @@ -29,7 +29,7 @@ func (x *Rat) GobEncode() ([]byte, error) { // this should never happen return nil, errors.New("Rat.GobEncode: numerator too large") } - binary.BigEndian.PutUint32(buf[j-4:j], uint32(n)) + byteorder.BePutUint32(buf[j-4:j], uint32(n)) j -= 1 + 4 b := ratGobVersion << 1 // make space for sign bit if x.a.neg { @@ -54,7 +54,7 @@ func (z *Rat) GobDecode(buf []byte) error { return fmt.Errorf("Rat.GobDecode: encoding version %d not supported", b>>1) } const j = 1 + 4 - ln := binary.BigEndian.Uint32(buf[j-4 : j]) + ln := byteorder.BeUint32(buf[j-4 : j]) if uint64(ln) > math.MaxInt-j { return errors.New("Rat.GobDecode: invalid length") } diff --git a/src/math/rand/v2/pcg.go b/src/math/rand/v2/pcg.go index 77708d799e..4ccd5e320b 100644 --- a/src/math/rand/v2/pcg.go +++ b/src/math/rand/v2/pcg.go @@ -6,6 +6,7 @@ package rand import ( "errors" + "internal/byteorder" "math/bits" ) @@ -30,32 +31,12 @@ func (p *PCG) Seed(seed1, seed2 uint64) { p.lo = seed2 } -// binary.bigEndian.Uint64, copied to avoid dependency -func beUint64(b []byte) uint64 { - _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808 - return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 | - uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56 -} - -// binary.bigEndian.PutUint64, copied to avoid dependency -func bePutUint64(b []byte, v uint64) { - _ = b[7] // early bounds check to guarantee safety of writes below - b[0] = byte(v >> 56) - b[1] = byte(v >> 48) - b[2] = byte(v >> 40) - b[3] = byte(v >> 32) - b[4] = byte(v >> 24) - b[5] = byte(v >> 16) - b[6] = byte(v >> 8) - b[7] = byte(v) -} - // MarshalBinary implements the encoding.BinaryMarshaler interface. func (p *PCG) MarshalBinary() ([]byte, error) { b := make([]byte, 20) copy(b, "pcg:") - bePutUint64(b[4:], p.hi) - bePutUint64(b[4+8:], p.lo) + byteorder.BePutUint64(b[4:], p.hi) + byteorder.BePutUint64(b[4+8:], p.lo) return b, nil } @@ -66,8 +47,8 @@ func (p *PCG) UnmarshalBinary(data []byte) error { if len(data) != 20 || string(data[:4]) != "pcg:" { return errUnmarshalPCG } - p.hi = beUint64(data[4:]) - p.lo = beUint64(data[4+8:]) + p.hi = byteorder.BeUint64(data[4:]) + p.lo = byteorder.BeUint64(data[4+8:]) return nil } -- 2.48.1