Comparing Variable slices of Fixed Arrays

I want to compare variable-width slices of fixed arrays. Since the part selects (-:,+:) require a constant width, I can’t use those.

My first attempt was to try something like:


bit [31:0] array1[100];
bit [31:0] array2[100];
int length_to_compare = 70; // This needs to be able to change

if (array1[0:length_to_compare] == array2[0:length_to_compare]) // This doesn't work
   // Do Something

But this generates a compilation error.

I next tried to use the streaming operators:


// Compiles but has runtime errors
if( {>>{array1 with [0:length_to_compare]}} == {>>{array1 with [0:length_to_compare]}} ) 
   // Do Something

What is the simplest way to accomplish this task?

In reply to ce_2015:

array1.and() with (item.index >= length_to_compare || item == array2[item.index])

In reply to dave_59:

In reply to ce_2015:

array1.and() with (item.index >= length_to_compare || item == array2[item.index])

Absolute pure genius!

Your solution made me go look up in the LRM that the .and() method can do alternate "AND"ing operation when you use the with clause to do something other than just AND the elements of the same array together.

Thank you Dave!

In reply to dave_59:

I am mildly confused about this part- item.index>=length_to_compare

Does this limit comparison to length_to_compare (starting from 0) or starts comparison from length_to_compare to the end of the array?

The clause under the with condition, as long as it evaluates to TRUE, the elements are ANDED. Is this statement true. If so, I believe, this would convey the intent more clearly -

array1.and() with (item.index<=length_to_compare ? item == array2[item.index] : 1)
module m;
bit [31:0] array1[100];
bit [31:0] array2[100];
int length_to_compare = 70; // This needs to be able to change

initial
  begin
    foreach(array1[i]) begin
      if (i>length_to_compare) begin
        array1[i] = i+4;
        array2[i] = i+3;
      end
      else begin
        array1[i] = i;
        array2[i] = i;
      end
  end
end

  initial begin
    #2;
    $display("%d", array1.and() with (item.index<=length_to_compare ? item == array2[item.index] : 1));
  end
  
endmodule

In reply to kernalmode1:

It ensures that anything above length_to_compare is always true and only compares with the other array elements whose indicies are < length_to_compare.

In reply to kernalmode1:

Never mind. I got what you said. I just thought the different coding style just made the intent clearer for people like me, who are not very advanced users of the language.

In reply to dave_59:

Is there something similar also for packed array with more than one dimension specified on the left side? For example:
bit [2:0][31:0][100:0] array1;
bit [2:0][31:0][100:0] array2;
How can I check if array1[2:0][31:0][length_to_compare:0] == array2[2:0][31:0][length_to_compare:0]?

In reply to davide.cattaneo@nokia.com:

With packed arrays you can create a bit-mask, but you can only select contiguous bits of an array. You’ll have to use a for loop.

mask = (101'b1 << length_to_compare) - 1;
compare = 1;
for(int d1=0;d1<3;d1++) begin : d1_loop
     if (compare == 0 break;
     for(int d2=0;d2<32;d2++) begin : d2_loop
        compare &= (mask & array1[d1][d2]) == (mask & array2[d1][d2]);
        if (compare == 0) break;
     end : d2_loop
end : d1_loop

Hi Dave,

This is not working.
Please let me know if I have missed something.

Thanks,

In reply to UVM_geek:

you have missed declarations of mask and compare, plus a bunch of other typos.