How many matches in two queues

I have two queues of sequence items, one from the dut and one from the ref. I’d like to count the amount of transactions that match so I thought about using the sum reduction method:

my_seq_item ref_queue[$];
my_seq_item dut_queue[$];

// find_matches() relies on caller to make sure the queues are not empty
function int find_matches();
  return dut_queue.sum() with (item.compare(ref_queue[item.index]);
endfunction

but apparently it doesn’t behave like I would expect.

The 1800-2017 states (7.12.4):

The following reduction methods are supported:
— sum() returns the sum of all the array elements or, if a with clause is specified, returns the sum of the values yielded by evaluating the expression for each array element.

I assume the the ‘sum of the values yielded’ would be the sum of 1'b1 and 1'b0 reported by the compare operation.

Should I cast the result into a bit datatype for the sum to work? Like this:

function int find_matches();
  return dut_queue.sum() with (item.compare(bit'(ref_queue[item.index]));
endfunction

I’m trying the above and will report, but if in the meantime someone is able to catch another issue that would be great.

The result of the compare method is a single bit, so the result of the sum method will also be a single bit without a cast. You want to cast the result to an int, not the inputs to the compare method.

function int find_matches();
  return dut_queue.sum() with (int'(item.compare(ref_queue[item.index]));
endfunction
1 Like

Hi Dave, actually what I wrote was not what I intended! What I said I was going to do it was supposed to be:

function int find_matches();
  return dut_queue.sum() with (bit'(item.compare(ref_queue[item.index]));
endfunction

Nonetheless, what you say makes sense, I assumed (wrongly) that summing bits will automatically be cast to an int but I’m not sure why I thought so.

Thanks a lot!