|
|
--- |
|
|
license: mit |
|
|
tags: |
|
|
- pytorch |
|
|
- safetensors |
|
|
- threshold-logic |
|
|
- neuromorphic |
|
|
- arithmetic |
|
|
- divider |
|
|
--- |
|
|
|
|
|
# threshold-divider |
|
|
|
|
|
4-bit unsigned integer divider as threshold circuit using restoring division algorithm. |
|
|
|
|
|
## Circuit |
|
|
|
|
|
``` |
|
|
A[3:0] βββ (dividend, 0-15) |
|
|
ββββΊ Divider βββ¬βββΊ Q[3:0] (quotient) |
|
|
B[3:0] βββ (divisor, 1-15) ββββΊ R[3:0] (remainder) |
|
|
|
|
|
A = Q Γ B + R, where R < B |
|
|
``` |
|
|
|
|
|
## Algorithm (Restoring Division) |
|
|
|
|
|
``` |
|
|
P = 0 (partial remainder) |
|
|
for i = 3 downto 0: |
|
|
P = (P << 1) | A[i] // shift in next dividend bit |
|
|
diff = P - B |
|
|
if diff >= 0: // P >= B |
|
|
Q[i] = 1 |
|
|
P = diff // keep subtraction result |
|
|
else: |
|
|
Q[i] = 0 // restore P (don't subtract) |
|
|
R = P (final remainder) |
|
|
``` |
|
|
|
|
|
## Architecture |
|
|
|
|
|
Each of 4 stages contains: |
|
|
|
|
|
| Component | Function | Neurons | |
|
|
|-----------|----------|---------| |
|
|
| 4-bit Subtractor | P - B | 28 | |
|
|
| 4Γ MUX | Select P or diff | 12 | |
|
|
|
|
|
**Total: 160 neurons, 496 parameters, 8 layers** |
|
|
|
|
|
## Example |
|
|
|
|
|
``` |
|
|
13 Γ· 3: |
|
|
Stage 3: P=0, shift in 1 β P=1, 1<3, Q[3]=0 |
|
|
Stage 2: P=1, shift in 1 β P=3, 3β₯3, Q[2]=1, P=0 |
|
|
Stage 1: P=0, shift in 0 β P=0, 0<3, Q[1]=0 |
|
|
Stage 0: P=0, shift in 1 β P=1, 1<3, Q[0]=0 |
|
|
Result: Q=0100=4, R=0001=1 |
|
|
Verify: 4Γ3+1=13 β |
|
|
``` |
|
|
|
|
|
## Usage |
|
|
|
|
|
```python |
|
|
from safetensors.torch import load_file |
|
|
|
|
|
w = load_file('model.safetensors') |
|
|
|
|
|
# Division by zero returns Q=15, R=0 |
|
|
# All 240 test cases verified (16 Γ 15 non-zero divisors) |
|
|
``` |
|
|
|
|
|
## Files |
|
|
|
|
|
``` |
|
|
threshold-divider/ |
|
|
βββ model.safetensors |
|
|
βββ create_safetensors.py |
|
|
βββ config.json |
|
|
βββ README.md |
|
|
``` |
|
|
|
|
|
## License |
|
|
|
|
|
MIT |
|
|
|