System Verilog Constraint 3D array sum of the elements

HI All,

Sorry, I tried to use “code formatting”, but somehow I could not usse it, hence, I am just copy pasting as it is

To Generate a 3D vector [3][3][3] with values between -10 and 10.
array sum should be greater than 0.

.sum() method is not working here, could you please advice?

module p1;
  class c1;
    rand int addr[3][3][3];
   constraint c1{
     foreach (addr[a]){
       foreach (addr[b]){
         foreach (addr[c]){
           addr[a][b][c]inside {[-10:10]};
     }
       }
       }
           addr.sum()>0;
           //addr.sum() with (int'(item))>0;
       }
    function void post_randomize();
     $display("elements %0p", addr);
    endfunction
  endclass
  c1 obj;
  initial begin
    obj = new;
    repeat (1)
    obj.randomize();
  end
endmodule

Output:

xmvlog: *E,WAATYP (testbench.sv,12|18): This type is illegal in this context [SystemVerilog].


           addr.sum()>0;

The array reduction .sum() method is defined to reduce a single dimension of an array. For 3 dimensions, you need to nest the methods

foreach (addr[a,b,c]) addr[a][b][c]inside {[-10:10];
addr.sum(D1) with ( D1.sum(D2) with ( D2.sum(D3) ) ) > 0;
module p1;
  class c1;
    rand int addr[3][3][3];
   constraint c1{
     foreach (addr[a,b,c])
       addr[a][b][c]inside {[-10:10]};
	   addr.sum(D1) with ( D1.sum(D2) with ( D2.sum(D3) ) ) > 0;
       }
    function void post_randomize();
     $display("elements %0p", addr);
    endfunction
  endclass
  c1 obj;
  initial begin
    obj = new;
    repeat (6)
    obj.randomize();
  end
endmodule

This code is giving an error “D3 undeclared variable”.
Can you please hlep me to resolve?

You have run into a tool specific issue that doesn’t allow iterator declaration without the with () clause. You can simply remove it.

@Thirumalesh ,

You can use default iterator argument (item) instead like this:

module p1;
  class c1;
     rand int addr[3][3][3];
     
     constraint addr_sum {
        foreach(addr[a, b, c]) addr[a][b][c] inside {[-10:10]};
        addr.sum() with(item.sum() with(item.sum())) > 0;
     }
  
     function void post_randomize();
       $display("elements %0p", addr);
     endfunction
  endclass

  c1 obj;
  initial begin
    obj = new;
    repeat (6) obj.randomize();
  end
  
endmodule

OUTPUT:

# elements '{'{'{6, 7, 7}, '{-5, -4, 4}, '{-6, 5, -3}}, '{'{-2, 0, 5}, '{1, 8, -5}, '{-8, -3, -7}}, '{'{9, -7, -8}, '{-9, 6, 6}, '{1, 4, 8}}}
# elements '{'{'{-5, -2, -2}, '{4, 8, -8}, '{2, 4, 4}}, '{'{-4, 1, -1}, '{8, 5, -10}, '{-7, 7, 5}}, '{'{-5, -9, -9}, '{10, -10, -1}, '{9, 10, 8}}}
# elements '{'{'{-9, -7, -7}, '{6, -9, 7}, '{3, 9, -10}}, '{'{-6, 7, 10}, '{5, 5, 7}, '{-7, 3, -10}}, '{'{8, 4, -6}, '{-4, -10, 1}, '{10, -1, 5}}}
# elements '{'{'{2, 5, -8}, '{0, -8, 2}, '{10, 4, 9}}, '{'{-7, 2, -1}, '{-3, 6, -5}, '{-7, 2, -3}}, '{'{8, 1, -3}, '{-3, 6, -1}, '{0, 1, 1}}}
# elements '{'{'{-1, -1, 2}, '{10, -8, 7}, '{-10, -3, -5}}, '{'{7, -2, 2}, '{-7, -4, 10}, '{-3, -2, 6}}, '{'{-9, 10, 7}, '{6, -1, 4}, '{-3, -4, -3}}}
# elements '{'{'{7, -5, -3}, '{5, 4, 3}, '{-10, 8, -4}}, '{'{5, 8, 1}, '{-8, -6, 0}, '{0, 9, 4}}, '{'{7, -6, -6}, '{-2, 7, 4}, '{-3, 7, -6}}}

Thanks,
Naveen Kadian