-
Notifications
You must be signed in to change notification settings - Fork 2
/
smoothfunctions.va
267 lines (239 loc) · 6.67 KB
/
smoothfunctions.va
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// This Verilog-A file contains the implementations of some common smooth
// functions and safe functions.
//
// Here is a list of the available functions:
//
// smooth functions with inputs (x, smoothing):
// smoothabs, dsmoothabs, ddsmoothabs,
// smoothclip, dsmoothclip, ddsmoothclip,
// smoothstep, dsmoothstep,
// smoothsign, dsmoothsign,
//
// smooth functions with inputs (a, b, smoothing):
// smoothmin, dsmoothmin_da, dsmoothmin_db,
// smoothmax, dsmoothmax_da, dsmoothmax_db,
//
// smooth functions with inputs (a, b, x, smoothing):
// smoothswitch, dsmoothswitch_da, dsmoothswitch_db, dsmoothswitch_dx,
//
// safe functions with inputs (x, maxslope):
// safeexp, dsafeexp,
// safesinh, dsafesinh,
//
// safe functions with inputs (x, smoothing):
// safelog, dsafelog,
// safesqrt, dsafesqrt,
//
// Parameter smoothing controls the smoothness and accuracy of the functions.
// The smaller smoothing is, the closer the function is to the non-smooth
// version.
//
// Parameter maxslope controls the maximum slope of safeexp and safesinh
// functions.
analog function real smoothabs;
input x, smoothing;
real x, smoothing;
begin
smoothabs = sqrt(x*x + smoothing) - sqrt(smoothing);
end
endfunction // smoothabs
analog function real dsmoothabs;
input x, smoothing;
real x, smoothing;
begin
dsmoothabs = x/sqrt(x*x + smoothing);
end
endfunction // dsmoothabs
analog function real ddsmoothabs;
input x, smoothing;
real x, smoothing;
begin
ddsmoothabs = -x/pow(smoothabs(x, smoothing), 2) *
dsmoothabs(x, smoothing) + 1/smoothabs(x, smoothing);
end
endfunction // ddsmoothabs
analog function real smoothclip;
input x, smoothing;
real x, smoothing;
begin
smoothclip = 0.5*(smoothabs(x, smoothing) + x);
end
endfunction // smoothclip
analog function real dsmoothclip;
input x, smoothing;
real x, smoothing;
begin
dsmoothclip = 0.5*(dsmoothabs(x, smoothing)) + 0.5;
end
endfunction // dsmoothclip
analog function real ddsmoothclip;
input x, smoothing;
real x, smoothing;
begin
ddsmoothclip = 0.5*ddsmoothabs(x, smoothing);
end
endfunction // ddsmoothclip
analog function real smoothstep;
input x, smoothing;
real x, smoothing;
begin
smoothstep = dsmoothclip(x, smoothing);
end
endfunction // smoothstep
analog function real dsmoothstep;
input x, smoothing;
real x, smoothing;
begin
dsmoothstep = ddsmoothclip(x, smoothing);
end
endfunction // dsmoothstep
analog function real smoothsign;
input x, smoothing;
real x, smoothing;
begin
smoothsign = 2*smoothstep(x, smoothing)-1;
end
endfunction // smoothsign
analog function real dsmoothsign;
input x, smoothing;
real x, smoothing;
begin
dsmoothsign = 2*dsmoothstep(x, smoothing);
end
endfunction // dsmoothsign
analog function real smoothmin;
input a, b, smoothing;
real a, b, smoothing;
begin
smoothmin = 0.5*(a + b - smoothabs(a-b,smoothing));
end
endfunction // smoothmin
analog function real dsmoothmin_da;
input a, b, smoothing;
real a, b, smoothing;
begin
dsmoothmin_da = 0.5*(1 - dsmoothabs(a-b, smoothing));
end
endfunction // dsmoothmin_da
analog function real dsmoothmin_db;
input a, b, smoothing;
real a, b, smoothing;
begin
dsmoothmin_db = 0.5*(1 + dsmoothabs(a-b, smoothing));
end
endfunction // dsmoothmin_db
analog function real smoothmax;
input a, b, smoothing;
real a, b, smoothing;
begin
smoothmax = 0.5*(a + b + smoothabs(a-b, smoothing));
end
endfunction // smoothmax
analog function real dsmoothmax_da;
input a, b, smoothing;
real a, b, smoothing;
begin
dsmoothmax_da = 0.5*(1 + dsmoothabs(a-b, smoothing));
end
endfunction // dsmoothmax_da
analog function real dsmoothmax_db;
input a, b, smoothing;
real a, b, smoothing;
begin
dsmoothmax_db = 0.5*(1 - dsmoothabs(a-b, smoothing));
end
endfunction // dsmoothmax_db
analog function real smoothswitch;
input a, b, x, smoothing;
real a, b, x, smoothing, oof;
begin
oof = smoothstep(x, smoothing);
smoothswitch = a*(1-oof) + b*oof;
end
endfunction // smoothswitch
analog function real dsmoothswitch_da;
input a, b, x, smoothing;
real a, b, x, smoothing, oof;
begin
oof = smoothstep(x, smoothing);
dsmoothswitch_da = 1-oof;
end
endfunction // dsmoothswitch_da
analog function real dsmoothswitch_db;
input a, b, x, smoothing;
real a, b, x, smoothing, oof;
begin
oof = smoothstep(x, smoothing);
dsmoothswitch_db = oof;
end
endfunction // dsmoothswitch_db
analog function real dsmoothswitch_dx;
input a, b, x, smoothing;
real a, b, x, smoothing, doof;
begin
doof = dsmoothstep(x, smoothing);
dsmoothswitch_dx = (-a+b) * doof;
end
endfunction // dsmoothswitch_dx
analog function real safeexp;
input x, maxslope;
real x, maxslope, breakpoint;
begin
breakpoint = ln(maxslope);
safeexp = exp(x*(x <= breakpoint))*(x <= breakpoint) +
(x>breakpoint)*(maxslope + maxslope*(x-breakpoint));
end
endfunction // safeexp
analog function real dsafeexp;
input x, maxslope;
real x, maxslope, breakpoint;
begin
breakpoint = ln(maxslope);
dsafeexp = exp(x*(x <= breakpoint))*(x <= breakpoint) +
(x>breakpoint)*maxslope;
end
endfunction // dsafeexp
analog function real safesinh;
input x, maxslope;
real x, maxslope;
begin
safesinh = 0.5*(safeexp(x, maxslope) - safeexp(-x, maxslope));
end
endfunction // safesinh
analog function real dsafesinh;
input x, maxslope;
real x, maxslope;
begin
dsafesinh = 0.5*(dsafeexp(x, maxslope) - dsafeexp(-x, maxslope));
end
endfunction // dsafesinh
analog function real safelog;
input x, smoothing;
real x, smoothing;
begin
safelog = ln(smoothclip(x, smoothing) + 1e-16);
end
endfunction // safelog
analog function real dsafelog;
input x, smoothing;
real x, smoothing;
begin
dsafelog = 1/(smoothclip(x, smoothing) + 1e-16)
* dsmoothclip(x, smoothing);
end
endfunction // dsafelog
analog function real safesqrt;
input x, smoothing;
real x, smoothing;
begin
safesqrt = sqrt(smoothclip(x, smoothing) + 1e-16);
end
endfunction // safesqrt
analog function real dsafesqrt;
input x, smoothing;
real x, smoothing;
begin
dsafesqrt = 0.5 / sqrt(smoothclip(x, smoothing) + 1e-16)
* dsmoothclip(x, smoothing);
end
endfunction // dsafesqrt