Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore elementwise min/max on SIMD, with explicit naming. #24136

Merged
merged 4 commits into from
Apr 25, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 55 additions & 26 deletions stdlib/public/core/SIMDVector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,17 +467,15 @@ extension SIMD where Scalar: Comparable {
return lhs .> Self(repeating: rhs)
}

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

@_alwaysEmitIntoClient
public func clamped(lowerBound: Self, upperBound: Self) -> Self {
return Swift.min(upperBound, Swift.max(lowerBound, self))
return pointwiseMin(upperBound, pointwiseMax(lowerBound, self))
}
*/
}

extension SIMD where Scalar: FixedWidthInteger {
Expand Down Expand Up @@ -550,6 +548,42 @@ extension SIMD where Scalar: FloatingPoint {
public static var one: Self {
return Self(repeating: 1)
}

/// The lanewise minimum of two vectors.
///
/// Each element of the result is the minimum of the corresponding elements
/// of the inputs.
@_alwaysEmitIntoClient
public static func min(_ a: Self, _ b: Self) -> Self {
var result = Self()
for i in result.indices {
result[i] = Scalar.minimum(a[i], b[i])
}
return result
}

/// The lanewise maximum of two vectors.
///
/// Each element of the result is the minimum of the corresponding elements
/// of the inputs.
@_alwaysEmitIntoClient
public static func max(_ a: Self, _ b: Self) -> Self {
var result = Self()
for i in result.indices {
result[i] = Scalar.maximum(a[i], b[i])
}
return result
}

@_alwaysEmitIntoClient
public mutating func clamp(lowerBound: Self, upperBound: Self) {
self = self.clamped(lowerBound: lowerBound, upperBound: upperBound)
}

@_alwaysEmitIntoClient
public func clamped(lowerBound: Self, upperBound: Self) -> Self {
return pointwiseMin(upperBound, pointwiseMax(lowerBound, self))
}
}

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

/*
Temporarily removed while we investigate compile-time regressions caused by
introducing these global functions.

/// The lanewise minimum of two vectors.
///
/// Each element of the result is the minimum of the corresponding elements
/// of the inputs.
@_alwaysEmitIntoClient
public func min<V>(_ lhs: V, _ rhs: V) -> V
where V: SIMD, V.Scalar: Comparable {
var result = V()
public func pointwiseMin<T>(_ a: T, _ b: T) -> T
where T: SIMD, T.Scalar: Comparable {
var result = T()
for i in result.indices {
result[i] = min(lhs[i], rhs[i])
result[i] = min(a[i], b[i])
}
return result
}

/// The lanewise maximum of two vectors.
///
/// Each element of the result is the maximum of the corresponding elements
/// Each element of the result is the minimum of the corresponding elements
/// of the inputs.
@_alwaysEmitIntoClient
public func max<V>(_ lhs: V, _ rhs: V) -> V
where V: SIMD, V.Scalar: Comparable {
var result = V()
public func pointwiseMax<T>(_ a: T, _ b: T) -> T
where T: SIMD, T.Scalar: Comparable {
var result = T()
for i in result.indices {
result[i] = max(lhs[i], rhs[i])
result[i] = max(a[i], b[i])
}
return result
}
Expand All @@ -1381,26 +1411,25 @@ where V: SIMD, V.Scalar: Comparable {
/// Each element of the result is the minimum of the corresponding elements
/// of the inputs.
@_alwaysEmitIntoClient
public func min<V>(_ lhs: V, _ rhs: V) -> V
where V: SIMD, V.Scalar: FloatingPoint {
var result = V()
public func pointwiseMin<T>(_ a: T, _ b: T) -> T
where T: SIMD, T.Scalar: FloatingPoint {
var result = T()
for i in result.indices {
result[i] = V.Scalar.minimum(lhs[i], rhs[i])
result[i] = T.Scalar.minimum(a[i], b[i])
}
return result
}

/// The lanewise maximum of two vectors.
///
/// Each element of the result is the maximum of the corresponding elements
/// Each element of the result is the minimum of the corresponding elements
stephentyrone marked this conversation as resolved.
Show resolved Hide resolved
/// of the inputs.
@_alwaysEmitIntoClient
public func max<V>(_ lhs: V, _ rhs: V) -> V
where V: SIMD, V.Scalar: FloatingPoint {
var result = V()
public func pointwiseMax<T>(_ a: T, _ b: T) -> T
where T: SIMD, T.Scalar: FloatingPoint {
var result = T()
for i in result.indices {
result[i] = V.Scalar.maximum(lhs[i], rhs[i])
result[i] = T.Scalar.maximum(a[i], b[i])
}
return result
}
*/