Skip to content

Commit e843a3a

Browse files
committed
fix for macos 11 onwards
1 parent 1767883 commit e843a3a

File tree

4 files changed

+100
-73
lines changed

4 files changed

+100
-73
lines changed

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name = "AppleAccelerate"
22
uuid = "13e28ba4-7ad8-5781-acae-3021b1ed3924"
33
version = "0.3.1"
44

5+
[deps]
6+
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
7+
58
[compat]
69
julia = "1"
710

@@ -13,4 +16,4 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1316
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1417

1518
[targets]
16-
test = [ "DSP", "Statistics", "LinearAlgebra", "Test", "Random"]
19+
test = ["DSP", "Statistics", "LinearAlgebra", "Test", "Random"]

src/AppleAccelerate.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
__precompile__()
22
module AppleAccelerate
33

4-
const libacc = "/System/Library/Frameworks/Accelerate.framework/Accelerate"
4+
using Libdl
55

6-
if !isfile(libacc)
7-
error("Accelerate framework not found at $(libacc)")
6+
try
7+
global const libacc = dlopen("/System/Library/Frameworks/Accelerate.framework/Accelerate")
8+
catch
9+
error("Accelerate framework not found.")
810
end
911

12+
get_fptr(s) = dlsym(libacc, s)
13+
1014
include("Array.jl")
1115
include("DSP.jl")
1216
include("Util.jl")

src/Array.jl

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ for (T, suff) in ((Float64, ""), (Float32, "f"))
2727

