Skip to content

Commit

Permalink
Added conv, xcorr, and tests. Rewrote test suite for Float32/64. Upda…
Browse files Browse the repository at this point in the history
…ted .travis
  • Loading branch information
rprechelt committed Mar 9, 2016
1 parent 24e43e0 commit 9bb657d
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 111 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -2,7 +2,6 @@ language: julia
os:
- osx
julia:
- 0.3
- 0.4
- nightly
notifications:
Expand All @@ -11,4 +10,4 @@ notifications:
before_install:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
script:
- julia -e 'versioninfo(); Pkg.init(); Pkg.clone(pwd()); Pkg.clone("FactCheck"); Pkg.test("AppleAccelerate")'
- julia -e 'versioninfo(); Pkg.init(); Pkg.clone(pwd()); Pkg.test("AppleAccelerate")'
2 changes: 1 addition & 1 deletion REQUIRE
@@ -1 +1 @@
julia 0.3
julia 0.4
53 changes: 46 additions & 7 deletions src/AppleAccelerate.jl
Expand Up @@ -130,15 +130,52 @@ for (T, suff) in ((Float64, ""), (Float32, "f"))
end


for (T, suff) in ((Float64, "D"), (Float32, ""))

@eval begin
function conv(X::Array{$T}, K::Array{$T})
ksize = length(K)
xsize = length(X)
rsize = xsize + ksize - 1
x_padded::Array{$T} = [zeros($T, ksize-1); X; zeros($T, ksize)]
result = Array($T, rsize)
ccall(($(string("vDSP_conv", suff), libacc)), Void,
(Ptr{$T}, Int64, Ptr{$T}, Int64, Ptr{$T}, Int64, UInt64, UInt64),
x_padded, 1, pointer(K, ksize), -1, result, 1, rsize, ksize)
return result
end
end


@eval begin
function xcorr(X::Array{$T}, K::Array{$T})
ksize = length(K)
xsize = length(X)
rsize = xsize + ksize - 1
x_padded::Array{$T} = [zeros($T, ksize-1); X; zeros($T, ksize)]
result = Array($T, rsize)
ccall(($(string("vDSP_conv", suff), libacc)), Void,
(Ptr{$T}, Int64, Ptr{$T}, Int64, Ptr{$T}, Int64, UInt64, UInt64),
x_padded, 1, K, 1, result, 1, rsize, ksize)
return result
end
end

@eval begin
function xcorr(X::Array{$T})
return xcorr(X, X)
end
end
end


function plan_dct(n,k::Integer)
@assert isinteger(Base.log2(n))
@assert 2k4
ccall(("vDSP_DCT_CreateSetup",libacc),Ptr{Void},(Ptr{Void},Cint,Cint),C_NULL,n,k)
end




function dct(r::Vector{Float32},plan)
n=length(r)
@assert isinteger(Base.log2(n))
Expand All @@ -150,7 +187,6 @@ end
dct(r::Vector{Float32},k::Integer=2)=dct(r,plan_dct(length(r),k))



if VERSION>=v"0.4.0-rc1"
tupletypelength(a)=length(a.parameters)
else
Expand All @@ -170,11 +206,11 @@ macro replaceBase(fs...)
end
e = quote
if tupletypelength(methods($f).defs.sig) == 1
(Base.$f)(X::Array{Float64}) = ($fa)(X)
(Base.$f)(X::Array{Float32}) = ($fa)(X)
(Base.$f)(X::Array{Float64, 1}) = ($fa)(X)
(Base.$f)(X::Array{Float32, 1}) = ($fa)(X)
else
(Base.$f)(X::Array{Float64},Y::Array{Float64}) = ($fa)(X,Y)
(Base.$f)(X::Array{Float32},Y::Array{Float32}) = ($fa)(X,Y)
(Base.$f)(X::Array{Float64, 1}, Y::Array{Float64, 1}) = ($fa)(X,Y)
(Base.$f)(X::Array{Float32, 1}, Y::Array{Float32, 1}) = ($fa)(X,Y)
end
end
push!(b.args,e)
Expand All @@ -183,6 +219,9 @@ macro replaceBase(fs...)
end





# const FFT_FORWARD = +1
# const FFT_INVERSE = -1
# const SIGNAL_STRIDE = 1
Expand Down
231 changes: 130 additions & 101 deletions test/runtests.jl
Expand Up @@ -10,87 +10,91 @@ end
srand(7)
N = 1_000

@testset "Rounding" begin
X = 100*randn(N)
@testset "Testing $f" for f in [:floor,:ceil,:trunc,:round]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
end
end


@testset "Logarithmic" begin
X = exp(10*randn(N))
@testset "Testing $f" for f in [:log,:log2,:log10]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
end

Y = expm1(10*randn(N))
@testset "Testing $f" for f in [:log1p]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(Y) fb(Y)
for T in (Float32, Float64)
@testset "Rounding::$T" begin
X::Array{T} = 100*randn(N)
@testset "Testing $f:$T" for f in [:floor,:ceil,:trunc,:round]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
end
end
end


@testset "Exponential" begin
X = 100*randn(N)
@testset "Testing $f" for f in [:exp,:exp2,:expm1]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
for T in (Float32, Float64)
@testset "Logarithmic::$T" begin
X::Array{T} = exp(10*randn(N))
@testset "Testing $f::$T" for f in [:log,:log2,:log10, :log1p]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
end
end
end


