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
-
使用VHDL-2008中的标准浮点库。
-
这个库可以在标准float和FPU之间转换吗? @BrianDrummond
-
这与MATLAB有什么关系?
-
Matlab具有num2hex()命令,该命令可以转换为双精度。 我想它可能有一些命令可以指定其他精度,@ rayryeng
-
您能为输入和相应的输出值添加几个示例吗? 几乎可以肯定地使用VHDL中的float_pkg来完成,但是我需要一个示例来确切地了解如何进行。
-
我已经为您添加了问题2 EDIT中的说明。 @rick
-
谢谢,我想我没有清楚说明我在想什么,我的意思是一个数值示例(输入值->转换->输出值)。
-
输入:-49.0215463456输出:-0x404882C207D8D9A4 [其IEEE双精度,我需要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中的float32,float64和float128类型。但是,可以使用类型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 ); |
-
谢谢。但是我一直坚持添加库。我在此链接中尝试过,但对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中包括在内,但无法正常工作。我无法识别主代码中使用的库函数。
快速浏览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_float和to_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; |
-
我还是不知道如何用它来做我的工作...!您付出了很大的努力,但我没有得到好处。您能告诉我如何用这个将浮点数转换为任意精度的浮点数吗?一个小例子?
-
那么,您尝试了什么?在哪里遇到了困难?
-
好的。让我说一下我的立场。我正在做我的FYP,以在Verilog中的FPGA上实现海洋模型。我已经做到了,但是区域效率不高。现在,我需要使用Flopoco [vhdl中的fpu算术代码生成器]进行此操作。我已经以某种方式学习了Verilog,但对VHDL还是很幼稚。那就是为什么我不明白你在回答中说的话...