File size: 1,680 Bytes
1ec87d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
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