如何将 HLS 任意精度类型转换为复合类型
How can I convert an HLS arbitrary precision type into a composite type
我正在编写一个带有 AXI4 流输入的 HLS 单元。流中的几个词构成了我想要访问的结构。例如:
struct eth_header {
ap_uint<48> dest;
ap_uint<48> source;
ap_uint<16> proto;
}
我可以轻松地缓冲流中的单词并将它们连接成一个大的 ap_uint<112>
。但是,我非常想将 ap_uint<112>
转换成一个像上面的 eth_header
这样的好结构,我可以使用字段语法访问它。我找不到一个好的方法来做到这一点。我不能强制转换或使用联合,因为 ap_uint
class 不是 POD。
是否可以以某种方式转换类型(无需为每个字段编写显式代码)?
编辑:不清楚是否需要从流中的几个单词转换结构。
我最终编写了明确的代码来进行转换。例如:
struct eth_header {
ap_uint<48> dest;
ap_uint<48> source;
ap_uint<16> proto;
static const int width = 112;
eth_header(const ap_uint<width>& d) :
dest (d( 47, 0)),
source(d( 95, 48)),
proto (d(111, 96))
{}
operator ap_uint<width>()
{
return (hls_helpers::swap16(proto), source, dest);
}
};
这很丑陋,但这是唯一对我有用的东西。
如前所述,出于不同的原因,最好的方法是自己定义一个小结构,其中包含您需要的数据和您喜欢的数据类型。例如,使用 float:
struct my_data{
float data;
bool last;
};
并且,如果您使用的是 AXI4 流媒体接口:
#define N 32
void my_function(my_data input[N], my_data output[N])
{
#pragma HLS INTERFACE axis port=output
#pragma HLS INTERFACE axis port=input
#pragma HLS INTERFACE s_axilite port=return
float tmp_data;
float tmp_last;
int k=0;
for(k=0;k<N;k++)
{
tmp_data[k] = input[k].data;
tmp_last[k] = input[k].last;
}
//...do something here
for(k=0;k<25;k++)
{
output[k].data = tmp_data[k];
output[k].last = tmp_last[k];
}
}
我正在编写一个带有 AXI4 流输入的 HLS 单元。流中的几个词构成了我想要访问的结构。例如:
struct eth_header {
ap_uint<48> dest;
ap_uint<48> source;
ap_uint<16> proto;
}
我可以轻松地缓冲流中的单词并将它们连接成一个大的 ap_uint<112>
。但是,我非常想将 ap_uint<112>
转换成一个像上面的 eth_header
这样的好结构,我可以使用字段语法访问它。我找不到一个好的方法来做到这一点。我不能强制转换或使用联合,因为 ap_uint
class 不是 POD。
是否可以以某种方式转换类型(无需为每个字段编写显式代码)?
编辑:不清楚是否需要从流中的几个单词转换结构。
我最终编写了明确的代码来进行转换。例如:
struct eth_header {
ap_uint<48> dest;
ap_uint<48> source;
ap_uint<16> proto;
static const int width = 112;
eth_header(const ap_uint<width>& d) :
dest (d( 47, 0)),
source(d( 95, 48)),
proto (d(111, 96))
{}
operator ap_uint<width>()
{
return (hls_helpers::swap16(proto), source, dest);
}
};
这很丑陋,但这是唯一对我有用的东西。
如前所述
struct my_data{
float data;
bool last;
};
并且,如果您使用的是 AXI4 流媒体接口:
#define N 32
void my_function(my_data input[N], my_data output[N])
{
#pragma HLS INTERFACE axis port=output
#pragma HLS INTERFACE axis port=input
#pragma HLS INTERFACE s_axilite port=return
float tmp_data;
float tmp_last;
int k=0;
for(k=0;k<N;k++)
{
tmp_data[k] = input[k].data;
tmp_last[k] = input[k].last;
}
//...do something here
for(k=0;k<25;k++)
{
output[k].data = tmp_data[k];
output[k].last = tmp_last[k];
}
}