File size: 1,742 Bytes
5993426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- arithmetic
- adder
---

# threshold-carryselect-adder

4-bit carry-select adder as threshold circuit. Faster than ripple-carry by pre-computing results for both possible carry values.

## Circuit

```
A[3:0] ──┐
B[3:0] ──┼──► CSel Adder ──┬──► S[3:0]
Cin    β”€β”€β”˜                 └──► Cout
```

## How It Works

```
Block 0 (bits 0-1): Ripple-carry with actual Cin
  └──► produces C1 (carry into bit 2)

Block 1 (bits 2-3): Pre-compute BOTH possibilities
  Path A: assume Cin=0 β†’ S2_0, S3_0, Cout_0
  Path B: assume Cin=1 β†’ S2_1, S3_1, Cout_1

Final: MUX selects Path A or B based on C1
```

## Timing Advantage

```
Ripple-Carry:  [FA0]──►[FA1]──►[FA2]──►[FA3]  (4 stages)

Carry-Select:  [FA0]──►[FA1]──┐
               [HA2]──►[FA3]  β”œβ”€β”€β–Ί[MUX]  (3 stages)
               [FA2]──►[FA3]β”€β”€β”˜
```

Block 1 computes in parallel with Block 0.

## Architecture

| Component | Count | Neurons |
|-----------|-------|---------|
| Block 0 (2 FAs) | 2 | 14 |
| Block 1 cin=0 (HA+FA) | 2 | 11 |
| Block 1 cin=1 (2 FAs) | 2 | 14 |
| Output MUXes | 3 | 9 |

**Total: 48 neurons, 149 parameters, 4 layers**

## Trade-offs

- More neurons than ripple-carry (48 vs ~28)
- Faster critical path (3 stages vs 4)
- Scales well: 8-bit uses 4x2-bit blocks

## Usage

```python
from safetensors.torch import load_file

w = load_file('model.safetensors')

# All 512 test cases verified (16 Γ— 16 Γ— 2)
```

## Files

```
threshold-carryselect-adder/
β”œβ”€β”€ model.safetensors
β”œβ”€β”€ create_safetensors.py
β”œβ”€β”€ config.json
└── README.md
```

## License

MIT