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: Add functions to convert between uints and byte arrays #39078
Comments
We already have functions very much like this in |
The functions in encoding/binary are more expensive than these. |
Using encoding/binary forces you to specify the desired endianness, which seems desirable. Using native host endianness is a path to trouble in the long run. I don't think we should encourage it. |
Fair point. Can we add functions that avoid slicing, even when specifying endianness? Slicing introduces overhead when the goal is to convert a single word. |
Are the encoding/binary functions inlinable? If they are, the slicing costs should disappear in practice. Do you have a benchmark comparing your suggested functions and the encoding/binary functions? |
If This code:
already compiles to just 2 instructions (2 MOVQ, from the input stack slot to a register, and from the register to the output stack slot). |
Ok, thanks. Given that the encoding/binary functions are being inlined, please feel free to close this, or lmk and I'll close it. |
I propose adding functions to convert between uints and byte arrays of the same size. At a minimum, two new functions to convert between uint64 and [8]byte. For symmetry, there can be functions to convert between uint32 and [4]byte, and between uint16 and [2]byte. These functions would go into math/bits.
or
The implementation can be inlined and compiled down to a single word copy. It is essentially a type cast.
The text was updated successfully, but these errors were encountered: