File size: 4,613 Bytes
66ce5d6 107e84b 66ce5d6 107e84b 66ce5d6 107e84b 66ce5d6 107e84b 66ce5d6 107e84b 66ce5d6 107e84b 66ce5d6 107e84b 66ce5d6 107e84b 66ce5d6 107e84b 66ce5d6 107e84b 66ce5d6 | 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 | ---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- arithmetic
- parallel-prefix
- adder
---
# threshold-han-carlson
4-bit Han-Carlson parallel prefix adder. Hybrid design combining Kogge-Stone's logarithmic depth with Brent-Kung's reduced wiring complexity.
## Function
```
S[3:0], Cout = A[3:0] + B[3:0] + Cin
```
Computes 4-bit addition with carry-in/carry-out using parallel prefix computation.
## Han-Carlson Structure (4-bit)
```
G3,P3 G2,P2 G1,P1 G0,P0 Cin
β β β β β
β β β β β
Level 1 ββββββββββ ββββββββββ β β Odd-position prefix (like Brent-Kung)
β β β
β β β
Level 2 βββββββββββββββββββ β β Kogge-Stone style merge
β β
β β
Level 3 ββββββββββ β β Back-propagate to even positions
β β
β β
G3:0 G2:0 G1:0 G0 β
β β β β β
βΌ βΌ βΌ βΌ β
ββββββ΄βββββ¬ββββ΄ββββ¬βββββ΄βββββ¬ββββ΄ββββββββ
β β β β
XOR XOR XOR XOR
β β β β
βΌ βΌ βΌ βΌ
Cout S3 S2 S1 S0
```
## Design Philosophy
Han-Carlson is a **hybrid parallel prefix adder** that interpolates between two extremes:
| Property | Kogge-Stone | Han-Carlson | Brent-Kung |
|----------|-------------|-------------|------------|
| Depth | logβ(n) | logβ(n) + 1 | 2Β·logβ(n) - 2 |
| Prefix cells | nΒ·logβ(n) - n + 1 | (n/2)Β·logβ(n) | 2n - 2 - logβ(n) |
| Wiring tracks | High | Medium | Low |
| Fanout | Uniform | Mixed | Low |
For n=4: Depth=3, Cells=4, offering the best balance for small adders.
## Parallel Prefix Operation
The fundamental operation combines (Generate, Propagate) pairs:
```
(G_high, P_high) β (G_low, P_low) = (G_high + P_highΒ·G_low, P_highΒ·P_low)
```
Where:
- G_i = A_i AND B_i (generate carry)
- P_i = A_i XOR B_i (propagate carry)
## Truth Table (Examples)
| A (dec) | B (dec) | Cin | S (dec) | Cout | Equation |
|---------|---------|-----|---------|------|----------|
| 0 | 0 | 0 | 0 | 0 | 0+0+0=0 |
| 5 | 3 | 0 | 8 | 0 | 5+3+0=8 |
| 7 | 7 | 0 | 14 | 0 | 7+7+0=14 |
| 15 | 1 | 0 | 0 | 1 | 15+1+0=16 |
| 15 | 15 | 1 | 15 | 1 | 15+15+1=31 |
## Layer 0 Weights (Generate/Propagate)
Each bit position computes G and P from inputs:
```
G_i: weights[A_i]=1, weights[B_i]=1, bias=-2 (AND gate)
P_i: XOR decomposition using OR/NAND/AND
```
## Parameters
| | |
|---|---|
| Inputs | 9 (A[3:0], B[3:0], Cin) |
| Outputs | 5 (S[3:0], Cout) |
| Neurons | 32 |
| Layers | 5 |
| Parameters | 132 |
| Magnitude | 56 |
## Usage
```python
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def han_carlson_add(a3, a2, a1, a0, b3, b2, b1, b0, cin):
# Generate and Propagate
g = [a0 & b0, a1 & b1, a2 & b2, a3 & b3]
p = [a0 ^ b0, a1 ^ b1, a2 ^ b2, a3 ^ b3]
# Level 1: odd positions
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: top merge
g30 = g32 | (p32 & g10)
# Level 3: back-propagate
g20 = g[2] | (p[2] & g10)
# Carries and sums
c0 = g[0] | (p[0] & cin)
c1 = g10 | (p10 & cin)
c2 = g20 | (p[2] & p10 & cin)
c3 = g30 | (p32 & p10 & cin)
return p[3]^c2, p[2]^c1, p[1]^c0, p[0]^cin, c3
# Example: 5 + 3 = 8
s3, s2, s1, s0, cout = han_carlson_add(0,1,0,1, 0,0,1,1, 0)
print(f"Result: {cout*16 + s3*8 + s2*4 + s1*2 + s0}") # 8
```
## Verification
All 512 input combinations (16 Γ 16 Γ 2) verified correct.
## Files
```
threshold-han-carlson/
βββ model.safetensors # Threshold network weights
βββ create_safetensors.py # Weight generation + verification
βββ config.json # Circuit metadata
βββ README.md
```
## References
- Han, T., & Carlson, D. A. (1987). "Fast area-efficient VLSI adders"
- Parallel prefix adders are fundamental to high-performance ALU design
## License
MIT
|