35 lines
827 B
Text
35 lines
827 B
Text
F xor(a, b)
|
||
R (a & !b) | (b & !a)
|
||
|
||
F ha(a, b)
|
||
R (xor(a, b), a & b)
|
||
|
||
F fa(a, b, ci)
|
||
V (s0, c0) = ha(ci, a)
|
||
V (s1, c1) = ha(s0, b)
|
||
R (s1, c0 | c1)
|
||
|
||
F fa4(a, b)
|
||
V width = 4
|
||
V ci = [0B] * width
|
||
V co = [0B] * width
|
||
V s = [0B] * width
|
||
L(i) 0 .< width
|
||
(s[i], co[i]) = fa(a[i], b[i], I i != 0 {co[i - 1]} E 0)
|
||
R (s, co.last)
|
||
|
||
F int2bus(n, width = 4)
|
||
R reversed(bin(n).zfill(width)).map(c -> Int(c))
|
||
|
||
F bus2int(b)
|
||
R sum(enumerate(b).filter2((i, bit) -> bit).map2((i, bit) -> 1 << i))
|
||
|
||
V width = 4
|
||
V tot = [0B] * (width + 1)
|
||
L(a) 0 .< 2 ^ width
|
||
L(b) 0 .< 2 ^ width
|
||
V (ta, tlast) = fa4(int2bus(a), int2bus(b))
|
||
L(i) 0 .< width
|
||
tot[i] = ta[i]
|
||
tot[width] = tlast
|
||
assert(a + b == bus2int(tot), ‘totals don't match: #. + #. != #.’.format(a, b, String(tot)))
|