Hi, in below code, 48’ is used for overflow protection but I dont understand why the casting with 48 and not 32. So in questa sim, “array.sum() == 10;” works fine without casting i.e. values of the array are chosen within 0 to 10 but in VCS simulator, it chooses really high value without casting, even though array is unsigned int. So it needs the casting. And the casting works only with 48. I know this is a tool related question but I would like to see if someone can explain why 48’ works.
class arr;
rand int unsigned array[];
constraint array_sum_c {
array.size() == 10;
array.sum() with (48'(item)) == 10;
}
endclass
module top;
arr a;
initial begin
a = new();
a.randomize();
$display("%0p", a.array);
end
endmodule
If you put the randomization in a repeat loop, you will see that all tools produce some results that overflow.
If the largest element of your array is 32’hFFFF_FFFF and there are 10 elements, you need to make sure that (32’hFFFF_FFFF * 4’hA) does not overflow. That means you need to sum the elements using a width of (32+4) = 36 bits or prevent overflow.
In reply to kuki2002:
why the casting with 48 and not 32.
sum the elements using a width of (32+4) = 36 bits
Running the code, the result shows any number over 36 (upto 64) chosen for casting returns the correct sum of 10 (values chosen between 0-10).
If the largest element of your array is 32’hFFFF_FFFF and there are 10 elements, you need to make sure that (32’hFFFF_FFFF * 4’hA) does not overflow.
Could you please elaborate on this. I cannot seem to understand why should this be needed.
By this: " array.sum() == 10;", we are instructing the solver to sum all elements and constraint it to 10 - correct?
Why is this additional logic - where 32’hFFFF_FFFF*4’hA need to be considered?
The question asks why a casting width of 48 works and not 32. The answer is that any width greater than 35 works for the example shown. Why 48 was chosen does not really matter.
Thank you, Dave.
So, are you saying the default size of “10” in this constraint
(array.sum() with (48'(item)) == **10**;)
is 32 bits.
Hence the sum of 'h13 + (9 * 'hFFFF_FFFF)when exceeds 32 bits, it gets truncated to lower 32 bits that is == 10.
Code I ran:
class arr;
rand int unsigned array[];
constraint array_sum_c {
array.size() == 2;
//array.sum() == 10; //with (48'(item)) == 10;
array.sum() with (4'(item)) == 'd10;
}
endclass
module top;
arr a;
initial begin
a = new();
repeat(5) begin
void'(a.randomize());
$display("%p", a.array);
$display("sum = %0x", a.array.sum());
end
end
endmodule
I have rerun the original program in this thread constraining the array size to 2 and qualifying size of array item with 'd4. I get the following result:
'{556349363, 4253430535}
sum = 1eaf6aba
'{540725342, 2194362796}
sum = a306220a
'{936386833, 1457103433}
sum = 8ea9c35a
'{1157917028, 2189035990}
sum = c77e733a
'{1767679275, 2519606847}
sum = ff8acb6a
In this case, the lower 32 bits of sum is not 10.
How does the constraint solver assumes the constraint “==10” is met?