Repository navigation
Expand file tree
/
Copy pathtyped.go
More file actions
754 lines (702 loc) · 19.3 KB
/
Copy pathtyped.go
File metadata and controls
754 lines (702 loc) · 19.3 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
package ndarray
import (
"math"
"math/cmplx"
"github.com/go-ndarray/ndarray/internal/kernels"
)
// Inner loops for the dtypes other than Float64. Each takes contiguous storage
// slices of one element type, already converted to the result dtype and
// broadcast to the result shape; the operation code is switched on once, so
// every loop is a tight loop over one element type. The generated
// zz_dtypes_gen.go picks the instantiation from the storage type.
// binCode selects a binary elementwise operation.
type binCode uint8
const (
opAdd binCode = iota
opSub
opMul
opDiv
opMax
opMin
)
// cmpCode selects a comparison.
type cmpCode uint8
const (
opEq cmpCode = iota
opNe
opLt
opLe
opGt
opGe
)
// binNum applies an arithmetic operation. opDiv is only used for inexact
// types: integer division is true division, done in a float type.
func binNum[T numT](op binCode, d, x, y []T) {
x, y = x[:len(d)], y[:len(d)] // so the loops below need no bounds checks
switch op {
case opAdd:
for i := range d {
d[i] = x[i] + y[i]
}
case opSub:
for i := range d {
d[i] = x[i] - y[i]
}
case opMul:
for i := range d {
d[i] = x[i] * y[i]
}
case opDiv:
for i := range d {
d[i] = x[i] / y[i]
}
}
}
// binInt applies an arithmetic or max/min operation to integers.
func binInt[T intT](op binCode, d, x, y []T) {
x, y = x[:len(d)], y[:len(d)]
switch op {
case opMax:
for i := range d {
d[i] = max(x[i], y[i])
}
case opMin:
for i := range d {
d[i] = min(x[i], y[i])
}
default:
binNum(op, d, x, y)
}
}
// binFloat32 applies an arithmetic or max/min operation to float32 (Float64
// has its own path). Max and min propagate NaN, as NumPy's maximum and minimum
// do (and Go's builtins). The arithmetic runs on kernels.Bin32, whose codes
// are opAdd..opDiv: one IEEE operation per lane, the scalar loop's result bit
// for bit.
func binFloat32(op binCode, d, x, y []float32) {
x, y = x[:len(d)], y[:len(d)]
switch op {
case opMax:
for i := range d {
d[i] = max(x[i], y[i])
}
case opMin:
for i := range d {
d[i] = min(x[i], y[i])
}
default:
kernels.Bin32(int(op), d, x, y)
}
}
// binComplex applies an arithmetic or max/min operation to complex numbers.
// Max and min order lexicographically, real part first, and propagate NaN, as
// NumPy does.
func binComplex[T complexT](op binCode, d, x, y []T) {
x, y = x[:len(d)], y[:len(d)]
switch op {
case opMax:
for i := range d {
d[i] = pickComplex(x[i], y[i], true)
}
case opMin:
for i := range d {
d[i] = pickComplex(x[i], y[i], false)
}
default:
binNum(op, d, x, y)
}
}
// pickComplex returns the larger (or smaller) of x and y in NumPy's
// lexicographic order; a NaN in either gives that operand, x first.
func pickComplex[T complexT](x, y T, larger bool) T {
xc, yc := complex128(x), complex128(y)
if cmplx.IsNaN(xc) {
return x
}
if cmplx.IsNaN(yc) {
return y
}
if lessComplex(xc, yc) == larger {
return y
}
return x
}
// lessComplex is NumPy's lexicographic order on complex numbers.
func lessComplex(x, y complex128) bool {
return real(x) < real(y) || (real(x) == real(y) && imag(x) < imag(y))
}
// binBool applies an operation to booleans: NumPy's add and maximum are "or",
// multiply and minimum are "and". Subtraction is an error before this is
// reached, and division converts to a float type first.
func binBool(op binCode, d, x, y []bool) {
x, y = x[:len(d)], y[:len(d)]
switch op {
case opAdd, opMax:
for i := range d {
d[i] = x[i] || y[i]
}
case opMul, opMin:
for i := range d {
d[i] = x[i] && y[i]
}
}
}
// cmpOrdered compares real values.
func cmpOrdered[T realT](op cmpCode, d []bool, x, y []T) {
x, y = x[:len(d)], y[:len(d)]
switch op {
case opEq:
for i := range d {
d[i] = x[i] == y[i]
}
case opNe:
for i := range d {
d[i] = x[i] != y[i]
}
case opLt:
for i := range d {
d[i] = x[i] < y[i]
}
case opLe:
for i := range d {
d[i] = x[i] <= y[i]
}
case opGt:
for i := range d {
d[i] = x[i] > y[i]
}
case opGe:
for i := range d {
d[i] = x[i] >= y[i]
}
}
}
// cmpComplex compares complex values: equality componentwise, order
// lexicographic as NumPy defines it (false when either side has a NaN).
func cmpComplex[T complexT](op cmpCode, d []bool, x, y []T) {
x, y = x[:len(d)], y[:len(d)]
for i := range d {
a, b := complex128(x[i]), complex128(y[i])
nan := cmplx.IsNaN(a) || cmplx.IsNaN(b)
switch op {
case opEq:
d[i] = a == b
case opNe:
d[i] = a != b
case opLt:
d[i] = !nan && lessComplex(a, b)
case opLe:
d[i] = !nan && !lessComplex(b, a)
case opGt:
d[i] = !nan && lessComplex(b, a)
case opGe:
d[i] = !nan && !lessComplex(a, b)
}
}
}
// cmpBool compares booleans, with false < true.
func cmpBool(op cmpCode, d []bool, x, y []bool) {
x, y = x[:len(d)], y[:len(d)]
for i := range d {
a, b := x[i], y[i]
switch op {
case opEq:
d[i] = a == b
case opNe:
d[i] = a != b
case opLt:
d[i] = !a && b
case opLe:
d[i] = !a || b
case opGt:
d[i] = a && !b
case opGe:
d[i] = a || !b
}
}
}
// unaryCode selects a unary operation on an inexact type.
type unaryCode uint8
const (
uSqrt unaryCode = iota
uExp
uLog
uLog2
uLog10
uSin
uCos
uTan
uFloor
uCeil
uRound
uSquare
uNeg
uAbs
uPow
uRoundHalfAway
)
// realFuncs are the float64 functions behind the unary codes.
var realFuncs = [...]func(float64) float64{
uSqrt: math.Sqrt, uExp: math.Exp, uLog: math.Log, uLog2: log2,
uLog10: math.Log10, uSin: math.Sin, uCos: math.Cos, uTan: math.Tan,
uFloor: math.Floor, uCeil: math.Ceil, uRound: math.RoundToEven,
uRoundHalfAway: math.Round,
uSquare: func(x float64) float64 { return x * x },
uNeg: func(x float64) float64 { return -x },
uAbs: math.Abs,
}
// complexFuncs are the complex128 functions behind the unary codes; nil where
// NumPy does not define the operation on complex numbers.
var complexFuncs = [...]func(complex128) complex128{
uSqrt: cmplx.Sqrt, uExp: cmplx.Exp, uLog: cmplx.Log,
uLog2: func(z complex128) complex128 { return cmplx.Log(z) / math.Ln2 },
uLog10: cmplx.Log10, uSin: cmplx.Sin, uCos: cmplx.Cos, uTan: cmplx.Tan,
uFloor: nil, uCeil: nil,
uRound: func(z complex128) complex128 { return complex(math.RoundToEven(real(z)), math.RoundToEven(imag(z))) },
uRoundHalfAway: func(z complex128) complex128 { return complex(math.Round(real(z)), math.Round(imag(z))) },
uSquare: func(z complex128) complex128 { return z * z },
uNeg: func(z complex128) complex128 { return -z },
uAbs: nil, // complex abs is real-valued: see absStore
}
// unaryFloat32 applies f, computed in float64 and rounded, to float32
// elements. For the correctly rounded operations (sqrt, floor, ceil, round,
// negation, abs, square) this is exactly the float32 result: float64 carries
// more than twice float32's precision, so the second rounding cannot change it.
func unaryFloat32(f func(float64) float64, d, x []float32) {
x = x[:len(d)]
for i := range d {
d[i] = float32(f(float64(x[i])))
}
}
// unaryComplex applies f to complex elements, computing complex64 in
// complex128.
func unaryComplex[T complexT](f func(complex128) complex128, d, x []T) {
x = x[:len(d)]
for i := range d {
d[i] = T(f(complex128(x[i])))
}
}
// powFloat32 raises float32 elements to p, computed in float64.
func powFloat32(p float64, d, x []float32) {
for i := range d {
d[i] = float32(math.Pow(float64(x[i]), p))
}
}
// powComplex raises complex elements to p.
func powComplex[T complexT](p complex128, d, x []T) {
for i := range d {
d[i] = T(cmplx.Pow(complex128(x[i]), p))
}
}
// negInt negates integers, wrapping as Go and NumPy do.
func negInt[T intT](d, x []T) {
for i := range d {
d[i] = -x[i]
}
}
// absInt is the absolute value of integers; the minimum of a signed type is
// its own absolute value, as in NumPy.
func absInt[T intT](d, x []T) {
for i := range d {
v := x[i]
if v < 0 {
v = -v
}
d[i] = v
}
}
// squareInt squares integers, wrapping.
func squareInt[T intT](d, x []T) {
for i := range d {
d[i] = x[i] * x[i]
}
}
// absComplex is the modulus of complex elements, into the real type of the
// same precision.
func absComplex[T complexT, R floatT](d []R, x []T) {
for i := range d {
d[i] = R(cmplx.Abs(complex128(x[i])))
}
}
// Reductions over the [outer][axisLen][inner] layout the axis reductions use:
// dst[o*inner+j] combines src[(o*axisLen+k)*inner+j] over k. axisLen > 0.
// reduceCode selects a reduction.
type reduceCode uint8
const (
rSum reduceCode = iota
rProd
rMax
rMin
)
// reduceNum sums or multiplies along the axis.
func reduceNum[T numT](op reduceCode, dst, src []T, outer, axisLen, inner int) {
if inner == 1 {
// The reduced axis is contiguous (the last axis, or the whole
// array): fold each run directly. The loop below would take a
// slice and run a one-iteration loop per element there, 6.8 ns an
// element for a float32 sum on POWER9.
for o := 0; o < outer; o++ {
dst[o] = foldRun(op, src[o*axisLen:(o+1)*axisLen])
}
return
}
for o := 0; o < outer; o++ {
row := dst[o*inner : (o+1)*inner]
copy(row, src[o*axisLen*inner:(o*axisLen+1)*inner])
for k := 1; k < axisLen; k++ {
s := src[(o*axisLen+k)*inner : (o*axisLen+k+1)*inner]
if op == rSum {
for j := range row {
row[j] += s[j]
}
} else {
for j := range row {
row[j] *= s[j]
}
}
}
}
}
// foldRun reduces one contiguous run (len(s) > 0) as NumPy's add and
// multiply reductions do: the result starts as the first element and the
// rest is added to it, pairwise for a floating sum (pairwiseSum), in order
// otherwise. Integer sums are exact in any order, and NumPy multiplies in
// order.
func foldRun[T numT](op reduceCode, s []T) T {
acc := s[0]
rest := s[1:]
if op == rSum {
switch r := any(rest).(type) {
case []float32: // Float64 sums never come here: they have kernels
return any(any(acc).(float32) + pairwiseSum(r)).(T)
case []complex64:
return any(any(acc).(complex64) + pairwiseSumComplex(r)).(T)
case []complex128:
return any(any(acc).(complex128) + pairwiseSumComplex(r)).(T)
}
for _, v := range rest {
acc += v
}
return acc
}
for _, v := range rest {
acc *= v
}
return acc
}
// foldWidened sums or multiplies each contiguous run of src (bools or
// integers narrower than 64 bits) into dst, NumPy's accumulator for them:
// []int64 for bools and signed integers, []uint64 for unsigned. Each element
// is widened as it is read; the arithmetic, wrap-around included, is that of
// converting first and folding after.
func foldWidened(op reduceCode, dst, src any, outer, axisLen int) {
switch s := src.(type) {
case []bool:
d := dst.([]int64)
for o := range outer {
var acc int64 = 1 // a product of bools is 1 unless one is false
if op == rSum {
acc = 0
}
for _, v := range s[o*axisLen : (o+1)*axisLen] {
switch {
case op == rSum && v:
acc++
case op == rProd && !v:
acc = 0
}
}
d[o] = acc
}
case []int8:
widenRuns(op, dst.([]int64), s, outer, axisLen)
case []int16:
widenRuns(op, dst.([]int64), s, outer, axisLen)
case []int32:
widenRuns(op, dst.([]int64), s, outer, axisLen)
case []uint8:
widenRuns(op, dst.([]uint64), s, outer, axisLen)
case []uint16:
widenRuns(op, dst.([]uint64), s, outer, axisLen)
case []uint32:
widenRuns(op, dst.([]uint64), s, outer, axisLen)
}
}
func widenRuns[S int8 | int16 | int32 | uint8 | uint16 | uint32, D int64 | uint64](op reduceCode, dst []D, src []S, outer, axisLen int) {
for o := range outer {
run := src[o*axisLen : (o+1)*axisLen]
if op == rSum {
dst[o] = sumWiden[S, D](run)
} else {
dst[o] = prodWiden[S, D](run)
}
}
}
// sumWiden and prodWiden fold a run with four accumulators. Integer addition
// and multiplication wrap modulo 2^64, so they are associative and the order
// changes nothing: the result is the in-order fold's, exactly. One loop per
// operation, so the operation is not re-read per element (on amd64 the
// single loop reloaded it from the stack every iteration), and four
// independent chains, so the loop is no longer one dependent add an element:
// it had run 50% slower on a Zen 3 just by being moved across a 64-byte line.
//
// The loops consume the slice (s = s[4:] under len(s) >= 4) rather than index
// it at i+1..i+3: with indices, the compiler proved none of them in range and
// emitted four bounds checks an iteration (amd64 disassembly); this way it
// proves all four from the loop condition and emits none.
func sumWiden[S int8 | int16 | int32 | uint8 | uint16 | uint32, D int64 | uint64](s []S) D {
var a0, a1, a2, a3 D
for len(s) >= 4 {
a0 += D(s[0])
a1 += D(s[1])
a2 += D(s[2])
a3 += D(s[3])
s = s[4:]
}
for _, v := range s {
a0 += D(v)
}
return (a0 + a1) + (a2 + a3)
}
func prodWiden[S int8 | int16 | int32 | uint8 | uint16 | uint32, D int64 | uint64](s []S) D {
var a0, a1, a2, a3 D = 1, 1, 1, 1
for len(s) >= 4 {
a0 *= D(s[0])
a1 *= D(s[1])
a2 *= D(s[2])
a3 *= D(s[3])
s = s[4:]
}
for _, v := range s {
a0 *= D(v)
}
return (a0 * a1) * (a2 * a3)
}
// pwBlock is NumPy's PW_BLOCKSIZE: runs up to this length are summed with
// eight accumulators, longer ones are halved.
const pwBlock = 128
// pairwiseSum is NumPy's FLOAT_pairwise_sum
// (numpy/_core/src/umath/loops_utils.h.src, v2.5.3): rounding error O(lg n)
// instead of O(n), in the same order of additions.
//
// The eight accumulators are fed by kernels.AddBlocks8F32, a vector kernel on
// amd64 and arm64 that adds the same lanes in the same order, so the sums are
// the scalar loop's bit for bit.
func pairwiseSum(a []float32) float32 {
n := len(a)
switch {
case n < 8:
// From -0, so that a sum of only -0 stays -0.
res := float32(math.Copysign(0, -1))
for _, v := range a {
res += v
}
return res
case n <= pwBlock:
var r [8]float32
copy(r[:], a[:8])
i := n - n%8
kernels.AddBlocks8F32(&r, a[8:i])
res := ((r[0] + r[1]) + (r[2] + r[3])) + ((r[4] + r[5]) + (r[6] + r[7]))
for ; i < n; i++ {
res += a[i]
}
return res
}
n2 := n / 2
n2 -= n2 % 8
return pairwiseSum(a[:n2]) + pairwiseSum(a[n2:])
}
// pairwiseSumComplex is NumPy's CFLOAT/CDOUBLE_pairwise_sum: the same scheme
// over the interleaved real and imaginary parts, counted in floats (two per
// element), so four accumulators per part and blocks of 64 elements.
func pairwiseSumComplex[C complex64 | complex128](a []C) C {
n := 2 * len(a) // NumPy's n counts floats
switch {
case n < 8:
re, im := math.Copysign(0, -1), math.Copysign(0, -1)
var acc C = C(complex(re, im))
for _, v := range a {
acc += v
}
return acc
case n <= pwBlock:
// r[0], r[2], r[4], r[6] hold real parts and r[1], r[3], r[5], r[7]
// imaginary parts, of elements 0..3 of each block of four.
var r [4]C
copy(r[:], a[:4])
i := 4
for ; i < len(a)-len(a)%4; i += 4 {
r[0] += a[i]
r[1] += a[i+1]
r[2] += a[i+2]
r[3] += a[i+3]
}
res := (r[0] + r[1]) + (r[2] + r[3])
for ; i < len(a); i++ {
res += a[i]
}
return res
}
n2 := n / 2
n2 -= n2 % 8
return pairwiseSumComplex(a[:n2/2]) + pairwiseSumComplex(a[n2/2:])
}
// reduceOrdered takes the maximum or minimum along the axis; for floats a NaN
// wins, as in NumPy.
func reduceOrdered[T realT](op reduceCode, dst, src []T, outer, axisLen, inner int) {
for o := 0; o < outer; o++ {
row := dst[o*inner : (o+1)*inner]
copy(row, src[o*axisLen*inner:(o*axisLen+1)*inner])
for k := 1; k < axisLen; k++ {
s := src[(o*axisLen+k)*inner : (o*axisLen+k+1)*inner]
if op == rMax {
for j := range row {
row[j] = max(row[j], s[j])
}
} else {
for j := range row {
row[j] = min(row[j], s[j])
}
}
}
}
}
// reduceComplex takes the lexicographic maximum or minimum along the axis.
func reduceComplex[T complexT](op reduceCode, dst, src []T, outer, axisLen, inner int) {
for o := 0; o < outer; o++ {
row := dst[o*inner : (o+1)*inner]
copy(row, src[o*axisLen*inner:(o*axisLen+1)*inner])
for k := 1; k < axisLen; k++ {
s := src[(o*axisLen+k)*inner : (o*axisLen+k+1)*inner]
for j := range row {
row[j] = pickComplex(row[j], s[j], op == rMax)
}
}
}
}
// reduceBool is "any" for the maximum and "all" for the minimum.
func reduceBool(op reduceCode, dst, src []bool, outer, axisLen, inner int) {
for o := 0; o < outer; o++ {
row := dst[o*inner : (o+1)*inner]
copy(row, src[o*axisLen*inner:(o*axisLen+1)*inner])
for k := 1; k < axisLen; k++ {
s := src[(o*axisLen+k)*inner : (o*axisLen+k+1)*inner]
for j := range row {
if op == rMax {
row[j] = row[j] || s[j]
} else {
row[j] = row[j] && s[j]
}
}
}
}
}
// argOrdered writes the index of the first maximum (or minimum) along the
// axis; for floats the first NaN wins, as in NumPy.
func argOrdered[T realT](larger bool, dst []int64, src []T, outer, axisLen, inner int) {
for o := 0; o < outer; o++ {
for j := 0; j < inner; j++ {
best, bi := src[o*axisLen*inner+j], 0
for k := 1; k < axisLen && best == best; k++ {
v := src[(o*axisLen+k)*inner+j]
if v != v || (larger && v > best) || (!larger && v < best) {
best, bi = v, k
}
}
dst[o*inner+j] = int64(bi)
}
}
}
// argComplex is argOrdered in NumPy's lexicographic complex order.
func argComplex[T complexT](larger bool, dst []int64, src []T, outer, axisLen, inner int) {
for o := 0; o < outer; o++ {
for j := 0; j < inner; j++ {
best, bi := complex128(src[o*axisLen*inner+j]), 0
for k := 1; k < axisLen && !cmplx.IsNaN(best); k++ {
v := complex128(src[(o*axisLen+k)*inner+j])
if cmplx.IsNaN(v) || (larger && lessComplex(best, v)) || (!larger && lessComplex(v, best)) {
best, bi = v, k
}
}
dst[o*inner+j] = int64(bi)
}
}
}
// argBool is argOrdered with false < true.
func argBool(larger bool, dst []int64, src []bool, outer, axisLen, inner int) {
for o := 0; o < outer; o++ {
for j := 0; j < inner; j++ {
bi := 0
for k := 0; k < axisLen; k++ {
if src[(o*axisLen+k)*inner+j] == larger {
bi = k
break
}
}
dst[o*inner+j] = int64(bi)
}
}
}
// scanNum is the cumulative sum or product along the axis; dst has src's
// layout.
func scanNum[T numT](op reduceCode, dst, src []T, outer, axisLen, inner int) {
for o := 0; o < outer; o++ {
base := o * axisLen * inner
copy(dst[base:base+inner], src[base:base+inner])
for k := 1; k < axisLen; k++ {
prev := dst[base+(k-1)*inner : base+k*inner]
cur := dst[base+k*inner : base+(k+1)*inner]
s := src[base+k*inner : base+(k+1)*inner]
for j := range cur {
if op == rSum {
cur[j] = prev[j] + s[j]
} else {
cur[j] = prev[j] * s[j]
}
}
}
}
}
// clipOrdered limits real elements to [lo, hi]; NaN stays NaN, as in NumPy.
func clipOrdered[T realT](d, x []T, lo, hi T) {
for i := range d {
d[i] = min(max(x[i], lo), hi)
}
}
// whereStore picks t where c is true and f otherwise.
func whereT[T elemT](d []T, c []bool, t, f []T) {
for i := range d {
if c[i] {
d[i] = t[i]
} else {
d[i] = f[i]
}
}
}
// matmulNum multiplies an (m x k) by a (k x n) row-major matrix into d.
func matmulNum[T numT](d, x, y []T, m, k, n int) {
for i := 0; i < m; i++ {
row := d[i*n : (i+1)*n]
for p := 0; p < k; p++ {
xv := x[i*k+p]
yr := y[p*n : (p+1)*n]
for j := range row {
row[j] += xv * yr[j]
}
}
}
}
// matmulBool is the matrix product over the booleans: "or" of "and"s.
func matmulBool(d, x, y []bool, m, k, n int) {
for i := 0; i < m; i++ {
for p := 0; p < k; p++ {
if !x[i*k+p] {
continue
}
for j := 0; j < n; j++ {
d[i*n+j] = d[i*n+j] || y[p*n+j]
}
}
}
}