1
0
mirror of https://github.com/golang/go synced 2024-11-15 00:10:28 -07:00

math/rand/v2, math/big: use internal/byteorder

Change-Id: Id07f16d14133ee539bc2880b39641c42418fa6e2
GitHub-Last-Rev: 7b327d508f
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>
This commit is contained in:
Mateusz Poliwczak 2024-05-11 06:59:46 +00:00 committed by Gopher Robot
parent 7e196c04ed
commit 509bbeb407
5 changed files with 17 additions and 36 deletions

View 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

View 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:])
}

View 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

View 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")
}

View 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
}