# 基础008_定浮点转化[floating point IP]

一、IP核综述

1）half：半精度，16位 = 1位符号 + 5 位指数 + 10位小数

2）single：单精度，32位 = 1位符号 +8 位指数 + 23位小数

3）double：双精度，64位 = 1位符号 + 11 位指数 + 52位小数

其中 DSP slice usage：

In short, it is generally best to use DSP slices if you have them avaialable. They are free, faster, and lower power. If you are running low on them, you can go back and balance speed with area to use DSP slices at the bottlenecks of the design and use fabric where you don't need the speed. 简而言之，DSP用的越多，时序可以越快。

block memory usage：

假设现在需要求：13.24的自然对数【对数也可以借助CORDIC直接进行定点计算】。

13.24设置定点数：

s_axis_a_tdata自动扩展为8的整数倍，如此处为18，则输出理论上为24bits，其余补零即可。

testbench文件：

timescale 1ns / 1ps
/*
Function: DPRAM for data aligned
Author: Gui.
Data: 2018年5月15日22:43:11
*/
module ln_tb;
//parameter

//logic
logic clk,rst;
logic signed [17:0] data;
//initial
initial begin
clk = 0;
rst = 1;
data <= 0;
#8
rst = 0;
#20
data <= 18'b011010011110101110;//+13.24
#1000
\$stop;
end

always #2 clk = ~clk;
//main
logic m_axis_result_tvalid;
logic [31:0] m_axis_result_tdata;

//fix to float
fix_to_float fix_to_float_inst
(
.aclk(clk),
.s_axis_a_tvalid(1'b1),
.s_axis_a_tdata({6'b0,data}),
.m_axis_result_tvalid(m_axis_result_tvalid),
.m_axis_result_tdata(m_axis_result_tdata)//output
);
//float calculation: ln(outout)
logic [31:0] result;
floating_point_0 floating_point_0_inst
(
.aclk(clk),
.s_axis_a_tvalid(1'b1),
.s_axis_a_tdata(m_axis_result_tdata),
.m_axis_result_tvalid(),
.m_axis_result_tdata(result)
);
//float to fix , ignored
endmodule

>> dec2bin(hex2dec(num2hex((single(13.24))))) %浮点的定点显示

%如果给了定点显示，计算对应的浮点值的指令？

• 浮点数转十六进制数
num2hex(single(13.24))
• 十六进制转浮点数（正数为例，负数提取一个符号位即可，同时浮点->十六进制，只算绝对值）
function output = hex2float(a)
%convert hex to float
num = dec2bin(hex2dec((a)));
L = length(num);
exppos = num((L-23):-1:1);
expnum = 0;
for i = 1:length(exppos)
expnum = expnum + str2num(exppos(i))*2^(i-1);
end
expnum = expnum - 2^7 + 1;
fracpos = num(end-23:end);
fracnum = 0;
for i = 2:length(exppos);
fracnum = fracnum + str2num(fracpos(i))*2^(-i+1);
end
fracnum = fracnum + 1;
output = fracnum*2^expnum;
`

从浮点角度对比FPGA与MATLAB输出：

MATLAB结果：

posted @ 2018-05-16 00:05  桂。  阅读(3099)  评论(0编辑  收藏  举报