Jump to content

std::randomize( vs. randomize( vs. this.randomize( and scope

Recommended Posts

I had expected all 3 of these calls to randomize to return values using the constraint.  However, the first does not.  Can anyone tell me why?

(note: In the following code, there is no local randomize method defined.  So, I think that all randomize functions are the same...and perhaps only scope varies.)  
Code, followed by sim results:
class thursday_seq extends junk_seq_seq;  //which extends uvm_sequence

   rand int count;
   constraint c1 { count >= 2; count <= 9; }

   function new(string name="thursday_seq");
      if (std::randomize(count)) begin 
         `uvm_info("",$psprintf(" cnt=%0d .....",count),UVM_LOW) end
      else $finish;
      if (randomize(count)) begin 
         `uvm_info("",$psprintf(" cnt=%0d .....",count),UVM_LOW) end
      else $finish;
      if (this.randomize()) begin 
         `uvm_info("",$psprintf(" cnt=%0d .....",count),UVM_LOW) end
      else $finish;

UVM_INFO @ 0: reporter@ []  cnt=415747889 .....
UVM_INFO @ 0: reporter@ []  cnt=9 .....
UVM_INFO @ 0: reporter@ []  cnt=2 .....



It seems what is happening is that while std::randomize and randomize are calling the same method in the standard package, the standard version is oblivious to local constraints.

I see in 1800-2012.pdf (SV spec), sec. 18.5.2 "The randomize() method is virtual and therefore honors constraints of the object on which it was called, ..." (highlighting mine)



Later in the spec, there is reference to the 'scope of the randomize('  which confuses me a bit, if the constraints are always to be honored.  (Although, I suppose in the first two cases above "of the object on which it was called" is not true, b/c I don't call it on the object, but on a property of the object.)  Is that correct?


After doing a bunch more reading, I am going to continue with this post for feedback.  Here is something more I learned.


This quote in section "18.12 Randomization of scope variables—std::randomize()" I think explains it all for me.


"The scope randomize function, std::randomize(), enables users to randomize data in the current
scope without the need to define a class or instantiate a class object."



I'm a bit unsure, but I think the answer to my question is this.

"without the need to define a class" and "in the current scope", from above, imply that std::randomize performs only on the scope that is passed to it.   i.e. if I pass it a class object, then it knows of that object's constraints.    If I pass it a data-member of a class, then the scope that data-member exists in is not seen (i.e. any constraints which are in the scope above that data-member are not seen).  Though they could be replicated as inline constraints.).   

The local randomize knows of all the constraints in the object from which it is called.


Do I understand this correctly?  I think I just walked myself through understanding this.  Comments welcome.




note: using Cadence's IUS12.1-s004

Link to comment
Share on other sites

Within a non-static class method, randomize() and this.randomize() are the same method call. You can think of any method call as having an implicit this argument. method_call() is really method_call(.this(this)) and obj.method_call() is really method_call(.this(obj)). The scope of the randomize() object is the this argument inside the randomize method and that is the object whose constraints are to be met.


std::randomize is just a function in a package, it is not a method. The only constraints it observes are the one specified by an optional with clause or if the variable being randomized is itself a class object with constraints. You are not restricted to randomizing variables in the current scope, and they don't even have to be class members. It just randomizes what ever variables you put into its argument list. So std::randomize(count) randomizes an int with not constraints since count is not a class variable.

std::randomize(this) would be the same as this.randomize() or just randomize().


The scope containing the call to obj.randomize() statement has significance when you have variables with the same name in both the calling and the object being randomizes scope used in a with clause constraint. The with clause assumes the constraint refers to the object being randomized first. If it can't find it in that object, it then searches for that variable in the scope where the call to randomize took place.


Link to comment
Share on other sites

  • 3 months later...
  • 2 weeks later...

Just to add to Dave's great explanation:


when you have variables with the same name in both the calling and the object being randomizes scope used in a with clause constraint.


SV 2009 added "local::" scope resuoltion to refer to calling class scope variable name.


Attached below is a screenshot from our UVM training that covers this very topic:



Ajeetha, CVC





Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...