一、vivado hls中union说明
1.vivado hls中union联合体和标准的c/c++联合体的union使用规则类似
2.vivado hls中需要对内存和数据类型进行比较准确的推断,从而来生成硬件
3.联合体中所有的成员共享一块内存,同一时刻只能使用其中的一个成员
4.在 HLS 中使用联合体时,需要注意数据类型的位宽和对齐,因为这会直接影响生成的硬件
5.在vivado hls中,联合体的综合是有条件的,需要避免使用指针和动态内存分配
6.联合体中使用数组和结构体是被允许的
7.联合体在vivado hls使用,比较常见的用途,是用于事项数据类型的转换
二、内存共享机制
// 案例1: 基本union使用
union DataUnion {
uint32_t word; // 32位
uint8_t bytes[4]; // 4个8位字节
struct {
uint8_t b0, b1, b2, b3;
} byte_struct;
};
void union_example1(ap_uint<32> input, ap_uint<8>& byte0) {
#pragma HLS INTERFACE ap_ctrl_none port=return
#pragma HLS INTERFACE ap_none port=input
#pragma HLS INTERFACE ap_none port=byte0
DataUnion data;
data.word = input; // 写入32位字
// 四个字节共享同一内存位置
byte0 = data.bytes[0]; // 读取低8位
// data.bytes[1], [2], [3] 对应高8位
}
三、数据位宽匹配设计
// 案例2: 位宽精确匹配
union FixedUnion {
ap_uint<12> data_12bit; // 12位
ap_uint<4> nibbles[3]; // 3×4=12位,正确匹配
// ap_uint<8> bytes[2]; // 错误:2×8=16位,不匹配!
// 可以使用struct进行位域控制
struct {
ap_uint<4> low : 4;
ap_uint<4> mid : 4;
ap_uint<4> high : 4;
} bits;
};
void union_example2(ap_uint<12> in_data,
ap_uint<4>& out_nibble) {
#pragma HLS PIPELINE II=1
FixedUnion fu;
fu.data_12bit = in_data;
// 通过不同方式访问相同数据
out_nibble = fu.nibbles[1]; // 中间4位
// 等效于:out_nibble = fu.bits.mid;
}
四、使用限制
// 案例3: HLS可综合的union用法
union HLS_Compatible {
// 只包含POD(普通旧数据类型)
float fval;
int ival;
ap_fixed<16,8> fixed_val; // HLS定点数类型
// 以下不可在union中使用:
// 1. 非POD类型(如std::string)
// 2. 动态内存分配
// 3. 虚函数
};
void type_conversion(float float_in, int& int_out) {
#pragma HLS INLINE
union {
float f;
int i;
} converter;
converter.f = float_in;
int_out = converter.i; // 浮点位模式解释为整数
}