Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
•
248 items
•
Updated
•
1
8-bit count leading zeros.
clz8(a7, a6, a5, a4, a3, a2, a1, a0) = number of leading zeros from MSB (0-8)
| Input | First 1 | CLZ | Output |
|---|---|---|---|
| 1xxxxxxx | bit 7 | 0 | 0000 |
| 01xxxxxx | bit 6 | 1 | 0001 |
| 001xxxxx | bit 5 | 2 | 0010 |
| 0001xxxx | bit 4 | 3 | 0011 |
| 00001xxx | bit 3 | 4 | 0100 |
| 000001xx | bit 2 | 5 | 0101 |
| 0000001x | bit 1 | 6 | 0110 |
| 00000001 | bit 0 | 7 | 0111 |
| 00000000 | none | 8 | 1000 |
Layer 1: Priority detection from MSB (9 neurons)
has7 = a7 (MSB is set, clz=0)
has6_first = a6 AND NOT(a7) (clz=1)
has5_first = a5 AND NOT(a6) AND NOT(a7) (clz=2)
...
has0_first = a0 AND NOT(a1..a7) (clz=7)
all_zero = NOT(any bit) (clz=8)
Layer 2: Binary encoding (4 neurons)
y0 = has6_first OR has4_first OR has2_first OR has0_first
y1 = has5_first OR has4_first OR has1_first OR has0_first
y2 = has3_first OR has2_first OR has1_first OR has0_first
y3 = all_zero
| Inputs | 8 |
| Outputs | 4 |
| Neurons | 13 |
| Layers | 2 |
| Parameters | 117 |
| Magnitude | 69 |
from safetensors.torch import load_file
# See model.py for full implementation
# clz8(1,0,0,0,0,0,0,0) = [0,0,0,0] = 0 (MSB set)
# clz8(0,0,0,0,1,0,0,0) = [0,1,0,0] = 4 (four leading zeros)
# clz8(0,0,0,0,0,0,0,0) = [1,0,0,0] = 8 (all zeros)
MIT