2
2
3
3
for (T, suff) in ((Float64, " " ), (Float32, " f" ))
4
4
5
- # 1 arg functions
6
- for f in (:ceil ,:floor ,:sqrt ,:rsqrt ,:rec ,
5
+ # 1- arg functions
6
+ onearg_funcs = (:ceil ,:floor ,:sqrt ,:rsqrt ,:rec ,
7
7
:exp ,:exp2 ,:expm1 ,:log ,:log1p ,:log2 ,:log10 ,
8
8
:sin ,:sinpi ,:cos ,:cospi ,:tan ,:tanpi ,:asin ,:acos ,:atan ,
9
9
:sinh ,:cosh ,:tanh ,:asinh ,:acosh ,:atanh )
10
- f! = symbol (" $(f) !" )
10
+ # Combine this with the list of renamed 1-arg funcs
11
+ onearg_funcs = (
12
+ ((x,x) for x in onearg_funcs). .. ,
13
+ (:trunc ,:int ),
14
+ (:round ,:nint ),
15
+ (:exponent ,:logb ),
16
+ (:abs ,:fabs )
17
+ )
18
+
19
+ for (f,fa) in onearg_funcs
20
+ f! = Symbol (" $(f) !" )
11
21
@eval begin
22
+ # Allocating variant
12
23
function ($ f)(X:: Array{$T} )
13
- out = Array ( $ T, size (X))
24
+ out = Array {$T} (undef, size (X))
14
25
($ f!)(out, X)
15
26
end
16
- function ($ f!)(out:: Array{$T} , X:: Array{$T} )
17
- ccall (($ (string (" vv" ,f,suff)),libacc),Void,
18
- (Ptr{$ T},Ptr{$ T},Ptr{Cint}),out,X,& length (X))
19
- out
20
- end
21
- end
22
- end
23
27
24
- # renamed 1 arg functions
25
- for (f,fa) in ((:trunc ,:int ),(:round ,:nint ),(:exponent ,:logb ),
26
- (:abs ,:fabs ))
27
- f! = symbol (" $(f) !" )
28
- @eval begin
29
- function ($ f)(X:: Array{$T} )
30
- out = Array ($ T,size (X))
31
- ($ f!)(out, X)
32
- end
28
+ # In-place mutating variant
33
29
function ($ f!)(out:: Array{$T} , X:: Array{$T} )
34
- ccall (($ (string (" vv" ,fa,suff)),libacc),Void ,
35
- (Ptr{$ T},Ptr{$ T},Ptr {Cint}),out,X,& length (X))
30
+ ccall (($ (string (" vv" ,fa,suff)),libacc),Cvoid ,
31
+ (Ptr{$ T},Ptr{$ T},Ref {Cint}),out,X,length (X))
36
32
out
37
33
end
38
34
end
39
35
end
40
36
37
+
41
38
# 2 arg functions
42
- for f in (:copysign ,:atan2 )
43
- f! = symbol (" $(f) !" )
39
+ twoarg_funcs = (
40
+ (:copysign ,:copysign ),
41
+ (:rem ,:fmod ),
42
+ (:div_float ,:div ),
43
+ (:atan ,:atan2 )
44
+ )
45
+
46
+ for (f, fa) in twoarg_funcs
47
+ f! = Symbol (" $(f) !" )
44
48
@eval begin
45
49
function ($ f)(X:: Array{$T} , Y:: Array{$T} )
46
50
size (X) == size (Y) || throw (DimensionMismatch (" Arguments must have same shape" ))
47
- out = Array ( $ T, size (X))
51
+ out = Array {$T} (undef, size (X))
48
52
($ f!)(out, X, Y)
49
53
end
50
54
function ($ f!)(out:: Array{$T} , X:: Array{$T} , Y:: Array{$T} )
51
- ccall (($ (string (" vv" ,f ,suff)),libacc),Void ,
52
- (Ptr{$ T},Ptr{$ T},Ptr{$ T},Ptr {Cint}),out,X,Y,& length (X))
55
+ ccall (($ (string (" vv" ,fa ,suff)),libacc),Cvoid ,
56
+ (Ptr{$ T},Ptr{$ T},Ptr{$ T},Ref {Cint}),out,X,Y,length (X))
53
57
out
54
58
end
55
59
end
56
60
end
57
61
58
62
# for some bizarre reason, vvpow/vvpowf reverse the order of arguments.
59
- for f in (:pow ,)
60
- f! = symbol (" $(f) !" )
63
+ for (f, fa) in (( :pow , :pow ) ,)
64
+ f! = Symbol (" $(f) !" )
61
65
@eval begin
62
66
function ($ f)(X:: Array{$T} , Y:: Array{$T} )
63
67
size (X) == size (Y) || throw (DimensionMismatch (" Arguments must have same shape" ))
64
- out = Array ( $ T, size (X))
68
+ out = Array {$T} (undef, size (X))
65
69
($ f!)(out, X, Y)
66
70
end
67
71
function ($ f!)(out:: Array{$T} , X:: Array{$T} , Y:: Array{$T} )
68
- ccall (($ (string (" vv" ,f,suff)),libacc),Void,
69
- (Ptr{$ T},Ptr{$ T},Ptr{$ T},Ptr{Cint}),out,Y,X,& length (X))
70
- out
71
- end
72
- end
73
- end
74
-
75
-
76
- # renamed 2 arg functions
77
- for (f,fa) in ((:rem ,:fmod ),(:fdiv ,:div ))
78
- f! = symbol (" $(f) !" )
79
- @eval begin
80
- function ($ f)(X:: Array{$T} , Y:: Array{$T} )
81
- size (X) == size (Y) || throw (DimensionMismatch (" Arguments must have same shape" ))
82
- out = Array ($ T,size (X))
83
- ($ f!)(out, X, Y)
84
- end
85
- function ($ f!)(out:: Array{$T} , X:: Array{$T} , Y:: Array{$T} )
86
- ccall (($ (string (" vv" ,fa,suff)),libacc),Void,
87
- (Ptr{$ T},Ptr{$ T},Ptr{$ T},Ptr{Cint}),out,X,Y,& length (X))
72
+ ccall (($ (string (" vv" ,fa,suff)),libacc),Cvoid,
73
+ (Ptr{$ T},Ptr{$ T},Ptr{$ T},Ref{Cint}),out,Y,X,length (X))
88
74
out
89
75
end
90
76
end
91
77
end
92
78
93
79
# two-arg return
94
- for f in (:sincos ,)
95
- f! = symbol (" $(f) !" )
80
+ for (f, fa) in (( :sincos , :sincos ) ,)
81
+ f! = Symbol (" $(f) !" )
96
82
@eval begin
97
83
function ($ f)(X:: Array{$T} )
98
- out1 = Array ( $ T, size (X))
99
- out2 = Array ( $ T, size (X))
84
+ out1 = Array {$T} (undef, size (X))
85
+ out2 = Array {$T} (undef, size (X))
100
86
($ f!)(out1, out2, X)
101
87
end
102
88
function ($ f!)(out1:: Array{$T} , out2:: Array{$T} , X:: Array{$T} )
103
- ccall (($ (string (" vv" ,f,suff)),libacc),Void ,
104
- (Ptr{$ T},Ptr{$ T},Ptr{$ T},Ptr {Cint}),out1,out2,X,& length (X))
89
+ ccall (($ (string (" vv" ,f,suff)),libacc),Cvoid ,
90
+ (Ptr{$ T},Ptr{$ T},Ptr{$ T},Ref {Cint}),out1,out2,X,length (X))
105
91
out1, out2
106
92
end
107
93
end
108
94
end
109
95
110
96
# complex return
111
97
for (f,fa) in ((:cis ,:cosisin ),)
112
- f! = symbol (" $(f) !" )
98
+ f! = Symbol (" $(f) !" )
113
99
@eval begin
114
100
function ($ f)(X:: Array{$T} )
115
- out = Array ( Complex{$ T}, size (X))
101
+ out = Array { Complex{$T}} (undef, size (X))
116
102
($ f!)(out, X)
117
103
end
118
104
function ($ f!)(out:: Array{Complex{$T}} , X:: Array{$T} )
119
- ccall (($ (string (" vv" ,fa,suff)),libacc),Void ,
120
- (Ptr{Complex{$ T}},Ptr{$ T},Ptr {Cint}),out,X,& length (X))
105
+ ccall (($ (string (" vv" ,fa,suff)),libacc),Cvoid ,
106
+ (Ptr{Complex{$ T}},Ptr{$ T},Ref {Cint}),out,X,length (X))
121
107
out
122
108
end
123
109
end
124
110
end
111
+
112
+ for (f, fa) in onearg_funcs
113
+ f! = Symbol (" $(f) !" )
114
+ @eval begin
115
+ # Broadcasting override such that f.(X) turns into f(X)
116
+ Base. copy (bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T, N}}} ) where {Style, Axes, N} = ($ f)(bc. args... )
117
+ Base. copyto! (dest:: Array{$T, N} , bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T, N}}} ) where {Style, Axes, N} = ($ f!)(dest, bc. args... )
118
+ end
119
+ end
120
+ for (f, fa) in (twoarg_funcs... ,(:pow ,:pow ))
121
+ f! = Symbol (" $(f) !" )
122
+ @eval begin
123
+ # Broadcasting override such that f.(X) turns into f(X)
124
+ Base. copy (bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T, N},Array{$T,N}}} ) where {Style, Axes, N} = ($ f)(bc. args... )
125
+ Base. copyto! (dest:: Array{$T, N} , bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T,N},Array{$T,N}}} ) where {Style, Axes, N} = ($ f!)(dest, bc. args... )
126
+ end
127
+ end
125
128
end
126
129
127
130
# Functions over single vectors that return scalars/tuples
@@ -132,7 +135,7 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
132
135
@eval begin
133
136
function ($ f)(X:: Vector{$T} )
134
137
val = Ref {$T} (0.0 )
135
- ccall (($ (string (" vDSP_" , fa, suff), libacc)), Void ,
138
+ ccall (($ (string (" vDSP_" , fa, suff), libacc)), Cvoid ,
136
139
(Ptr{$ T}, Int64, Ref{$ T}, UInt64),
137
140
X, 1 , val, length (X))
138
141
return val[]
@@ -145,7 +148,7 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
145
148
function ($ f)(X:: Vector{$T} )
146
149
index = Ref {Int} (0 )
147
150
val = Ref {$T} (0.0 )
148
- ccall (($ (string (" vDSP_" , fa, suff), libacc)), Void ,
151
+ ccall (($ (string (" vDSP_" , fa, suff), libacc)), Cvoid ,
149
152
(Ptr{$ T}, Int64, Ref{$ T}, Ref{Int}, UInt64),
150
153
X, 1 , val, index, length (X))
151
154
return (val[], index[]+ 1 )
@@ -159,7 +162,7 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
159
162
160
163
for (f, name) in ((:vadd , " addition" ), (:vsub , " subtraction" ),
161
164
(:vdiv , " division" ), (:vmul , " multiplication" ))
162
- f! = symbol (" $(f) !" )
165
+ f! = Symbol (" $(f) !" )
163
166
164
167
@eval begin
165
168
@doc """
@@ -169,7 +172,7 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
169
172
the result vector with computed value. *Returns:* **Vector{$($ T) }** `result`
170
173
""" ->
171
174
function ($ f!)(result:: Vector{$T} , X:: Vector{$T} , Y:: Vector{$T} )
172
- ccall (($ (string (" vDSP_" , f, suff), libacc)), Void ,
175
+ ccall (($ (string (" vDSP_" , f, suff), libacc)), Cvoid ,
173
176
(Ptr{$ T}, Int64, Ptr{$ T}, Int64, Ptr{$ T}, Int64, UInt64),
174
177
Y, 1 , X, 1 , result, 1 , length (result))
175
178
return result
0 commit comments