--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - floating-point - normalization --- # threshold-leading-one-detect 8-bit leading one detector. Finds the position of the highest-order 1 bit (MSB priority). ## Circuit ``` x[7:0] ──► LOD ──┬──► pos[2:0] (position 0-7) └──► valid (1 if any bit set) ``` ## Truth Table (examples) | Input | Binary | Position | Valid | |-------|--------|----------|-------| | 0 | 00000000 | 0 | 0 | | 1 | 00000001 | 0 | 1 | | 2 | 00000010 | 1 | 1 | | 128 | 10000000 | 7 | 1 | | 255 | 11111111 | 7 | 1 | | 170 | 10101010 | 7 | 1 | ## Algorithm ``` For each bit position i (7 down to 0): leader[i] = x[i] AND NOT(any bit j > i is set) Position is binary encoding of which leader is active. ``` ## Architecture | Component | Count | Neurons | |-----------|-------|---------| | Higher-than OR | 8 | 8 | | NOT higher | 8 | 8 | | Leader AND | 8 | 8 | | Position encode | 3 | 3 | | Valid OR | 1 | 1 | **Total: 28 neurons, 136 parameters, 3 layers** ## Applications - **Floating-point normalization**: Find exponent adjustment - **Priority encoding**: Highest-priority interrupt - **Integer log₂**: Position = floor(log₂(x)) - **Bit manipulation**: Count leading zeros = 7 - position ## Usage ```python from safetensors.torch import load_file w = load_file('model.safetensors') # For x = 0b00101100 (44): # Leading one at position 5 # log₂(44) ≈ 5 ``` ## Relation to CLZ Count Leading Zeros = 7 - position (when valid=1) ``` x = 0b00100000 → position=5 → CLZ=2 x = 0b10000000 → position=7 → CLZ=0 ``` ## Files ``` threshold-leading-one-detect/ ├── model.safetensors ├── create_safetensors.py ├── config.json └── README.md ``` ## License MIT