From f3a743981d0af6ead0c4f64ef4c299c342bba4de Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Fri, 10 May 2024 06:06:47 +0000 Subject: [PATCH] internal/byteorder: new package Currently in a lot of packages we define functions for appending/decoding mostly BigEndian data (see internal/chacha8rand, net/netip, internal/boring/sha, hash/crc64, and probably more), because we don't want to depend on encoding/binary, because of #54097. This change introduces a new package internal/byteorder, that will allow us to remove all of the functions and replace them with internal/byteorder. Updates #54097 Change-Id: I03e5ea1eb721dd98bdabdb25786f889cc5de54c5 GitHub-Last-Rev: 3f07d3dfb453a9e679395711f9b93e25f9340a3b GitHub-Pull-Request: golang/go#67183 Reviewed-on: https://go-review.googlesource.com/c/go/+/583298 Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Cherry Mui Commit-Queue: Ian Lance Taylor --- src/cmd/compile/internal/inline/inl.go | 22 ++++ src/go/build/deps_test.go | 1 + src/internal/byteorder/byteorder.go | 149 +++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 src/internal/byteorder/byteorder.go diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go index 3f65b2bbc9..931f79552a 100644 --- a/src/cmd/compile/internal/inline/inl.go +++ b/src/cmd/compile/internal/inline/inl.go @@ -545,6 +545,28 @@ opSwitch: } } } + + if n.Fun.Op() == ir.ONAME { + name := n.Fun.(*ir.Name) + if name.Class == ir.PFUNC { + // Special case: on architectures that can do unaligned loads, + // explicitly mark internal/byteorder methods as cheap, + // because in practice they are, even though our inlining + // budgeting system does not see that. See issue 42958. + if base.Ctxt.Arch.CanMergeLoads && name.Sym().Pkg.Path == "internal/byteorder" { + switch name.Sym().Name { + case "LeUint64", "LeUint32", "LeUint16", + "BeUint64", "BeUint32", "BeUint16", + "LePutUint64", "LePutUint32", "LePutUint16", + "BePutUint64", "BePutUint32", "BePutUint16", + "LeAppendUint64", "LeAppendUint32", "LeAppendUint16", + "BeAppendUint64", "BeAppendUint32", "BeAppendUint16": + cheap = true + } + } + } + } + if cheap { break // treat like any other node, that is, cost of 1 } diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go index 3ff7eb2ce2..f4973e92b1 100644 --- a/src/go/build/deps_test.go +++ b/src/go/build/deps_test.go @@ -67,6 +67,7 @@ var depsRules = ` internal/goos < internal/bytealg < internal/stringslite + < internal/byteorder < internal/itoa < internal/unsafeheader < runtime/internal/sys diff --git a/src/internal/byteorder/byteorder.go b/src/internal/byteorder/byteorder.go new file mode 100644 index 0000000000..ba37856ccd --- /dev/null +++ b/src/internal/byteorder/byteorder.go @@ -0,0 +1,149 @@ +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package byteorder provides functions for decoding and encoding +// little and big endian integer types from/to byte slices. +package byteorder + +func LeUint16(b []byte) uint16 { + _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808 + return uint16(b[0]) | uint16(b[1])<<8 +} + +func LePutUint16(b []byte, v uint16) { + _ = b[1] // early bounds check to guarantee safety of writes below + b[0] = byte(v) + b[1] = byte(v >> 8) +} + +func LeAppendUint16(b []byte, v uint16) []byte { + return append(b, + byte(v), + byte(v>>8), + ) +} + +func LeUint32(b []byte) uint32 { + _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808 + return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 +} + +func LePutUint32(b []byte, v uint32) { + _ = b[3] // early bounds check to guarantee safety of writes below + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + b[3] = byte(v >> 24) +} + +func LeAppendUint32(b []byte, v uint32) []byte { + return append(b, + byte(v), + byte(v>>8), + byte(v>>16), + byte(v>>24), + ) +} + +func LeUint64(b []byte) uint64 { + _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808 + return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | + uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 +} + +func LePutUint64(b []byte, v uint64) { + _ = b[7] // early bounds check to guarantee safety of writes below + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + b[3] = byte(v >> 24) + b[4] = byte(v >> 32) + b[5] = byte(v >> 40) + b[6] = byte(v >> 48) + b[7] = byte(v >> 56) +} + +func LeAppendUint64(b []byte, v uint64) []byte { + return append(b, + byte(v), + byte(v>>8), + byte(v>>16), + byte(v>>24), + byte(v>>32), + byte(v>>40), + byte(v>>48), + byte(v>>56), + ) +} + +func BeUint16(b []byte) uint16 { + _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808 + return uint16(b[1]) | uint16(b[0])<<8 +} + +func BePutUint16(b []byte, v uint16) { + _ = b[1] // early bounds check to guarantee safety of writes below + b[0] = byte(v >> 8) + b[1] = byte(v) +} + +func BeAppendUint16(b []byte, v uint16) []byte { + return append(b, + byte(v>>8), + byte(v), + ) +} + +func BeUint32(b []byte) uint32 { + _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808 + return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 +} + +func BePutUint32(b []byte, v uint32) { + _ = b[3] // early bounds check to guarantee safety of writes below + b[0] = byte(v >> 24) + b[1] = byte(v >> 16) + b[2] = byte(v >> 8) + b[3] = byte(v) +} + +func BeAppendUint32(b []byte, v uint32) []byte { + return append(b, + byte(v>>24), + byte(v>>16), + byte(v>>8), + byte(v), + ) +} + +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 +} + +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) +} + +func BeAppendUint64(b []byte, v uint64) []byte { + return append(b, + byte(v>>56), + byte(v>>48), + byte(v>>40), + byte(v>>32), + byte(v>>24), + byte(v>>16), + byte(v>>8), + byte(v), + ) +}