Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/go-blas/blas v0.2.1 // indirect
github.com/go-fft/fft v0.29.0 // indirect
github.com/go-fleettools/fleettools v0.81.0 // indirect
github.com/go-simd/floats v0.26.0 // indirect
github.com/go-simd/floats v0.35.0 // indirect
golang.org/x/sys v0.49.0 // indirect
)

Expand Down
4 changes: 2 additions & 2 deletions bench/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ github.com/go-fleettools/fleettools v0.81.0 h1:UnMflmNw5b82Z+7Ums7UVaW5RrGuACCVm
github.com/go-fleettools/fleettools v0.81.0/go.mod h1:y/o3p2HZAvto9J5G5gtPxhN45zgvaYFAXCY9RCK/lQw=
github.com/go-scikit/refbench v0.3.0 h1:ng9b3FRwOWyarkoHHLar1bs43TWi9TydZeO29Yr9iSw=
github.com/go-scikit/refbench v0.3.0/go.mod h1:PhJxvLwXiQeKLP2f5ndcb9k5k4PTvU1rSFJ/MKRCrU0=
github.com/go-simd/floats v0.26.0 h1:IovUqTYY7U+CPDh/4JJMoCGBzuYK6zpSOzuWg/tn4Ks=
github.com/go-simd/floats v0.26.0/go.mod h1:Aq5a6oY5pUqm9Hpi+iV2nrNmaaUb0rwHvUHIerXWSvA=
github.com/go-simd/floats v0.35.0 h1:5qUDVDvuRoQp//OkRSga9xO3jrF48YpovuMM/8Xt//c=
github.com/go-simd/floats v0.35.0/go.mod h1:Aq5a6oY5pUqm9Hpi+iV2nrNmaaUb0rwHvUHIerXWSvA=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.27.2
require (
github.com/go-blas/blas v0.2.1
github.com/go-fft/fft v0.29.0
github.com/go-simd/floats v0.26.0
github.com/go-simd/floats v0.35.0
)

require golang.org/x/sys v0.49.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ github.com/go-blas/blas v0.2.1 h1:5uZfmvXVZn9CjE+I3DalALqWHdKz+rbBs8VffH3dAZo=
github.com/go-blas/blas v0.2.1/go.mod h1:WjhlqzQ+RA8tS473f18QtcJl5Q0GonPs3RJIxT5G7Bk=
github.com/go-fft/fft v0.29.0 h1:WtJb5K3dmbLWREvvZ50od0fxtjIBiXrg7mVybz/nPoA=
github.com/go-fft/fft v0.29.0/go.mod h1:GN5V4TWCjV05ECqH1ONfjFM6Z1m2shqCdssxKGqQ41s=
github.com/go-simd/floats v0.26.0 h1:IovUqTYY7U+CPDh/4JJMoCGBzuYK6zpSOzuWg/tn4Ks=
github.com/go-simd/floats v0.26.0/go.mod h1:Aq5a6oY5pUqm9Hpi+iV2nrNmaaUb0rwHvUHIerXWSvA=
github.com/go-simd/floats v0.35.0 h1:5qUDVDvuRoQp//OkRSga9xO3jrF48YpovuMM/8Xt//c=
github.com/go-simd/floats v0.35.0/go.mod h1:Aq5a6oY5pUqm9Hpi+iV2nrNmaaUb0rwHvUHIerXWSvA=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
299 changes: 87 additions & 212 deletions internal/kernels/asmgen/amd64/gen.go

Large diffs are not rendered by default.

