Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
269 items
β’
Updated
β’
1
Adds two 8-bit numbers with carry-in. Eight cascaded full adders in ripple-carry architecture.
a0 b0 a1 b1 a2 b2 a3 b3 a4 b4 a5 b5 a6 b6 a7 b7
β β β β β β β β β β β β β β β β
ββ¬β ββ¬β ββ¬β ββ¬β ββ¬β ββ¬β ββ¬β ββ¬β
βΌ βΌ βΌ βΌ βΌ βΌ βΌ βΌ
ββββββ c0 ββββββ c1 ββββββ c2 ββββββ c3 ββββββ c4 ββββββ c5 ββββββ c6 ββββββ
βFA0 ββββββFA1 ββββββFA2 ββββββFA3 ββββββFA4 ββββββFA5 ββββββFA6 ββββββFA7 βββcout
ββββββ ββββββ ββββββ ββββββ ββββββ ββββββ ββββββ ββββββ
β β β β β β β β
βΌ βΌ βΌ βΌ βΌ βΌ βΌ βΌ
s0 s1 s2 s3 s4 s5 s6 s7
cinββ
Input: (a7..a0) + (b7..b0) + cin
Output: (cout s7..s0)
11111111 (255)
+ 00000001 ( 1)
ββββββββββ
100000000 (256)
255 + 1 = 256. The sum bits are all 0, cout = 1.
| Component | Per FA | Total (8 FAs) |
|---|---|---|
| Neurons | 9 | 72 |
| Parameters | 27 | 216 |
| Layers | 4 | 32 |
| Name | Bits | Description |
|---|---|---|
| a | a7..a0 | First 8-bit number (LSB = a0) |
| b | b7..b0 | Second 8-bit number (LSB = b0) |
| cin | 1 | Carry in |
| s | s7..s0 | 8-bit sum (LSB = s0) |
| cout | 1 | Carry out |
Worst-case: carry propagates through all 8 full adders.
cin β FA0 β FA1 β FA2 β FA3 β FA4 β FA5 β FA6 β FA7 β cout
This is 8 Γ 4 = 32 layers deep. For a byte-wide ALU, this latency is acceptable. For wider words, carry-lookahead or prefix adders would be preferred.
from safetensors.torch import load_file
w = load_file('model.safetensors')
def ripple_carry_8bit(a, b, cin):
"""a, b: 8-bit lists [a0..a7] (LSB first)"""
# See model.py for full implementation
pass
# Example: 200 + 100 = 300 (overflow to 9 bits)
a = [(200 >> i) & 1 for i in range(8)] # 200 in LSB-first
b = [(100 >> i) & 1 for i in range(8)] # 100 in LSB-first
sums, cout = ripple_carry_8bit(a, b, 0)
result = sum(bit << i for i, bit in enumerate(sums)) + (cout << 8)
# result = 300
| Bits | Full Adders | Neurons | Parameters | Max Depth |
|---|---|---|---|---|
| 2 | 2 | 18 | 54 | 8 |
| 4 | 4 | 36 | 108 | 16 |
| 8 | 8 | 72 | 216 | 32 |
| 16 | 16 | 144 | 432 | 64 |
| n | n | 9n | 27n | 4n |
threshold-ripplecarry8bit/
βββ model.safetensors
βββ model.py
βββ config.json
βββ README.md
MIT