Skip to content

Commit 9850150

Browse files
Restore elementwise min/max on SIMD, with explicit naming. (#24136)
* 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 4287afc commit 9850150

File tree

1 file changed

+54
-25
lines changed

1 file changed

+54
-25
lines changed

stdlib/public/core/SIMDVector.swift

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -467,17 +467,15 @@ extension SIMD where Scalar: Comparable {
467467
return lhs .> Self(repeating: rhs)
468468
}
469469

470-
/* Temporarily removed pending plan for Swift.min / Swift.max
471470
@_alwaysEmitIntoClient
472471
public mutating func clamp(lowerBound: Self, upperBound: Self) {
473472
self = self.clamped(lowerBound: lowerBound, upperBound: upperBound)
474473
}
475474

476475
@_alwaysEmitIntoClient
477476
public func clamped(lowerBound: Self, upperBound: Self) -> Self {
478-
return Swift.min(upperBound, Swift.max(lowerBound, self))
477+
return pointwiseMin(upperBound, pointwiseMax(lowerBound, self))
479478
}
480-
*/
481479
}
482480

483481
extension SIMD where Scalar: FixedWidthInteger {
@@ -550,6 +548,42 @@ extension SIMD where Scalar: FloatingPoint {
550548
public static var one: Self {
551549
return Self(repeating: 1)
552550
}
551+
552+
/// The lanewise minimum of two vectors.
553+
///
554+
/// Each element of the result is the minimum of the corresponding elements
555+
/// of the inputs.
556+
@_alwaysEmitIntoClient
557+
public static func min(_ a: Self, _ b: Self) -> Self {
558+
var result = Self()
559+
for i in result.indices {
560+
result[i] = Scalar.minimum(a[i], b[i])
561+
}
562+
return result
563+
}
564+
565+
/// The lanewise maximum of two vectors.
566+
///
567+
/// Each element of the result is the minimum of the corresponding elements
568+
/// of the inputs.
569+
@_alwaysEmitIntoClient
570+
public static func max(_ a: Self, _ b: Self) -> Self {
571+
var result = Self()
572+
for i in result.indices {
573+
result[i] = Scalar.maximum(a[i], b[i])
574+
}
575+
return result
576+
}
577+
578+
@_alwaysEmitIntoClient
579+
public mutating func clamp(lowerBound: Self, upperBound: Self) {
580+
self = self.clamped(lowerBound: lowerBound, upperBound: upperBound)
581+
}
582+
583+
@_alwaysEmitIntoClient
584+
public func clamped(lowerBound: Self, upperBound: Self) -> Self {
585+
return pointwiseMin(upperBound, pointwiseMax(lowerBound, self))
586+
}
553587
}
554588

555589
extension SIMD
@@ -1343,34 +1377,30 @@ public func all<Storage>(_ mask: SIMDMask<Storage>) -> Bool {
13431377
return mask._storage.max() < 0
13441378
}
13451379

1346-
/*
1347-
Temporarily removed while we investigate compile-time regressions caused by
1348-
introducing these global functions.
1349-
13501380
/// The lanewise minimum of two vectors.
13511381
///
13521382
/// Each element of the result is the minimum of the corresponding elements
13531383
/// of the inputs.
13541384
@_alwaysEmitIntoClient
1355-
public func min<V>(_ lhs: V, _ rhs: V) -> V
1356-
where V: SIMD, V.Scalar: Comparable {
1357-
var result = V()
1385+
public func pointwiseMin<T>(_ a: T, _ b: T) -> T
1386+
where T: SIMD, T.Scalar: Comparable {
1387+
var result = T()
13581388
for i in result.indices {
1359-
result[i] = min(lhs[i], rhs[i])
1389+
result[i] = min(a[i], b[i])
13601390
}
13611391
return result
13621392
}
13631393

13641394
/// The lanewise maximum of two vectors.
13651395
///
1366-
/// Each element of the result is the maximum of the corresponding elements
1396+
/// Each element of the result is the minimum of the corresponding elements
13671397
/// of the inputs.
13681398
@_alwaysEmitIntoClient
1369-
public func max<V>(_ lhs: V, _ rhs: V) -> V
1370-
where V: SIMD, V.Scalar: Comparable {
1371-
var result = V()
1399+
public func pointwiseMax<T>(_ a: T, _ b: T) -> T
1400+
where T: SIMD, T.Scalar: Comparable {
1401+
var result = T()
13721402
for i in result.indices {
1373-
result[i] = max(lhs[i], rhs[i])
1403+
result[i] = max(a[i], b[i])
13741404
}
13751405
return result
13761406
}
@@ -1381,11 +1411,11 @@ where V: SIMD, V.Scalar: Comparable {
13811411
/// Each element of the result is the minimum of the corresponding elements
13821412
/// of the inputs.
13831413
@_alwaysEmitIntoClient
1384-
public func min<V>(_ lhs: V, _ rhs: V) -> V
1385-
where V: SIMD, V.Scalar: FloatingPoint {
1386-
var result = V()
1414+
public func pointwiseMin<T>(_ a: T, _ b: T) -> T
1415+
where T: SIMD, T.Scalar: FloatingPoint {
1416+
var result = T()
13871417
for i in result.indices {
1388-
result[i] = V.Scalar.minimum(lhs[i], rhs[i])
1418+
result[i] = T.Scalar.minimum(a[i], b[i])
13891419
}
13901420
return result
13911421
}
@@ -1395,12 +1425,11 @@ where V: SIMD, V.Scalar: FloatingPoint {
13951425
/// Each element of the result is the maximum of the corresponding elements
13961426
/// of the inputs.
13971427
@_alwaysEmitIntoClient
1398-
public func max<V>(_ lhs: V, _ rhs: V) -> V
1399-
where V: SIMD, V.Scalar: FloatingPoint {
1400-
var result = V()
1428+
public func pointwiseMax<T>(_ a: T, _ b: T) -> T
1429+
where T: SIMD, T.Scalar: FloatingPoint {
1430+
var result = T()
14011431
for i in result.indices {
1402-
result[i] = V.Scalar.maximum(lhs[i], rhs[i])
1432+
result[i] = T.Scalar.maximum(a[i], b[i])
14031433
}
14041434
return result
14051435
}
1406-
*/

0 commit comments

Comments
 (0)