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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.27.1
toolchain go1.27.3

require (
github.com/pmezard/go-difflib v1.0.0
golang.org/x/tools v0.52.1-0.20261103172851-aba6dfd3c19d
rsc.io/script v0.0.2
)
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
Expand Down
47 changes: 19 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"go/format"
"go/token"
"os"
"os/exec"
"runtime"
"runtime/pprof"
"strings"

"github.com/pmezard/go-difflib/difflib"
"golang.org/x/tools/go/packages"

"github.com/mauri870/loopvec/internal/analysis"
Expand Down Expand Up @@ -319,34 +319,25 @@ func addBuildTag(src []byte, tag string) []byte {
return formatted
}

// printDiff prints a unified diff between orig and rewritten for path,
// using the system diff command. Both sides are labeled with path, so the
// header does not depend on the temporary files or on which diff is installed
// (GNU and BSD diff print their timestamps differently).
// printDiff prints a unified diff between orig and rewritten for path, with
// both sides labeled with path. It needs no diff command, so the output is the
// same everywhere.
func printDiff(path string, orig, rewritten []byte) error {
origFile, err := os.CreateTemp("", "loopvec-orig-*.go")
if err != nil {
return err
}
defer func() { _ = os.Remove(origFile.Name()) }()
if _, err := origFile.Write(orig); err != nil {
_ = origFile.Close()
return err
}
_ = origFile.Close()
return difflib.WriteUnifiedDiff(os.Stdout, difflib.UnifiedDiff{
A: diffLines(orig),
B: diffLines(rewritten),
FromFile: path,
ToFile: path,
Context: 3,
})
}

newFile, err := os.CreateTemp("", "loopvec-new-*.go")
if err != nil {
return err
}
defer func() { _ = os.Remove(newFile.Name()) }()
if _, err := newFile.Write(rewritten); err != nil {
_ = newFile.Close()
return err
// diffLines splits src into lines that keep their newline. difflib.SplitLines
// would add an empty line after a final newline.
func diffLines(src []byte) []string {
lines := strings.SplitAfter(string(src), "\n")
if lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
_ = newFile.Close()

out, _ := exec.Command("diff", "-u", "-L", path, "-L", path, origFile.Name(), newFile.Name()).Output()
_, err = os.Stdout.Write(out)
return err
return lines
}
62 changes: 53 additions & 9 deletions testdata/flag_diff.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# -d prints a unified diff, labeled with the file's path on both sides so the
# headers are the same with GNU and BSD diff, and leaves the source untouched.
# -d prints a unified diff, labeled with the file's path on both sides, and
# leaves the source untouched. loopvec computes the diff itself, so the output
# does not depend on a diff command and is checked whole.

cp ops.go ops.go.orig
loopvec -d .
cmpenv stderr stderr.want
stdout -count=1 '^--- \S+/ops\.go$'
stdout -count=1 '^\+\+\+ \S+/ops\.go$'
stdout -count=1 '^@@ -1,7 \+1,43 @@$'
stdout -count=1 '^\+//go:build goexperiment.simd$'
stdout -count=1 '^\+import \($'
stdout -count=1 '^- for i := range dst \{$'
stdout -count=1 '^\+ _v1.Add\(_v2\).StorePart\(dst\[_i:\]\)$'
cmpenv stdout stdout.want
cmp ops.go ops.go.orig
! exists ops_simd.go

Expand All @@ -28,3 +23,52 @@ func AddInt32s(dst, a, b []int32) {
}
-- stderr.want --
loopvec: rewrote 1 loop(s) in $WORK/ops.go
-- stdout.want --
--- $WORK/ops.go
+++ $WORK/ops.go
@@ -1,7 +1,43 @@
+//go:build goexperiment.simd
+
package test

+import (
+ "simd"
+ "unsafe"
+)
+
func AddInt32s(dst, a, b []int32) {
- for i := range dst {
- dst[i] = a[i] + b[i]
+ if _loopvecOverlap(dst, a) || _loopvecOverlap(dst, b) {
+ for i := range dst {
+ dst[i] = a[i] + b[i]
+ }
+ } else {
+ if len(dst) > 0 {
+ _ = a[len(dst)-1]
+ _ = b[len(dst)-1]
+ for _i := 0; _i < len(dst); {
+ _v1, _n := simd.LoadInt32sPart(a[_i:])
+ _v2, _ := simd.LoadInt32sPart(b[_i:])
+ _v1.Add(_v2).StorePart(dst[_i:])
+ _i += _n
+ }
+ }
}
}
+
+func _loopvecOverlap[T any](a, b []T, window ...int) bool {
+ aLo, aHi, bLo, bHi := 0, len(a), 0, len(b)
+ if len(window) == 4 {
+ aLo, aHi, bLo, bHi = window[0], window[1], window[2], window[3]
+ }
+ if aLo >= aHi || bLo >= bHi {
+ return false
+ }
+ size := unsafe.Sizeof(a[0])
+ aBase := uintptr(unsafe.Pointer(unsafe.SliceData(a)))
+ bBase := uintptr(unsafe.Pointer(unsafe.SliceData(b)))
+ aStart, aEnd := aBase+uintptr(aLo)*size, aBase+uintptr(aHi)*size
+ bStart, bEnd := bBase+uintptr(bLo)*size, bBase+uintptr(bHi)*size
+ return aStart < bEnd && bStart < aEnd
+}
Loading