File size: 1,270 Bytes
f3d0f2f | 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 | ---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
---
# threshold-clz4
4-bit count leading zeros.
## Function
clz4(a3, a2, a1, a0) = number of leading zeros from MSB
## Truth Table (selected rows)
| Input | First 1 | CLZ | Output |
|-------|---------|-----|--------|
| 1xxx | bit 3 | 0 | 000 |
| 01xx | bit 2 | 1 | 001 |
| 001x | bit 1 | 2 | 010 |
| 0001 | bit 0 | 3 | 011 |
| 0000 | none | 4 | 100 |
## Architecture
```
Layer 1: Priority detection from MSB
has3 = a3 (MSB is set, clz=0)
has2_first = a2 AND NOT(a3) (clz=1)
has1_first = a1 AND NOT(a2) AND NOT(a3) (clz=2)
has0_first = a0 AND NOT(a1) AND NOT(a2) AND NOT(a3) (clz=3)
all_zero = NOT(any bit) (clz=4)
Layer 2: Binary encoding
y0 = has2_first OR has0_first (clz is 1 or 3)
y1 = has1_first OR has0_first (clz is 2 or 3)
y2 = all_zero (clz is 4)
```
## Parameters
| | |
|---|---|
| Inputs | 4 |
| Outputs | 3 |
| Neurons | 8 |
| Layers | 2 |
| Parameters | 39 |
| Magnitude | 26 |
## Usage
```python
from safetensors.torch import load_file
# See model.py for full implementation
# clz4(1, 0, 0, 0) = [0, 0, 0] = 0 (MSB is set)
# clz4(0, 0, 1, 0) = [0, 1, 0] = 2 (two leading zeros)
# clz4(0, 0, 0, 0) = [1, 0, 0] = 4 (all zeros)
```
## License
MIT
|