Is there an alternative to sum() Constraint

Hi all ,

I was recently asked an alternative to $countones( a ) == N ;


  
  rand  bit [9:0] a ;
        bit [3:0] N ; // a can have Max 10 Ones !! 
 
 // constraint  CCOUNTONES {  $countones( a )  == N ;  }  // Need an Alternative for this 
 
 I came up with 2 solutions ::

(1)  $countbits() Function
(2)  Random helper Unpacked array and using sum() . 

Is there any other Solution possible ?

Is there an alternative to sum() Constraint ?


 
rand bit [3:0] A [ 4 ] ;

// constraint SUM { A.sum() == 10 ; }   // Any alternative to this ?


In reply to Have_A_Doubt:

$countones, $onehot, and $isunknown are just shortcuts for $countbits usage.

You could use a foreach loop with a helper unpacked array that is a cascading sum of the previous iteration. I’ll leave that as an exercise for you.

In reply to dave_59:

Hi Dave ,

Thanks for the suggestion .

Here is my code which works


class Sum_Alt ;

        int N ; // Size of Unpacked Arrays !!
        
	int M ; // Sum() of elements !!

   rand bit [3:0] A[] ;  // Need an  alternative to A.sum() !!     

   rand bit [9:0] A_helper[$] ; // Helper Array !!

   constraint SIZE  { A.size() == A_helper.size() ;  A.size() == N ; }
    
   constraint SUM_ALT { 

                            foreach(A_helper[i])
			   {
                              if ( i == 1 )  // When ( i != 0 )
                             {
                                A_helper[i] == A[i] + A[i-1] ;
			     }
			     else if ( i > 1 ) 
                             {
                                A_helper[i] == A[i] + A_helper[i-1] ;
			     }
			     else
                             {
                                A_helper[i] == A[i] ;
			     }
                           }
                       }

     constraint  SUMM  {  A_helper[ A_helper.size() - 1 ]  == M ; }   

  endclass

  Sum_Alt  obj ;

  initial  begin

     obj = new() ;
     obj.N = 4 ; 
     obj.M = 15 ; 

    if ( obj.randomize() )
     begin
       $display("Success with %p",obj);
     end
   else
     $display("Fails"); 

  end


Is there a better way to achieve the Same ?

Yea, convince your instructor that $countones is the best way.

Hi Dave,

I am trying to use helper int variable rather than array for cascading sum inside constraint(while simulation it troughing an error) could you please tell me what’s wrong in this approach?

class MyClass;

rand unsigned int current_sum;

rand int arr[50];

constraint sum_constraint {
foreach (arr[i]) {

      arr[i]  inside {[0:100]};            
     current_sum += arr[i];        
  }      
  current_sum == 100;    
}

endclass

@zalak_patel A few things to note

(1) Have you really tested your code ?

I observe 2 compilation issues whereas randomization failure is observed at run-time

a) rand unsigned int current_sum; // Should be rand int unsigned current_sum;

b) Operator += is illegal in constraint as it has assignment operator '=' in it's equivalent expression
   Constraints are essentially expressions and not assignment statements

(2) Constraints are not procedural in nature. They are solved in parallel.

Here is a small homework for you

class Class;

rand int unsigned current_sum;

rand int arr[50];

constraint sum_constraint {
   foreach (arr[i]) 
   {
      arr[i]  inside {[0:100]};            
     current_sum == current_sum + arr[i]; // Constraints are expressions       
   }      
     //current_sum == 100; // Commented out the conflict    
}
endclass

Observe the output and try to understand the reason behind it.