Initialization and Assignment from Constants (Literals) - 2022.2 English

Vitis High-Level Synthesis User Guide (UG1399)

Document ID
UG1399
Release Date
2022-12-07
Version
2022.2 English

You can initialize ap_[u]fixed variable with normal floating point constants of the usual C/C++ width:

  • 32 bits for type float
  • 64 bits for type double

That is, typically, a floating point value that is single precision type or in the form of double precision. Note that the value assigned to the fixed-point variable will be limited by the precision of the constant.

#include <ap_fixed.h>

ap_ufixed<30, 15> my15BitInt = 3.1415;
ap_fixed<42, 23> my42BitInt = -1158.987;
ap_ufixed<99, 40> = 287432.0382911;
ap_fixed<36,30> = -0x123.456p-1;

You can also use string initialization to ensure that all bits of the fixed-point variable are populated according to the precision described by the string.

ap_ufixed<2,  0> x     = "0b0.01";      // 0.25 in “dot” format
ap_ufixed<2,  0> y     = "0b01p-2";     // 0.25 in binary “scientific” format
ap_ufixed<2,  0> z     = "0x4p-4";     // 0.25 in hex “scientific” format 
ap_ufixed<62, 2> my_pi = “0b11.001001000011111101101010100010001000010110100011000010001101"; // pi with 60 fractional bits

The ap_[u]fixed types do not support initialization if they are used in an array of std::complex types.

typedef ap_fixed<DIN_W, 1, AP_TRN, AP_SAT> coeff_t; // MUST have IW >= 1
std::complex<coeff_t> twid_rom[REAL_SZ/2] = {{ 1, -0 },{ 0.9,-0.006 }, etc.}

The initialization values must first be cast to std::complex:

typedef ap_fixed<DIN_W, 1, AP_TRN, AP_SAT> coeff_t; // MUST have IW >= 1
std::complex<coeff_t> twid_rom[REAL_SZ/2] = {std::complex<coeff_t>( 1, -0 ), 
std::complex<coeff_t>(0.9,-0.006 ),etc.}