UnsignedAddWithOverflow constructor

UnsignedAddWithOverflow(
  1. Logic a,
  2. Logic b, {
  3. int? sumWidth,
  4. String name = 'unsigned_add_with_overflow',
  5. bool reserveName = false,
  6. bool reserveDefinitionName = false,
  7. String? definitionName,
})

Constructs an UnsignedAddWithOverflow of a and b.

Implementation

UnsignedAddWithOverflow(Logic a, Logic b,
    {int? sumWidth,
    super.name = 'unsigned_add_with_overflow',
    super.reserveName,
    super.reserveDefinitionName,
    String? definitionName})
    : sumWidth = sumWidth ?? max(a.width, b.width),
      super(
          definitionName: definitionName ??
              'UnsignedAddWithOverflow_A${a.width}_B${b.width}'
                  '_S${sumWidth ?? max(a.width, b.width)}') {
  a = addInput('a', a, width: a.width);
  b = addInput('b', b, width: b.width);
  final sum = addOutput('sum', width: this.sumWidth);
  final overflow = addOutput('overflow');

  // One extra bit above the widest operand and sum keeps the true sum.
  final w = max(max(a.width, b.width), this.sumWidth) + 1;
  final full = a.zeroExtend(w) + b.zeroExtend(w);
  sum <= full.slice(this.sumWidth - 1, 0);
  overflow <= full.slice(w - 1, this.sumWidth).or();
}