Skip to content

Move most of the simd operators into an optional module #20914

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

Merged
merged 4 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions stdlib/public/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ if(SWIFT_BUILD_STDLIB)
add_subdirectory(runtime)
add_subdirectory(stubs)
add_subdirectory(core)
add_subdirectory(SIMDOperators)
add_subdirectory(SwiftOnoneSupport)
endif()

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/MediaPlayer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ add_swift_target_library(swiftMediaPlayer ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPE
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
TARGET_SDKS IOS IOS_SIMULATOR

SWIFT_MODULE_DEPENDS_IOS Darwin AVFoundation CoreAudio CoreFoundation CoreGraphics CoreImage CoreMedia Dispatch Foundation Metal ObjectiveC QuartzCore simd UIKit os CoreData # auto-updated
SWIFT_MODULE_DEPENDS_IOS Darwin AVFoundation CoreAudio CoreFoundation CoreGraphics CoreImage CoreMedia Dispatch Foundation Metal ObjectiveC QuartzCore simd SIMDOperators UIKit os CoreData # auto-updated
FRAMEWORK_DEPENDS_WEAK MediaPlayer

DEPLOYMENT_VERSION_IOS ${SWIFTLIB_DEPLOYMENT_VERSION_MEDIAPLAYER_IOS}
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/SDK/simd/Quaternion.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Swift
import Darwin
import SIMDOperators
@_exported import simd

%for scalar in ['Float','Double']:
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/SDK/simd/simd.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Swift
import Darwin
@_exported import SIMDOperators
@_exported import simd

public extension SIMD {
Expand Down
7 changes: 7 additions & 0 deletions stdlib/public/SIMDOperators/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_swift_target_library(swiftSIMDOperators ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
Operators.swift

SWIFT_COMPILE_FLAGS "${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}"
SWIFT_COMPILE_FLAGS
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
)
325 changes: 325 additions & 0 deletions stdlib/public/SIMDOperators/Operators.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
// Implementations of integer operations. These should eventually all
// be replaced with @_semantics to lower directly to vector IR nodes.
public extension SIMD where Scalar : FixedWidthInteger {
@_transparent
var leadingZeroBitCount: Self {
var result = Self()
for i in indices { result[i] = Scalar(self[i].leadingZeroBitCount) }
return result
}

@_transparent
var trailingZeroBitCount: Self {
var result = Self()
for i in indices { result[i] = Scalar(self[i].trailingZeroBitCount) }
return result
}

@_transparent
var nonzeroBitCount: Self {
var result = Self()
for i in indices { result[i] = Scalar(self[i].nonzeroBitCount) }
return result
}

@_transparent
static prefix func ~(rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = ~rhs[i] }
return result
}

@_transparent
static func &(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] & rhs[i] }
return result
}

@_transparent
static func ^(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] ^ rhs[i] }
return result
}

@_transparent
static func |(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] | rhs[i] }
return result
}

@_transparent
static func &<<(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] &<< rhs[i] }
return result
}

@_transparent
static func &>>(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] &>> rhs[i] }
return result
}

@_transparent
static func &+(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] &+ rhs[i] }
return result
}

@_transparent
static func &-(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] &- rhs[i] }
return result
}

@_transparent
static func &*(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] &* rhs[i] }
return result
}

@_transparent
static func /(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] / rhs[i] }
return result
}

@_transparent
static func %(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] % rhs[i] }
return result
}
}

// Implementations of floating-point operations. These should eventually all
// be replaced with @_semantics to lower directly to vector IR nodes.
public extension SIMD where Scalar : FloatingPoint {
@_transparent
static func +(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] + rhs[i] }
return result
}

@_transparent
static func -(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] - rhs[i] }
return result
}

@_transparent
static func *(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] * rhs[i] }
return result
}

@_transparent
static func /(lhs: Self, rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = lhs[i] / rhs[i] }
return result
}

@_transparent
func addingProduct(_ lhs: Self, _ rhs: Self) -> Self {
var result = Self()
for i in result.indices { result[i] = self[i].addingProduct(lhs[i], rhs[i]) }
return result
}

@_transparent
func squareRoot( ) -> Self {
var result = Self()
for i in result.indices { result[i] = self[i].squareRoot() }
return result
}

@_transparent
func rounded(_ rule: FloatingPointRoundingRule) -> Self {
var result = Self()
for i in result.indices { result[i] = self[i].rounded(rule) }
return result
}
}

public extension SIMDMask {
@_transparent
static prefix func .!(rhs: SIMDMask) -> SIMDMask {
return SIMDMask(~rhs._storage)
}

@_transparent
static func .&(lhs: SIMDMask, rhs: SIMDMask) -> SIMDMask {
return SIMDMask(lhs._storage & rhs._storage)
}

@_transparent
static func .^(lhs: SIMDMask, rhs: SIMDMask) -> SIMDMask {
return SIMDMask(lhs._storage ^ rhs._storage)
}

@_transparent
static func .|(lhs: SIMDMask, rhs: SIMDMask) -> SIMDMask {
return SIMDMask(lhs._storage | rhs._storage)
}
}

