Tuesday, 26 September 2017

Array Manipulation Methods in SystemVerilog with example

SV provides build in methods to facilitate searching from array, array ordering and reduction.

Array locator methods:

Array locator methods operate on any unpacked array, including queues, but their return type is a queue.

Element locator methods (with clause is mandatory):
find() returns all the elements satisfying the given expression.
find_first() returns the first element satisfying the given expression.
find_last() returns the last element satisfying the given expression.

Index locator methods (with clause is mandatory):
find_index() returns the indices of all the elements satisfying the given expression.
find_first_index() returns the index of the first element satisfying the given expression.
find_last_index() returns the index of the last element satisfying the given expression.

Index locator methods return a queue of int for all arrays except associative arrays, which return a queue of the same type as the associative index type.
arrays that specify a wildcard index type shall not be allowed.

If no elements satisfy the given expression or the array is empty (in the case of a queue or dynamic array), then an empty queue is returned.

Index locator methods return a queue with the indices of all items that satisfy the expression.

The optional expression specified by the with clause shall evaluate to a Boolean value.

Let's go through below example,
---------------------------------------------------------------------
---------------------------------------------------------------------

Element locator methods (with clause is optional):
min() returns the element with the minimum value or whose expression evaluates to a minimum.
max() returns the element with the maximum value or whose expression evaluates to a maximum.
unique() returns all elements with unique values or whose expression evaluates to a unique value.
unique_index() returns the indices of all elements with unique values or whose expression evaluates to a unique value.

Let's go through below example,
---------------------------------------------------------------------
---------------------------------------------------------------------

Array reduction methods:


Array reduction methods may be applied to any unpacked array of integral values to reduce the array to a single value

sum() returns the sum of all the array elements or, if a with clause is specified, returns the sum of the values yielded by evaluating the expression for each array element.

product() returns the product of all the array elements or, if a with clause is specified, returns the product of the values yielded by evaluating the expression for each array element.

and() returns the bitwise AND ( & ) of all the array elements or, if a with clause is specified, returns the bitwise AND of the values yielded by evaluating the expression for each array element.

or() returns the bitwise OR ( | ) of all the array elements or, if a with clause is specified, returns the bitwise OR of the values yielded by evaluating the expression for each array element.

xor() returns the bitwise XOR ( ^ ) of all the array elements or, if a with clause is specified, returns the bitwise XOR of the values yielded by evaluating the expression for each array element.

Let's go through below example,
---------------------------------------------------------------------
---------------------------------------------------------------------

Array ordering methods:

Array ordering methods reorder the elements of any unpacked array (fixed or dynamically sized) except for associative arrays.

reverse() reverses the order of the elements in the array. Specifying a with clause shall be a compiler error.

sort() sorts the array in ascending order, optionally using the expression in the with clause.

rsort() sorts the array in descending order, optionally using the expression in the with clause.

shuffle() randomizes the order of the elements in the array. Specifying a with clause shall be a compiler error.

Let's go through below example,
---------------------------------------------------------------------
---------------------------------------------------------------------


Now let's see couple of practical examples, let's say we want to find number of 1s from bit array or bit queue or we want to find out number of non-zero elements in any array or queue.
---------------------------------------------------------------------
---------------------------------------------------------------------

11 comments:

  1. Hello Sagar,

    As a fellow blogger, I can see that you have really good content but it seems that not many readers have access to your posts.

    At chipverify.com, your blog posts will have a much bigger impact on the verification community because of site ranking in terms of daily users, professionals as well as students. Do not forget to check it out !

    Go to www.chipverify.com/blog and www.chipverify.com/connect

    ReplyDelete
  2. I think the method
    q.sort with(item % 2 == 0);
    will sort the values such that first odd and then even but not in the assending order.
    I checked by giving the values q={10,9,8,4,5,6,7};
    it gives the output queue q ={9,5,7,6,8,10,4};
    It just seperates even and odd but it will not sort in assending order

    ReplyDelete
    Replies
    1. module top();
      int unsigned q[$];
      initial begin
      q = {9, 1, 8, 3, 4, 4};
      $display("1. queue 'q' in decimal : %p", q);

      // First ODD and then EVEN in assending order
      q.sort with (item % 2 == 0);
      $display("4. After calling sort() function, queue 'q' in decimal : %p", q);

      // First EVEN and then ODD in assending order
      q.sort with (item % 2 != 0);
      $display("5. After calling sort() function, queue 'q' in decimal : %p", q);
      end
      endmodule : top

      //Output of VCS 2014.10:
      // 1. queue 'q' in decimal : '{'h9, 'h1, 'h8, 'h3, 'h4, 'h4}
      // 4. After calling sort() function, queue 'q' in decimal : '{'h1, 'h3, 'h9, 'h4, 'h4, 'h8}
      // 5. After calling sort() function, queue 'q' in decimal : '{'h4, 'h4, 'h8, 'h3, 'h1, 'h9}

      //Output of Incisive 15.20:
      // 1. queue 'q' in decimal : '{9, 1, 8, 3, 4, 4}
      // 4. After calling sort() function, queue 'q' in decimal : '{9, 1, 3, 8, 4, 4}
      // 5. After calling sort() function, queue 'q' in decimal : '{8, 4, 4, 9, 1, 3}

      Delete
    2. module top();
      int unsigned q[$];
      initial begin
      q = {9, 1, 8, 3, 4, 4};
      $display("1. queue 'q' in decimal : %p", q);

      q.sort(); //CHANGE
      // First ODD and then EVEN in assending order
      q.sort with (item % 2 == 0);
      $display("4. After calling sort() function, queue 'q' in decimal : %p", q);

      // First EVEN and then ODD in assending order
      q.sort with (item % 2 != 0);
      $display("5. After calling sort() function, queue 'q' in decimal : %p", q);
      end
      endmodule : top

      //Output of VCS 2014.10:
      // 1. queue 'q' in decimal : '{'h9, 'h1, 'h8, 'h3, 'h4, 'h4}
      // 4. After calling sort() function, queue 'q' in decimal : '{'h3, 'h9, 'h1, 'h8, 'h4, 'h4}
      // 5. After calling sort() function, queue 'q' in decimal : '{'h4, 'h8, 'h4, 'h9, 'h3, 'h1}

      //Output of Incisive 15.20:
      // 1. queue 'q' in decimal : '{9, 1, 8, 3, 4, 4}
      // 4. After calling sort() function, queue 'q' in decimal : '{1, 3, 9, 4, 4, 8}
      // 5. After calling sort() function, queue 'q' in decimal : '{4, 4, 8, 1, 3, 9}

      Delete
    3. As shown in above 2 examples, both Tool are giving different results.

      Delete
    4. Then it is the problem with the tools?

      Delete
  3. You can still use a brake on a non-machined wheel, but just know that the paint will eventually wear off of the area your brake pads contact.

    machining edmonton

    ReplyDelete
  4. My wife and i have been absolutely joyous when Ervin could round up his preliminary research while using the precious recommendations he had from your very own web page. It is now and again perplexing to just be giving freely tricks which the others could have been making money from. And we all do know we have the writer to thank because of that.milling services edmonton

    ReplyDelete
  5. It is the best time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I desire to read even more things about it! waterjet cutting services

    ReplyDelete
  6. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    Php projects with source code
    Online examination system in php
    Student management system in php
    Php projects for students
    Free source code for academic
    Academic projects provider in nashik
    Academic project free download

    ReplyDelete
  7. How come we can't see how this array methods are implemented?
    I think it's interesting because they are the only example in systemverilog of higher order functions.

    ReplyDelete