Difference in overflow while using byte and int

PROGRAM :–1

class array;
rand byte unsigned arr[];
constraint c_arr{
   arr.size() == 5;
   arr.sum() == 60; //inside {[1:1000]}; // if you want a positive result
   //foreach(arr[i]) arr[i] inside {[0:500]};
}
endclass

module tb;
array a=new();
int sum;
initial
repeat(10)
begin
a.randomize();
sum=0;
foreach(a.arr[i])
sum=sum+a.arr[i];
$display("sum =%0d elements of the array to sum up to <1000 ---->>  %0p",sum,a.arr);
end
endmodule

RESULT :–

sum =60 elements of the array to sum up to <1000 ---->> '{'h28, 'hb, 'h9, 'h0, 'h0}
sum =60 elements of the array to sum up to <1000 ---->> '{'h2, 'h0, 'h7, 'h0, 'h33}
sum =60 elements of the array to sum up to <1000 ---->> '{'h4, 'h0, 'h0, 'h35, 'h3}
sum =60 elements of the array to sum up to <1000 ---->> '{'hb, 'h10, 'h19, 'h8, 'h0}
sum =60 elements of the array to sum up to <1000 ---->> '{'h0, 'h37, 'h5, 'h0, 'h0}
sum =60 elements of the array to sum up to <1000 ---->> '{'h3, 'h0, 'h0, 'h38, 'h1}
sum =60 elements of the array to sum up to <1000 ---->> '{'h0, 'h5, 'h15, 'ha, 'h18}
sum =60 elements of the array to sum up to <1000 ---->> '{'ha, 'h0, 'h0, 'h0, 'h32}
sum =60 elements of the array to sum up to <1000 ---->> '{'h1a, 'h5, 'h2, 'h1b, 'h0}
sum =60 elements of the array to sum up to <1000 ---->> '{'h10, 'h1b, 'h11, 'h0, 'h0}

PROGRAM :–2

class array;
rand int unsigned arr[];
constraint c_arr{
   arr.size() == 5;
   arr.sum() == 60; //inside {[1:1000]}; // if you want a positive result
   //foreach(arr[i]) arr[i] inside {[0:500]};
}
endclass

module tb;
array a=new();
int sum;
initial
repeat(10)
begin
a.randomize();
sum=0;
foreach(a.arr[i])
sum=sum+a.arr[i];
$display("sum =%0d elements of the array to sum up to <1000 ---->>  %0p",sum,a.arr);
end
endmodule

RESULT:–

sum =60 elements of the array to sum up to <1000 ---->> '{'h4958832, 'h8ae8ecd3, 'hc4b9c064, 'h988848e0, 'h133f81f3}
sum =60 elements of the array to sum up to <1000 ---->> '{'h8e4ecd87, 'h4998543e, 'hb87f7c8b, 'h14cfff46, 'h5ac962a6}
sum =60 elements of the array to sum up to <1000 ---->> '{'he004869d, 'h390dda9c, 'h3670936e, 'hfbdf72e4, 'hb49d98b1}
sum =60 elements of the array to sum up to <1000 ---->> '{'h17d4fe53, 'h36b21455, 'hba7f13cd, 'h1d043a6a, 'hd9f59f5d}
sum =60 elements of the array to sum up to <1000 ---->> '{'h10d61bea, 'hd66d3faf, 'h23fe333c, 'h1f2447d9, 'hd59a298e}
sum =60 elements of the array to sum up to <1000 ---->> '{'h18028825, 'h8fb317cd, 'h7f98477f, 'hb7b3d42b, 'h20fe44a0}
sum =60 elements of the array to sum up to <1000 ---->> '{'hb6e94ce8, 'h3190c552, 'h750017d2, 'h59342392, 'h4951b29e}
sum =60 elements of the array to sum up to <1000 ---->> '{'hf0364918, 'hcd2504ea, 'hb7e8329a, 'h443f065b, 'h467d7945}
sum =60 elements of the array to sum up to <1000 ---->> '{'h8f09a442, 'h94eb9b2, 'h93c8b596, 'heff84f0e, 'he3e69da4}
sum =60 elements of the array to sum up to <1000 ---->> '{'h54d814c5, 'hb729f759, 'ha2c604ba, 'h52b1c34b, 'hfe862c19}

PROGRAM :–3

class array;
rand int unsigned arr[];
constraint c_arr{
   arr.size() == 5;
   arr.sum() < 60; //inside {[1:1000]}; // if you want a positive result
   //foreach(arr[i]) arr[i] inside {[0:500]};
}
endclass

