threshold-ctz4 / README.md
CharlesCNorton
Add 4-bit count trailing zeros threshold circuit
b827152
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- bit-manipulation
---
# threshold-ctz4
4-bit count trailing zeros. Returns the number of consecutive zero bits starting from the LSB.
## Function
ctz4(x3, x2, x1, x0) -> count (0-4)
- CTZ = 0 if x0 = 1 (no trailing zeros)
- CTZ = 1 if x0 = 0, x1 = 1
- CTZ = 2 if x0 = x1 = 0, x2 = 1
- CTZ = 3 if x0 = x1 = x2 = 0, x3 = 1
- CTZ = 4 if all bits are zero
## Truth Table (selected rows)
| Input | CTZ | Output |
|-------|-----|--------|
| 0001 | 0 | 000 |
| 0010 | 1 | 001 |
| 0100 | 2 | 010 |
| 1000 | 3 | 011 |
| 0000 | 4 | 100 |
| 0110 | 1 | 001 |
| 1100 | 2 | 010 |
## Architecture
```
x3 x2 x1 x0
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Layer 1: Prefix conditions β”‚
β”‚ p0 = (x0=0) β”‚
β”‚ p01 = (x0=x1=0) β”‚
β”‚ p012 = (x0=x1=x2=0) β”‚
β”‚ all_zero = (all=0) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Layer 2: One-hot position β”‚
β”‚ z1 = p0 AND x1 β”‚
β”‚ z2 = p01 AND x2 β”‚
β”‚ z3 = p012 AND x3 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Layer 3: Binary encoding β”‚
β”‚ y2 = all_zero β”‚
β”‚ y1 = z2 OR z3 β”‚
β”‚ y0 = z1 OR z3 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
y2 y1 y0
```
## Parameters
| | |
|---|---|
| Inputs | 4 |
| Outputs | 3 |
| Neurons | 10 |
| Layers | 3 |
| Parameters | 37 |
| Magnitude | 30 |
## Usage
```python
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
# ctz4(0,1,0,0) = 2 (binary 0100 has 2 trailing zeros)
# See model.py for full implementation
```
## Applications
- Finding the lowest set bit position
- Efficient division by powers of 2
- Bit manipulation in cryptography
- Hardware priority encoders
## License
MIT