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

cmd/compile: optimize for byte swaps #14366

Closed
dsnet opened this issue Feb 17, 2016 · 3 comments
Closed

cmd/compile: optimize for byte swaps #14366

dsnet opened this issue Feb 17, 2016 · 3 comments

Comments

@dsnet
Copy link
Member

dsnet commented Feb 17, 2016

Using go1.5.3

The following code segments all do byte swapping:

func bswap64a(n uint64) uint64 {
    return 0 |
        ((n & 0xff) << 56) |
        ((n & 0xff00) << 40) |
        ((n & 0xff0000) << 24) |
        ((n & 0xff000000) << 8) |
        ((n & 0xff00000000) >> 8) |
        ((n & 0xff0000000000) >> 24) |
        ((n & 0xff000000000000) >> 40) |
        ((n & 0xff00000000000000) >> 56)
}

func bswap64b(n uint64) uint64 {
    n = (n&0xff00ff00ff00ff00)>>8 | (n&0x00ff00ff00ff00ff)<<8
    n = (n&0xffff0000ffff0000)>>16 | (n&0x0000ffff0000ffff)<<16
    n = (n&0xffffffff00000000)>>32 | (n&0x00000000ffffffff)<<32
    return n
}

func bswap64c(n uint64) (x uint64) {
    for i := 0; i < 8; i++ {
        x <<= 8
        x |= n & 0xff
        n >>= 8
    }
    return x
}

It would be nice if the compiler can recognize at least one of these (probably the top one?) as byte swapping and use the BYTESWAP instruction or some equivalent.

@bradfitz bradfitz added this to the Unplanned milestone Feb 17, 2016
@bradfitz
Copy link
Contributor

/cc @randall77

@dsnet
Copy link
Member Author

dsnet commented May 8, 2016

/cc @dr2chase for his intrinsic idea.

@dsnet
Copy link
Member Author

dsnet commented May 24, 2017

The new math/bits package adds this functionality as an intrinsic.

@dsnet dsnet closed this as completed May 24, 2017
@golang golang locked and limited conversation to collaborators May 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants