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

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

Downloads last month
8
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Collection including phanerozoic/threshold-divider