Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
269 items
β’
Updated
β’
1
4-bit Ladner-Fischer parallel prefix adder. A parameterized adder family that provides tunable trade-offs between circuit depth and fanout.
S[3:0], Cout = A[3:0] + B[3:0] + Cin
G3,P3 G2,P2 G1,P1 G0,P0 Cin
β β β β β
β β β β β
Level 1 ββββββββββ€ ββββββββββ€ β β Pairwise combination
β β β β β
β β β β β
Level 2 βββββββββββββββββββ΄βββββββββ β β Variable fanout level
β β β
β β β
G3:0 G2:0 G1:0 G0 β
β β β β β
βΌ βΌ βΌ βΌ β
XOR XOR XOR XOR ββββββ
β β β β
βΌ βΌ βΌ βΌ
Cout S3 S2 S1 S0
Ladner-Fischer defines a parameterized family of parallel prefix networks. The parameter controls the trade-off:
Fanout ββββββββββββββββββββββββββββββββββΊ Depth
β β
β Brent-Kung βββ Ladner-Fischer βββ Sklansky
β β β β
β Low fanout Configurable Min depth
β Max depth Trade-off Max fanout
βΌ βΌ
The key insight: at each level, you can choose how many prefix cells to include. More cells = lower depth but higher fanout.
(G_high, P_high) β (G_low, P_low) = (G_high + P_highΒ·G_low, P_highΒ·P_low)
| A | B | Cin | Sum | Cout | Binary |
|---|---|---|---|---|---|
| 0000 | 0000 | 0 | 0000 | 0 | 0+0=0 |
| 0011 | 0001 | 0 | 0100 | 0 | 3+1=4 |
| 0111 | 0101 | 0 | 1100 | 0 | 7+5=12 |
| 1000 | 1000 | 0 | 0000 | 1 | 8+8=16 |
| 1111 | 1111 | 1 | 1111 | 1 | 15+15+1=31 |
G_i = A_i AND B_i
weights: [A_i: 1.0, B_i: 1.0], bias: -2.0
P_i = A_i XOR B_i
OR: [A_i: 1.0, B_i: 1.0], bias: -1.0
NAND: [A_i: -1.0, B_i: -1.0], bias: 1.0
AND: [OR: 1.0, NAND: 1.0], bias: -2.0
Combines G,P pairs according to the Ladner-Fischer pattern with chosen fanout parameter.
| Inputs | 9 (A[3:0], B[3:0], Cin) |
| Outputs | 5 (S[3:0], Cout) |
| Neurons | 32 |
| Layers | 5 |
| Parameters | 132 |
| Magnitude | 56 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def ladner_fischer_add(a3, a2, a1, a0, b3, b2, b1, b0, cin):
a = [a0, a1, a2, a3]
b = [b0, b1, b2, b3]
# Generate/Propagate
g = [a[i] & b[i] for i in range(4)]
p = [a[i] ^ b[i] for i in range(4)]
# Level 1: span 1
g10 = g[1] | (p[1] & g[0])
p10 = p[1] & p[0]
g32 = g[3] | (p[3] & g[2])
p32 = p[3] & p[2]
# Level 2: span 2
g30 = g32 | (p32 & g10)
p30 = p32 & p10
g20 = g[2] | (p[2] & g10)
# Final carries
c0 = g[0] | (p[0] & cin)
c1 = g10 | (p10 & cin)
c2 = g20 | (p[2] & p10 & cin)
c3 = g30 | (p30 & cin)
# Sums
s0 = p[0] ^ cin
s1 = p[1] ^ c0
s2 = p[2] ^ c1
s3 = p[3] ^ c2
return s3, s2, s1, s0, c3
# Verify: 7 + 5 = 12
result = ladner_fischer_add(0,1,1,1, 0,1,0,1, 0)
print(f"7 + 5 = {result[4]*16 + result[0]*8 + result[1]*4 + result[2]*2 + result[3]}")
All 512 input combinations (16 Γ 16 Γ 2) verified correct.
threshold-ladner-fischer/
βββ model.safetensors # Threshold network weights
βββ create_safetensors.py # Weight generation + exhaustive verification
βββ config.json # Circuit metadata
βββ README.md
MIT