]> Cypherpunks repositories - gostls13.git/commitdiff
math/rand/v2, math/big: use internal/byteorder
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Sat, 11 May 2024 06:59:46 +0000 (06:59 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 13 May 2024 21:31:58 +0000 (21:31 +0000)
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 <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/go/build/deps_test.go
src/math/big/floatmarsh.go
src/math/big/nat.go
src/math/big/ratmarsh.go
src/math/rand/v2/pcg.go

index 44976c735679ce483c227203315b5412f460423a..537de94a75f8f3e86a3dc6787f16ec4523f69c8a 100644 (file)
@@ -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
index 8a908cef28ab9f7a4bbb588cd45600f25cf1f059..16be94697147ed0b71a2c6b86a5ddc89834b358e 100644 (file)
@@ -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:])
        }
 
index 1d702c7726df2b2857f51c04f5ed7665e2a9e985..23b2a0b8ddf034726eca7368527e07fc5852bf20 100644 (file)
@@ -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
index 033fb4459df8a74bcd93fbaa3ed7d586444973ad..69628294531ff8c63f5c93aa6988ef843be8dc83 100644 (file)
@@ -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")
        }
index 77708d799e265d0dd7e6eeaf74bc356f9fb6b70c..4ccd5e320b971678d0c9b1906f7555d78509aa52 100644 (file)
@@ -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
 }