Array Reduction method example

Hi,
Have few doubts on how to write constraint using array reduction methods.

Assume the array size is 10.
1.Write a constraint to generate unique values in an array using array reduction method.
tried below two ways, buts seeing constraint solver issue.
i. arr.sum() with(int’(arr[item.index] != arr[item.index-1]));
ii. arr.sum() with(int’(item.index>0 → arr[item.index] != arr[item.index-1]));
2. Out of 10 elements only 3 elements should be same and remaining 7 elements should be unique.

Thanks,

For the first one you can do something like

  1. With array reduction
    rand int val;
    rand int arr[10];
    foreach(arr[i]){
    val!=arr[i]->arr.sum() with (int’(item)==arr[i]) ==1;
    }
  2. For the second one
    arr.sum() with (int’(item)==val)==3;

Hi Kani,
After trying the below code for second one(you suggested), i am seeing a constraint error.

module tb();
class simple;

rand bit[4:0] arr[10];
rand bit[4:0]val;

constraint c_values{
       arr.sum() with(int'(item)==val)==3;
 }

endclass

initial begin
simple s= new();
s.randomize();

end
endmodule

For second one , seeing a compilation error.

looks implication operator does not support.
constraint c_values{
foreach(arr[i]){
val!=arr[i]->arr.sum() with (int’(item)== arr[i])==1;
}

}
Error-[SE] Syntax error
Following verilog source has syntax error :
“testbench.sv”, 12: token is ‘\037777777742’
val!=arr[i]->arr.sum() with
(int\037777777742\037777777600\037777777631(item)== arr[i])==1;
^

Hi Happysri

There is one small syntax error that got missed and the code should be like, the int casting should be for the comparison result not for the item.

  1. With array reduction
    rand int val;
    rand int arr[10];
    foreach(arr[i]){
    val!=arr[i]->arr.sum() with (int’(item==arr[i])) ==1;
    }
  2. For the second one
    arr.sum() with (int’(item==val))==3;

Thanks Kani.
Its working as expected.

class abc;
  rand bit[7:0] arr[];
  rand bit[7:0] val;
  
  constraint arr_c{
    arr.size() == 10;
    val inside {[0:20]};
    foreach(arr[i]){
      arr[i] inside {[0:20]};
      if(arr[i] != val) arr.sum() with (int'(item == arr[i])) == 1;
    }
      arr.sum() with (int'(item == val)) == 3;
  }
  
  function void post_randomize();
                     //foreach(arr[i])
     $display("arr = %p", arr);                  
   endfunction
endclass

module tb;
 initial
    begin
      abc c1;
      c1 = new;
      repeat(10)
      c1.randomize();
    end
 
endmodule