关于vhdl:是否有任何软件可以将float转换为任意精度的FPU? [或Matlab解决方案]

Any Software to convert float to any-precision FPU? [or Matlab solution]

我想将许多浮点数转换为多精度FPU,例如" 0x4049000000 .....",执行计算,更改精度,然后再次执行计算,依此类推...

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

有什么建议?

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

编辑:这也称为基数无关的浮点,如此处和此处所述

第二编辑:IEEE单精度转换和IEEE双精度转换是示例。 您输入任何浮点数,例如 3.1454,您将获得二进制(十六进制)的IEEE(单精度或双精度)浮点值。 @rick


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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的对象来实现相同的效果,该对象的大小可以在其声明中定义:

1
2
3
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()函数:

1
2
3
4
5
-- 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);


快速浏览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,我想认为它们具有明显的行为。

所以答案是……是的。

1
2
3
4
5
6
7
  -- 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;

1
2
3
4
5
6
  -- 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;