2828
# In-place mutating variant
2929
function ($f!)(out::Array{$T}, X::Array{$T})
30-
ccall(($(string("vv",fa,suff)),libacc),Cvoid,
31-
(Ptr{$T},Ptr{$T},Ref{Cint}),out,X,length(X))
30+
fptr = get_fptr($(string("vv", fa, suff)))
31+
ccall(fptr, Cvoid,
32+
(Ptr{$T}, Ptr{$T}, Ref{Cint}), out, X, length(X))
3233
out
3334
end
3435
end
@@ -52,8 +53,9 @@ for (T, suff) in ((Float64, ""), (Float32, "f"))
5253
($f!)(out, X, Y)
5354
end
5455
function ($f!)(out::Array{$T}, X::Array{$T}, Y::Array{$T})
55-
ccall(($(string("vv",fa,suff)),libacc),Cvoid,
56-
(Ptr{$T},Ptr{$T},Ptr{$T},Ref{Cint}),out,X,Y,length(X))
56+
fptr = get_fptr($(string("vv", fa, suff)))
57+
ccall(fptr, Cvoid,
58+
(Ptr{$T}, Ptr{$T}, Ptr{$T}, Ref{Cint}), out, X, Y, length(X))
5759
out
5860
end
5961
end
@@ -69,8 +71,9 @@ for (T, suff) in ((Float64, ""), (Float32, "f"))
6971
($f!)(out, X, Y)
7072
end
7173
function ($f!)(out::Array{$T}, X::Array{$T}, Y::Array{$T})
72-
ccall(($(string("vv",fa,suff)),libacc),Cvoid,
73-
(Ptr{$T},Ptr{$T},Ptr{$T},Ref{Cint}),out,Y,X,length(X))
74+
fptr = get_fptr($(string("vv", fa, suff)))
75+
ccall(fptr, Cvoid,
76+
(Ptr{$T}, Ptr{$T}, Ptr{$T}, Ref{Cint}), out, Y, X, length(X))
7477
out
7578
end
7679
end
@@ -86,8 +89,9 @@ for (T, suff) in ((Float64, ""), (Float32, "f"))
8689
($f!)(out1, out2, X)
8790
end
8891
function ($f!)(out1::Array{$T}, out2::Array{$T}, X::Array{$T})
89-
ccall(($(string("vv",f,suff)),libacc),Cvoid,
90-
(Ptr{$T},Ptr{$T},Ptr{$T},Ref{Cint}),out1,out2,X,length(X))
92+
fptr = get_fptr($(string("vv", f, suff)))
93+
ccall(fptr, Cvoid,
94+
(Ptr{$T}, Ptr{$T}, Ptr{$T}, Ref{Cint}), out1, out2, X, length(X))
9195
out1, out2
9296
end
9397
end
@@ -102,8 +106,9 @@ for (T, suff) in ((Float64, ""), (Float32, "f"))
102106
($f!)(out, X)
103107
end
104108
function ($f!)(out::Array{Complex{$T}}, X::Array{$T})
105-
ccall(($(string("vv",fa,suff)),libacc),Cvoid,
106-
(Ptr{Complex{$T}},Ptr{$T},Ref{Cint}),out,X,length(X))
109+
fptr = get_fptr($(string("vv", fa, suff)))
110+
ccall(fptr, Cvoid,
111+
(Ptr{Complex{$T}}, Ptr{$T}, Ref{Cint}), out, X, length(X))
107112
out
108113
end
109114
end
@@ -135,9 +140,10 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
135140
@eval begin
136141
function ($f)(X::Vector{$T})
137142
val = Ref{$T}(0.0)
138-
ccall(($(string("vDSP_", fa, suff), libacc)), Cvoid,
139-
(Ptr{$T}, Int64, Ref{$T}, UInt64),
140-
X, 1, val, length(X))
143+
fptr = get_fptr($(string("vDSP_", fa, suff)))
144+
ccall(fptr, Cvoid,
145+
(Ptr{$T}, Int64, Ref{$T}, UInt64),
146+
X, 1, val, length(X))
141147
return val[]
142148
end
143149
end
@@ -148,10 +154,11 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
148154
function ($f)(X::Vector{$T})
149155
index = Ref{Int}(0)
150156
val = Ref{$T}(0.0)
151-
ccall(($(string("vDSP_", fa, suff), libacc)), Cvoid,
152-
(Ptr{$T}, Int64, Ref{$T}, Ref{Int}, UInt64),
153-
X, 1, val, index, length(X))
154-
return (val[], index[]+1)
157+
fptr = get_fptr($(string("vDSP_", fa, suff)))
158+
ccall(fptr, Cvoid,
159+
(Ptr{$T}, Int64, Ref{$T}, Ref{Int}, UInt64),
160+
X, 1, val, index, length(X))
161+
return (val[], index[] + 1)
155162
end
156163
end
157164
end
@@ -172,7 +179,8 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
172179
the result vector with computed value. *Returns:* **Vector{$($T)}** `result`
173180
""" ->
174181
function ($f!)(result::Vector{$T}, X::Vector{$T}, Y::Vector{$T})
175-
ccall(($(string("vDSP_", f, suff), libacc)), Cvoid,
182+
fptr = get_fptr($(string("vDSP_", f, suff)))
183+
ccall(fptr, Cvoid,
176184
(Ptr{$T}, Int64, Ptr{$T}, Int64, Ptr{$T}, Int64, UInt64),
177185
Y, 1, X, 1, result, 1, length(result))
178186
return result

src/DSP.jl

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ for (T, suff) in ((Float64, "D"), (Float32, ""))
4040
if (rsize < xsize + ksize - 1)
4141
error("'result' must have at least length(X) + length(K) - 1 elements")
4242
end
43-
xpadded::Vector{$T} = [zeros($T, ksize-1); X; zeros($T, ksize)]
44-
ccall(($(string("vDSP_conv", suff), libacc)), Cvoid,
45-
(Ptr{$T}, Int64, Ptr{$T}, Int64, Ptr{$T}, Int64, UInt64, UInt64),
46-
xpadded, 1, pointer(K, ksize), -1, result, 1, rsize, ksize)
43+
xpadded::Vector{$T} = [zeros($T, ksize - 1); X; zeros($T, ksize)]
44+
fptr = get_fptr($(string("vDSP_conv", suff)))
45+
ccall(fptr, Cvoid,
46+
(Ptr{$T}, Int64, Ptr{$T}, Int64, Ptr{$T}, Int64, UInt64, UInt64),
47+
xpadded, 1, pointer(K, ksize), -1, result, 1, rsize, ksize)
4748
return result
4849
end
4950
end
@@ -87,10 +88,11 @@ for (T, suff) in ((Float64, "D"), (Float32, ""))
8788
if (rsize < xsize + ysize - 1)
8889
error("'result' must have at least length(X) + length(Y) - 1 elements")
8990
end
90-
xpadded::Vector{$T} = [zeros($T, ysize-1); X; zeros($T, ysize)]
91-
ccall(($(string("vDSP_conv", suff), libacc)), Cvoid,
92-
(Ptr{$T}, Int64, Ptr{$T}, Int64, Ptr{$T}, Int64, UInt64, UInt64),
93-
xpadded, 1, Y, 1, result, 1, rsize, ysize)
91+
xpadded::Vector{$T} = [zeros($T, ysize - 1); X; zeros($T, ysize)]
92+
fptr = get_fptr($(string("vDSP_conv", suff)))
93+
ccall(fptr, Cvoid,
94+
(Ptr{$T}, Int64, Ptr{$T}, Int64, Ptr{$T}, Int64, UInt64, UInt64),
95+
xpadded, 1, Y, 1, result, 1, rsize, ysize)
9496
return result
9597
end
9698
end
@@ -134,7 +136,7 @@ for (T, suff) in ((Float64, "D"), (Float32, ""))
134136
end
135137

136138
## == Biquadratic/IIR filtering
137-
for (T, suff) in ((Float64, "D"), )
139+
for (T, suff) in ((Float64, "D"),)
138140

139141
"""
140142
Initializes a vDSP_biquad_setup for use with vDSP_biquad. A multi-section filter
@@ -145,14 +147,15 @@ for (T, suff) in ((Float64, "D"), )
145147
Returns: Biquad{T}
146148
"""
147149
@eval begin
148-
function biquadcreate(coefficients::Vector{$T}, sections::Int)
149-
if length(coefficients) < 5*sections
150+
function biquadcreate(coefficients::Vector{$T}, sections::Int)
151+
if length(coefficients) < 5 * sections
150152
error("Incomplete biquad specification provided - coefficients must
151153
contain 5 elements for each filter section")
152154
end
153-
setup = ccall(($(string("vDSP_biquad_CreateSetup", suff), libacc)), Ptr{Cvoid},
154-
(Ptr{$T}, UInt64),
155-
coefficients, sections)
155+
fptr = get_fptr($(string("vDSP_biquad_CreateSetup", suff)))
156+
setup = ccall(fptr, Ptr{Cvoid},
157+
(Ptr{$T}, UInt64),
158+
coefficients, sections)
156159
return Biquad($T, setup, sections)
157160
end
158161
end
@@ -167,14 +170,15 @@ for (T, suff) in ((Float64, "D"), )
167170
"""
168171
@eval begin
169172
function biquad(X::Vector{$T}, delays::Vector{$T}, numelem::Int, biquad::Biquad{$T})
170-
if length(delays) < (2*(biquad.sections)+2)
173+
if length(delays) < (2 * (biquad.sections) + 2)
171174
error("Incomplete delay specification provided - delays must contain 2*M + 2
172175
values where M is the number of sections in the biquad")
173176
end
174177
result::Vector{$T} = similar(X)
175-
ccall(($(string("vDSP_biquad", suff), libacc)), Cvoid,
176-
(Ptr{Cvoid}, Ptr{$T}, Ptr{$T}, Int64, Ptr{$T}, Int64, UInt64),
177-
biquad.setup, delays, X, 1, result, 1, numelem)
178+
fptr = get_fptr($(string("vDSP_biquad", suff)))
179+
ccall(fptr, Cvoid,
180+
(Ptr{Cvoid}, Ptr{$T}, Ptr{$T}, Int64, Ptr{$T}, Int64, UInt64),
181+
biquad.setup, delays, X, 1, result, 1, numelem)
178182
return result
179183
end
180184
end
@@ -189,9 +193,10 @@ for (T, suff) in ((Float64, "D"), )
189193
"""
190194
@eval begin
191195
function biquaddestroy(biquad::Biquad{$T})
192-
ccall(($(string("vDSP_biquad_DestroySetup", suff), libacc)), Cvoid,
193-
(Ptr{Cvoid}, ),
194-
biquad.setup)
196+
fptr = get_fptr($(string("vDSP_biquad_DestroySetup", suff)))
197+
ccall(fptr, Cvoid,
198+
(Ptr{Cvoid},),
199+
biquad.setup)
195200
end
196201
end
197202
end
@@ -205,7 +210,7 @@ Generates a Blackman window of length 'length'. Default return type
205210
is Vector{Float64}, but if rtype=Float32, Vector{Float32}
206211
will be returned.
207212
"""
208-
function blackman(length::Int, rtype::DataType=Float64)
213+
function blackman(length::Int, rtype::DataType = Float64)
209214
result::Vector{rtype} = Array{rtype}(undef, length)
210215
blackman!(result, length, 0)
211216
end
@@ -217,7 +222,7 @@ Generates a Hamming window of length 'length'. Default return type
217222
is Vector{Float64}, but if rtype=Float32, Vector{Float32}
218223
will be returned.
219224
"""
220-
function hamming(length::Int, rtype::DataType=Float64)
225+
function hamming(length::Int, rtype::DataType = Float64)
221226
result::Vector{rtype} = Array{rtype}(undef, length)
222227
hamming!(result, length, 0)
223228
end
@@ -229,15 +234,15 @@ Generates a denormalized Hanning window of length 'length'. Default
229234
return type is Vector{Float64}, but if rtype=Float32, Vector{Float32}
230235
will be returned.
231236
"""
232-
function hanning(length::Int, rtype::DataType=Float64)
237+
function hanning(length::Int, rtype::DataType = Float64)
233238
result::Vector{rtype} = Array{rtype}(undef, length)
234239
hanning!(result, length, 0)
235240
end
236241

237242
"""
238243
Alias function for `hanning`
239244
"""
240-
function hann(length::Int, rtype::DataType=Float64)
245+
function hann(length::Int, rtype::DataType = Float64)
241246
hanning(length, rtype)
242247
end
243248

@@ -251,10 +256,11 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
251256
Returns: Vector{$T}
252257
"""
253258
@eval begin
254-
function blackman!(result::Vector{$T}, length::Int, flag::Int=0)
255-
ccall(($(string("vDSP_blkman_window", suff), libacc)), Cvoid,
256-
(Ptr{$T}, UInt64, Int64),
257-
result, length, flag)
259+
function blackman!(result::Vector{$T}, length::Int, flag::Int = 0)
260+
fptr = get_fptr($(string("vDSP_blkman_window", suff)))
261+
ccall(fptr, Cvoid,
262+
(Ptr{$T}, UInt64, Int64),
263+
result, length, flag)
258264
return result
259265
end
260266
end
@@ -268,10 +274,11 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
268274
Returns: Vector{$T}
269275
"""
270276
@eval begin
271-
function hamming!(result::Vector{$T}, length::Int, flag::Int=0)
272-
ccall(($(string("vDSP_hamm_window", suff), libacc)), Cvoid,
273-
(Ptr{$T}, UInt64, Int64),
274-
result, length, flag)
277+
function hamming!(result::Vector{$T}, length::Int, flag::Int = 0)
278+
fptr = get_fptr($(string("vDSP_hamm_window", suff)))
279+
ccall(fptr, Cvoid,
280+
(Ptr{$T}, UInt64, Int64),
281+
result, length, flag)
275282
return result
276283
end
277284
end
@@ -289,10 +296,11 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
289296
Returns: Vector{$T}
290297
"""
291298
@eval begin
292-
function hanning!(result::Vector{$T}, length::Int, flag::Int=0)
293-
ccall(($(string("vDSP_hann_window", suff), libacc)), Cvoid,
294-
(Ptr{$T}, UInt64, Int64),
295-
result, length, flag)
299+
function hanning!(result::Vector{$T}, length::Int, flag::Int = 0)
300+
fptr = get_fptr($(string("vDSP_hann_window", suff)))
301+
ccall(fptr, Cvoid,
302+
(Ptr{$T}, UInt64, Int64),
303+
result, length, flag)
296304
return result
297305
end
298306
end
@@ -303,7 +311,7 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
303311
Returns: Vector{$T}
304312
"""
305313
@eval begin
306-
function hann!(result::Vector{$T}, length::Int, flag::Int=0)
314+
function hann!(result::Vector{$T}, length::Int, flag::Int = 0)
307315
hanning!(result, length, flag)
308316
end
309317
end
@@ -320,17 +328,18 @@ storage of the previous setup object.
320328
321329
Returns: DFTSetup
322330
"""
323-
function plan_dct(length::Int, dct_type::Int, previous=C_NULL)
331+
function plan_dct(length::Int, dct_type::Int, previous = C_NULL)
324332
n = trailing_zeros(length)
325333
f = length >> n
326-
if dct_type < 2 && dct_type > 4
334+
if dct_type < 2 && dct_type > 4
327335
error("DCT type ", dct_type, " is not supported. Only DCT types 2, 3 and 4 are supported")
328-
elseif !(n >= 4 && f in (1,3,5,15))
336+
elseif !(n >= 4 && f in (1, 3, 5, 15))
329337
error("Invalid DCT length. Length must be equal to f*(2^n) where f = 1,3,5,15 and n >= 4")
330338
end
331-
setup::Ptr{Cvoid} = ccall(("vDSP_DCT_CreateSetup", libacc), Ptr{Cvoid},
332-
(Ptr{Cvoid}, UInt64, UInt64),
333-
previous, length, dct_type)
339+
fptr = get_fptr("vDSP_DCT_CreateSetup")
340+
setup::Ptr{Cvoid} = ccall(fptr, Ptr{Cvoid},
341+
(Ptr{Cvoid}, UInt64, UInt64),
342+
previous, length, dct_type)
334343
return DFTSetup(Float32, setup, 0)
335344
end
336345

@@ -343,9 +352,11 @@ Returns: Vector{Float32}
343352
"""
344353
function dct(X::Vector{Float32}, setup::DFTSetup)
345354
result = similar(X)
346-
ccall(("vDSP_DCT_Execute", libacc), Cvoid,
347-
(Ptr{Cvoid}, Ptr{Float32}, Ptr{Float32}),
348-
setup.setup, X, result)
355+
fptr = get_fptr("vDSP_DCT_Execute")
356+
357+
ccall(fptr, Cvoid,
358+
(Ptr{Cvoid}, Ptr{Float32}, Ptr{Float32}),
359+
setup.setup, X, result)
349360
return result
350361
end
351362

@@ -356,7 +367,7 @@ This function does not require a separate call to dct_setup.
356367
357368
Returns: Vector{Float32}
358369
"""
359-
function dct(X::Vector{Float32}, dct_type::Int=2)
370+
function dct(X::Vector{Float32}, dct_type::Int = 2)
360371
setup = plan_dct(length(X), dct_type)
361372
return dct(X, setup)
362373
end
@@ -366,9 +377,10 @@ end
366377
Deinitializes a DFTSetup object created by plan_dct
367378
"""
368379
function plan_destroy(setup::DFTSetup)
369-
ccall(("vDSP_DFT_DestroySetup", libacc), Cvoid,
370-
(Ptr{Cvoid},),
371-
setup.setup)
380+
fptr = get_fptr("vDSP_DFT_DestroySetup")
381+
ccall(fptr, Cvoid,
382+
(Ptr{Cvoid},),
383+
setup.setup)
372384
end
373385

374386

0 commit comments

Comments
 (0)