--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - arithmetic - alu --- # threshold-alu4bit 4-bit Arithmetic Logic Unit as threshold circuit. Performs arithmetic and bitwise operations on two 4-bit operands. ## Circuit ``` A[3:0] ──┐ B[3:0] ──┼──► ALU ──┬──► R[3:0] (result) op[1:0] ──┘ ├──► Cout (carry out) └──► Zero (zero flag) ``` ## Operations | op | Operation | Description | |----|-----------|-------------| | 00 | ADD | R = A + B | | 01 | SUB | R = A - B (2's complement) | | 10 | AND | R = A & B | | 11 | XOR | R = A ^ B | ## Flags - **Cout**: Carry out from addition, borrow (inverted) from subtraction - **Zero**: Set when result is 0 ## Architecture | Component | Count | Neurons | |-----------|-------|---------| | Full Adders | 4 | 28 | | B XOR (for SUB) | 4 | 12 | | Bitwise AND | 4 | 4 | | Bitwise XOR | 4 | 12 | | Output MUX4 | 4 | 20 | | Zero detect | 1 | 1 | | Cout MUX | 1 | 5 | **Total: 82 neurons, 282 parameters, 5 layers** ## How Subtraction Works ``` A - B = A + (~B) + 1 (2's complement) When op=01: - B bits are XORed with 1 (inverted) - Carry-in is set to 1 - Result is A - B ``` ## Usage ```python from safetensors.torch import load_file w = load_file('model.safetensors') # Examples: # op=0: 5 + 3 = 8, cout=0, zero=0 # op=1: 5 - 3 = 2, cout=1, zero=0 # op=2: 5 & 3 = 1, cout=0, zero=0 # op=3: 5 ^ 3 = 6, cout=0, zero=0 ``` ## Files ``` threshold-alu4bit/ ├── model.safetensors ├── create_safetensors.py ├── config.json └── README.md ``` ## License MIT