threshold-carry-generate

Carry generate signal for carry-lookahead adders. Outputs 1 when the bit position will definitely generate a carry, regardless of incoming carry.

Circuit

    a       b
    β”‚       β”‚
    β””β”€β”€β”€β”¬β”€β”€β”€β”˜
        β”‚
        β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”
    β”‚  AND  β”‚
    β”‚ w:1,1 β”‚
    β”‚ b: -2 β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
       G(a,b)

Function

generate(a, b) = a AND b

A carry is generated at position i when both inputs are 1, because 1+1=10 in binary always produces a carry.

Truth Table

a b G Meaning
0 0 0 0+0=0, no carry
0 1 0 0+1=1, no carry
1 0 0 1+0=1, no carry
1 1 1 1+1=10, generates carry

Mechanism

The AND gate fires when both inputs contribute, guaranteeing a carry output:

  • Weighted sum = a + b
  • Bias = -2 requires both inputs to be 1
  • Result: outputs 1 only when a=1 AND b=1

Carry-Lookahead Context

In a carry-lookahead adder, each bit position computes two signals:

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 becomes:

C[i+1] = G[i] OR (P[i] AND C[i])

For multi-bit lookahead:

C[4] = G[3] OR (P[3] AND G[2]) OR (P[3] AND P[2] AND G[1]) OR (P[3] AND P[2] AND P[1] AND G[0]) OR (P[3] AND P[2] AND P[1] AND P[0] AND C[0])

Parameters

Weights [1, 1]
Bias -2
Inputs 2
Outputs 1
Neurons 1
Layers 1
Parameters 3
Magnitude 4

Usage

from safetensors.torch import load_file
import torch

w = load_file('model.safetensors')

def carry_generate(a, b):
    inp = torch.tensor([float(a), float(b)])
    return int((inp @ w['and.weight'].T + w['and.bias'] >= 0).item())

# Examples
print(carry_generate(0, 0))  # 0
print(carry_generate(0, 1))  # 0
print(carry_generate(1, 0))  # 0
print(carry_generate(1, 1))  # 1 (generates carry)

Applications

  • Carry-lookahead adders (CLA)
  • Parallel prefix adders (Kogge-Stone, Brent-Kung, etc.)
  • High-speed ALU design
  • Multiplier partial product reduction

Related Circuits

  • threshold-carry-propagate: The P signal (a XOR b)
  • threshold-carrylookahead4bit: 4-bit CLA using G and P
  • threshold-kogge-stone: Parallel prefix adder

Files

threshold-carry-generate/
β”œβ”€β”€ model.safetensors
β”œβ”€β”€ model.py
β”œβ”€β”€ create_safetensors.py
β”œβ”€β”€ config.json
└── README.md

License

MIT

Downloads last month
7
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Collection including phanerozoic/threshold-carry-generate