Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: sizeof: add bit sizes #40168

Open
arnottcr opened this issue Jul 11, 2020 · 2 comments
Open

proposal: sizeof: add bit sizes #40168

arnottcr opened this issue Jul 11, 2020 · 2 comments
Labels
Projects
Milestone

Comments

@arnottcr
Copy link
Contributor

@arnottcr arnottcr commented Jul 11, 2020

See the tail of the (open) CL 242018 for the (approved) proposal #29982. There are a number of use cases where bits, not bytes as unsafe.Sizeof and sizeof.Xxx speak in, are really useful and it is a shame that you have to manually derive them, const UintBits = sizeof.Uint * 8, as this is error prone. Instead we should expose constants for bit sizes too:

package sizeof

const (
	bitsSize = size * 8 // 32 or 64

	BitsBool       = 8
	BitsByte       = Uint8
	BitsComplex64  = 64
	BitsComplex128 = 128
	BitsFloat32    = 32
	BitsFloat64    = 64
	BitsInt        = bitsSize
	BitsUint8      = 8
	BitsInt16      = 16
	BitsInt32      = 32
	BitsInt64      = 64
	BitsRune       = BitsInt32
	BitsUint       = bitSize
	BitsUint8      = 8
	BitsUint16     = 16
	BitsUint32     = 32
	BitsUint64     = 64
	BitsUintptr    = bitSize
)

We could derive BitsXxx from Xxx * 8, but the current format seems more terse and intuitive. As well, the names could use a suffix, XxxBits, instead of the current prefix, since we get custom ordering for blocked constants, but to me, prefixes displays better and follows other standard library conventions: http.StatusXxx and xxx.ErrYyy.

Depending on the use case, we could instead export a constant or function for deriving these values. While the former is still prone to user error, it should be intuitive for users familiar with time.Duration. The latter seems like a blocker because functions cannot be compile time constant:

// Bits stores the number of bits in a byte.
//
// This is useful for converting byte type sizes into bits:
//	const UintBits = sizeof.Uint * sizeof.Bits
const Bits = 8
// Bits converts byte sizes into bits.
//
// This is useful for converting byte type sizes into bits:
//	var UintBits = sizeof.Bits(sizeof.Uint)
func Bits(bytes int) int { return bytes * 8 }
@gopherbot gopherbot added this to the Proposal milestone Jul 11, 2020
@gopherbot gopherbot added the Proposal label Jul 11, 2020
@arnottcr
Copy link
Contributor Author

@arnottcr arnottcr commented Aug 6, 2020

@ianlancetaylor: can you add this to the proposals project?

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals Aug 6, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 6, 2020

@arnottcr Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Proposals
Incoming
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.