floating-point - 将 real 转换为 IEEE double std_logic_vector(63 downto 0)

标签 floating-point vhdl ieee-754

这真的不应该这么难。

我想阅读原始的 64 位 IEEE 754 double-precision floating-point文件中的数据,并在 std_logic_vector(63 downto 0) 中使用它。我正在使用 ModelSim ALTERA 10.1b。

我尝试将原始二进制数据读入 64 位向量:

type double_file is file of std_logic_vector(63 downto 0);
file infile1: double_file open read_mode is "input1.bin";

variable input1 : std_logic_vector(63 downto 0) := (others => '0');

read(infile1, input1);

但这行不通。显然,ModelSim 试图将输入数据的每个 byte 解释为 std_logic ('U', 'Z' , '-' 等)。 enter image description here


不过,我可以成功地将数据读入真实的变量:

type real_file is file of real;
file infile1: real_file open read_mode is "input1.bin";

variable input1 : real;

read(infile1, input1);

但此时,我无法弄清楚如何将该真实变量转换为std_logic_vector(63 downto 0)。几乎所有 Google results只是说“你不能这样做;real 是不可合成的”。我完全理解 - 这只是为了模拟。

最佳答案

关键是 ieee.float_pkg .

首先,您使用 to_floatreal 转换为 float :

variable in1_r : real;
variable in1_f : float64;

in1_f := to_float(in1_r, in1_f);  -- in1_f passed for sizing

然后,您只需将 float64 转换为 slv:

variable in1_slv : std_logic_vector(63 downto 0);

in1_slv := to_std_logic_vector( in1_f );

这也可以用一行代码来完成,消除中间的 float64:

in1_slv <= to_std_logic_vector( to_float(in1_r, float64'high, -float64'low) );

关键是 to_float 需要知道目标大小。由于我们没有中间 float64 值,我们可以直接传递 exponent_widthfraction_width 参数,使用 的定义float64。查看 to_float64 的定义有帮助。

这个问题有些帮助:IEEE Float type to std_logic_vector conversion

关于floating-point - 将 real 转换为 IEEE double std_logic_vector(63 downto 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17780101/

相关文章:

java - 为什么在 Java 中比较 float 不一致?

python - 解释区间 [0, 1] 中明显联系的舍入方向上的惊人奇偶性

vhdl - 使用通用参数作为端口数组长度

用于 2to1 多路复用器的 VHDL

c - 为什么用 float 除以 10 的幂不如直接输入数字准确?

Python小数

python - 如果将最小的正浮点值乘以一个非零数,是否保证得到非零结果?

VHDL/vivado 语法错误

c# - 将 MBF 单精度和 double 转换为 IEEE

floating-point - 计算器中有趣的 float 错误 - 为什么会发生这种情况?