从常量初始化和赋值(字面值) - 2022.1 Chinese

Vitis 高层次综合用户指南 (UG1399)

Document ID
UG1399
Release Date
2022-06-07
Version
2022.1 简体中文

您可使用常用 C/C++ 宽度的正常浮点常量来初始化 ap_[u]fixed 变量:

  • 32 位(针对 float 类型)
  • 64 位(针对 double 类型)

即通常为单精度类型或双精度形式的浮点值。请注意分配到定点变量的值将受到常量精度的限制。

#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;

您也可以使用字符串初始化以确保根据字符串所描述的精度来填充定点值的所有位。

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

std::complex 类型的阵列中使用 ap_[u]fixed 类型时,后者不支持初始化。

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.}

初始化值必须首先强制转换为 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.}