Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
math:start [2025/09/22 22:22] – [Functions] rajitmath:start [2025/09/23 11:02] (current) – [Fixed point data type] rajit
Line 18: Line 18:
 This function takes an integer ''x'' of width ''Worig'', and zero extends it to return a result that has width ''Wnew''. It assumes that ''Wnew'' is at least ''Worig.'' This function takes an integer ''x'' of width ''Worig'', and zero extends it to return a result that has width ''Wnew''. It assumes that ''Wnew'' is at least ''Worig.''
  
-===== Datatypes =====+===== Fixed point data type =====
  
 The standard math library has a fixed point data type. The standard math library has a fixed point data type.
Line 26: Line 26:
 </code> </code>
  
-This datatype uses the ''Q(A,B)'' +This datatype uses the standard Q(A,B) fixed point format, where A bits are used for the integer part and B bits are used for the fractional part. This datatype defines operators for addition, subtraction, multiplication, division, as well as comparison operators. 
 + 
 +The following code fragment initializes the fixed point value ''x'' to 5.25. ''x.const(5.25)'' returns a parameterized integer corresponding to translating the real number 5.25 to the fixed point representation used by ''x''. Note that ''x.const()'' returns a ''pint'', so it is limited to 64 bits. ''x.set()'' is a macro that takes an integer of bitwidth (A+B) and sets ''x'' to the specified fixed point number. 
 +<code act> 
 +math::fixpoint<8,3> x; 
 +... 
 +chp { 
 +    x.set(x.const(5.25)); 
 +    ... 
 +
 +</code> 
 +Other ways to initialize ''x'' to the fixed point number corresponding to the integer representation ''0x0f'' (which corresponds to 1.875 for Q(8,3)) are: 
 +<code act> 
 +... 
 +chp { 
 +   x.x := 0x0f; 
 +   ... 
 +   x.set(0x0f); 
 +   ... 
 +
 +</code> 
 + 
 +For debugging purposes, two macros are also provided for displaying the value of a fixed point number. The ''log()'' macro just displays the number using the ''log()'' statement, while the ''log_p()'' displays it without a new line. 
 +<code act> 
 +chp {  
 +   ... 
 +   x.log(); 
 +   ... 
 +
 +</code>   
  
  
Line 92: Line 121:
 This can be used to convert a real constant value into its fixed point representation. Note that since ''pint'' values are 64-bit wide (max), this only works when ''A+B'' is at most 64. This can be used to convert a real constant value into its fixed point representation. Note that since ''pint'' values are 64-bit wide (max), this only works when ''A+B'' is at most 64.
  
 +These functions are used by the fixed point data type.