// These operations should never need @_semantics; they should be trivial
// wrappers around the core operations defined above.
public extension SIMD where Scalar : FixedWidthInteger {
@_transparent static func &(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) & rhs }
@_transparent static func ^(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) ^ rhs }
@_transparent static func |(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) | rhs }
@_transparent static func &<<(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &<< rhs }
@_transparent static func &>>(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &>> rhs }
@_transparent static func &+(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &+ rhs }
@_transparent static func &-(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &- rhs }
@_transparent static func &*(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) &* rhs }
@_transparent static func /(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) / rhs }
@_transparent static func %(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) % rhs }

@_transparent static func &(lhs: Self, rhs: Scalar) -> Self { return lhs & Self(repeating: rhs) }
@_transparent static func ^(lhs: Self, rhs: Scalar) -> Self { return lhs ^ Self(repeating: rhs) }
@_transparent static func |(lhs: Self, rhs: Scalar) -> Self { return lhs | Self(repeating: rhs) }
@_transparent static func &<<(lhs: Self, rhs: Scalar) -> Self { return lhs &<< Self(repeating: rhs) }
@_transparent static func &>>(lhs: Self, rhs: Scalar) -> Self { return lhs &>> Self(repeating: rhs) }
@_transparent static func &+(lhs: Self, rhs: Scalar) -> Self { return lhs &+ Self(repeating: rhs) }
@_transparent static func &-(lhs: Self, rhs: Scalar) -> Self { return lhs &- Self(repeating: rhs) }
@_transparent static func &*(lhs: Self, rhs: Scalar) -> Self { return lhs &* Self(repeating: rhs) }
@_transparent static func /(lhs: Self, rhs: Scalar) -> Self { return lhs / Self(repeating: rhs) }
@_transparent static func %(lhs: Self, rhs: Scalar) -> Self { return lhs % Self(repeating: rhs) }

@_transparent static func &=(lhs: inout Self, rhs: Self) { lhs = lhs & rhs }
@_transparent static func ^=(lhs: inout Self, rhs: Self) { lhs = lhs ^ rhs }
@_transparent static func |=(lhs: inout Self, rhs: Self) { lhs = lhs | rhs }
@_transparent static func &<<=(lhs: inout Self, rhs: Self) { lhs = lhs &<< rhs }
@_transparent static func &>>=(lhs: inout Self, rhs: Self) { lhs = lhs &>> rhs }
@_transparent static func &+=(lhs: inout Self, rhs: Self) { lhs = lhs &+ rhs }
@_transparent static func &-=(lhs: inout Self, rhs: Self) { lhs = lhs &- rhs }
@_transparent static func &*=(lhs: inout Self, rhs: Self) { lhs = lhs &* rhs }
@_transparent static func /=(lhs: inout Self, rhs: Self) { lhs = lhs / rhs }
@_transparent static func %=(lhs: inout Self, rhs: Self) { lhs = lhs % rhs }

@_transparent static func &=(lhs: inout Self, rhs: Scalar) { lhs = lhs & rhs }
@_transparent static func ^=(lhs: inout Self, rhs: Scalar) { lhs = lhs ^ rhs }
@_transparent static func |=(lhs: inout Self, rhs: Scalar) { lhs = lhs | rhs }
@_transparent static func &<<=(lhs: inout Self, rhs: Scalar) { lhs = lhs &<< rhs }
@_transparent static func &>>=(lhs: inout Self, rhs: Scalar) { lhs = lhs &>> rhs }
@_transparent static func &+=(lhs: inout Self, rhs: Scalar) { lhs = lhs &+ rhs }
@_transparent static func &-=(lhs: inout Self, rhs: Scalar) { lhs = lhs &- rhs }
@_transparent static func &*=(lhs: inout Self, rhs: Scalar) { lhs = lhs &* rhs }
@_transparent static func /=(lhs: inout Self, rhs: Scalar) { lhs = lhs / rhs }
@_transparent static func %=(lhs: inout Self, rhs: Scalar) { lhs = lhs % rhs }

