首页 > 解决方案 > 如何与 SoftFloat 库链接?

问题描述

我正在设计硬件来执行符合 IEEE-754 标准的浮点运算。SoftFloat 是一个库(由 John Hauser - UC Berkeley 编写),它是 IEEE-754 浮点行为的参考模型(用 C++ 编写)。它实现了执行标准指定的所有浮点计算的功能。我想使用 SoftFloat 为我的硬件实现生成测试用例。作者页面的链接是http://www.jhauser.us/arithmetic/SoftFloat.html

我从该页面上的链接 ( http://www.jhauser.us/arithmetic/SoftFloat-3e.zip )下载了 zip 文件,将其解压缩,并用于提供 Makefile 来构建它。我在 Windows10 上的 bash 下运行。在构建过程结束时,我得到了一个存档“softfloat.a”。

SoftFloat 实现的功能之一是融合浮点乘加。根据文档,这可以通过调用函数 f32_mulAdd 来调用,它的模板位于提供的头文件 softfloat.h 中。

在我的主代码 (sf.cc) 中,我包含该标头,使用同一标头中定义的类型定义该函数的参数,并使用以下代码构建代码:

gcc -o sf -I. sf.cc softfloat.a

这给出了以下错误:

/tmp/ccALxpC8.o: In function `main':
sf.cc:(.text+0x4f): undefined reference to `f32_mulAdd(float32_t, float32_t, float32_t)'
collect2: error: ld returned 1 exit status

f32_mulAdd 是在 softfloat.h 中定义的(我检查过),存档文件 softfloat.a 确实包含 f32_mulAdd.o(用 ar -t softfloat.a 检查)和 f32_mulAdd.c 在 SoftFloat 源代码中确实定义了函数(如“ float32_t f32_mulAdd(float32_t a, float32_t b, float32_t c)") 并且我对该函数的调用与定义一致,所以我不明白为什么会收到此消息。如果有人使用过 SoftFloat,请加入。

我在其中调用 SoftFloat 函数 f32_mulAdd 的源代码如下(我想让它首先编译/链接干净,然后我将添加语句来初始化变量 multiplier、multiplicand 和 addend 到特定值):

#include "platform.h"
#include "internals.h"
#include "softfloat.h"
#include "softfloat_types.h"

int float_rounding_mode = 0;

int main()
{
    uint8_t rounding_mode;
    uint8_t exceptions;

    uint32_t multiplier, multiplicand, addend, result;
    float32_t f_multiplier, f_multiplicand, f_addend, f_result;

    f_multiplier.v = multiplier;
    f_multiplicand.v = multiplicand;
    f_addend.v = addend;

    softfloat_roundingMode = rounding_mode;
    softfloat_exceptionFlags = 0;
    softfloat_detectTininess = softfloat_tininess_beforeRounding;

    f_result = f32_mulAdd(f_multiplier, f_multiplicand, f_addend);

    result = f_result.v;    
    exceptions = softfloat_exceptionFlags & 0x1f;

    return 0;
}

标签: c

解决方案


关键是将头文件包含更改为:

extern "C" {
    #include "softfloat.h"
}

此外,softfloat.h 是唯一需要的头文件。


推荐阅读