I tried to summaries array values with an help array of indexes.
my purpose is to summarize vals[ on active indexes[i] only]
rand int vals []; rand int indexes [];
int sum_vals = 0;
I have some constraint that generate the 2 queues with below values:
vals[0] = 8;
vals[1] = 64;
vals[2] = 16;
vals[3] = 8;
vals[4] = 8;
vals[5] = 8;
vals[6] = 8;
vals[7] = 8;
indexes[0] = 4;
indexes[1] = 0;
indexes[2] = 2;
I tried to sum it :
sum_vals = vals[indexes[0]] + vals[indexes[1]] + vals[indexes[2]];
this is my implementation :
constraint list_c {
foreach(indexes[i]){
indexes[i] inside {[0:7]};
sum_total_vals(vals[indexes[i]]) == 1;
}
}
function int sum_total_vals(int curr_val);
sum_vals += curr_val;
return 1;
endfunction
This is working for me but :
- I think I did it little complex, I will be happy to know if there is a finest way. maybe with “sum with” ( I tried, but didn’t success it. )
- in addition I get a warning :
Function sum_total_vals used in constraint is rosky and has write access to non-local/ persistent variable.