Hi,
Can someone help explain why output is 3 1 for the below usage of unique? how to interpret this arr.unique(x) with (x < 3)?
for x < 4, output is '{4, 1}.
for x > 6, output is '{1, 7}.
I am unable to figure out the pattern.
module tb;
int arr[7] = '{1, 2, 3, 4, 5, 6, 7};
initial begin
$display("%0p", arr.unique(x) with (x < 3));
end
endmodule;
Output: '{3, 1}
I got the code from here: SystemVerilog Array Manipulation
In reply to poojanm2002:
The unique array method returns element not the expression enclosed in with clause.Concider following example
module top();
int q[$];
int result[$];
initial begin
q={1,2,3,4,4,5};
$display("queue 'q' : %p", q);
result.delete();
result = q.unique(item)with(item>3);
$display("using unique: %p", result);
result.delete();
result = q.unique(item)with((item>3)*item);
$display("using unique *item : %p", result);
end
endmodule : top
================
queue ‘q’ : '{1, 2, 3, 4, 4, 5}
using unique: '{1, 4} //**Few simulators treats 1,2,3 as 1’s ,others treat them as 0’s
using unique *item : '{1, 4, 5}
In reply to poojanm2002:
When using a with_expression as part of a unique() method, the with_expression value is used to calculate uniqueness instead of the element’s value. When the with_expression is x<3, the result of that expression is one of two possible values: 1’b0, or 1’b1.
The with_expression for array elements 1 and 2 have the value 1’b1, and the remaining elements have the value 1’b0. The simulator is free to choose 1 or 2 as the unique element for 1’b1, and 3, 4, 5, 6, or 7 as the unique element for representing the value 1’b0.