drygauost253590142 2018-07-28 11:17
浏览 80
已采纳

具有后备功能的汇编功能实现

I have a function written on Go that I would like to optimize in assembly. I currently only want to write it for amd64 which is the most frequent target. Assembly for other targets may be added later. By default it should use the Go code.

Unfortunately I didn't manage to achieve this. Looking at the documentation I found online it seam that I have to declare an external function, and provide the assembly for every possible target.

I can provide a Go function with a different name, and then in the assembly code jump to it. This looks tedious and I can't ensure I have provided the assembly file for all possible targets.

Is there a way to provide a Go function, and an alternate version to use instead when compiling for particular targets (e.g. amd64) ?

  • 写回答

1条回答 默认 最新

  • dongshun1884 2018-07-28 11:34
    关注

    The standard way to do this is to have at least four files, three of which are protected by build constraints, either by use of the +build tag or by the filename.

    • wrapper.go:
      • Contains function and documentation for a wrapper function which calls the conditionally compiled implementation function
    • impl_generic.go
      • Go implementation for non-optimized platforms
    • impl_asm.go
      • Function declaration of any assembly implementations
    • impl_amd64.s
      • amd64 assembly implementation of your function

    wrapper.go

    package mypkg
    
    import "fmt"
    
    func Wrapper(s string) {
        fmt.Println(impl(s))
    }
    

    impl_generic.go

    // +build !amd64
    
    package mypkg
    
    func impl(s string) string {
        // TODO: non-assembly implementation
    }
    

    impl_asm.go

    // +build amd64
    
    package mypkg
    
    // defined in *.s
    
    func impl(s string) string
    

    impl_amd64.s

    TEXT ·impl(SB),$0
        // TODO: AMD64 implementation
        RET
    

    To add an assembly implementation for another architecture:

    1. Concat ,!$arch to the first impl_generic.go build constraint item
    2. Add $arch to the impl_asm.go build constraint list
    3. Write the assembly implementation in impl_$arch.s
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?