bluespec.com Forum Index bluespec.com
Bluespec Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Defining a new bounded type

 
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ...
View previous topic :: View next topic  
Author Message
chiraag



Joined: 06 Aug 2011
Posts: 29

PostPosted: Thu Jun 07, 2012 6:52 am    Post subject: Defining a new bounded type Reply with quote

Hey I want to define a new Bounded type B100 which is inherited from Bits#( 8 ) with a minbound of 0 and a maximum of 100. What would be the right way to do this?

I tried:
Code:

typedef UInt#(8) B100;
instance Bounded#(B100);
   minBound = 0;
   maxBound = 100;
endinstance

module mkTb (Empty);
   B100 testB100 = 125;
endmodule



This didn't work. The compiler didn't flag an error on the initialization and I got a funny compile warning
Code:

  Exporting orphan typeclass instance Bounded#(UInt#(8)). The instance's
  typeclass as well as all of the instance's source type parameters are
  defined in other packages. This can lead to confusing and inconsistent
  instance resolution if the orphan instance is not imported everywhere it
  could be used.

Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 500

PostPosted: Thu Jun 07, 2012 2:50 pm    Post subject: Re: Defining a new bounded type Reply with quote

When you write this:
Code:
typedef UInt#(8) B100;

you are not creating a new type. You are merely creating an alias. Anywhere that you write B100, it is equivalent to having written UInt#(8), and there is no error if you mix the two types.

What you want to do is this:
Code:
typedef struct {
   UInt#(8) rep;
} B100 deriving (...);

In the deriving clause you can derive all the type classes that exist for UInt#(8) that you want this type to also have. You probably want to include Bits, Eq, Literal, SizedLiteral, Ord, and Arith. But don't include Bounded, of course, because you will define your own.

I've used a struct here, but a tagged union with one tag with all also work.

Because you used an alias, the instance you defined is overlapping with the existing instance for UInt#(). By defining this instance, you're actually re-defining Bounded for all values of type UInt#(8)!
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 500

PostPosted: Thu Jun 07, 2012 2:59 pm    Post subject: Re: Defining a new bounded type Reply with quote

Also note that the Bounded typeclass doesn't apply in this example:
Code:
module mkTb (Empty);
   B100 testB100 = 125;
endmodule

The literal "125" is converted into the appropriate type by an implied application of the "fromInteger" function. This function is part of the Literal typeclass.

The Bounded class only defines the functions "maxBound" and "minBound". It does not affect anything else, such as how values are created from integers, or how arithmetic occurs.

So when I said that you should derive Literal and SizedLiteral, I was wrong. You'll want to define special instances of those classes for your new type. The "fromInteger" function, for example, will check that the integer is between 0 and 100 before using the Literal instance for UInt to get the result:
Code:

instance Literal#(B100);
   function B100 fromInteger(Integer x);
      if ((x < 0) || (x > 100))
         error("creating B100 from out-of-range value")
      return (B100 { rep : fromInteger(x) });
   endfunction
endinstance

Note that you probably also don't want to derive the Arith class! Because what happens if 75 is added to 75? You don't want the value 150, right? Because that's out of bounds. So you'll need to re-define how arithmetic works.

Fortunately, there is already a library package that defines numbers like this. Have a look in the source libraries provided with the release. There is a package called NumberTypes, which defines two possibilities: WrapNumber and BuffIndex. Or, if you need something different, you can modify those examples to do what you want.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ... All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
bluespec.com topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP