1

我正在计算极坐标系中两条线的交点:

typedef ap_fixed<16,3,AP_RND> t_lines_angle;
typedef ap_fixed<16,14,AP_RND> t_lines_rho;

bool get_intersection(
        hls::Polar_< t_lines_angle, t_lines_rho>* lineOne,
        hls::Polar_< t_lines_angle, t_lines_rho>* lineTwo,
        Point* point)
{
    float angleL1 = lineOne->angle.to_float();
    float angleL2 = lineTwo->angle.to_float();
    t_lines_angle rhoL1 = lineOne->rho.to_float();
    t_lines_angle rhoL2 = lineTwo->rho.to_float();
    t_lines_angle ct1=cosf(angleL1);
    t_lines_angle st1=sinf(angleL1);
    t_lines_angle ct2=cosf(angleL2);
    t_lines_angle st2=sinf(angleL2);
    t_lines_angle d=ct1*st2-st1*ct2;

    // we make sure that the lines intersect
    // which means that parallel lines are not possible
    point->X = (int)((st2*rhoL1-st1*rhoL2)/d);
    point->Y = (int)((-ct2*rhoL1+ct1*rhoL2)/d);

    return true;
}

在对我们的 FPGA 进行综合后,我看到浮点正弦(和余弦)的 4 种实现每次实现需要 4800 个 LUT,这 4 个函数的总和为 19000 个 LUT。我想通过使用定点正弦来减少 LUT 计数。我已经找到了CORDIC的实现,但我不确定如何使用它。该函数的输入是一个整数,但我有一个ap_fixed数据类型。如何将其映射ap_fixed到整数?以及如何将我的 3.13 固定点映射到所需的 2.14 固定点?

4

2 回答 2

1

在我的一位同事的帮助下,我想出了一个非常简单的解决方案,它不需要任何手写实现或固定点数据的操作:

使用#include "hls_math.h"hls::sinf()hls::cosf()功能。

重要的是要说函数的输入应该是ap_fixed<32, I>where I <= 32。函数的输出可以分配给不同的类型,例如,ap_fixed<16, I>

例子:

void CalculateSomeTrig(ap_fixed<16,5>* angle, ap_fixed<16,5>* output)
{
    ap_fixed<32,5> functionInput = *angle;
    *output = hls::sinf(functionInput);
}

LUT消耗:

在我的例子中,每个函数实现的 LUT 消耗减少到 400 个 LUT。

于 2017-12-07T08:45:31.017 回答
0

您可以使用位切片来获取 ap_fixed 变量的小数部分和整数部分,然后对它们进行操作以获得新的 ap_fixed。也许是这样的:

constexpr int max(int a, int b) { return a > b ? a : b; }

template <int W2, int I2, int W1, int I1>
ap_fixed<W2, I2> convert(ap_fixed<W1, I1> f)
{
    // Read fraction part as integer:
    ap_fixed<max(W2, W1) + 1, max(I2, I1) + 1> result = f(W1 - I1 - 1, 0);
    // Shift by the original number of bits in the fraction part
    result >>= W1 - I1;
    // Add the integer part
    result += f(W1 - 1, W1 - I1);
    return result;
}

我还没有很好地测试过这段代码,所以对它持保留态度。

于 2017-12-07T09:49:50.717 回答