124 changes: 8 additions & 116 deletions internal/kernels/asmgen/arm64/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ func main() {
// The float64 sum, sqrt, add/sub/mul/div and max/min kernels moved to
// go-simd/floats (K1 of the go-scikit performance campaign): sumKernel,
// sqrtKernel, binKernel and extremumKernel below are no longer emitted.
f.Add(dotKernel())
f.Add(bin32Kernel("add32NEON", "VFADD", "FADDS"))
f.Add(bin32Kernel("sub32NEON", "VFSUB", "FSUBS"))
f.Add(bin32Kernel("mul32NEON", "VFMUL", "FMULS"))
f.Add(bin32Kernel("div32NEON", "VFDIV", "FDIVS"))
f.Add(sqrt32Kernel())
// max32NEON/min32NEON moved to go-simd/floats (Float32Max, Float32Min;
// go-ndarray/ndarray#98).
// The GEMM kernels moved to github.com/go-blas/blas, which generates them.
Expand Down Expand Up @@ -140,68 +134,19 @@ func binKernel(name, vop, sop string) *emit.Function {
Raw("VLD1.P 64(R1), [V0.D2, V1.D2, V2.D2, V3.D2]"). // a
Raw("VLD1.P 64(R2), [V4.D2, V5.D2, V6.D2, V7.D2]") // b
for r := 0; r < 4; r++ {
b.Raw(fmt.Sprintf("%s V%d.D2, V%d.D2, V%d.D2", vop, 4+r, r, r)) // a OP b
b.Raw("%s", fmt.Sprintf("%s V%d.D2, V%d.D2, V%d.D2", vop, 4+r, r, r)) // a OP b
}
b.Raw("VST1.P [V0.D2, V1.D2, V2.D2, V3.D2], 64(R0)").
Raw("SUB $8, R3").Raw("B block").
Raw("tail:").Raw("CBZ R3, done").
Raw("FMOVD.P 8(R1), F0").Raw("FMOVD.P 8(R2), F1").
Raw(sop + " F1, F0, F0").
Raw("%s F1, F0, F0", sop).
Raw("FMOVD.P F0, 8(R0)").
Raw("SUB $1, R3").Raw("B tail").
Raw("done:").Ret()
return b.Func()
}

// sqrt32Kernel builds sqrt32NEON(dst, src *float32, n int): VFSQRT on .S4
// over 16 floats per iteration, FSQRTS for the tail; the correctly rounded
// float32 square root, bit-identical to float32(math.Sqrt(float64(x))).
func sqrt32Kernel() *emit.Function {
sig := arm64.Layout(
[]string{"dst", "src", "n"},
[]arm64.Type{arm64.Ptr, arm64.Ptr, arm64.Int64},
nil, nil,
)
b := arm64.NewFunc("sqrt32NEON", sig, 0)
b.LoadArg("dst", "R0").LoadArg("src", "R1").LoadArg("n", "R2").
Raw("block:").Raw("CMP $16, R2").Raw("BLT tail").
Raw("VLD1.P 64(R1), [V0.S4, V1.S4, V2.S4, V3.S4]")
for r := 0; r < 4; r++ {
b.Raw(fmt.Sprintf("VFSQRT V%d.S4, V%d.S4", r, r))
}
b.Raw("VST1.P [V0.S4, V1.S4, V2.S4, V3.S4], 64(R0)").
Raw("SUB $16, R2").Raw("B block").
Raw("tail:").Raw("CBZ R2, done").
Raw("FMOVS.P 4(R1), F0").Raw("FSQRTS F0, F0").Raw("FMOVS.P F0, 4(R0)").
Raw("SUB $1, R2").Raw("B tail").
Raw("done:").Ret()
return b.Func()
}

// bin32Kernel builds NAME(dst, a, b *float32, n int): dst[i] = a[i] OP b[i],
// 16 floats per iteration on the .S4 arrangement (VOP Vm, Vn, Vd: Vd = Vn OP
// Vm), then a scalar tail with sop. Each lane is one IEEE float32 operation,
// so the result is the scalar loop's bit for bit.
func bin32Kernel(name, vop, sop string) *emit.Function {
b := arm64.NewFunc(name, binSig(), 0)
b.LoadArg("dst", "R0").LoadArg("a", "R1").LoadArg("b", "R2").LoadArg("n", "R3")
b.Raw("block:").Raw("CMP $16, R3").Raw("BLT tail").
Raw("VLD1.P 64(R1), [V0.S4, V1.S4, V2.S4, V3.S4]"). // a
Raw("VLD1.P 64(R2), [V4.S4, V5.S4, V6.S4, V7.S4]") // b
for r := 0; r < 4; r++ {
b.Raw(fmt.Sprintf("%s V%d.S4, V%d.S4, V%d.S4", vop, 4+r, r, r)) // a OP b
}
b.Raw("VST1.P [V0.S4, V1.S4, V2.S4, V3.S4], 64(R0)").
Raw("SUB $16, R3").Raw("B block").
Raw("tail:").Raw("CBZ R3, done").
Raw("FMOVS.P 4(R1), F0").Raw("FMOVS.P 4(R2), F1").
Raw(sop + " F1, F0, F0").
Raw("FMOVS.P F0, 4(R0)").
Raw("SUB $1, R3").Raw("B tail").
Raw("done:").Ret()
return b.Func()
}

// blocks8Kernel builds blocks8NEON(r, a *float32, blocks int): for each of
// the blocks of eight float32 at a, in order, r[j] += a[8i+j]. The eight
// accumulators live in V0 (r[0:4]) and V1 (r[4:8]); VFADD on .S4 is one IEEE
Expand Down Expand Up @@ -242,21 +187,21 @@ func extremumKernel(name, vop, sop string) *emit.Function {
Raw("block:").Raw("CMP $8, R1").Raw("BLT fold").
Raw("VLD1.P 64(R0), [V4.D2, V5.D2, V6.D2, V7.D2]")
for r := 0; r < 4; r++ {
b.Raw(fmt.Sprintf("%s V%d.D2, V%d.D2, V%d.D2", vop, 4+r, r, r))
b.Raw("%s", fmt.Sprintf("%s V%d.D2, V%d.D2, V%d.D2", vop, 4+r, r, r))
}
b.Raw("SUB $8, R1").Raw("B block").
Raw("fold:").
Raw(vop+" V1.D2, V0.D2, V0.D2").
Raw(vop+" V3.D2, V2.D2, V2.D2").
Raw(vop+" V2.D2, V0.D2, V0.D2").
Raw("%s V1.D2, V0.D2, V0.D2", vop).
Raw("%s V3.D2, V2.D2, V2.D2", vop).
Raw("%s V2.D2, V0.D2, V0.D2", vop).
Raw("VMOV V0.D[0], R3").
Raw("VMOV V0.D[1], R4").
Raw("FMOVD R3, F0").
Raw("FMOVD R4, F9").
Raw(sop+" F9, F0, F0").
Raw("%s F9, F0, F0", sop).
Raw("tail:").Raw("CBZ R1, done").
Raw("FMOVD.P 8(R0), F10").
Raw(sop+" F10, F0, F0").
Raw("%s F10, F0, F0", sop).
Raw("SUB $1, R1").Raw("B tail").
Raw("done:").
StoreRet("F0", "ret").
Expand All @@ -271,59 +216,6 @@ func extremumKernel(name, vop, sop string) *emit.Function {
// exactly 1.0 is exact, and FMA(src, 1.0, acc) rounds identically to acc+src
// (the addend is the only inexact step), so this is a true vector add, not an
// approximation — it simply reaches it through the only vector-FP op available.
// dotKernel builds dotNEON(a, b *float64, n int) float64, the inner product
// with eight D2 accumulators V0..V7 (16 doubles per iteration): two loads per
// FMA make the loop load-bound, and eight chains keep the four FP pipes of an
// Apple core fed through the FMA latency. The accumulators are folded with
// FMLA against 1.0 (exact; written before Go 1.27 named VFADD), the two
// lanes added, and the tail fused in one element at a time with FMADDD, as gc
// fuses the scalar loop.
func dotKernel() *emit.Function {
sig := arm64.Layout(
[]string{"a", "b", "n"}, []arm64.Type{arm64.Ptr, arm64.Ptr, arm64.Int64},
[]string{"ret"}, []arm64.Type{arm64.Float64},
)
b := arm64.NewFunc("dotNEON", sig, 0)
b.LoadArg("a", "R0").LoadArg("b", "R1").LoadArg("n", "R2")
b.Raw("MOVD $0x3FF0000000000000, R3")
b.Raw("VDUP R3, V24.D2")
for r := 0; r < 8; r++ {
b.Raw(fmt.Sprintf("VEOR V%d.B16, V%d.B16, V%d.B16", r, r, r))
}
b.Raw("dblock:")
b.Raw("CMP $16, R2")
b.Raw("BLT dfold")
b.Raw("VLD1.P 64(R0), [V8.D2, V9.D2, V10.D2, V11.D2]")
b.Raw("VLD1.P 64(R0), [V12.D2, V13.D2, V14.D2, V15.D2]")
b.Raw("VLD1.P 64(R1), [V16.D2, V17.D2, V18.D2, V19.D2]")
b.Raw("VLD1.P 64(R1), [V20.D2, V21.D2, V22.D2, V23.D2]")
for r := 0; r < 8; r++ {
b.Raw(fmt.Sprintf("VFMLA V%d.D2, V%d.D2, V%d.D2", 16+r, 8+r, r)) // V(r) += a*b
}
b.Raw("SUB $16, R2")
b.Raw("B dblock")
b.Raw("dfold:")
for _, p := range [][2]int{{1, 0}, {3, 2}, {5, 4}, {7, 6}, {2, 0}, {6, 4}, {4, 0}} {
b.Raw(fmt.Sprintf("VFMLA V%d.D2, V24.D2, V%d.D2", p[0], p[1])) // V(p1) += V(p0)
}
b.Raw("VMOV V0.D[0], R3")
b.Raw("VMOV V0.D[1], R4")
b.Raw("FMOVD R3, F0")
b.Raw("FMOVD R4, F9")
b.Raw("FADDD F9, F0, F0")
b.Raw("dtail:")
b.Raw("CBZ R2, ddone")
b.Raw("FMOVD.P 8(R0), F10")
b.Raw("FMOVD.P 8(R1), F11")
b.Raw("FMADDD F11, F0, F10, F0") // F0 += F10*F11
b.Raw("SUB $1, R2")
b.Raw("B dtail")
b.Raw("ddone:")
b.StoreRet("F0", "ret")
b.Ret()
return b.Func()
}

func sumKernel() *emit.Function {
b := arm64.NewFunc("sumNEON", reduceSig(), 0)
b.LoadArg("a", "R0").
Expand Down
90 changes: 0 additions & 90 deletions internal/kernels/asmgen/loong64/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,9 @@ const (

func main() {
f := emit.NewFile("loong64")
f.Add(dotKernel())
// The float64 sum, sqrt and Add/Sub/Mul/Div kernels live in go-simd/floats
// (lanes_loong64_gen.go there) since floats v0.10.0.
for _, k := range []struct{ name, vec, sca string }{
{"addLASX", "XVADDD", "ADDD"}, {"subLASX", "XVSUBD", "SUBD"},
{"mulLASX", "XVMULD", "MULD"}, {"divLASX", "XVDIVD", "DIVD"},
} {
//"XVADDD" -> "VADDF", "ADDD" -> "ADDF": the LSX single-precision op.
f.Add(bin32Kernel(k.name[:3]+"32LSX", k.vec[1:len(k.vec)-1]+"F", k.sca[:len(k.sca)-1]+"F"))
}
f.Add(blocks8Kernel())
f.Add(sqrt32Kernel())
abs64 := f.Data("abs64", lanes(8, 0x7fffffffffffffff))
inf64 := f.Data("inf64", lanes(8, 0x7ff0000000000000))
// max32LSX/min32LSX moved to go-simd/floats (Float32Max, Float32Min;
Expand Down Expand Up @@ -70,32 +61,6 @@ func zero(b *loong64.Builder, n int) {
}
}

// dotKernel builds dotLASX(a, b *float64, n int) float64: four fused
// multiply-add accumulators (xvfmadd.d), folded, plus a fused scalar tail.
func dotKernel() *emit.Function {
sig := loong64.Layout([]string{"a", "b", "n"}, []loong64.Type{loong64.Ptr, loong64.Ptr, loong64.Int64},
[]string{"ret"}, []loong64.Type{loong64.Float64})
b := loong64.NewFunc("dotLASX", sig, 0)
b.LoadArg("a", "R4").LoadArg("b", "R6").LoadArg("n", "R5")
zero(b, 4)
b.Label("dloop").Raw("MOVV $16, R12").Raw("BLT R5, R12, dfold")
for k := 0; k < 4; k++ {
b.Raw("XVMOVQ %d(R4), X%d", 32*k, 4+k)
b.Raw("XVMOVQ %d(R6), X%d", 32*k, 8+k)
}
for k := 0; k < 4; k++ {
b.XVFMADDD(k, 4+k, 8+k, k) // X(k) = a*b + X(k)
}
b.Raw("ADDV $128, R4").Raw("ADDV $128, R6").Raw("ADDV $-16, R5").Raw("JMP dloop")
b.Label("dfold")
fold(b)
b.Label("dtail").Raw("BEQ R5, ddone").
Raw("MOVD 0(R4), F1").Raw("MOVD 0(R6), F2").Raw("FMADDD F0, F2, F1, F0"). // F0 = F1*F2 + F0; Go's order is Fa, Fk, Fj, Fd
Raw("ADDV $8, R4").Raw("ADDV $8, R6").Raw("ADDV $-1, R5").Raw("JMP dtail")
b.Label("ddone").StoreRet("F0", "ret").Ret()
return b.Func()
}

// lanes is a 16-byte vector constant: v in every lane of size bytes, each lane
// little-endian.
func lanes(size int, v uint64) []byte {
Expand Down Expand Up @@ -186,61 +151,6 @@ func extremumKernel(name string, bits int, isMax bool, abs, inf string) *emit.Fu
return b.Func()
}

// sqrt32Kernel builds sqrt32LSX(dst, src *float32, n int): vfsqrt.s over four
// 128-bit registers, 16 floats per iteration, SQRTF for the tail; the
// correctly rounded float32 square root, bit-identical to
// float32(math.Sqrt(float64(x))).
func sqrt32Kernel() *emit.Function {
sig := loong64.Layout([]string{"dst", "src", "n"}, []loong64.Type{loong64.Ptr, loong64.Ptr, loong64.Int64}, nil, nil)
b := loong64.NewFunc("sqrt32LSX", sig, 0)
b.LoadArg("dst", "R4").LoadArg("src", "R6").LoadArg("n", "R5")
b.Label("qloop").Raw("MOVV $16, R12").Raw("BLT R5, R12, qtail")
for k := 0; k < 4; k++ {
b.Raw("VMOVQ %d(R6), V%d", 16*k, k)
}
for k := 0; k < 4; k++ {
b.Raw("VFSQRTF V%d, V%d", k, 4+k)
}
for k := 0; k < 4; k++ {
b.Raw("VMOVQ V%d, %d(R4)", 4+k, 16*k)
}
b.Raw("ADDV $64, R4").Raw("ADDV $64, R6").Raw("ADDV $-16, R5").Raw("JMP qloop")
b.Label("qtail").Raw("BEQ R5, qdone").
Raw("MOVF 0(R6), F1").Raw("SQRTF F1, F1").Raw("MOVF F1, 0(R4)").
Raw("ADDV $4, R4").Raw("ADDV $4, R6").Raw("ADDV $-1, R5").Raw("JMP qtail")
b.Label("qdone").Ret()
return b.Func()
}

// bin32Kernel builds name(dst, a, b *float32, n int): dst[i] = a[i] OP b[i],
// 16 floats per iteration over four 128-bit LSX registers (four independent
// chains, as the block kernel's lesson on one LASX register taught), a scalar
// tail with sca. Each lane is one IEEE float32 operation, bit-identical to the
// scalar oracle.
func bin32Kernel(name, vec, sca string) *emit.Function {
sig := loong64.Layout([]string{"dst", "a", "b", "n"},
[]loong64.Type{loong64.Ptr, loong64.Ptr, loong64.Ptr, loong64.Int64}, nil, nil)
b := loong64.NewFunc(name, sig, 0)
b.LoadArg("dst", "R4").LoadArg("a", "R6").LoadArg("b", "R7").LoadArg("n", "R5")
b.Label("floop").Raw("MOVV $16, R12").Raw("BLT R5, R12, ftail")
for k := 0; k < 4; k++ {
b.Raw("VMOVQ %d(R6), V%d", 16*k, k)
b.Raw("VMOVQ %d(R7), V%d", 16*k, 4+k)
}
for k := 0; k < 4; k++ {
b.Raw("%s V%d, V%d, V%d", vec, 4+k, k, 8+k) // V(8+k) = V(k) OP V(4+k)
}
for k := 0; k < 4; k++ {
b.Raw("VMOVQ V%d, %d(R4)", 8+k, 16*k)
}
b.Raw("ADDV $64, R4").Raw("ADDV $64, R6").Raw("ADDV $64, R7").Raw("ADDV $-16, R5").Raw("JMP floop")
b.Label("ftail").Raw("BEQ R5, fdone").
Raw("MOVF 0(R6), F1").Raw("MOVF 0(R7), F2").Raw("%s F2, F1", sca).Raw("MOVF F1, 0(R4)").
Raw("ADDV $4, R4").Raw("ADDV $4, R6").Raw("ADDV $4, R7").Raw("ADDV $-1, R5").Raw("JMP ftail")
b.Label("fdone").Ret()
return b.Func()
}

// blocks8Kernel builds blocks8LSX(r, a *float32, blocks int): for each of
// the blocks of eight float32 at a, in order, r[j] += a[8i+j] (NumPy's
// pairwise-sum block). The eight accumulators are split over two 128-bit LSX
Expand Down
Loading
Loading