From 3ee5748f8702429cc8a457350b2207e7bb1ed520 Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Tue, 24 Sep 2019 15:19:10 +0200 Subject: [PATCH] =?UTF-8?q?Changed=20`pow=E2=80=A6`=20of=20=E2=80=99Arithm?= =?UTF-8?q?etic.swift=E2=80=99=20be=20implemented=20in=20terms=20of=20`pow?= =?UTF-8?q?InPlace`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Surge/Arithmetic/Arithmetic.swift | 40 +++++++---------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/Sources/Surge/Arithmetic/Arithmetic.swift b/Sources/Surge/Arithmetic/Arithmetic.swift index 3ad9376..5c31015 100644 --- a/Sources/Surge/Arithmetic/Arithmetic.swift +++ b/Sources/Surge/Arithmetic/Arithmetic.swift @@ -590,46 +590,30 @@ func exp2InPlace(_ lhs: inout X) where X.Eleme /// - Warning: does not support memory stride (assumes stride is 1). public func pow(_ lhs: X, _ rhs: Y) -> [Float] where X.Element == Float, Y.Element == Float { - return withUnsafeMemory(lhs, rhs) { lhsMemory, rhsMemory in - precondition(lhsMemory.stride == 1 && rhsMemory.stride == 1, "\(#function) does not support strided memory access") - - var lhsCount = Int32(lhs.count) - - var results = [Float](repeating: 0.0, count: lhs.count) - results.withUnsafeMutableBufferPointer { pointer in - vvpowf(pointer.baseAddress!, rhsMemory.pointer, lhsMemory.pointer, &lhsCount) - } - - return results - } + var results = Array(lhs) + powInPlace(&results, rhs) + return results } /// - Warning: does not support memory stride (assumes stride is 1). public func pow(_ lhs: X, _ rhs: Y) -> [Double] where X.Element == Double, Y.Element == Double { - return withUnsafeMemory(lhs, rhs) { lhsMemory, rhsMemory in - precondition(lhsMemory.stride == 1 && rhsMemory.stride == 1, "\(#function) does not support strided memory access") - - var lhsCount = Int32(lhs.count) - - var results = [Double](repeating: 0.0, count: lhs.count) - results.withUnsafeMutableBufferPointer { pointer in - vvpow(pointer.baseAddress!, rhsMemory.pointer, lhsMemory.pointer, &lhsCount) - } - - return results - } + var results = Array(lhs) + powInPlace(&results, rhs) + return results } /// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`. public func pow(_ lhs: X, _ rhs: Float) -> [Float] where X.Element == Float { - let rhs = [Float](repeating: rhs, count: lhs.count) - return pow(lhs, rhs) + var results = Array(lhs) + powInPlace(&results, rhs) + return results } /// - Warning: Allocates a temporary array from `rhs` via `Array(repeating: rhs, count: lhs.count)`. public func pow(_ lhs: X, _ rhs: Double) -> [Double] where X.Element == Double { - let rhs = [Double](repeating: rhs, count: lhs.count) - return pow(lhs, rhs) + var results = Array(lhs) + powInPlace(&results, rhs) + return results } // MARK: - Power: In Place