Skip to content

Commit 4b94685

Browse files
committed
Restore elementwise min/max on SIMD, but as statics instead of free functions.
Having these as free functions causes expression too complex issues due to overload vis-a-vis min<Collection>. There are (at least) three plausible solutions: 1. Fix the typechecker. This is infeasable in the short term; or more precisely, we do not know how much work is involved. 2. Give these operations different names. Candidates discussed with core team include "pointwiseMin", "elementwiseMin", "lanewiseMin"; these all suffer from the flaw that when someone writes "min" in a SIMD context, they are essentially always looking for either the horizontal minimum (reduction) on a single vector or--more often--the lanewise minimum of two vectors (this operation). It would be odd to give the operation that people actually want the unnecessarily verbose name. 3. Make these operations static; this is, in effect, a different name, but it's one which frequently allows eliding the qualifier: let x = v + .min(v, w) This isn't perfect; you will still need to spell out SIMD4.min( ) fairly often, but that's more concise than any of the proposed alternatives, and at least allows elision some of the time. Also, if you squint, you can pretend that the "." prefix is like ".<" and ".&" and indicates lanewise operation.
1 parent 8c22a1a commit 4b94685

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed

stdlib/public/core/SIMDVector.swift

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,32 @@ extension SIMD where Scalar: Comparable {
345345
public func max() -> Scalar {
346346
return indices.reduce(into: self[0]) { $0 = Swift.max($0, self[$1]) }
347347
}
348+
349+
/// The lanewise minimum of two vectors.
350+
///
351+
/// Each element of the result is the minimum of the corresponding elements
352+
/// of the inputs.
353+
@_alwaysEmitIntoClient
354+
public static func min(_ a: Self, _ b: Self) -> Self {
355+
var result = Self()
356+
for i in result.indices {
357+
result[i] = Swift.min(a[i], b[i])
358+
}
359+
return result
360+
}
361+
362+
/// The lanewise maximum of two vectors.
363+
///
364+
/// Each element of the result is the minimum of the corresponding elements
365+
/// of the inputs.
366+
@_alwaysEmitIntoClient
367+
public static func max(_ a: Self, _ b: Self) -> Self {
368+
var result = Self()
369+
for i in result.indices {
370+
result[i] = Swift.max(a[i], b[i])
371+
}
372+
return result
373+
}
348374
}
349375

350376
// These operations should never need @_semantics; they should be trivial
@@ -467,17 +493,15 @@ extension SIMD where Scalar: Comparable {
467493
return lhs .> Self(repeating: rhs)
468494
}
469495

470-
/* Temporarily removed pending plan for Swift.min / Swift.max
471496
@_alwaysEmitIntoClient
472497
public mutating func clamp(lowerBound: Self, upperBound: Self) {
473498
self = self.clamped(lowerBound: lowerBound, upperBound: upperBound)
474499
}
475500

476501
@_alwaysEmitIntoClient
477502
public func clamped(lowerBound: Self, upperBound: Self) -> Self {
478-
return Swift.min(upperBound, Swift.max(lowerBound, self))
503+
return Self.min(upperBound, Self.max(lowerBound, self))
479504
}
480-
*/
481505
}
482506

483507
extension SIMD where Scalar: FixedWidthInteger {
@@ -550,6 +574,42 @@ extension SIMD where Scalar: FloatingPoint {
550574
public static var one: Self {
551575
return Self(repeating: 1)
552576
}
577+
578+
/// The lanewise minimum of two vectors.
579+
///
580+
/// Each element of the result is the minimum of the corresponding elements
581+
/// of the inputs.
582+
@_alwaysEmitIntoClient
583+
public static func min(_ a: Self, _ b: Self) -> Self {
584+
var result = Self()
585+
for i in result.indices {
586+
result[i] = Scalar.minimum(a[i], b[i])
587+
}
588+
return result
589+
}
590+
591+
/// The lanewise maximum of two vectors.
592+
///
593+
/// Each element of the result is the minimum of the corresponding elements
594+
/// of the inputs.
595+
@_alwaysEmitIntoClient
596+
public static func max(_ a: Self, _ b: Self) -> Self {
597+
var result = Self()
598+
for i in result.indices {
599+
result[i] = Scalar.maximum(a[i], b[i])
600+
}
601+
return result
602+
}
603+
604+
@_alwaysEmitIntoClient
605+
public mutating func clamp(lowerBound: Self, upperBound: Self) {
606+
self = self.clamped(lowerBound: lowerBound, upperBound: upperBound)
607+
}
608+
609+
@_alwaysEmitIntoClient
610+
public func clamped(lowerBound: Self, upperBound: Self) -> Self {
611+
return Self.min(upperBound, Self.max(lowerBound, self))
612+
}
553613
}
554614

555615
extension SIMD
@@ -1342,65 +1402,3 @@ public func any<Storage>(_ mask: SIMDMask<Storage>) -> Bool {
13421402
public func all<Storage>(_ mask: SIMDMask<Storage>) -> Bool {
13431403
return mask._storage.max() < 0
13441404
}
1345-
1346-
/*
1347-
Temporarily removed while we investigate compile-time regressions caused by
1348-
introducing these global functions.
1349-
1350-
/// The lanewise minimum of two vectors.
1351-
///
1352-
/// Each element of the result is the minimum of the corresponding elements
1353-
/// of the inputs.
1354-
@_alwaysEmitIntoClient
1355-
public func min<V>(_ lhs: V, _ rhs: V) -> V
1356-
where V: SIMD, V.Scalar: Comparable {
1357-
var result = V()
1358-
for i in result.indices {
1359-
result[i] = min(lhs[i], rhs[i])
1360-
}
1361-
return result
1362-
}
1363-
1364-
/// The lanewise maximum of two vectors.
1365-
///
1366-
/// Each element of the result is the maximum of the corresponding elements
1367-
/// of the inputs.
1368-
@_alwaysEmitIntoClient
1369-
public func max<V>(_ lhs: V, _ rhs: V) -> V
1370-
where V: SIMD, V.Scalar: Comparable {
1371-
var result = V()
1372-
for i in result.indices {
1373-
result[i] = max(lhs[i], rhs[i])
1374-
}
1375-
return result
1376-
}
1377-
1378-
1379-
/// The lanewise minimum of two vectors.
1380-
///
1381-
/// Each element of the result is the minimum of the corresponding elements
1382-
/// of the inputs.
1383-
@_alwaysEmitIntoClient
1384-
public func min<V>(_ lhs: V, _ rhs: V) -> V
1385-
where V: SIMD, V.Scalar: FloatingPoint {
1386-
var result = V()
1387-
for i in result.indices {
1388-
result[i] = V.Scalar.minimum(lhs[i], rhs[i])
1389-
}
1390-
return result
1391-
}
1392-
1393-
/// The lanewise maximum of two vectors.
1394-
///
1395-
/// Each element of the result is the maximum of the corresponding elements
1396-
/// of the inputs.
1397-
@_alwaysEmitIntoClient
1398-
public func max<V>(_ lhs: V, _ rhs: V) -> V
1399-
where V: SIMD, V.Scalar: FloatingPoint {
1400-
var result = V()
1401-
for i in result.indices {
1402-
result[i] = V.Scalar.maximum(lhs[i], rhs[i])
1403-
}
1404-
return result
1405-
}
1406-
*/

0 commit comments

Comments
 (0)