Files
swift-mirror/stdlib/public/Darwin/Accelerate/vDSP_SlidingWindow.swift
Stephen Canon e6406d878d Drop @inline(__always) from Accelerate overlay; it doesn't do what you want. (#24641)
@inline(__always) does not imply inlinable, which means that it effectively does nothing in the context of the Accelerate overlay. I have replaced all of these with @inlinable where that can be done as a one-line change. Functions that switch over open enums and more complex API (DCT, DFT, FFT) will require more sophisticated corrections, which we can undertake in later commits. For now, they have been rolled back to simply being normal public API.
2019-05-09 17:10:34 -04:00

131 lines
4.9 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
extension vDSP {
/// Vector sliding window sum; single-precision.
///
/// - Parameter source: Single-precision input vector.
/// - Parameter windowLength: The number of consecutive elements to sum.
/// - Returns: Single-precision output vector.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func slidingWindowSum<U>(_ vector: U,
usingWindowLength windowLength: Int) -> [Float]
where
U: AccelerateBuffer,
U.Element == Float {
let n = vector.count - windowLength + 1
let result = Array<Float>(unsafeUninitializedCapacity: n) {
buffer, initializedCount in
slidingWindowSum(vector,
usingWindowLength: windowLength,
result: &buffer)
initializedCount = n
}
return result
}
/// Vector sliding window sum; single-precision.
///
/// - Parameter source: Single-precision input vector.
/// - Parameter windowLength: The number of consecutive elements to sum.
/// - Parameter result: Single-precision output vector.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func slidingWindowSum<U, V>(_ vector: U,
usingWindowLength windowLength: Int,
result: inout V)
where
U: AccelerateBuffer,
V: AccelerateMutableBuffer,
U.Element == Float,
V.Element == Float {
let n = result.count
precondition(vector.count == n + windowLength - 1)
result.withUnsafeMutableBufferPointer { dest in
vector.withUnsafeBufferPointer { src in
vDSP_vswsum(src.baseAddress!, 1,
dest.baseAddress!, 1,
vDSP_Length(n),
vDSP_Length(windowLength))
}
}
}
/// Vector sliding window sum; double-precision.
///
/// - Parameter source: Single-precision input vector.
/// - Parameter windowLength: The number of consecutive elements to sum.
/// - Returns: Single-precision output vector.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func slidingWindowSum<U>(_ vector: U,
usingWindowLength windowLength: Int) -> [Double]
where
U: AccelerateBuffer,
U.Element == Double {
let n = vector.count - windowLength + 1
let result = Array<Double>(unsafeUninitializedCapacity: n) {
buffer, initializedCount in
slidingWindowSum(vector,
usingWindowLength: windowLength,
result: &buffer)
initializedCount = n
}
return result
}
/// Vector sliding window sum; double-precision.
///
/// - Parameter source: Double-precision input vector.
/// - Parameter windowLength: The number of consecutive elements to sum.
/// - Parameter result: Double-precision output vector.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func slidingWindowSum<U, V>(_ vector: U,
usingWindowLength windowLength: Int,
result: inout V)
where
U: AccelerateBuffer,
V: AccelerateMutableBuffer,
U.Element == Double,
V.Element == Double {
let n = result.count
precondition(vector.count == n + windowLength - 1)
result.withUnsafeMutableBufferPointer { dest in
vector.withUnsafeBufferPointer { src in
vDSP_vswsumD(src.baseAddress!, 1,
dest.baseAddress!, 1,
vDSP_Length(n),
vDSP_Length(windowLength))
}
}
}
}