Sum() with in constraints

Hi I have the code below, I have got what I wanted which is the sum of all the len fields to be 5.
But that made me wonder, based on my code shouldn’t the .sum() be constrained to 5 instead of item.len being 5.
Code:

class enum_check;
  
      typedef struct packed {  
      bit[7:0] strm_num;     
      bit[7:0] spkr_num;     
      bit[7:0] len;     
      bit[7:0] rec_type;
  } spkr_rec_hdr;
 

  rand spkr_rec_hdr spkr_hdr[$];
  
  constraint c_len{
    spkr_hdr.size()==3;
    spkr_hdr.sum() with (int'(item.len)) == 5;
  }
  
  
endclass

output:
‘{’{strm_num:'h52, spkr_num:'h3b, lent:'h0, rec_type:'hdc}, '{strm_num:'h4e, spkr_num:'hf0, lent:'h3, rec_type:'h5f}, '{strm_num:'ha7, spkr_num:'hbc, lent:'h2, rec_type:'hc}}

In reply to CRVAddict:

Your sum constraint gets expanded to

{ ( int'(spkr_hdr[0].len) + int'(spkr_hdr[1].len) + int'(spkr_hdr[2].len) ) == 5; }

Not sure what you were expecting to be different. (Note that the output does not exactly match what you declared: len vs. lent)

In reply to CRVAddict:

module automatic test;
    typedef struct packed {
        bit [7:0] stream_num;
        bit [7:0] speaker_num;
        bit [7:0] length;
        bit [7:0] record_type;
    } speaker_record_hdr;
    class my_class;
        rand speaker_record_hdr my_hdr[$];
        
        
        constraint my_hdr_c {
            //1. Make sure that length field of the header is always is always 5 bit long
            my_hdr.size() == 10;
            //my_hdr.sum() with (int'(item.length)) == 5;
            //2. Make sure that length field of the header is always 5.
            foreach(my_hdr[i]) {
                my_hdr[i].length == 5;
            }
        }
    endclass : my_class

    initial begin
        my_class c;
        c = new();
        c.randomize();
        $display("The value of hdr is %p", c.my_hdr);
    end
endmodule : test

The output when the length is 5 bits long:
The value of hdr is ‘{’{stream_num:'h12, speaker_num:'h2c, length:'h1, record_type:'ha1}, '{stream_num:'he2, speaker_num:'h10, length:'h3, record_type:'h2e}, '{stream_num:'hcd, speaker_num:'h9, length:'h1, record_type:'hdf}}

The Output when the length itself is 5.
The value of hdr is ‘{’{stream_num:'h59, speaker_num:'h42, length:'h5, record_type:'hd8}, '{stream_num:'h6a, speaker_num:'hd5, length:'h5, record_type:'h85}, '{stream_num:'hcb, speaker_num:'hd3, length:'h5, record_type:'h82}, '{stream_num:'h45, speaker_num:'h48, length:'h5, record_type:'h95}, '{stream_num:'hfe, speaker_num:'h41, length:'h5, record_type:'haf}, '{stream_num:'hca, speaker_num:'hba, length:'h5, record_type:'h89}, '{stream_num:'h89, speaker_num:'heb, length:'h5, record_type:'h84}, '{stream_num:'h92, speaker_num:'h28, length:'h5, record_type:'h83}, '{stream_num:'h1d, speaker_num:'hd3, length:'h5, record_type:'hc2}, '{stream_num:'h7f, speaker_num:'h7d, length:'h5, record_type:'hee}}

Hi ,

I was wondering how can we add more constraint on item.len to restrict it to be only even numbers.

for eg.

spkr_hdr.sum() == 10,

item.len should be inside 2,4,8 and queue elements should be

'{
'{strm_num:'h52, spkr_num:'h3b, len:'h0, rec_type:'hdc},
'{strm_num:'h4e, spkr_num:'hf0, len:'h4, rec_type:'h5f},
'{strm_num:'ha7, spkr_num:'hbc, len:'h2, rec_type:'hc}},
'{strm_num:'h52, spkr_num:'h3b, len:'h0, rec_type:'hdc},
'{strm_num:'h4e, spkr_num:'hf0, len:'h2, rec_type:'h5f},
'{strm_num:'ha7, spkr_num:'hbc, len:'h0, rec_type:'hc}},
'{strm_num:'h52, spkr_num:'h3b, len:'h0, rec_type:'hdc},
'{strm_num:'h4e, spkr_num:'hf0, len:'h0, rec_type:'h5f},
'{strm_num:'ha7, spkr_num:'hbc, len:'h0, rec_type:'hc}},
'{strm_num:'ha7, spkr_num:'hbc, len:'h2, rec_type:'hc}},



//adding this is giving constraint faliure.
spkr_hdr.sum() with (int'(item.len) && (item.len %2 == 0)) == 10

//item.len %2 ==0 , i.e. the all possible even numbers lesser then 10


;

In reply to edaboy:

you can add separate constraint for length to be even as below:


 foreach(my_hdr[i]) {
                my_hdr[i].length %2 == 0;
            }

In reply to sv_uvm_learner_1:

Thanks, my bad. I was trying to add withing with.