--- 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