【问题标题】:Any Software to convert float to any-precision FPU? [or Matlab solution]任何将浮点数转换为任意精度 FPU 的软件? [或Matlab解决方案]
【发布时间】:2026-01-18 23:00:01
【问题描述】:

我想将很多浮点数转换为多精度FPU,例如'0x4049000000.....',执行计算,更改精度然后再次执行计算等等......

我知道理论为@9​​87654321@,但我想要一个软件、在线工具或 Matlab 解决方案来将 6000 多个数字转换为 0.0 ~ 51.0 范围内的 FPU 格式(如 IEEE 单精度)。

有什么建议吗?

注意:我需要自定义精度,我可以描述 Mentissa 和 Exponent 的位数。

编辑:也称为基数独立浮点,如herehere 所述

第二次编辑IEEE single precision convertIEEE double Precision convert 是示例。您输入任何浮点数,例如3.1454,您将获得二进制/十六进制的 IEEE(单精度或双精度)浮点值。 @rick

【问题讨论】:

  • 使用浮点库,VHDL-2008 中的标准。
  • 这个库可以在标准浮点数和 FPU 之间转换吗? @BrianDrummond
  • 这和 MATLAB 有什么关系?
  • Matlab 有 num2hex() 命令可以转换为双精度。我想,它可能有一些命令来指定其他精度,@rayryeng​​span>
  • 您能否添加几个带有输入和相应输出值的示例?几乎可以肯定用 VHDL 中的 float_pkg 来完成,但我需要一个例子来准确理解。

标签: matlab vhdl fpu


【解决方案1】:

快速查看 VHDL-2008 浮点库 float_pkg 可以看出,它实例化了一个通用包,其中设置了通用参数以匹配 IEEE 单精度浮点数。

package float_pkg is new IEEE.float_generic_pkg (...)

您应该在模拟器安装中找到这个库,无论您通常在哪里寻找标准库,例如numeric_std。在我的系统上它位于/opt/ghdl-0.32/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/vhdl/src/ieee2008/float_pkg.vhdl - 如果您在系统上找不到它,它可以在线获得,搜索“VHDL 2008 float package”应该可以让您到达那里。

您可以实例化这个通用包(以float_pkg 为例)以获得您喜欢的任何精度(在合理的范围内)。

快速查看IEEE.float_generic_pkg 表明它声明了函数to_floatto_real,我想它们具有明显的行为。

所以答案是……是的。

  -- real to float
  function to_float (
    arg                  : REAL;
    size_res             : UNRESOLVED_float;
    constant round_style : round_type := float_round_style;  -- rounding option
    constant denormalize : BOOLEAN    := float_denormalize)  -- Use IEEE extended FP
    return UNRESOLVED_float;

  -- float to real
  function to_real (
    arg                  : UNRESOLVED_float;  -- floating point input
    constant check_error : BOOLEAN    := float_check_error;  -- check for errors
    constant denormalize : BOOLEAN    := float_denormalize)  -- Use IEEE extended FP
    return REAL;

【讨论】:

  • 我仍然不知道如何使用它来完成我的工作......!你做了很好的努力,但我没有得到好处。你能告诉我如何用这个将浮点数转换为任意精度的浮点数吗?一个小例子?
  • 那么你尝试了哪些方法,你在哪里卡住了?
  • 好的。让我表明我的立场。我正在做我的 FYP 以在 Verilog 的 FPGA 上实现海洋模型。我已经做到了,但它的面积效率不高。现在我需要使用 Flopoco [vhdl 中的 fpu 算术代码生成器] 来完成它。我以某种方式学习了 Verilog,但我对 VHDL 很幼稚。这就是为什么我没有得到你在答案中所说的......
【解决方案2】:

float_pkg 应该是你所需要的。它为您提供了一些预定义的浮点类型(例如,IEEE 单精度和双精度),以及为小数和指数定义具有任意位数的自定义浮点值的能力。

根据您的数字示例,这里有一些代码可将 real 值转换为单精度、双精度和四精度浮点数。

library ieee;
use ieee.float_pkg.all;

entity floating_point_demo is
end;

architecture example of floating_point_demo is
begin
    process
        variable real_input_value: real := 49.0215463456;
        variable single_precision_float: float32;
        variable double_precision_float: float64;
        variable quadruple_precision_float: float128;
    begin
        single_precision_float := to_float(real_input_value, single_precision_float);
        report to_string(single_precision_float);
        report to_hstring(single_precision_float);

        double_precision_float := to_float(real_input_value, double_precision_float);
        report to_string(double_precision_float);
        report to_hstring(double_precision_float);

        quadruple_precision_float := to_float(real_input_value, quadruple_precision_float);
        report to_string(quadruple_precision_float);
        report to_hstring(quadruple_precision_float);

        wait;
    end process;
end;

上面的示例使用来自float_pkg 的类型float32float64float128。但是,您可以使用 float 类型的对象来实现相同的效果,其大小可以在其声明中定义:

variable single_precision_float: float(8 downto -23);
variable double_precision_float: float(11 downto -52);
variable quadruple_precision_float: float(15 downto -112);

要将float 转换为real 值,您可以使用to_real() 函数:

-- To print the real value of a float object:
report to_string(to_real(quadruple_precision_float));

-- To convert from a float and assign to a real:
real_value := to_real(quadruple_precision_float);

【讨论】:

  • 谢谢。但我坚持添加库。我在这个链接中尝试过,但没有帮助xilinx.com/support/documentation/sw_manuals/xilinx14_1/…
  • 如果您的综合工具与 VHDL-2008 不兼容,您可能必须使用兼容性浮点包:www.eda.org/fphdl
  • 我在这些目录中有库文件 float_pkg.vhd。 img42.com/RntS2。我从 ISE 中的 Project => New Vhdl Library 中包含了它们,但它不起作用。我无法识别主代码中使用的库函数。
最近更新 更多