Does Go optimize math.Float64frombits(binary.LittleEndian.Uint64[...])) to a single instruction?

101 views
Skip to first unread message

twp...@gmail.com

unread,
Nov 24, 2022, 6:15:47 PM11/24/22
to golang-nuts
tl;dr: on 64-bit little-endian machines, does

    x := math.Float64frombits(binary.LittleEndian.Uint64(byteSlice[:8]))

get optimized to a single move instruction from byteSlice[:8] to x?


Background info:

I'm currently writing a bunch of code that unmarshals binary data. A small example is this that unmarshals eight float64s, and a longer example is this that unmarshals an arbitrary number of float64s. In theory, when the endianness of the binary data matches the endianness of the architecture, these should become simple memory copies. Does Go 1.19 do this?

Although this is a performance question, performance is not a concern in my use case. This is just an idle "can Go do this yet?" question and I'm curious about the answer.


Thanks for any insight,
Tom

Ian Lance Taylor

unread,
Nov 24, 2022, 6:59:10 PM11/24/22
to twp...@gmail.com, golang-nuts
The godbolt.org website is a good way to play with this kind of
question. For example https://godbolt.org/z/fasvKa3dx shows, on lines
43 and 44, that, ignoring the check that the slice bounds check, this
code boils down to two instructions.

Ian
Reply all
Reply to author
Forward
0 new messages