As with initialization and assignment to ap_[u]fixed<>
variables, Vitis HLS supports printing values that require more than
64-bits to represent.
Using the C++ Standard Output Stream
The easiest way to output any value stored in an ap_[u]int
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:
- dec (decimal)
- hex (hexadecimal)
- oct (octal)
These allow formatting of the value as indicated.
The following example uses cout
to print values:
#include <iostream.h>
// Alternative: #include <iostream>
ap_ufixed<72> Val(“10fedcba9876543210”);
cout << Val << endl; // Yields: “313512663723845890576”
cout << hex << val << endl; // Yields: “10fedcba9876543210”
cout << oct << val << endl; // Yields: “41773345651416625031020”
Using the Standard C Library
You can also use the standard C library (#include
<stdio.h>
) to print out values larger than 64-bits:
- Convert the value to a C++
std::string
using theap_[u]fixed
classes methodto_string()
. - Convert the result to a null-terminated C character string using the
std::string
class methodc_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) (default)
- 8 (octal)
- 10 (decimal)
- 16 (hexadecimal)
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.
The following examples use printf
to print values:
ap_int<72> Val(“80fedcba9876543210”);
printf(“%s\n”, Val.to_string().c_str()); // => “80FEDCBA9876543210”
printf(“%s\n”, Val.to_string(10).c_str()); // => “-2342818482890329542128”
printf(“%s\n”, Val.to_string(8).c_str()); // => “401773345651416625031020”
printf(“%s\n”, Val.to_string(16, true).c_str()); // => “-7F0123456789ABCDF0”