-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-arithmetic.scm
468 lines (332 loc) · 12.1 KB
/
binary-arithmetic.scm
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
;; useful binary and arithmetic functions
;; binary list to true-false list
;; (binList2TrueFalseList '(1 1 0 1 0 0)) -> (#t #t #f #t #f #f)
(define (binList2TrueFalseList L)
(if (null? L)
L
(if (= (car L) 1)
(cons #t (binList2TrueFalseList (cdr L)))
(cons #f (binList2TrueFalseList (cdr L))))))
;; true-false list to binary list
;; > (trueFalseList2binList '(#t #t #f #f #t #t #t #t)) -> '(1 1 0 0 1 1 1 1)
(define (trueFalseList2binList L)
(map boolean->binary L))
;; number2binlist : convert a number in a list containing its binary number conversion
;; (number2binlist #b10110) --> (1 0 1 1 0)
(define (number2binlist n)
(cond
((zero? n) '(0))
((= 1 n) '(1))
((= (modulo n 2) 1) (append (number2binlist (quotient n 2)) (list 1)))
(else (append (number2binlist (quotient n 2)) (list 0)))))
;; (number->poly-base-2 #b10110) -> '(+ (^ 2 4) (^ 2 2) (^ 2 1))
;;
(define (number->poly-base-2 n)
(let ((deg 0) ;; polynom degree
(poly-prefix '())
(poly-prefix-simp '()))
(letrec
((number->poly-base-2-rec
(lambda (n)
;;(dv deg)
(cond
((zero? n) '(0))
;;((= 1 n) (quasiquote #;((2 ^ ,deg)) ((^ 2 ,deg))))
((= (modulo n 2) 1)
(let ((monomial (quasiquote #;((2 ^ ,deg)) ((^ 2 ,deg)))))
(set! deg (+ 1 deg))
(append
(number->poly-base-2-rec (quotient n 2))
monomial)))
(else
(set! deg (+ 1 deg))
(append (number->poly-base-2-rec (quotient n 2)) (list 0)))))))
(set! poly-prefix (cons '+ (number->poly-base-2-rec n)))
;;(dv poly-prefix)
(set! poly-prefix-simp (simplify poly-prefix))
;;(dv poly-prefix-simp)
poly-prefix-simp)))
(define (number->poly-base-k n k)
(let ((deg 0) ;; polynom degree
(poly-prefix '())
(poly-prefix-simp '()))
(letrec
((number->poly-base-k-rec
(lambda (n k)
(dv deg)
(let ((r (modulo n k))) ;; remainder
(cond
((zero? n) '(0))
;;((= 1 r) (quasiquote ((^ ,k ,deg))))
((not (zero? r))
(let ((monomial (quasiquote ((* ,r (^ ,k ,deg)))))
(q (quotient n k)))
(set! deg (+ 1 deg))
(append
(number->poly-base-k-rec q k)
monomial)))
(else
(set! deg (+ 1 deg))
(append
(number->poly-base-k-rec (quotient n k) k)
(list 0))))))))
(set! poly-prefix (cons '+ (number->poly-base-k-rec n k)))
(dv poly-prefix)
(set! poly-prefix-simp (simplify poly-prefix))
(dv poly-prefix-simp)
poly-prefix-simp)))
(define (number->poly-base-k-expt n k)
(let ((deg 0) ;; polynom degree
(poly-prefix '())
(poly-prefix-simp '()))
(letrec
((number->poly-base-k-rec-expt
(lambda (n k)
;;(dv deg)
(let ((r (modulo n k))) ;; remainder
(cond
((zero? n) '(0))
((not (zero? r))
(let ((monomial (quasiquote ((* ,r (expt ,k ,deg)))))
(q (quotient n k)))
(set! deg (+ 1 deg))
(append
(number->poly-base-k-rec-expt q k)
monomial)))
(else
(set! deg (+ 1 deg))
(append
(number->poly-base-k-rec-expt (quotient n k) k)
(list 0))))))))
(set! poly-prefix (cons '+ (number->poly-base-k-rec-expt n k)))
(dv poly-prefix)
(set! poly-prefix-simp (simplify poly-prefix))
(dv poly-prefix-simp)
poly-prefix-simp)))
;; (number->poly-base-2-infix #b10110) -> '((2 ^ 4) + (2 ^ 2) + (2 ^ 1))
;;
(define (number->poly-base-2-infix n)
(prefix->infix (number->poly-base-2 n)))
(define (number->poly-base-k-infix n k)
(prefix->infix (number->poly-base-k n k)))
;; binlist2number : convert a binary list to a number
;;> (binlist2number '(1 1 0 0 1 1 1 1)) -> 207
(define (binlist2number L)
;;(debug-mode-off)
(letrec ((revL (reverse L)) ; reversed list
(reverseBinList->number
(lambda (revBinLst expo) ; starting exposant
(when debug-mode
(display "calling reverseBinList") (newline)
(display "... revBinLst : ") (display revBinLst) (newline)
(display "... expo : ") (display expo) (newline) (newline))
(if (null? revBinLst)
0 ;; zero is not only represented by an empty list but it will do the job of ending computation
(let* ((Bk (first revBinLst))
(BkX2Pk (* Bk (expt 2 expo)))) ;; Bk * 2^k
(+
(begin
(when debug-mode
(display "...... Bk : ") (display Bk) (newline)
(display "...... Bk * (2 ^ expo) : ") (display BkX2Pk) (newline))
BkX2Pk)
(let ((restRBL2n (reverseBinList->number (rest revBinLst) (incf expo))))
(begin
(when debug-mode
(display "...... restRBL2n : ") (display restRBL2n) (newline))
restRBL2n )))))))
(result (reverseBinList->number revL 0))) ; expo set to 0 at beginning
;;(debug-mode-reload)
result))
(define (boolean->binary b)
(if b 1 0))
;; convert binary string number to carries string
;; (binary-string->carries " 101101110" ) -> " 1 11 111 "
(define (binary-string->carries s)
(string-replace s "0" " "))
;; convert binary string number to carries string
(define (binary-string->carries-c s)
(string-replace (string-replace s "0" " ") "1" "C"))
;; display binary numbers with padding
;; (padding #b10110) -> "0000000000010110"
(define (padding x)
(~r x #:base 2 #:min-width 24 #:pad-string "0"))
;; display binary numbers with padding
;; (padding-spc #b10110) -> " 10110"
;;
;; WARNING : use monospace font in DrRacket to get constant spacing
;;
(define (padding-spc x)
(~r x #:base 2 #:min-width 24 #:pad-string " "))
(define (display-binary-pad x)
(display (padding-spc x)))
;; (flag-set? #b10 #b11) -> #t
;; (flag-set? #b100 #b11) -> #f
(define (flag-set? f x)
(= (bitwise-and f x) f))
;; test the bit of k position
;; (bit-test? #b101 2) -> #t
;; (bit-test? #b101 1) -> #f
(define (bit-test? x k)
(flag-set? (expt 2 k) x))
;; (bit-value #b10110 2) -> 1
;; > (bit-value #b10110 0) -> 0
;; > (bit-value #b10110 3) -> 0
(define (bit-value n pos)
(bitwise-and 1
(arithmetic-shift n (- pos))))
;; logarithme binaire
(define (lb x)
(/ (log x) (log 2)))
;; > (size-bit #b1000) -> 4
;; > (size-bit #b1001) -> 4
;; > (size-bit #b001) -> 1
;; > (size-bit #b0) -> 1
(define (size-bit x)
(if (= 0 x) ;; to avoid error with lb
1
(inexact->exact
(+ 1
(floor (lb x))))))
;; shift left a binary number
(define-syntax shift-left
(syntax-rules ()
((_ x) (arithmetic-shift x 1))
((_ x n) (arithmetic-shift x n))))
;; shift right a binary number
(define-syntax shift-right
(syntax-rules ()
((_ x) (arithmetic-shift x -1))
((_ x n) (arithmetic-shift x (- n)))))
;; non symbolic functions
;;
;; compute Cout, the 'carry out' of the result of Cin + A + B
(define (compute-carry Cin A B)
;; (compute-carry #t #f #t) -> #t
(xor (and A B) (and Cin (xor A B))))
(define (compute-sum Cin A B)
;; (compute-sum #f #t #t) -> #f
;; (compute-sum #t #t #t) -> #t
(xor Cin (xor A B)))
(define-syntax macro-function-compare-2-bits-with-continuation ;; continuation version of macro-compare-2-bits
;; i need a macro because of external function to the clozure
(syntax-rules ()
((_) (let ((cnt 0)) ;; counter
(lambda (continuation b1 b2) (if (equal? b1 b2)
b1
(begin
(set! cnt (add1 cnt)) ;; we leave with continuation in case cpt > 1, we can have used a flag too instead of a counter
(when (> cnt 1) (continuation #f)) ;; escaping with the continuation
'x))))))) ;; return x in case of (b1,b2) = (O,1) or (1,0)
(define-syntax macro-return-function-compare-2-bits-with-kontinuation ;; continuation version of macro-compare-2-bits
;; i need a macro because of external function to the clozure
(syntax-rules (kontinuation)
((_) (let ((cnt 0)) ;; counter
(lambda (b1 b2) (if (equal? b1 b2)
b1
(begin
(set! cnt (add1 cnt))
(when (> cnt 1) (kontinuation #f)) ;; escaping with the continuation
'x)))))))
(define-syntax macro-compare-2-bits ;; i need a macro because of external variable to the clozure
(syntax-rules ()
((_ condition) (let ((cnt 0)) ;; counter
(lambda (b1 b2) (if (equal? b1 b2)
b1
(begin
(set! cnt (add1 cnt))
(when (> cnt 1) (set! condition #t))
'x)))))))
(define-syntax macro-compare-2-bits-with-continuation ;; continuation version of macro-compare-2-bits
;; i need a macro because of external function to the clozure
(syntax-rules ()
((_ continuation) (let ((cnt 0)) ;; counter
(lambda (b1 b2) (if (equal? b1 b2)
b1
(begin
(set! cnt (add1 cnt))
(when (> cnt 1) (continuation #f)) ;; escaping with the continuation
'x)))))))
(define-syntax macro-compare-2-bits-with-kontinuation ;; continuation version of macro-compare-2-bits
;; i need a macro because of external function to the clozure
(syntax-rules (#;kontinuation)
((_) (let ((cnt 0)) ;; counter
(lambda (b1 b2) (if (equal? b1 b2)
b1
(begin
(set! cnt (add1 cnt))
(when (> cnt 1) (kontinuation #f)) ;; escaping with the continuation
'x)))))))
;; function version of macro-compare-2-bits-with-continuation
;;
;; > (call/cc (lambda (k) (function-compare-2-bits-with-continuation k 1 0)))
;; cnt = 1
;; 'x
;; > (call/cc (lambda (k) (function-compare-2-bits-with-continuation k 1 0)))
;; cnt = 2
;; #f
(define function-compare-2-bits-with-continuation
(let ((cnt 0)) ;; counter
(lambda (continuation x y) (if (equal? x y)
x
(begin
(set! cnt (add1 cnt))
;;(dv cnt)
;;(newline)
(when (> cnt 1) (continuation #f)) ;; escaping with the continuation
'x)))))
;; exclusive or
(define (xor p q)
;; (xor #f #f) -> #f
;; (xor #t #f) -> #t
(or (and p (not q)) (and (not p) q)))
;; (xor 0 0) -> 0
;; (xor 1 0) -> 1
;;
;; for DrRacket Scheme
;;(bitwise-xor p q))
;; (if (and (equal? p 1) (equal? q 1))
;; 0
;; 1))
;; symbolic exclusive or
(define (symb-xor p q)
;; (symb-xor 'p 'q) -> '(or (and p (not q)) (and (not p) q))
`(or (and ,p (not ,q)) (and (not ,p) ,q)))
;; adder circuit functions
;; symbolic functions
;;
;; compute Sum symbolically
;; return result of Cin + A + B (Cin being 'carry in')
(define (symb-compute-sum Cin A B)
;; (symb-compute-sum 'Ci 'a 'b) -> '(or (and Ci (not (or (and a (not b)) (and (not a) b)))) (and (not Ci) (or (and a (not b)) (and (not a) b))))
;; (enlight-dnf (symb-compute-sum 'Ci 'a 'b)) -> (a^b^Ci)v(!a^!b^Ci)v(!a^b^!Ci)v(a^!b^!Ci)
(symb-xor Cin (symb-xor A B)))
(define (symb-compute-carry Cin A B)
;; (symb-compute-carry 'Ci 'a 'b)
;; -> '(or (and (and a b) (not (and Ci (or (and a (not b)) (and (not a) b)))))
;; (and (not (and a b)) (and Ci (or (and a (not b)) (and (not a) b)))))
;;
;; (enlight-dnf (symb-compute-carry 'Ci 'a 'b)) -> (a^b)v(a^b^!Ci)v(a^!b^Ci)v(!a^b^Ci)
;; (prefix->infix (simplify (n-arity (simplify-OR (simplify-AND (dnf (symb-compute-carry 'C 'a 'b)))))))
;; -> '((a and !b and C) or (!a and b and C) or (a and b) or (a and b and !C))
;; todo: mettre sous forme disjunctive minimale
(symb-xor `(and ,A ,B) `(and ,Cin ,(symb-xor A B))))
;; redondant avec size-bit
;; (define (binary-length n)
;; (if (= 0 n)
;; 1
;; (integer-length n)))
(define (last-bit-position n) (- (size-bit n) 1))
;;(count-ones #b11001010) -> 4
(define (count-ones n)
(define c 0)
(for-basic (i 0 (size-bit n))
(when (bit-test? n i)
(incf c)))
c)
(define (mult3 x)
(+ (shift-left x) x)) ;; return 2x+x
(define-syntax mac-mult3
(syntax-rules ()
((_ x)
(+ (shift-left x) x))))