cmd/compile,math: remove all sqrt assembly code

This CL make math.sqrt an intrinsic function, math.Sqrt is not affected
since compiler can inline it. With this change, we can remove all assembly
code for math.Sqrt that aims to speed up indirect call. The go compiler can
generate same or faster code (with regabi) for indirect call.

Benchmark on amd64:
name                 old time/op  new time/op  delta
SqrtIndirect         2.60ns ± 3%  1.03ns ± 4%  -60.24%  (p=0.000 n=10+10)
SqrtLatency          3.40ns ± 1%  3.32ns ± 1%   -2.26%  (p=0.000 n=10+8)
SqrtIndirectLatency  6.09ns ± 0%  3.31ns ± 0%  -45.67%  (p=0.000 n=10+10)
SqrtGoLatency        36.1ns ± 6%  34.6ns ± 1%     ~     (p=0.101 n=10+10)
SqrtPrime            2.53µs ± 2%  2.55µs ± 6%     ~     (p=0.398 n=9+9)

Change-Id: If4be0f242c1d9d4feca7d269fc9cd6e6066f163d
Reviewed-on: https://go-review.googlesource.com/c/go/+/421074
Run-TryBot: Wayne Zuo <wdvxdr@golangcn.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Wayne Zuo 2022-08-03 23:31:29 +08:00 committed by Keith Randall
parent 09932f95f5
commit c8000a18d6
13 changed files with 2 additions and 157 deletions

View file

@ -4302,7 +4302,7 @@ func InitTables() {
alias("runtime/internal/atomic", "CasRel", "runtime/internal/atomic", "Cas", lwatomics...)
/******** math ********/
addF("math", "Sqrt",
addF("math", "sqrt",
func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value {
return s.newValue1(ssa.OpSqrt, types.Types[types.TFLOAT64], args[0])
},

View file

@ -91,15 +91,10 @@ package math
// Sqrt(x < 0) = NaN
// Sqrt(NaN) = NaN
func Sqrt(x float64) float64 {
if haveArchSqrt {
return archSqrt(x)
}
return sqrt(x)
}
// Note: Sqrt is implemented in assembly on some systems.
// Others have assembly stubs that jump to func sqrt below.
// On systems where Sqrt is a single instruction, the compiler
// Note: On systems where Sqrt is a single instruction, the compiler
// may turn a direct call into a direct use of that instruction instead.
func sqrt(x float64) float64 {

View file

@ -1,12 +0,0 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "textflag.h"
// func archSqrt(x float64) float64
TEXT ·archSqrt(SB),NOSPLIT,$0
FMOVD x+0(FP),F0
FSQRT
FMOVDP F0,ret+8(FP)
RET

View file

@ -1,12 +0,0 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "textflag.h"
// func archSqrt(x float64) float64
TEXT ·archSqrt(SB), NOSPLIT, $0
XORPS X0, X0 // break dependency
SQRTSD x+0(FP), X0
MOVSD X0, ret+8(FP)
RET

View file

@ -1,20 +0,0 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "textflag.h"
// func archSqrt(x float64) float64
TEXT ·archSqrt(SB),NOSPLIT,$0
MOVB runtime·goarm(SB), R11
CMP $5, R11
BEQ arm5
MOVD x+0(FP),F0
SQRTD F0,F0
MOVD F0,ret+8(FP)
RET
arm5:
// Tail call to Go implementation.
// Can't use JMP, as in softfloat mode SQRTD is rewritten
// to a CALL, which makes this function have a frame.
RET ·sqrt(SB)

View file

@ -1,12 +0,0 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "textflag.h"
// func archSqrt(x float64) float64
TEXT ·archSqrt(SB),NOSPLIT,$0
FMOVD x+0(FP), F0
FSQRTD F0, F0
FMOVD F0, ret+8(FP)
RET

View file

@ -1,11 +0,0 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build 386 || amd64 || arm64 || arm || mips || mipsle || ppc64 || ppc64le || s390x || riscv64 || wasm
package math
const haveArchSqrt = true
func archSqrt(x float64) float64

View file

@ -1,19 +0,0 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build mips || mipsle
// +build mips mipsle
#include "textflag.h"
// func archSqrt(x float64) float64
TEXT ·archSqrt(SB),NOSPLIT,$0
#ifdef GOMIPS_softfloat
JMP ·sqrt(SB)
#else
MOVD x+0(FP), F0
SQRTD F0, F0
MOVD F0, ret+8(FP)
#endif
RET

View file

@ -1,13 +0,0 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !386 && !amd64 && !arm64 && !arm && !mips && !mipsle && !ppc64 && !ppc64le && !s390x && !riscv64 && !wasm
package math
const haveArchSqrt = false
func archSqrt(x float64) float64 {
panic("not implemented")
}

View file

@ -1,15 +0,0 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ppc64 || ppc64le
// +build ppc64 ppc64le
#include "textflag.h"
// func archSqrt(x float64) float64
TEXT ·archSqrt(SB),NOSPLIT,$0
FMOVD x+0(FP), F0
FSQRT F0, F0
FMOVD F0, ret+8(FP)
RET

View file

@ -1,12 +0,0 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "textflag.h"
// func archSqrt(x float64) float64
TEXT ·archSqrt(SB),NOSPLIT,$0
MOVD x+0(FP), F0
FSQRTD F0, F0
MOVD F0, ret+8(FP)
RET

View file

@ -1,12 +0,0 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "textflag.h"
// func archSqrt(x float64) float64
TEXT ·archSqrt(SB),NOSPLIT,$0
FMOVD x+0(FP), F1
FSQRT F1, F1
FMOVD F1, ret+8(FP)
RET

View file

@ -1,12 +0,0 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "textflag.h"
TEXT ·archSqrt(SB),NOSPLIT,$0
Get SP
F64Load x+0(FP)
F64Sqrt
F64Store ret+8(FP)
RET