Adds two 4-bit numbers with carry-in. Four cascaded full adders in the classic ripple-carry architecture.
a0 b0 cin a1 b1 a2 b2 a3 b3
│ │ │ │ │ │ │ │ │
└──┼──┘ └──┼──┘ └──┼──┘ └──┼──┘
▼ ▼ ▼ ▼
┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐
│ FA0 │───c0───│ FA1 │──c1──│ FA2 │──c2──│ FA3 │───cout
└───────┘ └───────┘ └───────┘ └───────┘
│ │ │ │
▼ ▼ ▼ ▼
s0 s1 s2 s3
Input: (a3 a2 a1 a0) + (b3 b2 b1 b0) + cin
Output: (cout s3 s2 s1 s0)
a b
│ │
└───┬───┘
▼
┌─────────┐
│ HA1 │ Half adder 1
└─────────┘
│ │
s1 c1
│ \
│ cin \
└──┬──┘ \
▼ \
┌─────────┐ \
│ HA2 │ │
└─────────┘ │
│ │ │
sum c2 │
│ │
└──┬───┘
▼
┌──────┐
│ OR │
└──────┘
│
▼
cout
1111 (15)
+ 0001 ( 1)
──────
10000 (16)
15 + 1 = 16, which overflows 4 bits. The result is s=[0,0,0,0] with cout=1.
This is 4 × 4 = 16 layers deep. Carry-lookahead would reduce this but requires more complex circuitry.
1from safetensors.torch import load_file
2
3w = load_file('model.safetensors')
4
5def ripple_carry_4bit(a, b, cin):
6 """a, b: 4-bit lists [a0,a1,a2,a3] (LSB first)"""
7 # See model.py for full implementation
8 pass
9
10# Example: 7 + 5 = 12
11a = [1, 1, 1, 0] # 7 in LSB-first
12b = [1, 0, 1, 0] # 5 in LSB-first
13sums, cout = ripple_carry_4bit(a, b, 0)
14# sums = [0, 0, 1, 1], cout = 0 → 12
threshold-ripplecarry4bit/
├── model.safetensors
├── model.py
├── config.json
└── README.md