--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - arithmetic --- # threshold-ripplecarry8bit Adds two 8-bit numbers with carry-in. Eight cascaded full adders in ripple-carry architecture. ## Circuit ``` 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) ``` ## Example ``` 11111111 (255) + 00000001 ( 1) ────────── 100000000 (256) ``` 255 + 1 = 256. The sum bits are all 0, cout = 1. ## Architecture | Component | Per FA | Total (8 FAs) | |-----------|--------|---------------| | Neurons | 9 | 72 | | Parameters | 27 | 216 | | Layers | 4 | 32 | ## Inputs/Outputs | 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 | ## Critical Path 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. ## Range - Input: 0-255 + 0-255 + 0-1 = 0-511 - Output: 9 bits (8-bit sum + 1-bit carry) ## Usage ```python 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 ``` ## Scaling | 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 | ## Files ``` threshold-ripplecarry8bit/ ├── model.safetensors ├── model.py ├── config.json └── README.md ``` ## License MIT