Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
269 items
β’
Updated
β’
1
Carry propagate signal for carry-lookahead adders. Outputs 1 when the bit position will propagate an incoming carry to the output.
a b
β β
βββββ¬ββββ€
β β β
βΌ β βΌ
βββββββββββββββββ
β OR βββ NAND β
βw:1,1 βββw:-1,-1β
βb: -1 βββb: +1 β
βββββββββββββββββ
β β β
βββββΌββββ
βΌ
ββββββββ
β AND β
βw: 1,1β
βb: -2 β
ββββββββ
β
βΌ
P(a,b)
propagate(a, b) = a XOR b
A carry is propagated at position i when exactly one input is 1. In this case, an incoming carry will be passed through to the output.
| a | b | P | Meaning |
|---|---|---|---|
| 0 | 0 | 0 | 0+0+Cin: Cin absorbed, not propagated |
| 0 | 1 | 1 | 0+1+Cin: Cin propagates through |
| 1 | 0 | 1 | 1+0+Cin: Cin propagates through |
| 1 | 1 | 0 | 1+1+Cin: generates carry, doesn't "propagate" |
XOR is implemented as AND(OR, NAND):
Layer 1:
Layer 2:
| a | b | OR | NAND | AND(OR,NAND) |
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 1 | 1 |
| 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 0 | 0 |
Together with Generate (G), Propagate (P) enables fast carry computation:
| Signal | Function | Meaning |
|---|---|---|
| G (Generate) | a AND b | Will produce carry regardless of Cin |
| P (Propagate) | a XOR b | Will pass through incoming carry |
The carry equation:
C[i+1] = G[i] OR (P[i] AND C[i])
In words: "Next carry is generated, OR propagated from previous."
Some texts define P = a OR b instead of a XOR b. Both work for carry-lookahead, but XOR is more common because:
| Inputs | 2 |
| Outputs | 1 |
| Neurons | 3 |
| Layers | 2 |
| Parameters | 9 |
| Magnitude | 10 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def carry_propagate(a, b):
inp = torch.tensor([float(a), float(b)])
or_out = int((inp @ w['or.weight'].T + w['or.bias'] >= 0).item())
nand_out = int((inp @ w['nand.weight'].T + w['nand.bias'] >= 0).item())
l1 = torch.tensor([float(or_out), float(nand_out)])
return int((l1 @ w['and.weight'].T + w['and.bias'] >= 0).item())
# Examples
print(carry_propagate(0, 0)) # 0
print(carry_propagate(0, 1)) # 1 (propagates)
print(carry_propagate(1, 0)) # 1 (propagates)
print(carry_propagate(1, 1)) # 0
threshold-carry-generate: The G signal (a AND b)threshold-xor: Same circuit, different namethreshold-carrylookahead4bit: Uses both G and Pthreshold-carry-propagate/
βββ model.safetensors
βββ model.py
βββ create_safetensors.py
βββ config.json
βββ README.md
MIT