Signed Arithmetic in Constraints

In reply to dave_59:

Hi Dave ,

[2] Using %0d instead of %p gives me Same results ::


      class SIGN_ADD ;
 
 typedef bit signed [2:0] bitsign ;
 
 rand bitsign  a , b , c , d ;
 rand bit signed [3:0] sum ;
 
 constraint SIGN_ADDITION { sum == ( c < d ) + a + b ; }  // c and d Unconstrained , right ??
 
  function void post_randomize();
    $display("a is %0d , b is %0d",a,b);
    $display("c is %0d , d is %0d",c,d); 
  endfunction

endclass 
 
SIGN_ADD obj ;
 
initial begin
 
   obj = new() ;
 
  if ( obj.randomize() )
    $display("Success with %p",obj); 
  else
    $display("Fails");
end
     

I see Similar Output ::

a is 7 , b is 7
c is 6 , d is 5
Success with {a:'h7, b:'h7, c:'h6, d:'h5, sum:-2} // [X]
a is 6 , b is 3
c is 0 , d is 0
Success with {a:'h6, b:'h3, c:'h0, d:'h0, sum:-7} // [Y]

If I were to do binary Addition ::
With , a + b + ( c < d ) is 4’b1110 i.e -2 since MSb is 1
With [Y] , a + b + ( c < d ) is 4’b1001 i.e -7 since MSb is 1

Since the 1st step would be to have Operands Sized to 4-bits ,
maybe a , b , c , d are 0-Padded instead os Sign Extension ??
[ 0-Padded since result of Relational Operator makes the expression Unsigned !! ]

[3] With in-line Constraint I have following Code for which I observe Failure !!


      class SIGN_ADD ;
 
 typedef bit signed [2:0] bitsign ;
 
 rand bitsign  a , b , c , d ;
 rand bit signed [3:0] sum ;
 
 constraint SIGN_ADDITION { sum == ( c < d ) + a + b ; }  // c and d Unconstrained , right ??
 
  function void post_randomize();
    $display("a is %0d , b is %0d",a,b);
    $display("c is %0d , d is %0d",c,d); 
  endfunction

endclass 
 
SIGN_ADD obj ;
 
initial begin
 
   obj = new() ;
 
  if ( obj.randomize() with { a < 0 ; b < 0 ; } ) // Constraint Failure !!
    $display("Success with %p",obj); 
  else
    $display("Fails");
end
     

This Failure I believe is due to the Expression being treated as Unsigned ( again due to result of relational Operator being Unsigned )

LRM 11.8.1 :: Comparison and reduction operator results are unsigned, regardless of the operands.