Constraint violation by tool for array size 4

I have following Code which works fine for array arr sizes other than 4.
Can someone let me know why Questa,Cadence and VCS is failing if I fix size to 4

// Code your testbench here
// or browse Examples
// Code your testbench here
// or browse Examples

  class a;
    rand int unsigned arr[4] ;//= {4,3,8,1};
    constraint myc {arr.sum == 10;}
  
  endclass
program test;

  
  a b;
  initial
  begin 
  int j;
    b = new;
  
    if(!b.randomize())$display("FAIL");
    foreach(b.arr[i])
      $display("Arr[i] = %d ",b.arr[i]);
    
    j=b.arr.sum();
    $display("\nsum %d\n",j);
    
    
  end
  
endprogram

Output is as below:

vsim -voptargs=+acc=npr

run -all

Arr[i] = 3930562772

Arr[i] = 2632216193

Arr[i] = 521137076

Arr[i] = 1506018561

sum 10

In reply to tro:

Why do you say it is failing?? The output seems to be proper … when you add all those unsigned integers you will get back 10 because arr.sum can hold only as much as unsigned integer and when such large numbers are added you will go over the maximum and roll back to 10.

Results are interesting.From edaplayground we I was using Mentor for array_size of 3, it was showing correct but for synopsys vcs it is showing as below:

class a;
  rand int unsigned arr[3] ;//= {4,3,8,1};
    constraint myc {arr.sum == 10;}
 
  endclass
program test;
 
 
  a b;
  initial
  begin 
  int j;
    b = new;
 
    assert(!b.randomize()); //$display("FAIL");
    foreach(b.arr[i])
      $display("Arr[i] = %d ",b.arr[i]);
 
    j=b.arr.sum();
    $display("\nsum %d\n",j);
 
 
  end
 
endprogram

I used above code.

**Chronologic VCS simulator copyright 1991-2019
Contains Synopsys proprietary information.
Compiler version P-2019.06-1; Runtime version P-2019.06-1; Jun 23 09:31 2020
“testbench.sv”, 15: test.unnamed$$_0.unnamed$$_1: started at 0ns failed at 0ns
Offending ‘(!b.randomize())’
Arr[i] = 3423460335
Arr[i] = 1686592622
Arr[i] = 3479881645

sum 10

$finish at simulation time 0
**
as you can see here, assertion is failing. that means randomization is failing,but I don’t know exact reason for that.

In reply to juhi_p:

It fails because your assertion statement is wrong. The randomize() function returns a 1 upon success, and a 0 upon failure, hence your assert() failure. Also, you should never use an assert statement with a task/function call because if you disable assertions to speed up simulation, these functions will never be called.

You want to check the return code from the randomize() call with an if statement, as was originally done:


if (!b.randomize()) begin
  $fatal("Randomize() call failed!");
end
else begin
  $display("Randomize() call was successful");
end

In reply to tro:

try

constraint myc {arr.sum(item) with(34’(item)) == 10;}

padded 2 bits to make sure there is no overflow…

edit: originally suggested int’(item), that was wrong…padding few bits is right