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 

Floating Point Operations

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



Joined: 09 Feb 2016
Posts: 2

PostPosted: Thu May 19, 2016 1:04 pm    Post subject: Floating Point Operations Reply with quote

Hi I am currently trying to convert my program from using Int#(16) to using Reals or Floats. I have read throughout the forum that currently bluespec only supports Real data types at compile time.
My program requires the use of reals/floats at run time and so i have been looking into using the floating point package found in the Math library.

I am currently running bluespec version: 2015.09.beta2

Is there any documentation for this package? If not is anyone able to assist with getting something like the following to compile?

Code:
q <= (q && (xin1 <= (xin2 + eps)))

where q is a Bool,
xin1, xin2, and eps are originally Int#(16) but should be float

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


Joined: 02 Nov 2007
Posts: 494

PostPosted: Wed May 25, 2016 1:41 pm    Post subject: Re: Floating Point Operations Reply with quote

I don't believe that there is any documentation besides the source code. The package defines instances of the Ord and Arith typeclasses for FloatingPoint, so the comparison operators (like "<=") and the arithmetic operators (like "+") are overloaded for this type. So you can certainly write expressions like this:
Code:
FloatingPoint#(24, 8) xin1 = ... ;
FloatingPoint#(24, 8) xin2 = ...;
FloatingPoint#(24, 8) eps = ...;
Bool q = True;
for (...)
  q <= q && (xin1 <= (xin2 + eps));

The FloatingPoint type takes two type parameters, which are the bit widths of the mantissa and the exponent. (In the source code, these are usually named "m" and "e", in parameterized functions.)

You can create a value of type FloatingPoint from an integer or real literal:
Code:
FloatingPoint#(24,8) x = 17;
FloatingPoint#(24,8) y = 3.14159;

These expressions implicitly use the "fromInteger" and "fromReal" functions, which you can also use explicitly:
Code:
Integer xi = 17;
FloatingPoint#(24,8) x = fromInteger(xi);
Real yr = 3.14159;
FloatingPoint#(24,8) y = fromReal(yr);

There does not appear to be a pre-defined function for creating a FloatingPoint from an Int, however you could write one yourself, that creates a FloatingPoint value by setting the mantissa and exponent fields with the right values.
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 494

PostPosted: Wed May 25, 2016 2:11 pm    Post subject: Re: Floating Point Operations Reply with quote

Ah, I may have spoken too soon about the comparison operators. They appear to only work at compile-time, on static values. If you need to operator on values from registers (or other state), use the "compareFP" function, and decide how to handle the NaN case.

This issue was brought up in another forum post. See http://bluespec.com/forum/viewtopic.php?p=2100#2100
Back to top
View user's profile Send private message
rowinsm



Joined: 09 Feb 2016
Posts: 2

PostPosted: Sat May 28, 2016 3:31 pm    Post subject: Reply with quote

Thanks for the clarification! Looks like it'll take some extra effort to get this to work nicely.
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