module tb;
array a=new();
int sum;
initial
repeat(10)
begin
a.randomize();
sum=0;
foreach(a.arr[i])
sum=sum+a.arr[i];
$display("sum =%0d elements of the array to sum up to <1000 ---->>  %0p",sum,a.arr);
end
endmodule

RESULT:–
sum =54 elements of the array to sum up to <1000 ---->> '{'h0, 'h0, 'h0, 'h0, 'h36}
sum =59 elements of the array to sum up to <1000 ---->> '{'h0, 'h2b, 'h10, 'h0, 'h0}
sum =35 elements of the array to sum up to <1000 ---->> '{'h0, 'h0, 'h23, 'h0, 'h0}
sum =42 elements of the array to sum up to <1000 ---->> '{'h4, 'h26, 'h0, 'h0, 'h0}
sum =43 elements of the array to sum up to <1000 ---->> '{'h0, 'h2b, 'h0, 'h0, 'h0}
sum =38 elements of the array to sum up to <1000 ---->> '{'h0, 'h0, 'h0, 'h24, 'h2}
sum =38 elements of the array to sum up to <1000 ---->> '{'h19, 'h0, 'h7, 'h0, 'h6}
sum =18 elements of the array to sum up to <1000 ---->> '{'h8, 'h0, 'h0, 'h0, 'ha}
sum =34 elements of the array to sum up to <1000 ---->> '{'h13, 'h0, 'he, 'h1, 'h0}
sum =32 elements of the array to sum up to <1000 ---->> '{'h0, 'h0, 'h16, 'h0, 'ha}

Case 1:
For int : arr.sum() == 60; -->No overflow values are printed.
For int : arr.sum() < 60; -->Overflow values are printed.
Case 2:
Was expecting overflow for byte datatype also.

In reply to Chandra Shekar N:

Yes, Overflow is expected in byte case too. you may notice it if you run more simulation (run 100 instead of 10).

In reply to Rahulkumar:

Thank you for the reply, Rahul. I had tried it for even 100000, in int I was able to observe whereas for byte there is no overflow.

The attached file includes the byte file with 100000 simulation.

https://drive.google.com/open?id=1YpF0mlSqt4mCROPiIxxarDzocMZ6vSOp

In reply to Chandra Shekar N:




class array;
rand byte unsigned arr[];
constraint c_arr{
   arr.size() == 5;
   arr.sum() == 60; //inside {[1:1000]}; // if you want a positive result
   //foreach(arr[i]) arr[i] inside {[0:500]};
}
endclass
 
module tb;
array a=new();
int sum;
initial
repeat(10)
begin
a.randomize();
sum=0;
foreach(a.arr[i])
sum=sum+a.arr[i];
$display("sum =%0d elements of the array to sum up to <1000 ---->>  %0p",sum,a.arr);
end
endmodule


///OUTPUT
# KERNEL: sum =572 elements of the array to sum up to <1000 ---->>  '{237, 35, 97, 196, 7}
# KERNEL: sum =572 elements of the array to sum up to <1000 ---->>  '{115, 149, 160, 144, 4}
# KERNEL: sum =572 elements of the array to sum up to <1000 ---->>  '{141, 250, 16, 155, 10}
# KERNEL: sum =828 elements of the array to sum up to <1000 ---->>  '{129, 245, 119, 113, 222}
# KERNEL: sum =572 elements of the array to sum up to <1000 ---->>  '{182, 74, 28, 157, 131}
# KERNEL: sum =316 elements of the array to sum up to <1000 ---->>  '{93, 134, 30, 14, 45}
# KERNEL: sum =828 elements of the array to sum up to <1000 ---->>  '{213, 183, 237, 10, 185}
# KERNEL: sum =828 elements of the array to sum up to <1000 ---->>  '{239, 73, 114, 213, 189}
# KERNEL: sum =828 elements of the array to sum up to <1000 ---->>  '{18, 111, 224, 254, 221}
# KERNEL: sum =572 elements of the array to sum up to <1000 ---->>  '{142, 147, 67, 166, 50}


In reply to Chandra Shekar N:
I think you have a problem with your tool, it’s not giving all possible values that would make the constraint evaluate true.

Thanks Rahul and Dave for the replies, and for solving the issue. Yes it is problem with tool while evaluating with constraints.

Regards
Chandra Shekar N