@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+' instead")
static func +(lhs: Self, rhs: Self) -> Self { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-' instead")
static func -(lhs: Self, rhs: Self) -> Self { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*' instead")
static func *(lhs: Self, rhs: Self) -> Self { fatalError() }

@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+' instead")
static func +(lhs: Self, rhs: Scalar) -> Self { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-' instead")
static func -(lhs: Self, rhs: Scalar) -> Self { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*' instead")
static func *(lhs: Self, rhs: Scalar) -> Self { fatalError() }

@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+' instead")
static func +(lhs: Scalar, rhs: Self) -> Self { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-' instead")
static func -(lhs: Scalar, rhs: Self) -> Self { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*' instead")
static func *(lhs: Scalar, rhs: Self) -> Self { fatalError() }

@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+=' instead")
static func +=(lhs: inout Self, rhs: Self) { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-=' instead")
static func -=(lhs: inout Self, rhs: Self) { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*=' instead")
static func *=(lhs: inout Self, rhs: Self) { fatalError() }

@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&+=' instead")
static func +=(lhs: inout Self, rhs: Scalar) { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&-=' instead")
static func -=(lhs: inout Self, rhs: Scalar) { fatalError() }
@available(*, unavailable, message: "integer vector types do not support checked arithmetic; use the wrapping operator '&*=' instead")
static func *=(lhs: inout Self, rhs: Scalar) { fatalError() }
}

public extension SIMD where Scalar : FloatingPoint {
@_transparent static prefix func -(rhs: Self) -> Self { return 0 - rhs }

@_transparent static func +(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) + rhs }
@_transparent static func -(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) - rhs }
@_transparent static func *(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) * rhs }
@_transparent static func /(lhs: Scalar, rhs: Self) -> Self { return Self(repeating: lhs) / rhs }

@_transparent static func +(lhs: Self, rhs: Scalar) -> Self { return lhs + Self(repeating: rhs) }
@_transparent static func -(lhs: Self, rhs: Scalar) -> Self { return lhs - Self(repeating: rhs) }
@_transparent static func *(lhs: Self, rhs: Scalar) -> Self { return lhs * Self(repeating: rhs) }
@_transparent static func /(lhs: Self, rhs: Scalar) -> Self { return lhs / Self(repeating: rhs) }

@_transparent static func +=(lhs: inout Self, rhs: Self) { lhs = lhs + rhs }
@_transparent static func -=(lhs: inout Self, rhs: Self) { lhs = lhs - rhs }
@_transparent static func *=(lhs: inout Self, rhs: Self) { lhs = lhs * rhs }
@_transparent static func /=(lhs: inout Self, rhs: Self) { lhs = lhs / rhs }

@_transparent static func +=(lhs: inout Self, rhs: Scalar) { lhs = lhs + rhs }
@_transparent static func -=(lhs: inout Self, rhs: Scalar) { lhs = lhs - rhs }
@_transparent static func *=(lhs: inout Self, rhs: Scalar) { lhs = lhs * rhs }
@_transparent static func /=(lhs: inout Self, rhs: Scalar) { lhs = lhs / rhs }

@_transparent func addingProduct(_ lhs: Scalar, _ rhs: Self) -> Self {
return self.addingProduct(Self(repeating: lhs), rhs)
}
@_transparent func addingProduct(_ lhs: Self, _ rhs: Scalar) -> Self {
return self.addingProduct(lhs, Self(repeating: rhs))
}
@_transparent mutating func addProduct(_ lhs: Self, _ rhs: Self) {
self = self.addingProduct(lhs, rhs)
}
@_transparent mutating func addProduct(_ lhs: Scalar, _ rhs: Self) {
self = self.addingProduct(lhs, rhs)
}
@_transparent mutating func addProduct(_ lhs: Self, _ rhs: Scalar) {
self = self.addingProduct(lhs, rhs)
}

@_transparent mutating func formSquareRoot( ) {
self = self.squareRoot()
}

@_transparent mutating func round(_ rule: FloatingPointRoundingRule) {
self = self.rounded(rule)
}
}

public extension SIMDMask {
@_transparent static func .&(lhs: Bool, rhs: SIMDMask) -> SIMDMask { return SIMDMask(repeating: lhs) .& rhs }
@_transparent static func .^(lhs: Bool, rhs: SIMDMask) -> SIMDMask { return SIMDMask(repeating: lhs) .^ rhs }
@_transparent static func .|(lhs: Bool, rhs: SIMDMask) -> SIMDMask { return SIMDMask(repeating: lhs) .| rhs }

@_transparent static func .&(lhs: SIMDMask, rhs: Bool) -> SIMDMask { return lhs .& SIMDMask(repeating: rhs) }
@_transparent static func .^(lhs: SIMDMask, rhs: Bool) -> SIMDMask { return lhs .^ SIMDMask(repeating: rhs) }
@_transparent static func .|(lhs: SIMDMask, rhs: Bool) -> SIMDMask { return lhs .| SIMDMask(repeating: rhs) }

@_transparent static func .&=(lhs: inout SIMDMask, rhs: SIMDMask) { lhs = lhs .& rhs }
@_transparent static func .^=(lhs: inout SIMDMask, rhs: SIMDMask) { lhs = lhs .^ rhs }
@_transparent static func .|=(lhs: inout SIMDMask, rhs: SIMDMask) { lhs = lhs .| rhs }

@_transparent static func .&=(lhs: inout SIMDMask, rhs: Bool) { lhs = lhs .& rhs }
@_transparent static func .^=(lhs: inout SIMDMask, rhs: Bool) { lhs = lhs .^ rhs }
@_transparent static func .|=(lhs: inout SIMDMask, rhs: Bool) { lhs = lhs .| rhs }
}
Loading