Files
swift-mirror/stdlib/public/Darwin/Accelerate/vDSP_PolarRectangularConversion.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

211 lines
8.6 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 {
/// Rectangular to polar conversion, single-precision.
///
/// - Parameter rectangularCoordinates: Source vector, represented as consecutive x, y pairs.
/// - Returns: Polar coordinates, represented as consecutive rho, (radius) theta (angle in radians) pairs.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func rectangularToPolar<U>(_ rectangularCoordinates: U) -> [Float]
where
U: AccelerateBuffer,
U.Element == Float {
let result = Array<Float>(unsafeUninitializedCapacity: rectangularCoordinates.count) {
buffer, initializedCount in
convert(rectangularCoordinates: rectangularCoordinates,
toPolarCoordinates: &buffer)
initializedCount = rectangularCoordinates.count
}
return result
}
/// Rectangular to polar conversion, single-precision.
///
/// - Parameter rectangularCoordinates: Source vector, represented as consecutive x, y pairs.
/// - Parameter polarCoordinates: Destination vector, represented as consecutive rho, (radius) theta (angle in radians) pairs.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func convert<U, V>(rectangularCoordinates: U,
toPolarCoordinates polarCoordinates: inout V)
where
U: AccelerateBuffer,
V: AccelerateMutableBuffer,
U.Element == Float,
V.Element == Float {
let n = rectangularCoordinates.count
precondition(polarCoordinates.count == n)
polarCoordinates.withUnsafeMutableBufferPointer { dest in
rectangularCoordinates.withUnsafeBufferPointer { src in
vDSP_polar(src.baseAddress!, 2,
dest.baseAddress!, 2,
vDSP_Length(n / 2))
}
}
}
/// Rectangular to polar conversion, double-precision.
///
/// - Parameter rectangularCoordinates: Source vector, represented as consecutive x, y pairs.
/// - Returns: Polar coordinates, represented as consecutive rho, (radius) theta (angle in radians) pairs.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func rectangularToPolar<U>(_ rectangularCoordinates: U) -> [Double]
where
U: AccelerateBuffer,
U.Element == Double {
let result = Array<Double>(unsafeUninitializedCapacity: rectangularCoordinates.count) {
buffer, initializedCount in
convert(rectangularCoordinates: rectangularCoordinates,
toPolarCoordinates: &buffer)
initializedCount = rectangularCoordinates.count
}
return result
}
/// Rectangular to polar conversion, double-precision.
///
/// - Parameter rectangularCoordinates: Source vector, represented as consecutive x, y pairs.
/// - Parameter polarCoordinates: Destination vector, represented as consecutive rho, (radius) theta (angle in radians) pairs.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func convert<U, V>(rectangularCoordinates: U,
toPolarCoordinates polarCoordinates: inout V)
where
U: AccelerateBuffer,
V: AccelerateMutableBuffer,
U.Element == Double,
V.Element == Double {
let n = rectangularCoordinates.count
precondition(polarCoordinates.count == n)
polarCoordinates.withUnsafeMutableBufferPointer { dest in
rectangularCoordinates.withUnsafeBufferPointer { src in
vDSP_polarD(src.baseAddress!, 2,
dest.baseAddress!, 2,
vDSP_Length(n / 2))
}
}
}
/// Polar to rectangular conversion, single-precision.
///
/// - Parameter polarCoordinates: Source vector, represented as consecutive rho, (radius) theta (angle in radians) pairs.
/// - Returns: Rectangular coordinates, represented as consecutive x, y pairs.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func polarToRectangular<U>(_ polarCoordinates: U) -> [Float]
where
U: AccelerateBuffer,
U.Element == Float {
let result = Array<Float>(unsafeUninitializedCapacity: polarCoordinates.count) {
buffer, initializedCount in
convert(polarCoordinates: polarCoordinates,
toRectangularCoordinates: &buffer)
initializedCount = polarCoordinates.count
}
return result
}
/// Polar to rectangular conversion, single-precision.
///
/// - Parameter polarCoordinates: Source vector, represented as consecutive rho, (radius) theta (angle in radians) pairs.
/// - Parameter rectangularCoordinates: Destination vector, represented as consecutive x, y pairs.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func convert<U, V>(polarCoordinates: U,
toRectangularCoordinates rectangularCoordinates: inout V)
where
U: AccelerateBuffer,
V: AccelerateMutableBuffer,
U.Element == Float,
V.Element == Float {
let n = rectangularCoordinates.count
precondition(polarCoordinates.count == n)
rectangularCoordinates.withUnsafeMutableBufferPointer { dest in
polarCoordinates.withUnsafeBufferPointer { src in
vDSP_rect(src.baseAddress!, 2,
dest.baseAddress!, 2,
vDSP_Length(n / 2))
}
}
}
/// Polar to rectangular conversion, double-precision.
///
/// - Parameter polarCoordinates: Source vector, represented as consecutive rho, (radius) theta (angle in radians) pairs.
/// - Returns: Rectangular coordinates, represented as consecutive x, y pairs.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func polarToRectangular<U>(_ polarCoordinates: U) -> [Double]
where
U: AccelerateBuffer,
U.Element == Double {
let result = Array<Double>(unsafeUninitializedCapacity: polarCoordinates.count) {
buffer, initializedCount in
convert(polarCoordinates: polarCoordinates,
toRectangularCoordinates: &buffer)
initializedCount = polarCoordinates.count
}
return result
}
/// Polar to rectangular conversion, double-precision.
///
/// - Parameter polarCoordinates: Source vector, represented as consecutive rho, (radius) theta (angle in radians) pairs.
/// - Parameter rectangularCoordinates: Destination vector, represented as consecutive x, y pairs.
@inlinable
@available(iOS 9999, macOS 9999, tvOS 9999, watchOS 9999, *)
public static func convert<U, V>(polarCoordinates: U,
toRectangularCoordinates rectangularCoordinates: inout V)
where
U: AccelerateBuffer,
V: AccelerateMutableBuffer,
U.Element == Double,
V.Element == Double {
let n = rectangularCoordinates.count
precondition(polarCoordinates.count == n)
rectangularCoordinates.withUnsafeMutableBufferPointer { dest in
polarCoordinates.withUnsafeBufferPointer { src in
vDSP_rectD(src.baseAddress!, 2,
dest.baseAddress!, 2,
vDSP_Length(n / 2))
}
}
}
}