Coverage report: /home/runner/work/geb/geb/src/seqn/trans.lisp
Kind | Covered | All | % |
expression | 108 | 610 | 17.7 |
branch | 2 | 56 | 3.6 |
Key
Not instrumented
Conditionalized out
Executed
Not executed
Both branches taken
One branch taken
Neither branch taken
1
(in-package :geb.seqn.trans)
3
(defun remove0-dom (morphism)
4
(remove 0 (dom morphism)))
6
;; Add range-n function
8
(defun range-constraints-dom (domain)
9
(cond ((null domain) nil)
11
(list (geb.vampir:range-n
12
(vamp:make-constant :const (car domain))
14
(intern (format nil "X~a" 1))))))
16
(cons (geb.vampir:range-n
17
(vamp:make-constant :const (car (last domain)))
18
(vamp:make-wire :var (intern (format nil "X~a" (length domain)))))
19
(range-constraints-dom (butlast domain))))))
21
(defmethod to-circuit ((morphism <seqn>) name)
22
"Turns a SeqN term into a Vamp-IR Gate with the given name
23
Note that what is happening is that we look at the domain of the morphism
24
and skip 0es, making non-zero entries into wires"
25
(let* ((wire-count (length (dom morphism)))
26
(wires (loop for i from 1 to wire-count
27
collect (vamp:make-wire :var (intern (format nil "X~a" i)
37
(mapcar #'to-vampir-opt
39
(filter-map (lambda (x)
40
(unless (zerop (car x))
42
(prod-list (cod morphism)
43
(to-vampir morphism wires nil)))))))))))
45
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46
;; SeqN to Vamp-IR Compilation
47
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49
(defmethod to-vampir ((obj <seqn>) values constraints)
50
"The method takes a list of values, i.e. a list of wires with
51
0-wide entries removed and spits out a list of wires to be later
52
made into a tuple or a single entry if the codomain is isomorphic
54
(declare (ignore constraints))
55
(declare (ignore values))
56
(geb.utils:subclass-responsibility obj))
58
(defun const-check (obj1 obj2)
59
(and (typep obj1 'vamp:constant)
60
(typep obj2 'vamp:constant)))
62
(defmethod to-vampir-opt ((obj vamp:application))
63
(let* ((zero (vamp:make-constant :const 0))
64
(one (vamp:make-constant :const 1))
65
(car (car (vamp:arguments obj)))
66
(cadr (cadr (vamp:arguments obj)))
67
(opcar (to-vampir-opt car))
68
(opcadr (to-vampir-opt cadr))
69
(const-check (const-check opcar opcadr)))
70
(cond ((obj-equalp (vamp:func obj) :isZero)
72
(if (= (vamp:const opcar)
76
((obj-equalp (vamp:func obj) :negative)
78
(if (< (vamp:const opcar)
82
(t (mapcar 'to-vampir-opt (vamp:arguments obj))))))
84
(defmethod to-vampir-opt ((obj vamp:constant))
87
(defmethod to-vampir-opt ((obj vamp:wire))
90
(defmethod to-vampir-opt ((obj geb.vampir.spec:infix))
91
(let* ((lhs (vamp:lhs obj))
93
(oplhs (to-vampir-opt lhs))
94
(oprhs (to-vampir-opt rhs))
95
(ob+ (obj-equalp (vamp:op obj) :+))
96
(ob- (obj-equalp (vamp:op obj) :-))
97
(ob/ (obj-equalp (vamp:op obj) :/))
98
(ob* (obj-equalp (vamp:op obj) :*)))
99
(if (const-check oplhs
101
(let ((constl (vamp:const oplhs))
102
(constr (vamp:const oprhs)))
103
(cond (ob+ (vamp:make-constant
104
:const (+ constl constr)))
105
(ob- (vamp:make-constant
106
:const (- constl constr)))
107
(ob* (vamp:make-constant
108
:const (* constl constr)))
109
(ob/ (vamp:make-constant
111
(multiple-value-bind (q)
115
(cond (ob+ (make-opt-plus oplhs oprhs))
116
(ob- (make-opt-minus oplhs oprhs))
117
(ob/ (make-opt-divide oplhs oprhs))
118
(ob* (make-opt-times oplhs oprhs))))))
120
(defun infix-creation (symbol value1 value2)
121
(vamp:make-infix :op symbol
125
;; Make wire function accessing the wire list
127
(defmethod to-vampir ((obj id) inputs constraints)
128
"Given a tuple (x1,...,xn) does nothing with it"
129
(declare (ignore constraints))
132
(defmethod to-vampir ((obj composition) inputs constraints)
133
"Compile the MCADR after feeding in appropriate
134
inputs and then feed them as entries to compiled MCAR"
135
(to-vampir (mcar obj)
136
(to-vampir (mcadr obj) inputs constraints)
139
(defmethod to-vampir ((obj parallel-seq) inputs constraints)
140
"Compile MCAR and MCADR and then apppend the tuples"
141
(let* ((mcar (mcar obj))
143
(lmcar (length (dom mcar))))
144
(append (to-vampir mcar
145
(subseq inputs 0 lmcar)
152
(defmethod to-vampir ((obj fork-seq) inputs constraints)
153
"Given a tuple (x1,...,xn) copies it twice"
154
(declare (ignore constraints))
155
;; Since we feed in wires, simply ask for its list of wires and appent
156
(append inputs inputs))
158
(defmethod to-vampir ((obj drop-nil) inputs constraints)
159
"Drops everything by producing nothing"
160
(declare (ignore inputs constraints))
161
(list (vamp:make-constant :const 0)))
163
(defmethod to-vampir ((obj remove-right) inputs constraints)
164
"We do not have nul inputs so does nothing"
165
(declare (ignore constraints))
168
(defmethod to-vampir ((obj remove-left) inputs constraints)
169
"We do not have nul inputs so does nothing"
170
(declare (ignore constraints))
173
(defmethod to-vampir ((obj drop-width) inputs constraints)
174
"The compilation does not produce dropping with domain inputs
175
wider than codomain ones appropriately. Hence we do not require range
176
checks here and simply project"
177
(declare (ignore constraints))
180
(defmethod to-vampir ((obj inj-length-left) inputs constraints)
181
"Look at the MCAR. Look at non-null wide entries and place
182
0-es in the outputs otherwise ignore"
183
(declare (ignore constraints))
185
(make-list (length (mcadr obj))
186
:initial-element (vamp:make-constant :const 0))))
188
(defmethod to-vampir ((obj inj-length-right) inputs constraints)
189
"Look at the MCADR. Look at non-null wide entries and place
190
0-es in the outputs "
191
(declare (ignore constraints))
192
(append (make-list (length (mcar obj))
193
:initial-element (vamp:make-constant :const 0))
196
(defmethod to-vampir ((obj inj-size) inputs constraints)
197
"During th ecompilation procedure the domain will not have larger
198
width than the codomain so we simply project"
199
(declare (ignore constraints))
202
(defmethod to-vampir ((obj branch-seq) inputs constraints)
203
"With the leftmost input being 1 or 0, pointwise do usual bit
204
branching. If 0 run the MCAR, if 1 run the MCADR"
205
(let* ((car (car inputs))
206
(left (to-vampir (mcar obj) (cdr inputs) constraints))
207
(right (to-vampir (mcadr obj) (cdr inputs) constraints)))
213
(vamp:make-constant :const 1)
216
(infix-creation :* car (cadr x))))
217
(prod-list left right))))
219
(defmethod to-vampir ((obj shift-front) inputs constraints)
220
"Takes the MCADR entry and moves it upward leaving everything
221
else fixed. Note that we have to be careful as inputs will have 0es
222
removed already and hence we cannot count as usual"
223
(declare (ignore constraints))
224
(let* ((mcadr (mcadr obj))
226
(append (list (nth mmcadr inputs))
227
(subseq inputs 0 mmcadr)
228
(subseq inputs mcadr))))
230
(defmethod to-vampir ((obj zero-bit) inputs constraints)
231
(declare (ignore inputs constraints))
232
(list (vamp:make-constant :const 0)))
234
(defmethod to-vampir ((obj one-bit) inputs constraints)
235
(declare (ignore inputs constraints))
236
(list (vamp:make-constant :const 1)))
238
(defmethod to-vampir ((obj seqn-add) inputs constraints)
239
(declare (ignore constraints))
240
(list (infix-creation :+ (car inputs) (cadr inputs))))
242
(defmethod to-vampir ((obj seqn-subtract) inputs constraints)
243
(declare (ignore constraints))
244
(list (infix-creation :- (car inputs) (cadr inputs))))
246
(defmethod to-vampir ((obj seqn-multiply) inputs constraints)
247
(declare (ignore constraints))
248
(list (infix-creation :* (car obj) (cadr obj))))
250
(defmethod to-vampir ((obj seqn-divide) inputs constraints)
251
(declare (ignore constraints))
252
(list (infix-creation :/ (car inputs) (cadr inputs))))
254
(defmethod to-vampir ((obj seqn-nat) inputs constraints)
255
(declare (ignore constraints))
256
(list (vamp:make-constant :const (mcadr obj))))
258
(defmethod to-vampir ((obj seqn-concat) inputs constraints)
259
(declare (ignore constraints))
260
(list (infix-creation :+
261
(infix-creation :* (car inputs)
263
:const (expt 2 (mcadr obj))))
266
(defmethod to-vampir ((obj seqn-decompose) inputs constraints)
267
(declare (ignore constraints))
268
(let* ((mcar (mcar obj))
270
(rng (vamp:make-application
272
:arguments (list (vamp:make-constant :const mcar)
274
(lst (list (vamp:make-constant :const (1- mcar))
276
(list (vamp:make-application :func :n_th
279
(vamp:make-application :func :drop_ith
282
(defun range-depth (x)
283
(let ((cadr (cadr (vamp:arguments x))))
284
(if (not (typep cadr 'vamp:application))
286
(1+ (range-depth (cadr (vamp:arguments (car (vamp:arguments cadr)))))))))
288
(defmethod to-vampir ((obj seqn-eq) inputs constraints)
289
(declare (ignore constraints))
290
(list (geb.vampir:isZero (infix-creation :-
293
(vamp:make-constant :const 0)))
295
(defmethod to-vampir ((obj seqn-lt) inputs constraints)
296
(declare (ignore constraints))
297
(list (geb.vampir:negative (vamp:make-constant :const (mcar obj))
301
(vamp:make-constant :const 0)))
303
(defmethod to-vampir ((obj seqn-mod) inputs constraints)
304
(declare (ignore constraints))
305
(let ((car (car inputs))
306
(cadr (cadr inputs)))
307
(if (const-check car cadr)
308
(list (vamp:make-constant :const (mod (vamp:const car) (vamp:const cadr))))
309
(list (geb.vampir:mod-n (vamp:make-constant :const (mcar obj))
312
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
314
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
317
;; happens when the first input is constant
318
(defun optimize-decompose (obj first-input)
319
(if (>= (vamp:const first-input) (expt 2 (1- obj)))
320
(list (vamp:make-constant
323
:const (- (vamp:const first-input) (expt 2 (1- obj)))))
324
(list (vamp:make-constant
328
(defun make-opt-plus (value1 value2)
329
(let ((base (infix-creation :+
332
(cond ((typep value1 'vamp:constant)
333
(if (zerop (vamp:const value1))
336
((typep value2 'vamp:constant)
337
(if (zerop (vamp:const value2))
343
(defun make-opt-minus (value1 value2)
344
(let ((base (infix-creation :-
347
(cond ((typep value2 'vamp:constant)
348
(if (zerop (vamp:const value2))
354
(defun make-opt-divide (value1 value2)
355
(let ((base (infix-creation :/
358
(cond ((typep value2 'vamp:constant)
359
(if (= (vamp:const value2) 1)
365
(defun make-opt-times (value1 value2)
366
(let ((base (infix-creation :*
369
(zero (vamp:make-constant :const 0)))
370
(cond ((typep value1 'vamp:constant)
371
(cond ((zerop (vamp:const value1))
373
((= (vamp:const value1) 1)
377
((typep value2 'vamp:constant)
378
(cond ((zerop (vamp:const value2))
380
((= (vamp:const value2) 1)