@testset "Trigonometric" begin
X = 10*randn(N)
@testset "Testing $f" for f in [:sin,:sinpi,:cos,:cospi,:tan,:atan] # tanpi not defined in Base
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
for T in (Float32, Float64)
@testset "Exponential::$T" begin
@testset "Testing $f::$T" for f in [:exp,:exp2,:expm1]
X::Array{T} = 10*randn(N)
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
end
end
end

Y = 10*randn(N)
@testset "Testing $f" for f in [:atan2]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X,Y) fb(X,Y)
end

Z = 2*rand(N)-1
@testset "Testing $f" for f in [:asin,:acos]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(Z) fb(Z)
for T in (Float32, Float64)
X::Array{T} = 10*randn(N)
@testset "Trigonometric::$T" begin
@testset "Testing $f::$T" for f in [:sin,:sinpi,:cos,:cospi,:tan,:atan] # tanpi not defined in Base
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
end

Y::Array{T} = 10*randn(N)
@testset "Testing $f::$T" for f in [:atan2]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X,Y) fb(X,Y)
end

Z::Array{T} = 2*rand(N)-1
@testset "Testing $f::$T" for f in [:asin,:acos]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(Z) fb(Z)
end
end
end


@testset "Hyperbolic" begin
X = 10*randn(N)
@testset "Testing $f" for f in [:sinh,:cosh,:tanh,:asinh]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
end

Y = exp(10*randn(N))+1
@testset "Testing $f" for f in [:acosh]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(Y) fb(Y)
end

Z = 2*rand(N)-1
@testset "Testing $f" for f in [:atanh]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(Z) fb(Z)
for T in (Float32, Float64)
@testset "Hyperbolic::$T" begin
X = 10*randn(N)
@testset "Testing $f::$T" for f in [:sinh,:cosh,:tanh,:asinh]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
end

Y = exp(10*randn(N))+1
@testset "Testing $f::$T" for f in [:acosh]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(Y) fb(Y)
end

Z = 2*rand(N)-1
@testset "Testing $f::$T" for f in [:atanh]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(Z) fb(Z)
end
end
end

Expand All @@ -103,56 +107,81 @@ end
end


@testset "Misc" begin
X = exp(10*randn(N))
@testset "Testing $f" for f in [:sqrt]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
for T in (Float32, Float64)
@testset "Convolution & Correlation::$T" begin
X::Array{T} = randn(N)
Y::Array{T} = randn(N)
@testset "Testing $f::$T" for f in [:conv, :xcorr]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fb(X, Y) fa(X, Y)
end

@testset "Testing $f::$T" for f in [:xcorr]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fb(X, copy(X)) fa(X)
end
end
end

Y = 10*randn(N)
@testset "Testing $f" for f in [:exponent, :abs]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(Y) fb(Y)
end

Z = 10*randn(N)
@testset "Testing $f" for f in [:copysign]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X,Y) fb(X,Y)
end
for T in (Float32, Float64)
@testset "Misc::$T" begin
X::Array{T} = exp(10*randn(N))
@testset "Testing $f::$T" for f in [:sqrt]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X) fb(X)
end

Y::Array{T} = 10*randn(N)
@testset "Testing $f::$T" for f in [:exponent, :abs]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(Y) fb(Y)
end

@test AppleAccelerate.rem(X,Y) == [rem(X[i], Y[i]) for i=1:length(X)]
Z::Array{T} = 10*randn(N)
@testset "Testing $f::$T" for f in [:copysign]
@eval fb = $f
@eval fa = AppleAccelerate.$f
@test fa(X,Y) fb(X,Y)
end

@test AppleAccelerate.rem(X,Y) == [rem(X[i], Y[i]) for i=1:length(X)]

end
end


@testset "Extra" begin
X = randn(N)
Y = abs(randn(N))
for T in (Float32, Float64)
@testset "Extra::$T" begin
X::Array{T} = randn(N)
Y::Array{T} = abs(randn(N))

@test AppleAccelerate.rec(X) 1./X
@test AppleAccelerate.rsqrt(Y) 1./sqrt(Y)
@test AppleAccelerate.pow(Y,X) Y.^X
@test AppleAccelerate.fdiv(X,Y) X./Y
@test AppleAccelerate.rec(X) 1./X
@test AppleAccelerate.rsqrt(Y) 1./sqrt(Y)
@test AppleAccelerate.pow(Y,X) Y.^X
@test AppleAccelerate.fdiv(X,Y) X./Y

@test AppleAccelerate.sincos(X)[1] sin(X)
@test AppleAccelerate.sincos(X)[2] cos(X)
@test AppleAccelerate.cis(X) cis(X)
@test AppleAccelerate.sincos(X)[1] sin(X)
@test AppleAccelerate.sincos(X)[2] cos(X)
@test AppleAccelerate.cis(X) cis(X)

end
end


@testset "Replace Base" begin
X = randn(N)
Y = abs(randn(N))
for T in (Float32, Float64)
@testset "Replace Base::$T" begin
X::Array{T} = randn(N)
Y::Array{T} = abs(randn(N))

AppleAccelerate.@replaceBase(sin, atan2, ./)
@test sin(X) == AppleAccelerate.sin(X)
@test atan2(X, Y) == AppleAccelerate.atan2(X, Y)
@test X ./ Y == AppleAccelerate.fdiv(X, Y)
AppleAccelerate.@replaceBase(sin, atan2, ./)
@test Base.sin(X) == AppleAccelerate.sin(X)
@test Base.atan2(X, Y) == AppleAccelerate.atan2(X, Y)
@test X ./ Y == AppleAccelerate.fdiv(X, Y)

end
end

0 comments on commit 9bb657d

Please sign in to comment.