-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimd.go
More file actions
54 lines (46 loc) · 1.57 KB
/
Copy pathsimd.go
File metadata and controls
54 lines (46 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2026, Janoš Guljaš <janos@resenje.org>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package schulze
import (
"unsafe"
)
// relaxRow applies the inner Floyd-Warshall widest path update on rowJ using rowI:
// rowJ[k] = max(rowJ[k], min(jip, rowI[k])) for all k in [0, n).
func relaxRow[N Number](rowI, rowJ unsafe.Pointer, jip N, n int) {
if n <= 0 {
return
}
// Fast SIMD path for standard int (int64 on 64-bit platforms)
if _, ok := any(jip).(int); ok {
relaxRowInt(rowI, rowJ, int(jip), n)
return
}
// Generic pure Go unrolled fallback for other numeric types
elemSize := unsafe.Sizeof(N(0))
const step = 8
mod := n % step
end := n - mod
for k := 0; k < end; k += step {
setStrengthValue(rowI, rowJ, uintptr(k), jip, elemSize)
setStrengthValue(rowI, rowJ, uintptr(k+1), jip, elemSize)
setStrengthValue(rowI, rowJ, uintptr(k+2), jip, elemSize)
setStrengthValue(rowI, rowJ, uintptr(k+3), jip, elemSize)
setStrengthValue(rowI, rowJ, uintptr(k+4), jip, elemSize)
setStrengthValue(rowI, rowJ, uintptr(k+5), jip, elemSize)
setStrengthValue(rowI, rowJ, uintptr(k+6), jip, elemSize)
setStrengthValue(rowI, rowJ, uintptr(k+7), jip, elemSize)
}
for k := end; k < n; k++ {
setStrengthValue(rowI, rowJ, uintptr(k), jip, elemSize)
}
}
func setStrengthValue[N Number](rowI, rowJ unsafe.Pointer, k uintptr, jip N, elemSize uintptr) {
ikp := (*N)(unsafe.Add(rowI, k*elemSize))
jkp := (*N)(unsafe.Add(rowJ, k*elemSize))
m := min(jip, *ikp)
if m > *jkp {
*jkp = m
}
}