Go Assembly by Example: Sqrt

This example is taken from Golang’s math package in its standard library. The function has been implemented in different assembly languages in order to leverage different architecture’s hardware support for the square root function.

  1. package math
  2. func Sqrt(x float64) float64

The Go compiler will look for files ending in _GOOS or _GOARCH or _GOOS_GOARCH to consider which assembly file to compile. See go/build.

  1. ∕∕ sqrt_386.s
  2. #include "textflag.h"
  3. TEXT ·Sqrt(SB),NOSPLIT,$0
  4. FMOVD x+0(FP),F0
  5. FSQRT
  6. FMOVDP F0,ret+8(FP)
  7. RET
  8. ∕∕ sqrt_amd64.s
  9. #include "textflag.h"
  10. TEXT ·Sqrt(SB), NOSPLIT, $0
  11. XORPS X0, X0 // break dependency
  12. SQRTSD x+0(FP), X0
  13. MOVSD X0, ret+8(FP)
  14. RET

amd64p32 is the native client (Nacl) amd64 instruction set with a 32-bit address space, 32-bit pointers and 32-bit type ints and uints. It is a sandbox architecture used for example in play.golang.org

  1. ∕∕ `sqrt_amd64p32.s`

As the amd64 assembly file is fine, we’ll use that.

  1. #include "sqrt_amd64.s"
  2. ∕∕ sqrt_arm.s
  3. #include "textflag.h"
  4. TEXT ·Sqrt(SB),NOSPLIT,$0
  5. MOVD x+0(FP),F0
  6. SQRTD F0,F0
  7. MOVD F0,ret+8(FP)
  8. RET
  9. ∕∕ sqrt_arm64.s
  10. #include "textflag.h"
  11. TEXT ·Sqrt(SB),NOSPLIT,$0
  12. FMOVD x+0(FP), F0
  13. FSQRTD F0, F0
  14. FMOVD F0, ret+8(FP)
  15. RET
  16. ∕∕ sqrt_mipsx.s

This file’s name doesn’t match any _GOOS.s pattern. Instead it uses the following comment to warn the compiler that both mips and mipsle can use this assembly file.

  1. ∕∕ +build mips mipsle
  2. #include "textflag.h"
  3. TEXT ·Sqrt(SB),NOSPLIT,$0
  4. MOVD x+0(FP), F0
  5. SQRTD F0, F0
  6. MOVD F0, ret+8(FP)
  7. RET
  8. ∕∕ sqrt_ppc64x.s
  9. ∕∕ +build ppc64 ppc64le
  10. #include "textflag.h"
  11. TEXT ·Sqrt(SB),NOSPLIT,$0
  12. FMOVD x+0(FP), F0
  13. FSQRT F0, F0
  14. FMOVD F0, ret+8(FP)
  15. RET
  16. ∕∕ sqart_s389x.s
  17. #include "textflag.h"
  18. TEXT ·Sqrt(SB),NOSPLIT,$0
  19. FMOVD x+0(FP), F1
  20. FSQRT F1, F1
  21. FMOVD F1, ret+8(FP)
  22. RET
ft_authoradmin  ft_create_time2018-02-03 13:12
 ft_update_time2018-02-03 13:13