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/asm: optimize "AND $0xffff, Rx" with BFC #20860

Closed
benshi001 opened this issue Jun 30, 2017 · 5 comments
Closed

cmd/asm: optimize "AND $0xffff, Rx" with BFC #20860

benshi001 opened this issue Jun 30, 2017 · 5 comments
Milestone

Comments

@benshi001
Copy link
Member

benshi001 commented Jun 30, 2017

Currently "AND $0x7fff, R9" is assembled to

ldr	r11, [pc, # offset]
and	r9, r9, r11

It can be optimized to a single instruction on ARMv7.

bfc	r9, # 15, # 17

It can be done in Go1.10 and assigned to me.

@benshi001
Copy link
Member Author

add it to Go1.10?

@odeke-em odeke-em added this to the Go1.10 milestone Jul 10, 2017
@odeke-em
Copy link
Member

@benshi001 done.

@benshi001
Copy link
Member Author

a &= 0xff00ffff is assembled to
a.go:13 0x812f0 e3e0b8ff MVN $16711680, R11
a.go:13 0x812f4 e000000b AND R11, R0, R0

Actually a more efficient way is
BIC $0x00ff0000, R0

@benshi001
Copy link
Member Author

benshi001 commented Sep 7, 2017

package main

import (
        "fmt"
        "time"
)

var a, b, c uint32

//go:noinline
func ss() {
        a &= 0xff00ffff
        b &= 0xff8000ff
        c &= 0xff007f00
        a &= 0xff01ffff
        b &= 0xff00007f
        c &= 0xfe00ff00
        a &= 0xff00ffff
        b &= 0xfe0000ff
        c &= 0xff80ff00
        a &= 0xff80ffff
        b &= 0xfc0000ff
        c &= 0xff01ff00
}

func main() {
        start := time.Now()
        for i := 0; i < 8192; i++ {
                ss()
        }
        d := time.Now().Sub(start)
        fmt.Println(d)
}

The above test program showed that BFC even cost more time than using constant pool. On my RP2 board, BFC cost 1.43ms while old way cost 1.315ms.

@benshi001
Copy link
Member Author

Though
"AND $0xFF00FFFFF, R0" is assembled to
MVN $16711680, R11
AND R11, R0, R0

a &= 0xff00ffff is compiled to
BIC $0x00ff0000, R0

by a SSA rule
(AND x (MVN y)) -> (BIC x y)

@golang golang locked and limited conversation to collaborators Sep 7, 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