File size: 1,550 Bytes
ad8baa5 |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- arithmetic
- signed
---
# threshold-twos-complement
4-bit two's complement negation circuit. Computes -A for signed integers.
## Circuit
```
A[3:0] βββΊ Negate βββ¬βββΊ N[3:0] (-A)
ββββΊ overflow (when A = -8)
```
## Algorithm
```
-A = ~A + 1
1. Invert all bits
2. Add 1
```
## Truth Table
| A (unsigned) | A (signed) | N (signed) | Overflow |
|--------------|------------|------------|----------|
| 0000 (0) | +0 | +0 | 0 |
| 0001 (1) | +1 | -1 | 0 |
| 0111 (7) | +7 | -7 | 0 |
| 1000 (8) | -8 | -8 | 1 |
| 1111 (15) | -1 | +1 | 0 |
## Overflow
The value -8 (1000) cannot be negated in 4-bit two's complement:
- Range: -8 to +7
- -(-8) = +8, which exceeds +7
- Result wraps to -8, overflow flag set
## Architecture
| Component | Count | Neurons |
|-----------|-------|---------|
| Bit inverters | 4 | 4 |
| Half-adder chain | 4 | 16 |
| Overflow detect | 1 | 2 |
**Total: 22 neurons, 63 parameters, 4 layers**
## Examples
```
-(+5) = ~(0101) + 1 = 1010 + 1 = 1011 = -5 β
-(-3) = ~(1101) + 1 = 0010 + 1 = 0011 = +3 β
-(-8) = ~(1000) + 1 = 0111 + 1 = 1000 = -8 (overflow!)
```
## Usage
```python
from safetensors.torch import load_file
w = load_file('model.safetensors')
# All 16 cases verified
# Overflow only when A = -8 (1000)
```
## Files
```
threshold-twos-complement/
βββ model.safetensors
βββ create_safetensors.py
βββ config.json
βββ README.md
```
## License
MIT
|