Array.sum method is not working as expected

If Change the size of A I am not getting the expected value can anyone help me why I am getting unexpected values


class C;
  rand bit [7:0] A[] ; //if my bit value is 7:0 is getting expected value
constraint c1 { A.size == 5; }
  constraint c2 { A.sum() with (int'(item)) < 1000; }
endclass

module top;
  C A[10];
  initial
    begin
      foreach(A[i])
        begin
          A[i] = new();
          assert(A[i].randomize());
         
          $display(" %p", A[i].A);
            
        end
      
    end
endmodule

run -all

'{52, 116, 27, 117, 9}

'{234, 128, 109, 243, 251}

'{103, 191, 223, 56, 62}

'{1, 143, 120, 157, 215}

'{130, 119, 51, 54, 248}

'{171, 55, 70, 59, 18}

'{134, 194, 18, 241, 91}

'{94, 212, 232, 79, 11}

'{20, 120, 110, 206, 223}

'{227, 253, 171, 58, 124}

exit


class C;
  rand bit [31:0] A[] ;
constraint c1 { A.size == 5; }
  constraint c2 { A.sum() with (int'(item)) < 1000; }
endclass

module top;
  C A[10];
  initial
    begin
      foreach(A[i])
        begin
          A[i] = new();
          assert(A[i].randomize());
         
          $display(" %p", A[i].A);
            
        end
      
    end
endmodule

run -all

'{3787357203, 205694960, 2308197910, 665118826, 4247110087}

'{972731610, 465127672, 2710588818, 3501954850, 3456330263}

'{3020650510, 1576741973, 3787254651, 372673491, 3891600393}

'{2634639171, 62035855, 3195722274, 123205191, 1369482221}

'{3523790041, 2686765230, 13219328, 2036180016, 3200139311}

'{3393038216, 1966423041, 2557274573, 715508048, 3743281444}

'{3428203505, 3012590047, 417466824, 3765525831, 1945975121}

'{390926889, 2541436557, 4244306482, 2478460877, 1509793635}

'{857388233, 2403345028, 3597653875, 1111139865, 3163724970}

'{2533588731, 1202244656, 764513822, 3665321193, 2591416844}

exit

In reply to marathuteja:

Your constraint does not handle overflow. Try:

  constraint c2 { A.sum() with (64'(item)) < 1000; }

@dave_59
I tried below example but not seeing expected results. Would you help me fix it ?
i expect all items to be within 5 and 10.

class C;
  rand bit [7:0] A[] ; 
constraint c1 { A.size == 5; }
  constraint c2 { A.sum() with (int'(item)) inside {[5:10]}; 
endclass

module top;
  C A[10];
  initial
    begin
      foreach(A[i])
        begin
          A[i] = new();
          assert(A[i].randomize());
         
          $display(" %p", A[i].A);
            
        end
      
    end
endmodule
# run -all
#  '{3, 1, 2, 0, 0}
#  '{4, 2, 0, 3, 1}
#  '{2, 1, 5, 1, 0}
#  '{1, 3, 0, 0, 6}
#  '{4, 0, 0, 1, 1}
#  '{2, 0, 2, 5, 1}
#  '{0, 1, 5, 4, 0}
#  '{2, 2, 0, 4, 2}
#  '{0, 1, 4, 4, 1}
#  '{3, 2, 4, 0, 0}

PS: Sorry I do not the way to format the source code. Im new here, appreciate if you tell me how to format this.

Look at this link “format your code” making your code easier for others to read. I have done that for you.

Your constraint makes the sum of the array A to be inside the range 5 to 10. If you want each element of A to be inside that range, then the constraint would be

foreach (A[i]) A[i] inside {[5:10]};
1 Like