What does the array reduction operator does in this below piece of code?

Hello !!

Came across this following piece of code,


    class pkt;
        rand bit [3:0] array[12];
        rand bit [3:0] value;
        int count;
        function new(int cnt);
            count = cnt;
            this.randomize();
        endfunction: new

        constraint c {
          array.or() with (item.index inside {[3:count]} && item == value);
        }
    endclass: pkt

  1. From the constraints its quite clear that that index of the item is getting chosen anywhere between 3 : count value. And obviously assigning the item to the value.
  2. But what does the “or” operator is doing here ? bcoz that’s more of a array reduction of doing bit-wise or of all the individual elements of the array right? Does or mean choosing any one of the elements between 3 : count ?
  3. What if I use the “sum” operator instead ?

How does that help in this context ? Can anyone explain the same ?

Thanks !

In reply to desperadorocks:

This constraint makes sure that at least one element of array within the index range of 3 to count equals value.

The with clause expression replaces each elements value in the reduction, and that expression has a 1-bit result. If you replaced or() with sum(), that would be equivalent to xor() for a 1-bit value. The instead of at least one, you would need an odd number of elements matching value.

In reply to dave_59:

Hello Dave !

Thanks for your comments.

Getting confused on the solution set of the 2nd part i.e. .sum() / xor.

  1. Below is the solution set with .or() operator.

array = '{'h9, 'h1, 'h8, 'hc, 'h1, 'h3, 'hd, 'h3, 'h2, 'h4, 'h4, 'h8}  and value = d
array = '{'h7, 'h0, 'h4, 'h3, 'h0, 'hb, 'ha, 'h8, 'hd, 'h9, 'h4, 'h3}  and value = 0
array = '{'h4, 'hb, 'h1, 'ha, 'he, 'hc, 'h8, 'he, 'h1, 'hb, 'h8, 'h6}  and value = e
array = '{'h8, 'hc, 'h3, 'hd, 'h1, 'h8, 'h9, 'h9, 'h1, 'hf, 'h1, 'ha}  and value = d
array = '{'hb, 'h9, 'ha, 'hd, 'he, 'h5, 'h8, 'hc, 'he, 'ha, 'h9, 'h9}  and value = 8
array = '{'ha, 'h6, 'h3, 'h7, 'h0, 'h7, 'h5, 'h8, 'h3, 'h8, 'h1, 'h8}  and value = 7
array = '{'h4, 'h4, 'h9, 'hc, 'h4, 'h2, 'hb, 'ha, 'h9, 'h0, 'h6, 'h1}  and value = 4
array = '{'h8, 'hb, 'hc, 'h1, 'h9, 'h9, 'hf, 'h2, 'h9, 'hc, 'hd, 'h0}  and value = 9
array = '{'h7, 'h9, 'he, 'ha, 'hf, 'h0, 'h1, 'h2, 'hd, 'hc, 'h6, 'hf}  and value = a
array = '{'h4, 'hf, 'h2, 'h9, 'h6, 'h1, 'h1, 'h2, 'hd, 'h3, 'h4, 'h6}  and value = 6

  1. Below is the solution set with .sum() operator.

array = '{'h9, 'h3, 'h0, 'hd, 'h3, 'h6, 'he, 'h3, 'h2, 'h4, 'h4, 'h8}  and value = d
array = '{'h7, 'h0, 'h4, 'h3, 'h0, 'hb, 'ha, 'h8, 'hd, 'h9, 'h4, 'h3}  and value = 0
array = '{'h4, 'hb, 'h1, 'ha, 'he, 'hc, 'h8, 'he, 'h1, 'hb, 'h8, 'h6}  and value = e
array = '{'h8, 'hc, 'h3, 'hd, 'h1, 'h8, 'h9, 'h9, 'h1, 'hf, 'h1, 'ha}  and value = d
array = '{'hb, 'h9, 'ha, 'hd, 'he, 'h5, 'h8, 'hc, 'he, 'ha, 'h9, 'h9}  and value = 8
array = '{'hb, 'ha, 'hd, 'h0, 'h0, 'h5, 'h7, 'h8, 'h3, 'h8, 'h1, 'h8}  and value = 7
array = '{'h4, 'h4, 'h9, 'hc, 'h4, 'h2, 'hb, 'ha, 'h9, 'h0, 'h6, 'h1}  and value = 4
array = '{'h1, 'hb, 'hc, 'h1, 'h8, 'h9, 'hf, 'h2, 'h9, 'hc, 'hd, 'h0}  and value = 9
array = '{'h7, 'h9, 'he, 'ha, 'hf, 'h0, 'h1, 'h2, 'hd, 'hc, 'h6, 'hf}  and value = a
array = '{'h4, 'hf, 'h2, 'h9, 'h6, 'h1, 'h1, 'h2, 'hd, 'h3, 'h4, 'h6}  and value = 6

.or() is clear because it make sure at least one element matches the condition. But for the .sum() operator when you say “you would need an odd number of elements matching value” meaning 1 or more [3,5,etc.] elements inside the array between [3:count] should be matching the value is it ?

For Eg: With one element matching,
array = '{'h9, 'h3, 'h0, 'hd, 'h3, 'h6, 'he, 'h3, 'h2, 'h4, 'h4, 'h8} and value = d // Since one d is matching with the value its valid.

Eg with odd number of element matching
array = '{'h9, 'h3, 'h0, 'hd, 'h3, 'hd, 'hd, 'h3, 'h2, 'h4, 'h4, 'h8} and value = d - Since 3 d’s are matching with the value, its valid. Is this correct ?

So “with” expression is like an final “if” condition with 1-bit result ? that is,

if its “or” , and if the results within the “with” is like “1, 0, 1, 0, 0, 0” - Since at least the 1bit result has 1 valid and since its “or” the condition passes.

if its “sum”/“xor”, and if the results within the “with” is like “1, 0, 1, 0, 1, 0” - Since there are 1-bit result with three 1 valid and since its “xor” the condition passes ? So if the number of 1-bit result is 2 for xor then the tool internally wont take up the solution ?

Kindly clarify the same. Thanks for your time !

In reply to desperadorocks:

It seems like you answered your own question.

In reply to desperadorocks:

What’s the Value for count used in your code ?

In reply to dave_59:

Thanks a lot Dave ! Appreciate that !

In reply to TC_2017:

Hi !! . Oh its 6… Just some default value.

In reply to dave_59:

In reply to desperadorocks:
It seems like you answered your own question.

Dave.
Just a follow-up, In this forum, I have seen lots questions regarding array reduction methods, specially in constraint. Will IEEE 1800 committee add some enhancement, clarification, or more examples in this area in next release ?