Problem array

Hi all,
logic[7:0] m[3][2] = ‘{’{1,2},‘{3,4},’{9,8}};
when i display m.sum with(item > 4), i supposed the ans is 2. but the simulator show me 0
why is that?
Thanks!

In reply to peter:

You have two problems. The array reduction methods like sum() all work on one dimension of an array. You need to sum() the sum() of each dimension.

The other problem is the result of a relational operator is a single bit, true or false. You need to cast the result to a larger width.

m.sum(d1) with (d1.sum(d2) with (int'(d2>4))

In reply to dave_59:
sum() works on one dimension of an array, so which dimension of mm that sum()works on?
how can i get d1 and d2 of mm?
Thanks a lot!

In reply to peter:

m.sum(d1) iterates over the fist dimension [3]. Then each d1.sum(d2) iterates over the second dimension [2]. d2 iterates over each logic [7:0] element.

In reply to dave_59:
i tried to run with your answer, simulator show me syntax error
in the sv spec, it seems that there is no input arg for sum() function. I just find the usage of array.sum()
thanks a lot!

In reply to peter:


module test;
  logic[7:0] m[3][2] = '{'{1,2},'{3,4},'{9,8}};
  int k;
  
initial begin
  k = m.sum(item1) with (item1.sum(item2) with (int'(item2>4)));
  $display ("K Value is %d",k);
end
  
endmodule

In reply to rag123:

i would like to know where the sum function define in the spec?
i can not find the input arg of the sum() in the spec
thanks!

In reply to dave_59:

Hi Dave,
I used this statement (m.sum with(item > 4 ? 1 : 0) ), but the simulator show correct ans(2).
Do you agree that my method should not work?
not sure whether there is problem with my simulator.
Thanks a lot!!

In reply to peter:
Section 7.12 Array manipulation methods defines all methods (which includes sum()) to accept an optional iterator_argument variable name. If you do not declare one, its default name becomes item. The LRM has the following example

logic [7:0] m [2][2] = '{ '{5, 10}, '{15, 20} };
int y;
y = m.sum with (item.sum with (item)); // y becomes 50 => 5+10+15+20

The first item variable is iterating of the first dimension of the array, and the second item a nested iteration of the second dimension. The example could also be written with explicitly declared iterator names

y = m.sum(item1) with (item1.sum(item2) with (item2)); // y becomes 50 => 5+10+15+20

The above has the benefit of not hiding the first iterator variable from the second nested iteration. Useful in more complex constraint equations.

All SystemVerilog simulators on EDAPlayground produce an error for this code

module top;
  logic[7:0] m[3][2] = '{'{1,2},'{3,4},'{9,8}};
  initial $display(m.sum with(item > 4 ? 1 : 0));
endmodule

The error is because item is this code iterates 3 times as a 1-dimensional array with a size of 2. You cannot use a relational operator > on an array.

In reply to dave_59:

Hi Dave,
Thanks for reply. I want to add first dimensional (5+10),using m.sum(item1) with (item1) does not work. what is the problem?
Thanks!!

In reply to peter:

m[0].sum;