In reply to dave_59:
In reply to shahkavish77:
All you have to do is unroll the sum method
( int'(arr[0] == i) + int'(arr[1] == i) + ... + int'(arr[99] == i) ) ==j;
( int'(arr[0]) + int'(arr[1]) + ... + int'(arr[99]) ) ==j;
why do we always cast the item to be an int when item refers to the actual array element. In the above example discussed in the thread the array element is an 8bit type which is good to hold values from 0-255.
Are we doing this to hold the sum of all the elements which if done may exceed an 8bit value and we are casting an int only to cover all possible scenarios? Even if this is the case I have seen wrong results when we don’t cast to an int type for item even if dealing with a sum < 255.
Please clarify as it has been very hard to find good info on the same. Thanks again as always. Here is another example which fails without a cast to int
class packet;
rand bit[3:0] add[$];
//constraint address_range { foreach(add[i])add[i] inside {[10:20]};}
constraint s {
add.size inside {[6:8]};
//(add.sum() with (int'(item))) ==7;//works
(add.sum() with (item)) ==7;//fails
}
endclass
module constr_inside;
initial begin
packet pkt;
pkt = new();
$display("------------------------------------");
repeat(1) begin
pkt.randomize();
$display("size = %0d",pkt.add.size);
foreach(pkt.add[i])$display("element i value = %0d",pkt.add[i]);
end
$display("------------------------------------");
end
endmodule