Debian Codegraph @

texmf-dist/tex/latex/sa-tikz/tikzlibraryswitching-architectures.code.tex

raw
1 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 %
3 % Sa-TikZ package v0.7a * * (C) Claudio Fiandrino 2012-2014
4 %
5 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
6
7 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
8 % LOADING NECESSARY LIBRARIES
9 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10
11 \usetikzlibrary{backgrounds,calc,positioning,decorations.pathreplacing}
12
13 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
14 % UTILITY
15 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
16
17 % PGFMATHISODD: 1 = true, 0 = false
18 % from TikZ 3.0.0
19 % Test for checking whether \pgfmathisodd is defined or not
20 % for compatibility with TikZ 2.10
21 %
22 \@ifundefined{pgfmathisodd}{
23 \pgfmathdeclarefunction{isodd}{1}{%
24 \begingroup
25 \pgfmathsetcount\c@pgfmath@counta{abs(int(#1))}%
26 \ifodd\c@pgfmath@counta
27 \def\pgfmathresult{1}%
28 \else
29 \def\pgfmathresult{0}%
30 \fi
31 \pgfmath@smuggleone\pgfmathresult
32 \endgroup}
33 }{}
34
35 % PGFMATHOMEGAROTATION:
36 %
37 % #1: number to be rotated
38 % #2: numbers of bits
39 % #3: output macro
40 %
41 \newcommand*{\pgfmathomegarotation}[3]{
42 \pgfmathisodd{#1}
43 \ifnum\pgfmathresult=1
44 \pgfmathparse{#1/2 + 2^#2}
45 \else
46 \pgfmathparse{#1/2}
47 \fi
48 \pgfmathtruncatemacro\res\pgfmathresult
49 \global\expandafter\edef\csname #3\endcsname{\res}
50 }
51
52 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
53 % KEY DEFINITION - Design choices
54 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
55
56 % * * * * * * * * * * * * * * * * * *
57 % CLOS
58 % * * * * * * * * * * * * * * * * * *
59
60 % N is the key representing the number of inputs x number of modules first stage
61 \pgfkeys{/tikz/.cd,%
62 N/.store in=\N,%
63 N=10,%
64 }%
65
66 % N label
67 \pgfkeys{/tikz/.cd,%
68 N label/.store in=\Nlabel,%
69 N label=N,%
70 }%
71
72 % r1 is the number of modules first stage
73 % m1 is the number of inputs first stage per module
74
75 \pgfkeys{/tikz/.cd,%
76 r1/.store in=\rone,%
77 r1=5,%
78 }%
79
80 % r1 label
81 \pgfkeys{/tikz/.cd,%
82 r1 label/.store in=\ronelabel,%
83 r1 label={r\ensuremath{_1}},%
84 }%
85
86 % m1 label
87 \pgfkeys{/tikz/.cd,%
88 m1 label/.store in=\monelabel,%
89 m1 label={m\ensuremath{_1}},%
90 }%
91
92 % r2 label
93 \pgfkeys{/tikz/.cd,%
94 r2 label/.store in=\rtwolabel,%
95 r2 label={r\ensuremath{_2}},%
96 }%
97
98 % M is the key representing the number of inputs x number of modules last stage
99 \pgfkeys{/tikz/.cd,%
100 M/.store in=\M,%
101 M=10,%
102 }%
103
104 % M label
105 \pgfkeys{/tikz/.cd,%
106 M label/.store in=\Mlabel,%
107 M label=M,%
108 }%
109
110 % r3 is the number of modules last stage
111 % m3 is the number of inputs last stage per module
112 \pgfkeys{/tikz/.cd,%
113 r3/.store in=\rthree,%
114 r3=5
115 }%
116
117 % r3 label
118 \pgfkeys{/tikz/.cd,%
119 r3 label/.store in=\rthreelabel,%
120 r3 label={r\ensuremath{_3}},%
121 }%
122
123 % m3 label
124 \pgfkeys{/tikz/.cd,
125 m3 label/.store in=\mthreelabel,%
126 m3 label={m\ensuremath{_3}},%
127 }%
128
129 % * * * * * * * * * * * * * * * * * *
130 % BENES
131 % * * * * * * * * * * * * * * * * * *
132
133 % P is the number of input/output ports
134 \pgfkeys{/tikz/.cd,%
135 P/.store in=\P,%
136 P=8,%
137 }%
138
139 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
140 % GENERAL SETTINGS - Keys and styles
141 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
142
143 % module customization
144 \pgfkeys{/tikz/.cd,%
145 module size/.store in=\modulesize,%
146 module size={1cm},%
147 }%
148
149 \pgfkeys{/tikz/.cd,%
150 module ysep/.store in=\moduleysep,%
151 module ysep={1.5},
152 }%
153
154 \pgfkeys{/tikz/.cd,%
155 module xsep/.store in=\modulexsep,%
156 module xsep={3},%
157 }%
158
159 \pgfkeys{/tikz/.cd,%
160 module font/.store in=\modulefont,%
161 module font={\normalfont},%
162 }%
163
164 \tikzset{module/.style={%
165 draw,rectangle, minimum size=\modulesize,
166 font=\modulefont,
167 }
168 }
169
170 \tikzset{module extensible/.style={%
171 draw,rectangle, minimum size=#1,
172 },
173 module extensible/.default={\modulesize}
174 }
175
176 \pgfkeys{/tikz/.cd,%
177 module label opacity/.store in=\modulelabelopacity,%
178 module label opacity={1},%
179 }%
180
181 \tikzset{module opacity/.style={
182 text opacity=\modulelabelopacity,
183 }
184 }
185
186 \pgfkeys{/tikz/.cd,%
187 pin length factor/.store in=\pinlength,%
188 pin length factor={1},%
189 }%
190
191 % setting labels in math mode
192
193 \tikzset{math mode labels/.style={%
194 execute at begin node=$,%
195 execute at end node=$,%
196 }
197 }
198 \pgfkeys{/tikz/.cd,%
199 use math mode labels/.is choice,%
200 use math mode labels/true/.style={math mode labels},%
201 use math mode labels/false/.style={},%
202 }%
203
204 \tikzset{set math mode labels/.style={%
205 use math mode labels=#1,%
206 },%
207 set math mode labels/.default=false,%
208 }
209
210 % disable the connections
211 \newif\ifconnectiondisabled%
212 \pgfkeys{/tikz/.cd, connections disabled/.is if=connectiondisabled}%
213 \pgfkeys{/tikz/.cd, connections disabled/.default=false}%
214
215 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
216 % CODE
217 % * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
218
219 % CLOS SNB
220 \tikzset{clos snb/.code={
221
222 % Number of ports per module
223 \pgfmathtruncatemacro{\mone}{\N/\rone}
224 \pgfmathtruncatemacro{\mthree}{\M/\rthree}
225
226 % COMPUTATION SNB CONDITION
227 \pgfmathtruncatemacro\rtwo{\mone+\mthree-1}
228
229 % MODULE 1
230 \foreach \i in {1,...,\rone}{
231 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
232 in
233 node[module,#1,module opacity,yshift=1cm] (r1-\i) at +(0,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
234
235 % INPUTS MODULE 1
236 % the number of inputs module one is exactly \mone
237 \pgfmathsetmacro\roneintervalspace{1/(\mone+1)}
238 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput]
239 in {1,...,\mone}
240 \draw ($(r1-\i.north west)!\roneinterval!(r1-\i.south west)-(0.5*\pinlength,0)$)node[scale=0.1](r1-\i-front input-\roneinput){}--($(r1-\i.north west)!\roneinterval!(r1-\i.south west)$) node[circle,draw,scale=0.1] (r1-\i-input-\roneinput) {};
241
242 % OUTPUTS MODULE 1
243 % the number of outputs of module one is the number of modules stage 2 \rtwo
244 \pgfmathsetmacro\roneintervalspace{1/(\rtwo+1)}
245 \foreach \roneoutput[evaluate=\roneoutput as \roneinterval using \roneintervalspace*\roneoutput]
246 in {1,...,\rtwo}
247 \node[circle,draw,scale=0.1] (r1-\i-output-\roneoutput)at($(r1-\i.north east)!\roneinterval!(r1-\i.south east)$) {};
248 }
249
250 % MODULE 2
251 \foreach \i in {1,...,\rtwo}{
252 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
253 in
254 node[module,#1,module opacity,yshift=1cm] (r2-\i) at +(\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
255
256 % INPUTS MODULE 2
257 % the number of inputs of module two is the number of modules stage 1 \rone
258 \pgfmathsetmacro\rtwointervalspace{1/(\rone+1)}
259 \foreach \rtwoinput[evaluate=\rtwoinput as \rtwointerval using \rtwointervalspace*\rtwoinput]
260 in {1,...,\rone}
261 \node[circle,draw,scale=0.1] (r2-\i-input-\rtwoinput)at($(r2-\i.north west)!\rtwointerval!(r2-\i.south west)$) {};
262
263 % OUTPUTS MODULE 2
264 % the number of outputs module two is exactly \rthree
265 \pgfmathsetmacro\rtwointervalspace{1/(\rthree+1)}
266 \foreach \rtwooutput[evaluate=\rtwooutput as \rtwointerval using \rtwointervalspace*\rtwooutput]
267 in {1,...,\rthree}
268 \node[circle,draw,scale=0.1] (r2-\i-output-\rtwooutput)at ($(r2-\i.north east)!\rtwointerval!(r2-\i.south east)$) {};
269
270 }
271
272 % MODULE 3
273 \foreach \i in {1,...,\rthree}{
274 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
275 in
276 node[module,#1,module opacity,yshift=1cm] (r3-\i) at +(2*\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
277
278 % INPUTS MODULE 3
279 % the number of inputs of module three is the number of modules stage 2 \rtwo
280 \pgfmathsetmacro\rthreeintervalspace{1/(\rtwo+1)}
281 \foreach \rthreeinput[evaluate=\rthreeinput as \rthreeinterval using \rthreeintervalspace*\rthreeinput]
282 in {1,...,\rtwo}
283 \node[circle,draw,scale=0.1] (r3-\i-input-\rthreeinput)at($(r3-\i.north west)!\rthreeinterval!(r3-\i.south west)$) {};
284
285 % OUTPUTS MODULE 3
286 % the number of outputs module three is exactly \mthree
287 \pgfmathsetmacro\rthreeintervalspace{1/(\mthree+1)}
288 \foreach \rthreeoutput[evaluate=\rthreeoutput as \rthreeinterval using \rthreeintervalspace*\rthreeoutput]
289 in {1,...,\mthree}
290 \draw ($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)+(0.5*\pinlength,0)$)node[scale=0.1](r3-\i-front output-\rthreeoutput){}--($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)$) node[circle,draw,scale=0.1] (r3-\i-output-\rthreeoutput) {};
291
292 }
293
294 % Test if connections should be removed
295 \ifconnectiondisabled
296 \relax
297 \else
298 % DRAWING CONNECTIONS
299 %% from r1 to r2
300 \foreach \startmodule in {1,...,\rone}{
301 \foreach \conn in {1,...,\rtwo}
302 \draw(r1-\startmodule-output-\conn)--(r2-\conn-input-\startmodule);
303 }
304 %% from r2 to r3
305 \foreach \startmodule in {1,...,\rthree}{
306 \foreach \conn in {1,...,\rtwo}
307 \draw(r3-\startmodule-input-\conn)--(r2-\conn-output-\startmodule);
308 }
309 \fi
310 },
311 }
312
313 \tikzset{clos snb example/.code={
314
315 % Number of ports per module
316 \pgfmathtruncatemacro{\mone}{\N/\rone}
317 \pgfmathtruncatemacro{\mthree}{\M/\rthree}
318
319 % COMPUTATION SNB CONDITION
320 \pgfmathtruncatemacro\rtwo{\mone+\mthree-1}
321
322 % MODULE 1
323 \node[module,#1,module opacity](r1-1) at (0,0) {1};
324 \node[below of=r1-1,yshift=0.75ex](r1-dots) {\vdots};
325 \node[module,#1,module opacity,below of=r1-dots](r1-2) {\rone};
326
327 \foreach \i in {1,2}{
328 % INPUTS MODULE 1
329 % just two modules
330 \pgfmathsetmacro\roneintervalspace{1/(2+1)}
331 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput]
332 in {1,2}
333 \draw ($(r1-\i.north west)!\roneinterval!(r1-\i.south west)-(0.5*\pinlength,0)$)node[scale=0.1](r1-\i-front input-\roneinput){}--($(r1-\i.north west)!\roneinterval!(r1-\i.south west)$) node[circle,draw,scale=0.1] (r1-\i-input-\roneinput) {};
334
335 % OUTPUTS MODULE 1
336 % just two modules
337 \pgfmathsetmacro\roneintervalspace{1/(2+1)}
338 \foreach \roneoutput[evaluate=\roneoutput as \roneinterval using \roneintervalspace*\roneoutput]
339 in {1,2}
340 \node[circle,draw,scale=0.1] (r1-\i-output-\roneoutput)at($(r1-\i.north east)!\roneinterval!(r1-\i.south east)$) {};
341 }
342
343 % MODULE 2
344 \node[module,#1,module opacity](r2-1) at (\modulexsep,0) {1};
345 \node[below of=r2-1,yshift=0.75ex](r2-dots) {\vdots};
346 \node[module,#1,module opacity,below of=r2-dots](r2-2) {\rtwo};
347
348 \foreach \i in {1,2}{
349 % INPUTS MODULE 2
350 % just two modules
351 \pgfmathsetmacro\rtwointervalspace{1/(2+1)}
352 \foreach \rtwoinput[evaluate=\rtwoinput as \rtwointerval using \rtwointervalspace*\rtwoinput]
353 in {1,2}
354 \node[circle,draw,scale=0.1] (r2-\i-input-\rtwoinput)at($(r2-\i.north west)!\rtwointerval!(r2-\i.south west)$) {};
355
356 % OUTPUTS MODULE 2
357 % just two modules
358 \pgfmathsetmacro\rtwointervalspace{1/(2+1)}
359 \foreach \rtwooutput[evaluate=\rtwooutput as \rtwointerval using \rtwointervalspace*\rtwooutput]
360 in {1,2}
361 \node[circle,draw,scale=0.1] (r2-\i-output-\rtwooutput)at ($(r2-\i.north east)!\rtwointerval!(r2-\i.south east)$) {};
362 }
363
364 % MODULE 3
365 \node[module,#1,module opacity](r3-1) at (2*\modulexsep,0) {1};
366 \node[below of=r3-1,yshift=0.75ex](r3-dots) {\vdots};
367 \node[module,#1,module opacity,below of=r3-dots](r3-2) {\rthree};
368
369 \foreach \i in {1,2}{
370 % INPUTS MODULE 3
371 % just two modules
372 \pgfmathsetmacro\rthreeintervalspace{1/(2+1)}
373 \foreach \rthreeinput[evaluate=\rthreeinput as \rthreeinterval using \rthreeintervalspace*\rthreeinput]
374 in {1,2}
375 \node[circle,draw,scale=0.1] (r3-\i-input-\rthreeinput)at($(r3-\i.north west)!\rthreeinterval!(r3-\i.south west)$) {};
376
377 % OUTPUTS MODULE 3
378 % just two modules
379 \pgfmathsetmacro\rthreeintervalspace{1/(2+1)}
380 \foreach \rthreeoutput[evaluate=\rthreeoutput as \rthreeinterval using \rthreeintervalspace*\rthreeoutput]
381 in {1,2}
382 \draw ($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)+(0.5*\pinlength,0)$)node[scale=0.1](r3-\i-front output-\rthreeoutput){}--($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)$) node[circle,draw,scale=0.1] (r3-\i-output-\rthreeoutput) {};
383 }
384
385 % DRAWING CONNECTIONS
386 %% from r1 to r2
387 \foreach \startmodule in {1,2}{
388 \foreach \conn in {1,2}
389 \draw(r1-\startmodule-output-\conn)--(r2-\conn-input-\startmodule);
390 }
391 %% from r2 to r3
392 \foreach \startmodule in {1,2}{
393 \foreach \conn in {1,2}
394 \draw(r3-\startmodule-input-\conn)--(r2-\conn-output-\startmodule);
395 }
396
397 % SETTING LABELS
398 \node[below of=r1-2,set math mode labels] {\mone~\ensuremath{\times}~\rtwo};
399 \node[below of=r2-2,set math mode labels] {\rone~\ensuremath{\times}~\rthree};
400 \node[below of=r3-2,set math mode labels] {\rtwo~\ensuremath{\times}~\mthree};
401 \draw[decorate,decoration={brace}]($(r1-2-front input-2)-(0.1,0)$)--($(r1-1-front input-1)-(0.1,0)$) node[midway,left=0.1cm,set math mode labels]{\N};
402 \draw[decorate,decoration={brace}]($(r3-1-front output-1)+(0.1,0)$)--($(r3-2-front output-2)+(0.1,0)$) node[midway,right=0.1cm,set math mode labels]{\M};
403 },
404 }
405
406 % CLOS REAR
407
408 \tikzset{clos rear/.code={
409
410 % Number of ports per module
411 \pgfmathtruncatemacro{\mone}{\N/\rone}
412 \pgfmathtruncatemacro{\mthree}{\M/\rthree}
413
414 % COMPUTATION REAR CONDITION
415 \pgfmathtruncatemacro\rtwo{max(\mone,\mthree)}
416
417 % MODULE 1
418 \foreach \i in {1,...,\rone}{
419 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
420 in
421 node[module,#1,module opacity,yshift=1cm] (r1-\i) at +(0,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
422
423 % INPUTS MODULE 1
424 % the number of inputs module one is exactly \mone
425 \pgfmathsetmacro\roneintervalspace{1/(\mone+1)}
426 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput]
427 in {1,...,\mone}
428 \draw ($(r1-\i.north west)!\roneinterval!(r1-\i.south west)-(0.5*\pinlength,0)$)node[scale=0.1](r1-\i-front input-\roneinput){}--($(r1-\i.north west)!\roneinterval!(r1-\i.south west)$) node[circle,draw,scale=0.1] (r1-\i-input-\roneinput) {};
429
430 % OUTPUTS MODULE 1
431 % the number of outputs of module one is the number of modules stage 2 \rtwo
432 \pgfmathsetmacro\roneintervalspace{1/(\rtwo+1)}
433 \foreach \roneoutput[evaluate=\roneoutput as \roneinterval using \roneintervalspace*\roneoutput]
434 in {1,...,\rtwo}
435 \node[circle,draw,scale=0.1] (r1-\i-output-\roneoutput)at($(r1-\i.north east)!\roneinterval!(r1-\i.south east)$) {};
436 }
437
438 % MODULE 2
439 \foreach \i in {1,...,\rtwo}{
440 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
441 in
442 node[module,#1,module opacity,yshift=1cm] (r2-\i) at +(\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
443
444 % INPUTS MODULE 2
445 % the number of inputs of module two is the number of modules stage 1 \rone
446 \pgfmathsetmacro\rtwointervalspace{1/(\rone+1)}
447 \foreach \rtwoinput[evaluate=\rtwoinput as \rtwointerval using \rtwointervalspace*\rtwoinput]
448 in {1,...,\rone}
449 \node[circle,draw,scale=0.1] (r2-\i-input-\rtwoinput)at($(r2-\i.north west)!\rtwointerval!(r2-\i.south west)$) {};
450
451 % OUTPUTS MODULE 2
452 % the number of outputs module two is exactly \rthree
453 \pgfmathsetmacro\rtwointervalspace{1/(\rthree+1)}
454 \foreach \rtwooutput[evaluate=\rtwooutput as \rtwointerval using \rtwointervalspace*\rtwooutput]
455 in {1,...,\rthree}
456 \node[circle,draw,scale=0.1] (r2-\i-output-\rtwooutput)at ($(r2-\i.north east)!\rtwointerval!(r2-\i.south east)$) {};
457
458 }
459
460 % MODULE 3
461 \foreach \i in {1,...,\rthree}{
462 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
463 in
464 node[module,#1,module opacity,yshift=1cm] (r3-\i) at +(2*\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
465
466 % INPUTS MODULE 3
467 % the number of inputs of module three is the number of modules stage 2 \rtwo
468 \pgfmathsetmacro\rthreeintervalspace{1/(\rtwo+1)}
469 \foreach \rthreeinput[evaluate=\rthreeinput as \rthreeinterval using \rthreeintervalspace*\rthreeinput]
470 in {1,...,\rtwo}
471 \node[circle,draw,scale=0.1] (r3-\i-input-\rthreeinput)at($(r3-\i.north west)!\rthreeinterval!(r3-\i.south west)$) {};
472
473 % OUTPUTS MODULE 3
474 % the number of outputs module three is exactly \mthree
475 \pgfmathsetmacro\rthreeintervalspace{1/(\mthree+1)}
476 \foreach \rthreeoutput[evaluate=\rthreeoutput as \rthreeinterval using \rthreeintervalspace*\rthreeoutput]
477 in {1,...,\mthree}
478 \draw ($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)+(0.5*\pinlength,0)$)node[scale=0.1](r3-\i-front output-\rthreeoutput){}--($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)$) node[circle,draw,scale=0.1] (r3-\i-output-\rthreeoutput) {};
479 }
480
481 % Test if connections should be removed
482 \ifconnectiondisabled
483 \relax
484 \else
485 % DRAWING CONNECTIONS
486 %% from r1 to r2
487 \foreach \startmodule in {1,...,\rone}{
488 \foreach \conn in {1,...,\rtwo}
489 \draw(r1-\startmodule-output-\conn)--(r2-\conn-input-\startmodule);
490 }
491 %% from r2 to r3
492 \foreach \startmodule in {1,...,\rthree}{
493 \foreach \conn in {1,...,\rtwo}
494 \draw(r3-\startmodule-input-\conn)--(r2-\conn-output-\startmodule);
495 }
496 \fi
497 }
498 }
499
500 \tikzset{clos rear example/.code={
501
502 % Number of ports per module
503 \pgfmathtruncatemacro{\mone}{\N/\rone}
504 \pgfmathtruncatemacro{\mthree}{\M/\rthree}
505
506 % COMPUTATION REAR CONDITION
507 \pgfmathtruncatemacro\rtwo{max(\mone,\mthree)}
508
509 % MODULE 1
510 \node[module,#1,module opacity](r1-1) at (0,0) {1};
511 \node[below of=r1-1,yshift=0.75ex](r1-dots) {\vdots};
512 \node[module,#1,module opacity,below of=r1-dots](r1-2) {\rone};
513
514 \foreach \i in {1,2}{
515 % INPUTS MODULE 1
516 % just two modules
517 \pgfmathsetmacro\roneintervalspace{1/(2+1)}
518 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput]
519 in {1,2}
520 \draw ($(r1-\i.north west)!\roneinterval!(r1-\i.south west)-(0.5*\pinlength,0)$)node[scale=0.1](r1-\i-front input-\roneinput){}--($(r1-\i.north west)!\roneinterval!(r1-\i.south west)$) node[circle,draw,scale=0.1] (r1-\i-input-\roneinput) {};
521
522 % OUTPUTS MODULE 1
523 % just two modules
524 \pgfmathsetmacro\roneintervalspace{1/(2+1)}
525 \foreach \roneoutput[evaluate=\roneoutput as \roneinterval using \roneintervalspace*\roneoutput]
526 in {1,2}
527 \node[circle,draw,scale=0.1] (r1-\i-output-\roneoutput)at($(r1-\i.north east)!\roneinterval!(r1-\i.south east)$) {};
528 }
529
530 % MODULE 2
531 \node[module,#1,module opacity](r2-1) at (\modulexsep,0) {1};
532 \node[below of=r2-1,yshift=0.75ex](r2-dots) {\vdots};
533 \node[module,#1,module opacity,below of=r2-dots](r2-2) {\rtwo};
534
535 \foreach \i in {1,2}{
536 % INPUTS MODULE 2
537 % just two modules
538 \pgfmathsetmacro\rtwointervalspace{1/(2+1)}
539 \foreach \rtwoinput[evaluate=\rtwoinput as \rtwointerval using \rtwointervalspace*\rtwoinput]
540 in {1,2}
541 \node[circle,draw,scale=0.1] (r2-\i-input-\rtwoinput)at($(r2-\i.north west)!\rtwointerval!(r2-\i.south west)$) {};
542
543 % OUTPUTS MODULE 2
544 % just two modules
545 \pgfmathsetmacro\rtwointervalspace{1/(2+1)}
546 \foreach \rtwooutput[evaluate=\rtwooutput as \rtwointerval using \rtwointervalspace*\rtwooutput]
547 in {1,2}
548 \node[circle,draw,scale=0.1] (r2-\i-output-\rtwooutput)at ($(r2-\i.north east)!\rtwointerval!(r2-\i.south east)$) {};
549 }
550
551 % MODULE 3
552 \node[module,#1,module opacity](r3-1) at (2*\modulexsep,0) {1};
553 \node[below of=r3-1,yshift=0.75ex](r3-dots) {\vdots};
554 \node[module,#1,module opacity,below of=r3-dots](r3-2) {\rthree};
555
556 \foreach \i in {1,2}{
557 % INPUTS MODULE 3
558 % just two modules
559 \pgfmathsetmacro\rthreeintervalspace{1/(2+1)}
560 \foreach \rthreeinput[evaluate=\rthreeinput as \rthreeinterval using \rthreeintervalspace*\rthreeinput]
561 in {1,2}
562 \node[circle,draw,scale=0.1] (r3-\i-input-\rthreeinput)at($(r3-\i.north west)!\rthreeinterval!(r3-\i.south west)$) {};
563
564 % OUTPUTS MODULE 3
565 % just two modules
566 \pgfmathsetmacro\rthreeintervalspace{1/(2+1)}
567 \foreach \rthreeoutput[evaluate=\rthreeoutput as \rthreeinterval using \rthreeintervalspace*\rthreeoutput]
568 in {1,2}
569 \draw ($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)+(0.5*\pinlength,0)$)node[scale=0.1](r3-\i-front output-\rthreeoutput){}--($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)$) node[circle,draw,scale=0.1] (r3-\i-output-\rthreeoutput) {};
570 }
571
572 % DRAWING CONNECTIONS
573 %% from r1 to r2
574 \foreach \startmodule in {1,2}{
575 \foreach \conn in {1,2}
576 \draw(r1-\startmodule-output-\conn)--(r2-\conn-input-\startmodule);
577 }
578 %% from r2 to r3
579 \foreach \startmodule in {1,2}{
580 \foreach \conn in {1,2}
581 \draw(r3-\startmodule-input-\conn)--(r2-\conn-output-\startmodule);
582 }
583
584 % SETTING LABELS
585 \node[below of=r1-2, set math mode labels] {\mone~\ensuremath{\times}~\rtwo};
586 \node[below of=r2-2, set math mode labels] {\rone~\ensuremath{\times}~\rthree};
587 \node[below of=r3-2, set math mode labels] {\rtwo~\ensuremath{\times}~\mthree};
588 \draw[decorate,decoration={brace}]($(r1-2-front input-2)-(0.1,0)$)--($(r1-1-front input-1)-(0.1,0)$) node[midway,left=0.1cm, set math mode labels]{\N};
589 \draw[decorate,decoration={brace}]($(r3-1-front output-1)+(0.1,0)$)--($(r3-2-front output-2)+(0.1,0)$) node[midway,right=0.1cm, set math mode labels]{\M};
590 },
591 }
592
593 % CLOS EXAMPLE WITH LABELS
594
595 \tikzset{clos example with labels/.code={
596
597 % Number of ports per module
598 \pgfmathtruncatemacro{\mone}{\N/\rone}
599 \pgfmathtruncatemacro{\mthree}{\M/\rthree}
600
601 % COMPUTATION REAR CONDITION
602 \pgfmathtruncatemacro\rtwo{max(\mone,\mthree)}
603
604 % MODULE 1
605 \node[module,#1,module opacity](r1-1) at (0,0) {1};
606 \node[below of=r1-1,yshift=0.75ex](r1-dots) {\vdots};
607 \node[module,#1,module opacity,below of=r1-dots](r1-2) {\ronelabel};
608
609 \foreach \i in {1,2}{
610 % INPUTS MODULE 1
611 % just two modules
612 \pgfmathsetmacro\roneintervalspace{1/(2+1)}
613 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput]
614 in {1,2}
615 \draw ($(r1-\i.north west)!\roneinterval!(r1-\i.south west)-(0.5*\pinlength,0)$)node[scale=0.1](r1-\i-front input-\roneinput){}--($(r1-\i.north west)!\roneinterval!(r1-\i.south west)$) node[circle,draw,scale=0.1] (r1-\i-input-\roneinput) {};
616
617 % OUTPUTS MODULE 1
618 % just two modules
619 \pgfmathsetmacro\roneintervalspace{1/(2+1)}
620 \foreach \roneoutput[evaluate=\roneoutput as \roneinterval using \roneintervalspace*\roneoutput]
621 in {1,2}
622 \node[circle,draw,scale=0.1] (r1-\i-output-\roneoutput)at($(r1-\i.north east)!\roneinterval!(r1-\i.south east)$) {};
623 }
624
625 % MODULE 2
626 \node[module,#1,module opacity](r2-1) at (\modulexsep,0) {1};
627 \node[below of=r2-1,yshift=0.75ex](r2-dots) {\vdots};
628 \node[module,#1,module opacity,below of=r2-dots](r2-2) {\rtwolabel};
629
630 \foreach \i in {1,2}{
631 % INPUTS MODULE 2
632 % just two modules
633 \pgfmathsetmacro\rtwointervalspace{1/(2+1)}
634 \foreach \rtwoinput[evaluate=\rtwoinput as \rtwointerval using \rtwointervalspace*\rtwoinput]
635 in {1,2}
636 \node[circle,draw,scale=0.1] (r2-\i-input-\rtwoinput)at($(r2-\i.north west)!\rtwointerval!(r2-\i.south west)$) {};
637
638 % OUTPUTS MODULE 2
639 % just two modules
640 \pgfmathsetmacro\rtwointervalspace{1/(2+1)}
641 \foreach \rtwooutput[evaluate=\rtwooutput as \rtwointerval using \rtwointervalspace*\rtwooutput]
642 in {1,2}
643 \node[circle,draw,scale=0.1] (r2-\i-output-\rtwooutput)at ($(r2-\i.north east)!\rtwointerval!(r2-\i.south east)$) {};
644 }
645
646 % MODULE 3
647 \node[module,#1,module opacity](r3-1) at (2*\modulexsep,0) {1};
648 \node[below of=r3-1,yshift=0.75ex](r3-dots) {\vdots};
649 \node[module,#1,module opacity,below of=r3-dots](r3-2) {\rthreelabel};
650
651 \foreach \i in {1,2}{
652 % INPUTS MODULE 3
653 % just two modules
654 \pgfmathsetmacro\rthreeintervalspace{1/(2+1)}
655 \foreach \rthreeinput[evaluate=\rthreeinput as \rthreeinterval using \rthreeintervalspace*\rthreeinput]
656 in {1,2}
657 \node[circle,draw,scale=0.1] (r3-\i-input-\rthreeinput)at($(r3-\i.north west)!\rthreeinterval!(r3-\i.south west)$) {};
658
659 % OUTPUTS MODULE 3
660 % just two modules
661 \pgfmathsetmacro\rthreeintervalspace{1/(2+1)}
662 \foreach \rthreeoutput[evaluate=\rthreeoutput as \rthreeinterval using \rthreeintervalspace*\rthreeoutput]
663 in {1,2}
664 \draw ($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)+(0.5*\pinlength,0)$)node[scale=0.1](r3-\i-front output-\rthreeoutput){}--($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)$) node[circle,draw,scale=0.1] (r3-\i-output-\rthreeoutput) {};
665 }
666
667 % DRAWING CONNECTIONS
668 %% from r1 to r2
669 \foreach \startmodule in {1,2}{
670 \foreach \conn in {1,2}
671 \draw(r1-\startmodule-output-\conn)--(r2-\conn-input-\startmodule);
672 }
673 %% from r2 to r3
674 \foreach \startmodule in {1,2}{
675 \foreach \conn in {1,2}
676 \draw(r3-\startmodule-input-\conn)--(r2-\conn-output-\startmodule);
677 }
678
679 % SETTING LABELS
680 \node[below of=r1-2,set math mode labels] {\monelabel~\ensuremath{\times}~\rtwolabel};
681 \node[below of=r2-2,set math mode labels] {\ronelabel~\ensuremath{\times}~\rthreelabel};
682 \node[below of=r3-2,set math mode labels] {\rtwolabel~\ensuremath{\times}~\mthreelabel};
683 \draw[decorate,decoration={brace}]($(r1-2-front input-2)-(0.1,0)$)--($(r1-1-front input-1)-(0.1,0)$) node[midway,left=0.1cm,set math mode labels]{\Nlabel};
684 \draw[decorate,decoration={brace}]($(r3-1-front output-1)+(0.1,0)$)--($(r3-2-front output-2)+(0.1,0)$) node[midway,right=0.1cm,set math mode labels]{\Mlabel};
685 },
686 }
687
688 % BENES
689 % uses modules 2x2
690
691 \tikzset{benes/.code={
692
693 % Number of ports per module
694 \pgfmathtruncatemacro{\m}{2}
695
696 % Numbers of modules in the second stage
697 \pgfmathtruncatemacro\rtwo{\m}
698
699 % Number of modules in the first/third stage
700 \pgfmathtruncatemacro{\r}{\P/\m}
701
702 \ifnum\P=4
703 \def\increment{0-\i*0.5*\r*\moduleysep}
704 \def\xincrement{\r*0.25*\modulexsep}
705 \else
706 \def\increment{0-\i*0.39*\r*\moduleysep}
707 \def\xincrement{\r*0.2*\modulexsep}
708 \fi
709
710 % MODULE 1
711 \foreach \i in {1,...,\r}{
712 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
713 in
714 node[module,#1,module opacity,yshift=1cm] (r1-\i) at +(0,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
715
716 % INPUTS MODULE 1
717 % the number of inputs module one is exactly \mone
718 \pgfmathsetmacro\roneintervalspace{1/(\m+1)}
719 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput]
720 in {1,...,\m}
721 \draw ($(r1-\i.north west)!\roneinterval!(r1-\i.south west)-(0.5*\pinlength,0)$)node[scale=0.1](r1-\i-front input-\roneinput){}--($(r1-\i.north west)!\roneinterval!(r1-\i.south west)$) node[circle,draw,scale=0.1] (r1-\i-input-\roneinput) {};
722
723 % OUTPUTS MODULE 1
724 % the number of outputs of module one is the number of modules stage 2 \rtwo
725 \pgfmathsetmacro\roneintervalspace{1/(\rtwo+1)}
726 \foreach \roneoutput[evaluate=\roneoutput as \roneinterval using \roneintervalspace*\roneoutput]
727 in {1,...,\rtwo}
728 \node[circle,draw,scale=0.1] (r1-\i-output-\roneoutput)at($(r1-\i.north east)!\roneinterval!(r1-\i.south east)$) {};
729 }
730
731 % MODULE 2
732 \foreach \i in {1,...,\rtwo}{
733
734 \path let \n1 = {int(0-\i)}, \n2={\increment}
735 in
736 node[module extensible={\r*0.5*\modulesize},#1,module opacity,yshift=1cm] (r2-\i) at +(\xincrement,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
737
738 % INPUTS MODULE 2
739 % the number of inputs of module two is the number of modules stage 1 \rone
740 \pgfmathsetmacro\rtwointervalspace{1/(\r+1)}
741 \foreach \rtwoinput[evaluate=\rtwoinput as \rtwointerval using \rtwointervalspace*\rtwoinput]
742 in {1,...,\r}
743 \node[circle,draw,scale=0.1] (r2-\i-input-\rtwoinput)at($(r2-\i.north west)!\rtwointerval!(r2-\i.south west)$) {};
744
745 % OUTPUTS MODULE 2
746 % the number of outputs module two is exactly \rthree
747 \pgfmathsetmacro\rtwointervalspace{1/(\r+1)}
748 \foreach \rtwooutput[evaluate=\rtwooutput as \rtwointerval using \rtwointervalspace*\rtwooutput]
749 in {1,...,\r}
750 \node[circle,draw,scale=0.1] (r2-\i-output-\rtwooutput)at ($(r2-\i.north east)!\rtwointerval!(r2-\i.south east)$) {};
751
752 }
753
754 % MODULE 3
755 \foreach \i in {1,...,\r}{
756 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
757 in
758 node[module,#1,module opacity,yshift=1cm] (r3-\i) at +(2*\xincrement,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
759
760 % INPUTS MODULE 3
761 % the number of inputs of module three is the number of modules stage 2 \rtwo
762 \pgfmathsetmacro\rthreeintervalspace{1/(\rtwo+1)}
763 \foreach \rthreeinput[evaluate=\rthreeinput as \rthreeinterval using \rthreeintervalspace*\rthreeinput]
764 in {1,...,\rtwo}
765 \node[circle,draw,scale=0.1] (r3-\i-input-\rthreeinput)at($(r3-\i.north west)!\rthreeinterval!(r3-\i.south west)$) {};
766
767 % OUTPUTS MODULE 3
768 % the number of outputs module three is exactly \m
769 \pgfmathsetmacro\rthreeintervalspace{1/(\m+1)}
770 \foreach \rthreeoutput[evaluate=\rthreeoutput as \rthreeinterval using \rthreeintervalspace*\rthreeoutput]
771 in {1,...,\m}
772 \draw ($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)+(0.5*\pinlength,0)$)node[scale=0.1](r3-\i-front output-\rthreeoutput){}--($(r3-\i.north east)!\rthreeinterval!(r3-\i.south east)$) node[circle,draw,scale=0.1] (r3-\i-output-\rthreeoutput) {};
773 }
774
775 % Test if connections should be removed
776 \ifconnectiondisabled
777 \relax
778 \else
779 % DRAWING CONNECTIONS
780 %% from r1 to r2
781 \foreach \startmodule in {1,...,\r}{
782 \foreach \conn in {1,...,\rtwo}
783 \draw(r1-\startmodule-output-\conn)--(r2-\conn-input-\startmodule);
784 }
785 %% from r2 to r3
786 \foreach \startmodule in {1,...,\r}{
787 \foreach \conn in {1,...,\rtwo}
788 \draw(r3-\startmodule-input-\conn)--(r2-\conn-output-\startmodule);
789 }
790 \fi
791 }
792 }
793
794 % BENES COMPLETE
795
796 \tikzset{benes complete/.code={
797
798 % Number of ports per module
799 \pgfmathtruncatemacro{\m}{2}
800
801 % Number of modules in the first/third stage
802 \pgfmathtruncatemacro{\r}{\P/\m}
803
804 % Number of stages
805 \pgfmathtruncatemacro{\stages}{2*round(log2(\P))-1}
806
807 % MODULES for all stages
808 \foreach \s [evaluate=\s as \numstage using int(\s-1)] in {1,...,\stages}{
809 \ifnum\s=1
810 % FIRST MODULE
811 \foreach \i in {1,...,\r}{
812 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
813 in
814 node[module,#1,module opacity,yshift=1cm] (r\s-\i) at +(0,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
815
816 % INPUTS MODULE 1
817 % the number of inputs module one is exactly \mone
818 \pgfmathsetmacro\roneintervalspace{1/(\m+1)}
819 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput]
820 in {1,...,\m}
821 \draw ($(r1-\i.north west)!\roneinterval!(r1-\i.south west)-(0.5*\pinlength,0)$)node[scale=0.1](r1-\i-front input-\roneinput){}--($(r1-\i.north west)!\roneinterval!(r1-\i.south west)$) node[circle,draw,scale=0.1] (r1-\i-input-\roneinput) {};
822
823 % OUTPUTS MODULE 1
824 % the number of outputs of module one is the number of modules stage 2
825 \pgfmathsetmacro\roneintervalspace{1/(\m+1)}
826 \foreach \roneoutput[evaluate=\roneoutput as \roneinterval using \roneintervalspace*\roneoutput]
827 in {1,...,\m}
828 \node[circle,draw,scale=0.1] (r1-\i-output-\roneoutput)at($(r1-\i.north east)!\roneinterval!(r1-\i.south east)$) {};
829 }
830 \fi
831 \ifnum\s=\stages
832 % FINAL MODULE
833 \foreach \i in {1,...,\r}{
834 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
835 in
836 node[module,#1,module opacity,yshift=1cm] (r\s-\i) at +(\numstage*0.6*\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
837
838 % INPUTS MODULE \s
839 % the number of inputs of module three is the number of modules stage 2 \rtwo
840 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
841 \foreach \rinput[evaluate=\rinput as \rinterval using \rintervalspace*\rinput]
842 in {1,...,\m}
843 \node[circle,draw,scale=0.1] (r\s-\i-input-\rinput)at($(r\s-\i.north west)!\rinterval!(r\s-\i.south west)$) {};
844
845 % OUTPUTS MODULE \s
846 % the number of outputs module three is exactly \mthree
847 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
848 \foreach \routput[evaluate=\routput as \rinterval using \rintervalspace*\routput]
849 in {1,...,\m}
850 \draw ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)+(0.5*\pinlength,0)$)node[scale=0.1](r\s-\i-front output-\routput){}--($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)$) node[circle,draw,scale=0.1] (r\s-\i-output-\routput) {};
851 }
852 \fi
853 \pgfmathparse{and(\s>1,\s<\stages)}
854 \let\cond\pgfmathresult
855 \ifnum\cond=1
856 % INTERMEDIATE MODULEs
857 \foreach \i in {1,...,\r}{
858 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep}
859 in
860 node[module,#1,module opacity,yshift=1cm] (r\s-\i) at +(\numstage*0.6*\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
861
862 % INPUTS MODULE \s
863 % the number of inputs of module three is the number of modules stage 2 \rtwo
864 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
865 \foreach \rinput[evaluate=\rinput as \rinterval using \rintervalspace*\rinput]
866 in {1,...,\m}
867 \node[circle,draw,scale=0.1] (r\s-\i-input-\rinput)at($(r\s-\i.north west)!\rinterval!(r\s-\i.south west)$) {};
868
869 % OUTPUTS MODULE \s
870 % the number of outputs module three is exactly \mthree
871 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
872 \foreach \routput[evaluate=\routput as \rinterval using \rintervalspace*\routput]
873 in {1,...,\m}
874 \node[circle,draw,scale=0.1] (r\s-\i-output-\routput) at($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)$) {};
875 }
876 \fi
877 }
878 % end modules
879
880
881 % Test if connections should be removed
882 \ifconnectiondisabled
883 \relax
884 \else
885 % CONNECTIONS
886
887 % the algorithm works for all the stages a part from the two in the middle
888 \ifnum\P>4 % in this case there are just two stages, thus the algorithm fails: treat it separately
889 % Compute \stages/2: they are the stages from left to the middle or from right to the middle
890 \pgfmathparse{floor(divide(\stages,2))}
891 \pgfmathtruncatemacro\stagesondirection{\pgfmathresult-1}
892
893 % on left
894 \foreach \stg[evaluate=\stg as \nextstg using int(\stg+1)] in {1,...,\stagesondirection}{
895 \pgfmathtruncatemacro\applicationon{\P/(2^\stg)}% number of modules over which the algorithm is applied
896 \pgfmathtruncatemacro\repetition{int(2^(\stg-1))}% the algorithm should be repeated for \repetition times
897 \foreach \t in {1,...,\repetition}{
898 \pgfmathtruncatemacro\startingpoint{1+((\t-1)*\applicationon)}
899 \pgfmathtruncatemacro\endingpoint{(\startingpoint+\applicationon)-1}
900 \foreach \startmodule in {\startingpoint,...,\endingpoint}{
901 \pgfmathisodd{\startmodule}
902 \ifnum\t=1
903 \ifnum\pgfmathresult=1
904 % if odd
905 \pgfmathtruncatemacro\endmodulei{int((\startmodule+1)/2)}
906 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+1+\applicationon)/2)}
907 \draw(r\stg-\startmodule-output-1)--(r\nextstg-\endmodulei-input-1);
908 \draw(r\stg-\startmodule-output-2)--(r\nextstg-\endmoduleii-input-1);
909 \else
910 % if even
911 \pgfmathtruncatemacro\endmodulei{int((\startmodule)/2)}
912 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+\applicationon)/2)}
913 \draw(r\stg-\startmodule-output-1)--(r\nextstg-\endmodulei-input-2);
914 \draw(r\stg-\startmodule-output-2)--(r\nextstg-\endmoduleii-input-2);
915 \fi
916 \fi
917 \ifnum\t=2
918 \ifnum\pgfmathresult=1
919 % if odd
920 \pgfmathtruncatemacro\endmodulei{int((\startmodule+1)/2+(\applicationon/2))}
921 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+1+\applicationon)/2+(\applicationon/2))}
922 \draw(r\stg-\startmodule-output-1)--(r\nextstg-\endmodulei-input-1);
923 \draw(r\stg-\startmodule-output-2)--(r\nextstg-\endmoduleii-input-1);
924 \else
925 % if even
926 \pgfmathtruncatemacro\endmodulei{int((\startmodule)/2+(\applicationon/2))}
927 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+\applicationon)/2+(\applicationon/2))}
928 \draw(r\stg-\startmodule-output-1)--(r\nextstg-\endmodulei-input-2);
929 \draw(r\stg-\startmodule-output-2)--(r\nextstg-\endmoduleii-input-2);
930 \fi
931 \fi
932 \ifnum\t>2
933 \ifnum\pgfmathresult=1
934 % if odd
935 \pgfmathtruncatemacro\endmodulei{int((\startmodule+1)/2+(\applicationon/2)+((\applicationon/2)*(\t-2)))}
936 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+1+\applicationon)/2+(\applicationon/2)+((\applicationon/2)*(\t-2)))}
937 \draw(r\stg-\startmodule-output-1)--(r\nextstg-\endmodulei-input-1);
938 \draw(r\stg-\startmodule-output-2)--(r\nextstg-\endmoduleii-input-1);
939 \else
940 % if even
941 \pgfmathtruncatemacro\endmodulei{int((\startmodule)/2+(\applicationon/2)+((\applicationon/2)*(\t-2)))}
942 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+\applicationon)/2+(\applicationon/2)+((\applicationon/2)*(\t-2)))}
943 \draw(r\stg-\startmodule-output-1)--(r\nextstg-\endmodulei-input-2);
944 \draw(r\stg-\startmodule-output-2)--(r\nextstg-\endmoduleii-input-2);
945 \fi
946 \fi
947 }
948 }
949 }
950
951 % on the right
952
953 \foreach \stg[evaluate=\stg as \currstg using int(\stages-(\stg-1)),
954 evaluate=\stg as \nextstg using int(\currstg-1)] in {1,...,\stagesondirection}{
955 \pgfmathtruncatemacro\applicationon{\P/(2^\stg)}% number of modules over which the algorithm is applied
956 \pgfmathtruncatemacro\repetition{int(2^(\stg-1))}% the algorithm should be repeated for \repetition times
957 \foreach \t in {1,...,\repetition}{
958 \pgfmathtruncatemacro\startingpoint{1+((\t-1)*\applicationon)}
959 \pgfmathtruncatemacro\endingpoint{(\startingpoint+\applicationon)-1}
960 \foreach \startmodule in {\startingpoint,...,\endingpoint}{
961 \pgfmathisodd{\startmodule}
962 \ifnum\t=1
963 \ifnum\pgfmathresult=1
964 % if odd
965 \pgfmathtruncatemacro\endmodulei{int((\startmodule+1)/2)}
966 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+1+\applicationon)/2)}
967 \draw(r\currstg-\startmodule-input-1)--(r\nextstg-\endmodulei-output-1);
968 \draw(r\currstg-\startmodule-input-2)--(r\nextstg-\endmoduleii-output-1);
969 \else
970 % if even
971 \pgfmathtruncatemacro\endmodulei{int((\startmodule)/2)}
972 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+\applicationon)/2)}
973 \draw(r\currstg-\startmodule-input-1)--(r\nextstg-\endmodulei-output-2);
974 \draw(r\currstg-\startmodule-input-2)--(r\nextstg-\endmoduleii-output-2);
975 \fi
976 \fi
977 \ifnum\t=2
978 \ifnum\pgfmathresult=1
979 % if odd
980 \pgfmathtruncatemacro\endmodulei{int((\startmodule+1)/2+(\applicationon/2))}
981 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+1+\applicationon)/2+(\applicationon/2))}
982 \draw(r\currstg-\startmodule-input-1)--(r\nextstg-\endmodulei-output-1);
983 \draw(r\currstg-\startmodule-input-2)--(r\nextstg-\endmoduleii-output-1);
984 \else
985 % if even
986 \pgfmathtruncatemacro\endmodulei{int((\startmodule)/2+(\applicationon/2))}
987 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+\applicationon)/2+(\applicationon/2))}
988 \draw(r\currstg-\startmodule-input-1)--(r\nextstg-\endmodulei-output-2);
989 \draw(r\currstg-\startmodule-input-2)--(r\nextstg-\endmoduleii-output-2);
990 \fi
991 \fi
992 \ifnum\t>2
993 \ifnum\pgfmathresult=1
994 % if odd
995 \pgfmathtruncatemacro\endmodulei{int((\startmodule+1)/2+(\applicationon/2)+((\applicationon/2)*(\t-2)))}
996 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+1+\applicationon)/2+(\applicationon/2)+((\applicationon/2)*(\t-2)))}
997 \draw(r\currstg-\startmodule-input-1)--(r\nextstg-\endmodulei-output-1);
998 \draw(r\currstg-\startmodule-input-2)--(r\nextstg-\endmoduleii-output-1);
999 \else
1000 % if even
1001 \pgfmathtruncatemacro\endmodulei{int((\startmodule)/2+(\applicationon/2)+((\applicationon/2)*(\t-2)))}
1002 \pgfmathtruncatemacro\endmoduleii{int((\startmodule+\applicationon)/2+(\applicationon/2)+((\applicationon/2)*(\t-2)))}
1003 \draw(r\currstg-\startmodule-input-1)--(r\nextstg-\endmodulei-output-2);
1004 \draw(r\currstg-\startmodule-input-2)--(r\nextstg-\endmoduleii-output-2);
1005 \fi
1006 \fi
1007 }
1008 }
1009 }
1010
1011 \fi
1012
1013
1014 % * * * *
1015 % 2 Intermediate stages
1016
1017 % Compute \stages/2
1018 \pgfmathparse{floor(divide(\stages,2))}
1019 \pgfmathtruncatemacro\middlestage{\pgfmathresult}
1020 \pgfmathtruncatemacro\middlestagei{int(\middlestage+1)}
1021 \pgfmathtruncatemacro\middlestageii{int(\middlestagei+1)}
1022
1023 % Drawing
1024 \foreach \startmodule in {1,...,\r}{
1025 \pgfmathisodd{\startmodule}
1026 \ifnum\pgfmathresult=1
1027 % if odd
1028 \pgfmathtruncatemacro\endmodule{int(\startmodule+1)}
1029 \draw(r\middlestage-\startmodule-output-1)--(r\middlestagei-\startmodule-input-1);
1030 \draw(r\middlestage-\startmodule-output-2)--(r\middlestagei-\endmodule-input-1);
1031 \draw(r\middlestagei-\startmodule-output-1)--(r\middlestageii-\startmodule-input-1);
1032 \draw(r\middlestagei-\startmodule-output-2)--(r\middlestageii-\endmodule-input-1);
1033 \else
1034 % if even
1035 \pgfmathtruncatemacro\endmodule{int(\startmodule-1)}
1036 \draw(r\middlestage-\startmodule-output-1)--(r\middlestagei-\endmodule-input-2);
1037 \draw(r\middlestage-\startmodule-output-2)--(r\middlestagei-\startmodule-input-2);
1038 \draw(r\middlestagei-\startmodule-output-1)--(r\middlestageii-\endmodule-input-2);
1039 \draw(r\middlestagei-\startmodule-output-2)--(r\middlestageii-\startmodule-input-2);
1040 \fi
1041 }
1042 % end connections
1043 \fi % disable connections
1044 }
1045 }
1046
1047 % BANYAN NETWORKS
1048
1049 % BANYAN-OMEGA (thanks to João Gabriel Reis)
1050
1051 \tikzset{banyan omega/.code={
1052 % Number of ports per module
1053 \pgfmathtruncatemacro{\m}{2}
1054
1055 % Number of modules in each stage
1056 \pgfmathtruncatemacro{\r}{\P/\m}
1057
1058 % Number of stages
1059 \pgfmathtruncatemacro{\stages}{round(log2(\P))}
1060
1061 % Modules for all stages
1062 \foreach \s [evaluate=\s as \numstage using int(\s-1)] in {0,...,\stages}{
1063 \ifnum\s=0
1064 % Invisible modules
1065 \foreach \i in {1,...,\r}{
1066 \path let \n2={-\i*\moduleysep} in
1067 node[rectangle,minimum height=\modulesize,#1,module opacity,xshift=\modulesize/2,yshift=1cm]
1068 (r\s-\i) at +(-0.6*\modulexsep,\n2) {};
1069
1070 % Invisible modules outputs
1071 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
1072 \foreach \routput[evaluate=\routput as \rinterval using \rintervalspace*\routput] in {1,...,\m}
1073 \draw ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)+(0.5*0.3*\pinlength,0)$) node[coordinate] (r\s-\i-front output-\routput) {} --
1074 ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)$)node[coordinate](r\s-\i-output-\routput) {};
1075 \foreach \routput[evaluate=\routput as \rinterval using \rintervalspace*\routput] in {1,...,\m}
1076 \draw ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)-(0.5*0.3*\pinlength,0)$) node[circle,draw,scale=0.1] (r\s-\i-front input-\routput) {} --
1077 ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)$)node[coordinate](r\s-\i-input-\routput) {};
1078 }
1079 \fi
1080 \ifnum\s=\stages
1081 % Final Module
1082 \foreach \i in {1,...,\r}{
1083 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep} in node[module,#1,module opacity,yshift=1cm] (r\s-\i) at
1084 +(\numstage*0.6*\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
1085
1086 % Final module inputs
1087 \pgfmathsetmacro\roneintervalspace{1/(\m+1)}
1088 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput] in {1,...,\m}
1089 \draw ($(r\s-\i.north west)!\roneinterval!(r\s-\i.south west)-(0.5*0.3*\pinlength,0)$) node[coordinate](r\s-\i-front input-\roneinput) {} --
1090 ($(r\s-\i.north west)!\roneinterval!(r\s-\i.south west)$)node[circle,draw,scale=0.1] (r\s-\i-input-\roneinput) {};
1091
1092 % Final module outputs
1093 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
1094 \foreach \routput[evaluate=\routput as \rinterval using \rintervalspace*\routput] in {1,...,\m}
1095 \draw ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)+(0.5*0.3*\pinlength,0)$) node[coordinate](r\s-\i-front output-\routput) {} --
1096 ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)$)node[circle,draw,scale=0.1] (r\s-\i-output-\routput) {};
1097 }
1098 \fi
1099
1100 \pgfmathparse{and(\s>0,\s<\stages)}
1101 \let\cond\pgfmathresult
1102 \ifnum\cond=1
1103 % Intermediate modules
1104 \foreach \i in {1,...,\r}{
1105 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep} in node[module,#1,module opacity,yshift=1cm]
1106 (r\s-\i) at +(\numstage*0.6*\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
1107
1108 % Intermediate modules inputs
1109 \pgfmathsetmacro\roneintervalspace{1/(\m+1)}
1110 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput] in {1,...,\m}
1111 \draw ($(r\s-\i.north west)!\roneinterval!(r\s-\i.south west)-(0.5*0.3*\pinlength,0)$) node[coordinate](r\s-\i-front input-\roneinput) {} --
1112 ($(r\s-\i.north west)!\roneinterval!(r\s-\i.south west)$)node[circle,draw,scale=0.1] (r\s-\i-input-\roneinput) {};
1113
1114 % Intermediate modules outputs
1115 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
1116 \foreach \routput[evaluate=\routput as \rinterval using \rintervalspace*\routput] in {1,...,\m}
1117 \draw ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)+(0.5*0.3*\pinlength,0)$) node[coordinate](r\s-\i-front output-\routput) {} --
1118 ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)$)node[circle,draw,scale=0.1] (r\s-\i-output-\routput) {};
1119 }
1120 \fi
1121 }
1122
1123 % Test if connections should be removed
1124 \ifconnectiondisabled
1125 \relax
1126 \else
1127 % Connections
1128 \foreach \stg[evaluate=\stg as \prevstg using int(\stg - 1)] in {1,...,\stages}{
1129 \foreach \startmod in {1,...,\r}{
1130 \pgfmathomegarotation{2*(\startmod - 1)}{\stages - 1}{address}
1131 \pgfmathtruncatemacro\endmodi{\address/2 + 1}
1132 \pgfmathtruncatemacro\endmodii{\endmodi + \r/2}
1133 \pgfmathtruncatemacro\cond{mod(\startmod,2)}
1134 \ifnum\cond=0
1135 \draw(r\stg-\startmod-front input-1)--(r\prevstg-\endmodi-front output-2);
1136 \draw(r\stg-\startmod-front input-2)--(r\prevstg-\endmodii-front output-2);
1137 \else
1138 \draw(r\stg-\startmod-front input-1)--(r\prevstg-\endmodi-front output-1);
1139 \draw(r\stg-\startmod-front input-2)--(r\prevstg-\endmodii-front output-1);
1140 \fi
1141 }
1142 }
1143 \fi
1144 }
1145 }
1146
1147 % BANYAN-FLIP
1148
1149 \tikzset{banyan flip/.code={
1150 % Number of ports per module
1151 \pgfmathtruncatemacro{\m}{2}
1152
1153 % Number of modules in each stage
1154 \pgfmathtruncatemacro{\r}{\P/\m}
1155
1156 % Number of stages
1157 \pgfmathtruncatemacro{\stages}{round(log2(\P))}
1158
1159 % Modules for all stages
1160 \foreach \s [evaluate=\s as \numstage using int(\s-1)] in {0,...,\stages}{
1161 \ifnum\s=0
1162 % Final Module
1163 \foreach \i in {1,...,\r}{
1164 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep} in node[module,#1,module opacity,yshift=1cm] (r\s-\i) at
1165 +(\numstage*0.6*\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
1166
1167 % Final module inputs
1168 \pgfmathsetmacro\roneintervalspace{1/(\m+1)}
1169 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput] in {1,...,\m}
1170 \draw ($(r\s-\i.north west)!\roneinterval!(r\s-\i.south west)-(0.5*0.3*\pinlength,0)$) node[coordinate](r\s-\i-front input-\roneinput) {} --
1171 ($(r\s-\i.north west)!\roneinterval!(r\s-\i.south west)$) node[circle,draw,scale=0.1] (r\s-\i-input-\roneinput) {};
1172
1173 % Final module outputs
1174 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
1175 \foreach \routput[evaluate=\routput as \rinterval using \rintervalspace*\routput] in {1,...,\m}
1176 \draw ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)+(0.5*0.3*\pinlength,0)$) node[coordinate](r\s-\i-front output-\routput) {} --
1177 ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)$) node[circle,draw,scale=0.1] (r\s-\i-output-\routput) {};
1178 }
1179 \fi
1180 \ifnum\s=\stages
1181 % Invisible modules
1182 \foreach \i in {1,...,\r}{
1183 \path let \n2={-\i*\moduleysep} in
1184 node[rectangle,minimum height=\modulesize,#1,module opacity,xshift=\modulesize/2,yshift=1cm]
1185 (r\s-\i) at +(\numstage*0.415*\modulexsep,\n2) {};
1186
1187 % Invisible modules outputs
1188 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
1189 \foreach \roneinput[evaluate=\roneinput as \rinterval using \rintervalspace*\roneinput] in {1,...,\m}
1190 \draw ($(r\s-\i.north west)!\rinterval!(r\s-\i.south west)$)node [coordinate](r\s-\i-input-\roneinput) {} --
1191 ($(r\s-\i.north west)!\rinterval!(r\s-\i.south west)-(0.5*0.3*\pinlength,0)$)
1192 node [coordinate] (r\s-\i-front input-\roneinput) {};
1193 \foreach \roneinput[evaluate=\roneinput as \rinterval using \rintervalspace*\roneinput] in {1,...,\m}
1194 \draw ($(r\s-\i.north west)!\rinterval!(r\s-\i.south west)$)node [coordinate](r\s-\i-input-\roneinput) {} --
1195 ($(r\s-\i.north west)!\rinterval!(r\s-\i.south west)+(0.5*0.3*\pinlength,0)$)
1196 node [circle,draw,scale=0.1] (r\s-\i-front output-\roneinput) {};
1197 }
1198 \fi
1199 \pgfmathparse{and(\s>0,\s<\stages)}
1200 \let\cond\pgfmathresult
1201 \ifnum\cond=1
1202 % Intermediate modules
1203 \foreach \i in {1,...,\r}{
1204 \path let \n1 = {int(0-\i)}, \n2={0-\i*\moduleysep} in node[module,#1,module opacity,yshift=1cm]
1205 (r\s-\i) at +(\numstage*0.6*\modulexsep,\n2) {\pgfmathparse{int(multiply(\n1,-1))}\pgfmathresult};
1206
1207 % Intermediate modules inputs
1208 \pgfmathsetmacro\roneintervalspace{1/(\m+1)}
1209 \foreach \roneinput[evaluate=\roneinput as \roneinterval using \roneintervalspace*\roneinput] in {1,...,\m}
1210 \draw ($(r\s-\i.north west)!\roneinterval!(r\s-\i.south west)-(0.5*0.3*\pinlength,0)$) node[coordinate](r\s-\i-front input-\roneinput) {} --
1211 ($(r\s-\i.north west)!\roneinterval!(r\s-\i.south west)$) node[circle,draw,scale=0.1] (r\s-\i-input-\roneinput) {};
1212
1213 % Intermediate modules outputs
1214 \pgfmathsetmacro\rintervalspace{1/(\m+1)}
1215 \foreach\routput[evaluate=\routput as \rinterval using \rintervalspace*\routput] in {1,...,\m}
1216 \draw ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)+(0.5*0.3*\pinlength,0)$) node[coordinate](r\s-\i-front output-\routput) {} --
1217 ($(r\s-\i.north east)!\rinterval!(r\s-\i.south east)$) node[circle,draw,scale=0.1] (r\s-\i-output-\routput) {};
1218 }
1219 \fi
1220 }
1221
1222 % Test if connections should be removed
1223 \ifconnectiondisabled
1224 \relax
1225 \else
1226 % Connections
1227 \foreach \stg[evaluate=\stg as \prevstg using int(\stg - 1)] in {1,...,\stages}{
1228 \foreach \startmod in {1,...,\r}{
1229 \pgfmathomegarotation{2*(\startmod - 1)}{\stages - 1}{address}
1230 \pgfmathtruncatemacro\endmodi{\address/2 + 1}
1231 \pgfmathtruncatemacro\endmodii{\endmodi + \r/2}
1232 \pgfmathtruncatemacro\cond{mod(\startmod,2)}
1233 \ifnum\cond=0
1234 \draw(r\stg-\startmod-front input-1)--(r\prevstg-\endmodi-front output-2);
1235 \draw(r\stg-\startmod-front input-2)--(r\prevstg-\endmodii-front output-2);
1236 \else
1237 \draw(r\stg-\startmod-front input-1)--(r\prevstg-\endmodi-front output-1);
1238 \draw(r\stg-\startmod-front input-2)--(r\prevstg-\endmodii-front output-1);
1239 \fi
1240 }
1241 }
1242 \fi
1243 }
1244 }
1245
1246
1247
1248 \endinput