Support for Console I/O (Printing) - 2021.2 English

Vitis High-Level Synthesis User Guide (UG1399)

Document ID
UG1399
ft:locale
English (United States)
Release Date
2021-12-15
Version
2021.2 English

As with initialization and assignment to ap_[u]fixed<> variables, Vitis HLS supports printing values that require more than 64 bits to represent.

The easiest way to output any value stored in an ap_[u]fixed variable is to use the C++ standard output stream, std::cout (#include <iostream> or <iostream.h>). The stream insertion operator, “<<“, is overloaded to correctly output the full range of values possible for any given ap_[u]fixed variable. The following stream manipulators are also supported, allowing formatting of the value as shown.

  • dec (decimal)
  • hex (hexadecimal)
  • oct (octal)
    #include <iostream.h>
    // Alternative: #include <iostream>
    
    ap_fixed<6,3, AP_RND, AP_WRAP> Val = 3.25;
    
    cout << Val << endl;     // Yields: 3.25

Using the Standard C Library

You can also use the standard C library (#include <stdio.h>) to print out values larger than 64-bits:

  1. Convert the value to a C++ std::string using the ap_[u]fixed classes method to_string().
  2. Convert the result to a null-terminated C character string using the std::string class method c_str().

Optional Argument One (Specifying the Radix)

You can pass the ap[u]int::to_string() method an optional argument specifying the radix of the numerical format desired. The valid radix argument values are:

  • 2 (binary)
  • 8 (octal
  • 10 (decimal)
  • 16 (hexadecimal) (default)

Optional Argument Two (Printing as Signed Values)

A second optional argument to ap_[u]int::to_string() specifies whether to print the non-decimal formats as signed values. This argument is boolean. The default value is false, causing the non-decimal formats to be printed as unsigned values.

ap_fixed<6,3, AP_RND, AP_WRAP> Val = 3.25;

printf("%s \n", in2.to_string().c_str()); // Yields: 0b011.010
printf("%s \n", in2.to_string(10).c_str()); //Yields: 3.25

The ap_[u]fixed types are supported by the following C++ manipulator functions:

  • setprecision
  • setw
  • setfill

The setprecision manipulator sets the decimal precision to be used. It takes one parameter f as the value of decimal precision, where n specifies the maximum number of meaningful digits to display in total (counting both those before and those after the decimal point).

The default value of f is 6, which is consistent with native C float type.

ap_fixed<64, 32> f =3.14159;
cout << setprecision (5) << f << endl;
cout << setprecision (9) << f << endl;
f = 123456;
cout << setprecision (5) << f << endl;

The example above displays the following results where the printed results are rounded when the actual precision exceeds the specified precision:

   3.1416
   3.14159
   1.2346e+05

The setw manipulator:

  • Sets the number of characters to be used for the field width.
  • Takes one parameter w as the value of the width

    where

    • w determines the minimum number of characters to be written in some output representation.

If the standard width of the representation is shorter than the field width, the representation is padded with fill characters. Fill characters are controlled by the setfill manipulator which takes one parameter f as the padding character.

For example, given:

    ap_fixed<65,32> aa = 123456;
    int precision = 5;
    cout<<setprecision(precision)<<setw(13)<<setfill('T')<<a<<endl;

The output is:

     TTT1.2346e+05