[SV]SystemVerilog数组缩减法(Array Reduction methods)

                                   Array Reduction methods

       SystemVerilog Array Reduction methods operate on an unpacked array to reduce the array to a single value.built-in array reduction methods are,

  1. sum()            returns the sum of all the array elements
  2. product()       returns the product of all the array elements
  3. and()             returns the bit-wise AND ( & ) of all the array elements
  4. or()                returns the bit-wise OR ( | ) of all the array elements
  5. xor()              returns the logical XOR ( ^ ) of all the array elements

       ‘with’ clause is allowed for sort and rsort methods.About ‘with’:expression specified in “with” clause will be evaluated for each array element and performs the operation on an array.

一、Array reduction methods SUM and PRODUCT

       On calling sum() method sum of array_1 elements (1,2,3,4) will be returned to variable t_sum.
       On calling product() method product of array_1 elements (1,2,3,4) will be returned to variable t_product.

module fixedsize_array;
   
  //declaration of array’s
  int array_1[4];
  
  int t_sum,t_product;
   
  initial begin
    //array initialization
    array_1  = '{1,2,3,4};
 
    t_sum = array_1.sum(); //t_sum = 1+2+3+4
    $display("Sum of array_1 is \t%0d",t_sum);
 
    t_product = array_1.product(); //t_product = 1*2*3*4
    $display("product of array_1 is \t%0d",t_product);
  end
   
endmodule

     Simulator Output 

     Sum of array_1 is 10
     product of array_1 is 24

二、Array reduction method AND

       On calling and() method, bit-wise and (&) will be performed on all the array elements and returned.Let’s consider an example of an array with 2, 3 and 4 elements.

module fixedsize_array;
   
  //declaration of array’s
  int array_1[2];
  int array_2[2];
  int array_3[3];
  int array_4[4];
   
  int b_and;
   
  initial begin
    //array initialization
    array_1  = '{2,3};
    array_2  = '{2,1};
    array_3  = '{10,9,8};
    array_4  = '{3,5,7,9};
     
    b_and = array_1.and();
    $display("Bit-wise AND of array_1 is \t%0d",b_and);
     
    b_and = array_2.and();
    $display("Bit-wise AND of array_2 is \t%0d",b_and);
     
    b_and = array_3.and();
    $display("Bit-wise AND of array_3 is \t%0d",b_and);   
 
    b_and = array_4.and();
    $display("Bit-wise AND of array_4 is \t%0d",b_and);   
  end
endmodule

       Simulator Output 

       Bit-wise AND of array_1 is 2
       Bit-wise AND of array_2 is 0
       Bit-wise AND of array_3 is 8
       Bit-wise AND of array_4 is 1

扫描二维码关注公众号,回复: 10182402 查看本文章

       Execute the above code on 

三、Array reduction method OR

       On calling or() method, bit-wise or (|) will be performed on all the array elements and returned.Let’s consider an example of an array with 2, 3 and 4 elements.

module fixedsize_array;
   
  //declaration of array’s
  int array_1[2];
  int array_2[2];
  int array_3[3];
  int array_4[4];
   
  int b_or;
   
  initial begin
    //array initialization
    array_1  = '{2,3};
    array_2  = '{2,1};
    array_3  = '{10,9,8};
    array_4  = '{3,5,7,9};
     
    b_or = array_1.or();
    $display("bit-wise OR of array_1 is \t%0d",b_or);
     
    b_or = array_2.or();
    $display("bit-wise OR of array_2 is \t%0d",b_or);
     
    b_or = array_3.or();
    $display("bit-wise OR of array_3 is \t%0d",b_or);   
 
    b_or = array_4.or();
    $display("bit-wise OR of array_4 is \t%0d",b_or);   
  end
   
endmodule

       Simulator Output 

       bit-wise OR of array_1 is 3
       bit-wise OR of array_2 is 3
       bit-wise OR of array_3 is 11
       bit-wise OR of array_4 is 15

       Execute the above code on 

发布了140 篇原创文章 · 获赞 81 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/gsjthxy/article/details/105079848
今日推荐