Skip to content

Commit 16f6074

Browse files
[5.1] Restore elementwise min/max on SIMD (#24772)
* 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. * Really remove static min and max on simd. One slipped through. (#24283)
1 parent 2eb737d commit 16f6074

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

stdlib/public/core/SIMDVector.swift

Lines changed: 28 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,16 @@ extension SIMD where Scalar: FloatingPoint {
550548
public static var one: Self {
551549
return Self(repeating: 1)
552550
}
551+
552+
@_alwaysEmitIntoClient
553+
public mutating func clamp(lowerBound: Self, upperBound: Self) {
554+
self = self.clamped(lowerBound: lowerBound, upperBound: upperBound)
555+
}
556+
557+
@_alwaysEmitIntoClient
558+
public func clamped(lowerBound: Self, upperBound: Self) -> Self {
559+
return pointwiseMin(upperBound, pointwiseMax(lowerBound, self))
560+
}
553561
}
554562

555563
extension SIMD
@@ -1343,34 +1351,30 @@ public func all<Storage>(_ mask: SIMDMask<Storage>) -> Bool {
13431351
return mask._storage.max() < 0
13441352
}
13451353

1346-
/*
1347-
Temporarily removed while we investigate compile-time regressions caused by
1348-
introducing these global functions.
1349-
13501354
/// The lanewise minimum of two vectors.
13511355
///
13521356
/// Each element of the result is the minimum of the corresponding elements
13531357
/// of the inputs.
13541358
@_alwaysEmitIntoClient
1355-
public func min<V>(_ lhs: V, _ rhs: V) -> V
1356-
where V: SIMD, V.Scalar: Comparable {
1357-
var result = V()
1359+
public func pointwiseMin<T>(_ a: T, _ b: T) -> T
1360+
where T: SIMD, T.Scalar: Comparable {
1361+
var result = T()
13581362
for i in result.indices {
1359-
result[i] = min(lhs[i], rhs[i])
1363+
result[i] = min(a[i], b[i])
13601364
}
13611365
return result
13621366
}
13631367

13641368
/// The lanewise maximum of two vectors.
13651369
///
1366-
/// Each element of the result is the maximum of the corresponding elements
1370+
/// Each element of the result is the minimum of the corresponding elements
13671371
/// of the inputs.
13681372
@_alwaysEmitIntoClient
1369-
public func max<V>(_ lhs: V, _ rhs: V) -> V
1370-
where V: SIMD, V.Scalar: Comparable {
1371-
var result = V()
1373+
public func pointwiseMax<T>(_ a: T, _ b: T) -> T
1374+
where T: SIMD, T.Scalar: Comparable {
1375+
var result = T()
13721376
for i in result.indices {
1373-
result[i] = max(lhs[i], rhs[i])
1377+
result[i] = max(a[i], b[i])
13741378
}
13751379
return result
13761380
}
@@ -1381,11 +1385,11 @@ where V: SIMD, V.Scalar: Comparable {
13811385
/// Each element of the result is the minimum of the corresponding elements
13821386
/// of the inputs.
13831387
@_alwaysEmitIntoClient
1384-
public func min<V>(_ lhs: V, _ rhs: V) -> V
1385-
where V: SIMD, V.Scalar: FloatingPoint {
1386-
var result = V()
1388+
public func pointwiseMin<T>(_ a: T, _ b: T) -> T
1389+
where T: SIMD, T.Scalar: FloatingPoint {
1390+
var result = T()
13871391
for i in result.indices {
1388-
result[i] = V.Scalar.minimum(lhs[i], rhs[i])
1392+
result[i] = T.Scalar.minimum(a[i], b[i])
13891393
}
13901394
return result
13911395
}
@@ -1395,12 +1399,11 @@ where V: SIMD, V.Scalar: FloatingPoint {
13951399
/// Each element of the result is the maximum of the corresponding elements
13961400
/// of the inputs.
13971401
@_alwaysEmitIntoClient
1398-
public func max<V>(_ lhs: V, _ rhs: V) -> V
1399-
where V: SIMD, V.Scalar: FloatingPoint {
1400-
var result = V()
1402+
public func pointwiseMax<T>(_ a: T, _ b: T) -> T
1403+
where T: SIMD, T.Scalar: FloatingPoint {
1404+
var result = T()
14011405
for i in result.indices {
1402-
result[i] = V.Scalar.maximum(lhs[i], rhs[i])
1406+
result[i] = T.Scalar.maximum(a[i], b[i])
14031407
}
14041408
return result
14051409
}
1406-
*/

0 commit comments

Comments
 (0)