mirror of
https://github.com/golang/go
synced 2024-11-27 02:51:24 -07:00
5cad8d41ca
This commit optimized math.Abs function implementation on mipsx. Tested on loongson 3A2000. goos: linux goarch: mipsle pkg: math │ oldmath │ newmath │ │ sec/op │ sec/op vs base │ Acos-4 282.6n ± 0% 282.3n ± 0% ~ (p=0.140 n=7) Acosh-4 506.1n ± 0% 451.8n ± 0% -10.73% (p=0.001 n=7) Asin-4 272.3n ± 0% 272.2n ± 0% ~ (p=0.808 n=7) Asinh-4 529.7n ± 0% 475.3n ± 0% -10.27% (p=0.001 n=7) Atan-4 208.2n ± 0% 207.9n ± 0% ~ (p=0.134 n=7) Atanh-4 503.4n ± 1% 449.7n ± 0% -10.67% (p=0.001 n=7) Atan2-4 310.5n ± 0% 310.5n ± 0% ~ (p=0.928 n=7) Cbrt-4 359.3n ± 0% 358.8n ± 0% ~ (p=0.121 n=7) Ceil-4 203.9n ± 0% 204.0n ± 0% ~ (p=0.600 n=7) Compare-4 23.11n ± 0% 23.11n ± 0% ~ (p=0.702 n=7) Compare32-4 19.09n ± 0% 19.12n ± 0% ~ (p=0.070 n=7) Copysign-4 33.20n ± 0% 34.02n ± 0% +2.47% (p=0.001 n=7) Cos-4 422.5n ± 0% 385.4n ± 1% -8.78% (p=0.001 n=7) Cosh-4 628.0n ± 0% 545.5n ± 0% -13.14% (p=0.001 n=7) Erf-4 193.7n ± 2% 192.7n ± 1% ~ (p=0.430 n=7) Erfc-4 192.8n ± 1% 193.0n ± 0% ~ (p=0.245 n=7) Erfinv-4 220.7n ± 1% 221.5n ± 2% ~ (p=0.272 n=7) Erfcinv-4 221.3n ± 1% 220.4n ± 2% ~ (p=0.738 n=7) Exp-4 471.4n ± 0% 435.1n ± 0% -7.70% (p=0.001 n=7) ExpGo-4 470.6n ± 0% 434.0n ± 0% -7.78% (p=0.001 n=7) Expm1-4 243.1n ± 0% 243.4n ± 0% ~ (p=0.417 n=7) Exp2-4 463.1n ± 0% 427.0n ± 0% -7.80% (p=0.001 n=7) Exp2Go-4 462.4n ± 0% 426.2n ± 5% -7.83% (p=0.001 n=7) Abs-4 37.000n ± 0% 8.039n ± 9% -78.27% (p=0.001 n=7) Dim-4 18.09n ± 0% 18.11n ± 0% ~ (p=0.094 n=7) Floor-4 151.9n ± 0% 151.8n ± 0% ~ (p=0.190 n=7) Max-4 116.7n ± 1% 116.7n ± 1% ~ (p=0.842 n=7) Min-4 116.6n ± 1% 116.6n ± 0% ~ (p=0.464 n=7) Mod-4 1244.0n ± 0% 980.9n ± 0% -21.15% (p=0.001 n=7) Frexp-4 199.0n ± 0% 146.7n ± 0% -26.28% (p=0.001 n=7) Gamma-4 516.4n ± 0% 479.3n ± 1% -7.18% (p=0.001 n=7) Hypot-4 169.8n ± 0% 117.8n ± 2% -30.62% (p=0.001 n=7) HypotGo-4 170.8n ± 0% 117.5n ± 0% -31.21% (p=0.001 n=7) Ilogb-4 160.8n ± 0% 109.5n ± 0% -31.90% (p=0.001 n=7) J0-4 1.359µ ± 0% 1.305µ ± 0% -3.97% (p=0.001 n=7) J1-4 1.386µ ± 0% 1.334µ ± 0% -3.75% (p=0.001 n=7) Jn-4 2.864µ ± 0% 2.758µ ± 0% -3.70% (p=0.001 n=7) Ldexp-4 202.9n ± 0% 151.7n ± 0% -25.23% (p=0.001 n=7) Lgamma-4 234.0n ± 0% 234.3n ± 0% ~ (p=0.199 n=7) Log-4 444.1n ± 0% 407.9n ± 0% -8.15% (p=0.001 n=7) Logb-4 157.8n ± 0% 121.6n ± 0% -22.94% (p=0.001 n=7) Log1p-4 354.8n ± 0% 315.4n ± 0% -11.10% (p=0.001 n=7) Log10-4 453.9n ± 0% 417.9n ± 0% -7.93% (p=0.001 n=7) Log2-4 245.3n ± 0% 209.1n ± 0% -14.76% (p=0.001 n=7) Modf-4 126.6n ± 0% 126.6n ± 0% ~ (p=0.126 n=7) Nextafter32-4 112.5n ± 0% 112.5n ± 0% ~ (p=0.853 n=7) Nextafter64-4 141.7n ± 0% 141.6n ± 0% ~ (p=0.331 n=7) PowInt-4 878.8n ± 1% 758.3n ± 1% -13.71% (p=0.001 n=7) PowFrac-4 1.809µ ± 0% 1.615µ ± 0% -10.72% (p=0.001 n=7) Pow10Pos-4 18.10n ± 0% 18.12n ± 0% ~ (p=0.464 n=7) Pow10Neg-4 17.09n ± 0% 17.09n ± 0% ~ (p=0.263 n=7) Round-4 68.36n ± 0% 68.33n ± 0% ~ (p=0.325 n=7) RoundToEven-4 78.40n ± 0% 78.40n ± 0% ~ (p=0.934 n=7) Remainder-4 894.0n ± 1% 753.4n ± 1% -15.73% (p=0.001 n=7) Signbit-4 18.09n ± 0% 18.09n ± 0% ~ (p=0.761 n=7) Sin-4 389.8n ± 1% 389.8n ± 0% ~ (p=0.995 n=7) Sincos-4 416.0n ± 0% 415.9n ± 0% ~ (p=0.361 n=7) Sinh-4 634.6n ± 4% 585.6n ± 1% -7.72% (p=0.001 n=7) SqrtIndirect-4 8.035n ± 0% 8.036n ± 0% ~ (p=0.523 n=7) SqrtLatency-4 8.039n ± 0% 8.037n ± 0% ~ (p=0.218 n=7) SqrtIndirectLatency-4 8.040n ± 0% 8.040n ± 0% ~ (p=0.652 n=7) SqrtGoLatency-4 895.7n ± 0% 896.6n ± 0% +0.10% (p=0.004 n=7) SqrtPrime-4 5.406µ ± 0% 5.407µ ± 0% ~ (p=0.592 n=7) Tan-4 406.1n ± 0% 405.8n ± 1% ~ (p=0.435 n=7) Tanh-4 627.6n ± 0% 545.5n ± 0% -13.08% (p=0.001 n=7) Trunc-4 146.7n ± 1% 146.7n ± 0% ~ (p=0.755 n=7) Y0-4 1.359µ ± 0% 1.310µ ± 0% -3.61% (p=0.001 n=7) Y1-4 1.351µ ± 0% 1.301µ ± 0% -3.70% (p=0.001 n=7) Yn-4 2.829µ ± 0% 2.729µ ± 0% -3.53% (p=0.001 n=7) Float64bits-4 14.08n ± 0% 14.07n ± 0% ~ (p=0.069 n=7) Float64frombits-4 19.09n ± 0% 19.10n ± 0% ~ (p=0.755 n=7) Float32bits-4 13.06n ± 0% 13.07n ± 1% ~ (p=0.586 n=7) Float32frombits-4 13.06n ± 0% 13.06n ± 0% ~ (p=0.853 n=7) FMA-4 606.9n ± 0% 606.8n ± 0% ~ (p=0.393 n=7) geomean 201.1n 185.4n -7.81% Change-Id: I6d41a97ad3789ed5731588588859ac0b8b13b664 Reviewed-on: https://go-review.googlesource.com/c/go/+/484675 Reviewed-by: Rong Zhang <rongrong@oss.cipunited.com> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Than McIntosh <thanm@google.com>
248 lines
5.9 KiB
Go
248 lines
5.9 KiB
Go
// asmcheck
|
|
|
|
// 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.
|
|
|
|
package codegen
|
|
|
|
import "math"
|
|
|
|
var sink64 [8]float64
|
|
|
|
func approx(x float64) {
|
|
// amd64/v2:-".*x86HasSSE41" amd64/v3:-".*x86HasSSE41"
|
|
// amd64:"ROUNDSD\t[$]2"
|
|
// s390x:"FIDBR\t[$]6"
|
|
// arm64:"FRINTPD"
|
|
// ppc64x:"FRIP"
|
|
// wasm:"F64Ceil"
|
|
sink64[0] = math.Ceil(x)
|
|
|
|
// amd64/v2:-".*x86HasSSE41" amd64/v3:-".*x86HasSSE41"
|
|
// amd64:"ROUNDSD\t[$]1"
|
|
// s390x:"FIDBR\t[$]7"
|
|
// arm64:"FRINTMD"
|
|
// ppc64x:"FRIM"
|
|
// wasm:"F64Floor"
|
|
sink64[1] = math.Floor(x)
|
|
|
|
// s390x:"FIDBR\t[$]1"
|
|
// arm64:"FRINTAD"
|
|
// ppc64x:"FRIN"
|
|
sink64[2] = math.Round(x)
|
|
|
|
// amd64/v2:-".*x86HasSSE41" amd64/v3:-".*x86HasSSE41"
|
|
// amd64:"ROUNDSD\t[$]3"
|
|
// s390x:"FIDBR\t[$]5"
|
|
// arm64:"FRINTZD"
|
|
// ppc64x:"FRIZ"
|
|
// wasm:"F64Trunc"
|
|
sink64[3] = math.Trunc(x)
|
|
|
|
// amd64/v2:-".*x86HasSSE41" amd64/v3:-".*x86HasSSE41"
|
|
// amd64:"ROUNDSD\t[$]0"
|
|
// s390x:"FIDBR\t[$]4"
|
|
// arm64:"FRINTND"
|
|
// wasm:"F64Nearest"
|
|
sink64[4] = math.RoundToEven(x)
|
|
}
|
|
|
|
func sqrt(x float64) float64 {
|
|
// amd64:"SQRTSD"
|
|
// 386/sse2:"SQRTSD" 386/softfloat:-"SQRTD"
|
|
// arm64:"FSQRTD"
|
|
// arm/7:"SQRTD"
|
|
// mips/hardfloat:"SQRTD" mips/softfloat:-"SQRTD"
|
|
// mips64/hardfloat:"SQRTD" mips64/softfloat:-"SQRTD"
|
|
// wasm:"F64Sqrt"
|
|
// ppc64x:"FSQRT"
|
|
return math.Sqrt(x)
|
|
}
|
|
|
|
func sqrt32(x float32) float32 {
|
|
// amd64:"SQRTSS"
|
|
// 386/sse2:"SQRTSS" 386/softfloat:-"SQRTS"
|
|
// arm64:"FSQRTS"
|
|
// arm/7:"SQRTF"
|
|
// mips/hardfloat:"SQRTF" mips/softfloat:-"SQRTF"
|
|
// mips64/hardfloat:"SQRTF" mips64/softfloat:-"SQRTF"
|
|
// wasm:"F32Sqrt"
|
|
// ppc64x:"FSQRTS"
|
|
return float32(math.Sqrt(float64(x)))
|
|
}
|
|
|
|
// Check that it's using integer registers
|
|
func abs(x, y float64) {
|
|
// amd64:"BTRQ\t[$]63"
|
|
// arm64:"FABSD\t"
|
|
// s390x:"LPDFR\t",-"MOVD\t" (no integer load/store)
|
|
// ppc64x:"FABS\t"
|
|
// riscv64:"FABSD\t"
|
|
// wasm:"F64Abs"
|
|
// arm/6:"ABSD\t"
|
|
// mips64/hardfloat:"ABSD\t"
|
|
// mips/hardfloat:"ABSD\t"
|
|
sink64[0] = math.Abs(x)
|
|
|
|
// amd64:"BTRQ\t[$]63","PXOR" (TODO: this should be BTSQ)
|
|
// s390x:"LNDFR\t",-"MOVD\t" (no integer load/store)
|
|
// ppc64x:"FNABS\t"
|
|
sink64[1] = -math.Abs(y)
|
|
}
|
|
|
|
// Check that it's using integer registers
|
|
func abs32(x float32) float32 {
|
|
// s390x:"LPDFR",-"LDEBR",-"LEDBR" (no float64 conversion)
|
|
return float32(math.Abs(float64(x)))
|
|
}
|
|
|
|
// Check that it's using integer registers
|
|
func copysign(a, b, c float64) {
|
|
// amd64:"BTRQ\t[$]63","ANDQ","ORQ"
|
|
// s390x:"CPSDR",-"MOVD" (no integer load/store)
|
|
// ppc64x:"FCPSGN"
|
|
// riscv64:"FSGNJD"
|
|
// wasm:"F64Copysign"
|
|
sink64[0] = math.Copysign(a, b)
|
|
|
|
// amd64:"BTSQ\t[$]63"
|
|
// s390x:"LNDFR\t",-"MOVD\t" (no integer load/store)
|
|
// ppc64x:"FCPSGN"
|
|
// riscv64:"FSGNJD"
|
|
// arm64:"ORR", -"AND"
|
|
sink64[1] = math.Copysign(c, -1)
|
|
|
|
// Like math.Copysign(c, -1), but with integer operations. Useful
|
|
// for platforms that have a copysign opcode to see if it's detected.
|
|
// s390x:"LNDFR\t",-"MOVD\t" (no integer load/store)
|
|
sink64[2] = math.Float64frombits(math.Float64bits(a) | 1<<63)
|
|
|
|
// amd64:"ANDQ","ORQ"
|
|
// s390x:"CPSDR\t",-"MOVD\t" (no integer load/store)
|
|
// ppc64x:"FCPSGN"
|
|
// riscv64:"FSGNJD"
|
|
sink64[3] = math.Copysign(-1, c)
|
|
}
|
|
|
|
func fma(x, y, z float64) float64 {
|
|
// amd64/v3:-".*x86HasFMA"
|
|
// amd64:"VFMADD231SD"
|
|
// arm/6:"FMULAD"
|
|
// arm64:"FMADDD"
|
|
// s390x:"FMADD"
|
|
// ppc64x:"FMADD"
|
|
// riscv64:"FMADDD"
|
|
return math.FMA(x, y, z)
|
|
}
|
|
|
|
func fms(x, y, z float64) float64 {
|
|
// riscv64:"FMSUBD"
|
|
return math.FMA(x, y, -z)
|
|
}
|
|
|
|
func fnma(x, y, z float64) float64 {
|
|
// riscv64:"FNMADDD"
|
|
return math.FMA(-x, y, z)
|
|
}
|
|
|
|
func fnms(x, y, z float64) float64 {
|
|
// riscv64:"FNMSUBD"
|
|
return math.FMA(x, -y, -z)
|
|
}
|
|
|
|
func fromFloat64(f64 float64) uint64 {
|
|
// amd64:"MOVQ\tX.*, [^X].*"
|
|
// arm64:"FMOVD\tF.*, R.*"
|
|
// ppc64x:"MFVSRD"
|
|
return math.Float64bits(f64+1) + 1
|
|
}
|
|
|
|
func fromFloat32(f32 float32) uint32 {
|
|
// amd64:"MOVL\tX.*, [^X].*"
|
|
// arm64:"FMOVS\tF.*, R.*"
|
|
return math.Float32bits(f32+1) + 1
|
|
}
|
|
|
|
func toFloat64(u64 uint64) float64 {
|
|
// amd64:"MOVQ\t[^X].*, X.*"
|
|
// arm64:"FMOVD\tR.*, F.*"
|
|
// ppc64x:"MTVSRD"
|
|
return math.Float64frombits(u64+1) + 1
|
|
}
|
|
|
|
func toFloat32(u32 uint32) float32 {
|
|
// amd64:"MOVL\t[^X].*, X.*"
|
|
// arm64:"FMOVS\tR.*, F.*"
|
|
return math.Float32frombits(u32+1) + 1
|
|
}
|
|
|
|
// Test that comparisons with constants converted to float
|
|
// are evaluated at compile-time
|
|
|
|
func constantCheck64() bool {
|
|
// amd64:"(MOVB\t[$]0)|(XORL\t[A-Z][A-Z0-9]+, [A-Z][A-Z0-9]+)",-"FCMP",-"MOVB\t[$]1"
|
|
// s390x:"MOV(B|BZ|D)\t[$]0,",-"FCMPU",-"MOV(B|BZ|D)\t[$]1,"
|
|
return 0.5 == float64(uint32(1)) || 1.5 > float64(uint64(1<<63))
|
|
}
|
|
|
|
func constantCheck32() bool {
|
|
// amd64:"MOV(B|L)\t[$]1",-"FCMP",-"MOV(B|L)\t[$]0"
|
|
// s390x:"MOV(B|BZ|D)\t[$]1,",-"FCMPU",-"MOV(B|BZ|D)\t[$]0,"
|
|
return float32(0.5) <= float32(int64(1)) && float32(1.5) >= float32(int32(-1<<31))
|
|
}
|
|
|
|
// Test that integer constants are converted to floating point constants
|
|
// at compile-time
|
|
|
|
func constantConvert32(x float32) float32 {
|
|
// amd64:"MOVSS\t[$]f32.3f800000\\(SB\\)"
|
|
// s390x:"FMOVS\t[$]f32.3f800000\\(SB\\)"
|
|
// ppc64x:"FMOVS\t[$]f32.3f800000\\(SB\\)"
|
|
// arm64:"FMOVS\t[$]\\(1.0\\)"
|
|
if x > math.Float32frombits(0x3f800000) {
|
|
return -x
|
|
}
|
|
return x
|
|
}
|
|
|
|
func constantConvertInt32(x uint32) uint32 {
|
|
// amd64:-"MOVSS"
|
|
// s390x:-"FMOVS"
|
|
// ppc64x:-"FMOVS"
|
|
// arm64:-"FMOVS"
|
|
if x > math.Float32bits(1) {
|
|
return -x
|
|
}
|
|
return x
|
|
}
|
|
|
|
func nanGenerate64() float64 {
|
|
// Test to make sure we don't generate a NaN while constant propagating.
|
|
// See issue 36400.
|
|
zero := 0.0
|
|
// amd64:-"DIVSD"
|
|
inf := 1 / zero // +inf. We can constant propagate this one.
|
|
negone := -1.0
|
|
|
|
// amd64:"DIVSD"
|
|
z0 := zero / zero
|
|
// amd64:"MULSD"
|
|
z1 := zero * inf
|
|
// amd64:"SQRTSD"
|
|
z2 := math.Sqrt(negone)
|
|
return z0 + z1 + z2
|
|
}
|
|
|
|
func nanGenerate32() float32 {
|
|
zero := float32(0.0)
|
|
// amd64:-"DIVSS"
|
|
inf := 1 / zero // +inf. We can constant propagate this one.
|
|
|
|
// amd64:"DIVSS"
|
|
z0 := zero / zero
|
|
// amd64:"MULSS"
|
|
z1 := zero * inf
|
|
return z0 + z1
|
|
}
|