File size: 1,328 Bytes
c0340f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-ffs4

4-bit find first set (priority encoder). Returns 1-indexed position of the least significant set bit.

## Function

ffs4(a3, a2, a1, a0) = position of first (least significant) 1 bit, 0 if no bits set

## Truth Table (selected rows)

| Input | First bit | Output (y2,y1,y0) |
|-------|-----------|-------------------|
| 0000 | none | 000 (0) |
| 0001 | bit 0 | 001 (1) |
| 0010 | bit 1 | 010 (2) |
| 0011 | bit 0 | 001 (1) |
| 0100 | bit 2 | 011 (3) |
| 1000 | bit 3 | 100 (4) |
| 1010 | bit 1 | 010 (2) |

## Architecture

```
Layer 1: Priority detection
  has0 = a0 (bit 0 is set)
  has1_first = a1 AND NOT(a0)
  has2_first = a2 AND NOT(a1) AND NOT(a0)
  has3_first = a3 AND NOT(a2) AND NOT(a1) AND NOT(a0)

Layer 2: Binary encoding
  y0 = has0 OR has2_first (ffs is 1 or 3)
  y1 = has1_first OR has2_first (ffs is 2 or 3)
  y2 = has3_first (ffs is 4)
```

## Parameters

| | |
|---|---|
| Inputs | 4 |
| Outputs | 3 |
| Neurons | 7 |
| Layers | 2 |
| Parameters | 35 |
| Magnitude | 22 |

## Usage

```python
from safetensors.torch import load_file
# See model.py for full implementation

# ffs4(1, 0, 1, 0) = [0, 1, 0] = 2 (bit 1 is first set)
# ffs4(0, 0, 0, 1) = [0, 0, 1] = 1 (bit 0 is first set)
```

## License

MIT