Log2Ceil constructor

Log2Ceil(
  1. Logic inp, {
  2. String name = 'log2Ceil',
  3. bool reserveName = false,
  4. bool reserveDefinitionName = false,
  5. String? definitionName,
})

Constructs a Log2Ceil of inp.

Implementation

Log2Ceil(Logic inp,
    {super.name = 'log2Ceil',
    super.reserveName,
    super.reserveDefinitionName,
    String? definitionName})
    : super(definitionName: definitionName ?? 'Log2Ceil_W${inp.width}') {
  inp = addInput('inp', inp, width: inp.width);
  final w = inp.width;
  final outWidth = w.bitLength;
  final log = addOutput('log', width: outWidth);

  final predecessor =
      mux(inp.eq(0), Const(0, width: w), inp - Const(1, width: w));
  final leadingZeros = CountLeadingZeros(predecessor).count;
  log <=
      (Const(w, width: outWidth) - leadingZeros.slice(outWidth - 1, 0))
          .slice(outWidth - 1, 0);
}