中科亿海微浮点数转换定点数

news2025/1/16 8:00:22

引言

        浮点数转换定点数是一种常见的数值转换技术,用于将浮点数表示转换为定点数表示。浮点数表示采用指数和尾数的形式,可以表示较大范围的数值,但存在精度有限的问题。而定点数表示则采用固定小数点位置的形式,具有固定的精度和范围。在某些应用中,特别是在嵌入式系统和数字信号处理领域,使用定点数表示可以更加高效地进行计算。然而,很多算法和库函数都是基于浮点数实现的,因此需要将浮点数转换为定点数才能在这些系统中使用。浮点数转换定点数的背景涉及到浮点数和定点数的表示方法、精度要求以及性能考虑等方面。在转换过程中需要考虑相应的位宽、小数点位置、舍入方式等参数的选择,以保证转换后的定点数能够准确地表示原始浮点数,并满足应用的要求。此外,浮点数转换定点数也可能涉及到溢出、舍入误差、舍入方式选择等问题。因此,在实际应用中需要仔细分析和评估转换算法的性能和精度,并根据具体的应用场景进行调整和优化。

背景

        浮点数转换定点数的概念和技术并没有一个具体的发明者或起源人。它是在计算机科学和数字信号处理等领域的发展过程中逐渐形成和应用的。浮点数和定点数的概念最早可以追溯到计算机科学的早期发展阶段。在计算机领域的先驱们,如冯·诺依曼、约翰·冯·诺伊曼、阿兰·图灵等,对于数字表示和计算方法进行了深入研究,为后来的浮点数和定点数概念的发展奠定了基础。随着计算机技术的快速发展,浮点数和定点数的表示方法和转换技术也得到了更多的研究和应用。在数字信号处理领域,特别是在嵌入式系统和通信领域,浮点数转换定点数被广泛应用于各种算法和信号处理任务。因此,浮点数转换定点数作为一种技术和方法,并没有一个特定的发明者或起源人,而是在计算机科学和数字信号处理领域的长期积累和发展中形成的。不同的研究者和工程师在不同的应用场景中对该技术进行了不同的改进和优化,推动了该技术的应用和发展。

软核开发

module top (
		input	wire	clk,
		input   wire 	rst_n,
		input 			The_uart_en_w,
		input	[31:0]	The_uart_data
);

		wire [31:0]    	B;
		wire [31:0]     C;
		wire 			valid;
		wire			complete;
		wire [23:0]	    intpart; //结果整数部分
		wire [23:0]	    decpart; //结果小数部分
		
		assign  B  =  32'd0;
		
decimals u_decimals(
	.clk				(clk					),
	.rst_n			    (rst_n					),
	.valid			    (valid					),		//转换开始使能
	.symbol			    (C[31]					),   	//符号位
	.exponent		    (C[30:23]				), 	//阶码
	.trail			    (C[22:0]				),	 	//尾数
	.intpart			(intpart				),		//结果整数部分
	.decpart			(decpart				),		//结果小数部分
	.complete		    (complete				)		//执行完成信号
);

floatingpoint32 u_floatingpoint32(
   .clk				    (clk					),
	.rst_n			    (rst_n					),
	.en				    (The_uart_en_w			),//数据转换开始
	.A					(The_uart_data			),
	.B					(B						),
	.C					(C						),
	.valid			    (valid					)
); 
	 
	 
	 
endmodule 
module decimals (
	input 		wire		clk,
	input		wire		rst_n,
	input					valid,
	input 					symbol,   	//符号位
	input [7:0]				exponent, 	//阶码
	input [22:0]			trail,	 	//尾数
	output reg [23:0]		intpart, 	//结果整数部分
	output reg [23:0]		decpart, 	//结果小数部分
	output reg 				complete 	//执行完成信号
);

//------------------------------------------------------------------------------
//----------- Registers decpartlarations -------------------------------------------
//------------------------------------------------------------------------------ 
	reg [3:0]		state					= 0;
	reg [5:0]		ctate					= 0;
	reg [2:0]		mimi					= 0;
	reg [2:0]       ter						= 0;
	reg [2:0]		trai_ter				= 0;
	reg [7:0]   	exponent_reg_d0   		= 0;					//寄存1
	reg [22:0]  	trail_reg_d0      		= 0;					//寄存1
	reg [23:0]  	trail_reg_d1      		= 0;					//寄存2
	reg [23:0]  	trail_reg_d2      		= 0;					//寄存2
	reg [23:0]		trail_reg_d3      		= 0;  					//寄存3
	reg [23:0]  	trail_reg_d4      		= 0;					//寄存4
	reg				trai1_one  				= 0;					//单位寄存
	reg				trai2_one  				= 0;					//单位寄存
	reg				trai3_one  				= 0;					//单位寄存
	reg				trai4_one  				= 0;					//单位寄存
	reg				trai5_one  				= 0;					//单位寄存
	reg				trai6_one  				= 0;					//单位寄存
	reg				trai7_one  				= 0;					//单位寄存
	reg				trai8_one  				= 0;					//单位寄存
	reg				trai9_one  				= 0;					//单位寄存
	reg				trail0_one 				= 0;					//单位寄存
	reg				trail1_one 				= 0;
	reg 			trail2_one 				= 0;
	reg 			trail3_one 				= 0;
	reg 			trail4_one 				= 0;
	reg 			trail5_one 				= 0;
	reg 			trail6_one 				= 0;
	reg 			trail7_one 				= 0;
	reg 			trail8_one 				= 0;
	reg 			trail9_one 				= 0;
	reg 			trai20_one 				= 0;
	reg 			trai21_one 				= 0;
	reg 			trai22_one 				= 0;
	reg 			trai23_one 				= 0;
	reg 			trai24_one 				= 0;
	reg				chic					= 0;
	reg				start					= 0;					//启动判断		
	reg				etart					= 0;					//启动判断
	reg 			judge					= 0;					//判断m有多少零
	reg [5:0]		cnt						= 0;
	reg [5:0]   	sum						= 0;
	reg [5:0]   	com						= 0;
	reg [5:0]   	count					= 0;
	reg [20:0]  	bucket					= 0;
	reg [5:0]   	select					= 0;
	reg [20:0]   	cno						= 0;
	reg [4:0]   	ltate					= 0;
	reg 		    a0,a1,a2,a3,a4,a5,a6,a7,a8,a9;
	reg			    a10,a11,a12,a13,a14,a15,a16,a17;
	reg			    a18,a19 				= 0;
	reg				ltate_done;
	reg [35:0]  	cno_ride 				= 0;
	reg [23:0]  	cno_ride_reg		    = 0;
	reg 			s0,s1 					= 0;
	reg				g0,g1 					= 0;
	
//------------------------------------------------------------------------------
//----------- Wires decpartlarations -----------------------------------------------
//------------------------------------------------------------------------------
	wire 			pos_valid;
	wire 			pos_chic;
	
//------------------------------------------------------------------------------
//----------- Local Parameters -------------------------------------------------
//------------------------------------------------------------------------------
	parameter		idle		=		'd0;	//初始化状态
	parameter		expo		=		'd1;	//阶码计算
	parameter		trai		=		'd2;  	//尾码计算
	parameter		trai_1		=		'd3;  	//尾码计算1
	parameter		trai_2   	=     	'd4;  	//尾码计算2
	parameter		trai_3		=		'd5;	//尾码计算3
	parameter		decparti    =     	'd6;  	//数据结合
	parameter		resu		=		'd7;	//结果
	parameter		stop		=		'd8;  	//结束
	
//------------------------------------------------------------------------------
//----------- Assign/Always Blocks ---------------------------------------------
//------------------------------------------------------------------------------
	assign		pos_valid		=	s0	&	(!s1);
	assign		pos_chic		=	g0	&	(!g1);
	
always @ (posedge clk or negedge rst_n) begin 
 if (!rst_n)
	begin 
		s0	<=	'd0;
		s1	<=	'd0;
	end 
 else 
	begin 
		s0	<=	valid;
		s1	<=	s0;
	end 
end 
	
always @ (posedge clk or negedge rst_n) begin 
 if (!rst_n)
	begin 
		g0	<=	'd0;
		g1	<=	'd0;
	end 
 else 
	begin 
		g0	<=	chic;
		g1	<=	g0;
	end 
end 
	
	//	判断多少 0
always @ (posedge clk or negedge rst_n) begin 
 if (!rst_n)
  begin 
	judge	<=	1'b0;
	mimi	<=	3'd0;
	ter     <=  3'd0;
	cnt		<=	6'd0;
  end 
 else 
  case (mimi)
	0:begin 
		if (start)
			mimi	<=	3'd1;
		else 
			mimi	<=	mimi;
	end 
	1:begin 
		if (ter == 5)
		 begin 
			ter		<=	3'd0;
			mimi	<=	3'd2;
		 end 
		else 
		 begin 
			ter		<=	ter	+	1'b1;
			cnt		<=	ctate;
		 end 
	end 
	2:begin 
		if (ter == 5)
		 begin 
			ter		<=	3'd0;
			mimi	<=	3'd0;
			judge   <=  1'b0;
		 end 
		else 
		 begin 
			judge	<=	1'b1;
			ter		<=	ter	+	1'b1;
		 end 
	end 
	default : ;
endcase 
end 
	
always @ (*) begin 
	if      (trail[0] == 1) ctate = 0; 
	else if (trail[1] == 1) ctate = 1; 
	else if (trail[2] == 1) ctate = 2; 
	else if (trail[3] == 1) ctate = 3; 
	else if (trail[4] == 1) ctate = 4; 
	else if (trail[5] == 1) ctate = 5; 
	else if (trail[6] == 1) ctate = 6; 
	else if (trail[7] == 1) ctate = 7; 
	else if (trail[8] == 1) ctate = 8;
	else if (trail[9] == 1) ctate = 9;
	else if (trail[10]== 1) ctate = 10;
	else if (trail[11]== 1) ctate = 11;
	else if (trail[12]== 1) ctate = 12;
	else if (trail[13]== 1) ctate = 13;
	else if (trail[14]== 1) ctate = 14;
	else if (trail[15]== 1) ctate = 15;
	else if (trail[16]== 1) ctate = 16;
	else if (trail[17]== 1) ctate = 17;
	else if (trail[18]== 1) ctate = 18;
	else if (trail[19]== 1) ctate = 19;
	else if (trail[20]== 1) ctate = 20;
	else if (trail[21]== 1) ctate = 21;
	else if (trail[22]== 1) ctate = 22;
end 
	
always @ (posedge clk or negedge rst_n) begin 
	if (!rst_n)
	 begin 
	    intpart   					<= 'd0;
		decpart   					<= 'd0;
		exponent_reg_d0     		<= 'd0;
		trail_reg_d0	    		<= 'd0;
		start						<= 'd0;
		sum				 			<= 'd0;
        trai1_one		 			<= 'd0;		//单位寄存
        trai2_one		 			<= 'd0;		//单位寄存
        trai3_one		 			<= 'd0;		//单位寄存
        trai4_one		 			<= 'd0;		//单位寄存
        trai5_one		 			<= 'd0;		//单位寄存
        trai6_one		 			<= 'd0;		//单位寄存
        trai7_one		 			<= 'd0;		//单位寄存
        trai8_one		 			<= 'd0;		//单位寄存
        trai9_one		 			<= 'd0;		//单位寄存
        trail0_one		 			<= 'd0;		//单位寄存
		trail1_one		 			<= 'd0;
		trail2_one		 			<= 'd0;
		trail3_one		 			<= 'd0;
		trail4_one		 			<= 'd0;
		trail5_one		 			<= 'd0;
		trail6_one		 			<= 'd0;
		trail7_one		 			<= 'd0;
		trail8_one		 			<= 'd0;
		trail9_one		 			<= 'd0;
		trai20_one		 			<= 'd0;
        trai21_one		 			<= 'd0;
        trai22_one		 			<= 'd0;
        trai23_one		 			<= 'd0;
        trai24_one		 			<= 'd0;
		trail_reg_d1	 			<= 'd0;
		trail_reg_d2   			    <= 'd0;
		count					    <= 'd0;
		trail_reg_d3	 			<= 'd0;
		trail_reg_d4	 			<= 'd0;
		sum				 			<= 'd0;
		chic				 		<= 'd0;
		cno_ride			 		<= 'd0;
		cno_ride_reg	 			<= 'd0;
		com				 			<= 'd0;
		state				 		<= idle;
		complete			 		<= 'd0;
		trai_ter					<= 'd0;
	 end 
	else 
	 case (state)
			idle:begin 
							if (pos_valid)
							 begin 
							   trai1_one		 <= 'd0;		//单位寄存
							   trai2_one		 <= 'd0;		//单位寄存
							   trai3_one		 <= 'd0;		//单位寄存
							   trai4_one		 <= 'd0;		//单位寄存
							   trai5_one		 <= 'd0;		//单位寄存
							   trai6_one		 <= 'd0;		//单位寄存
							   trai7_one		 <= 'd0;		//单位寄存
							   trai8_one		 <= 'd0;		//单位寄存
							   trai9_one		 <= 'd0;		//单位寄存
							   trail0_one		 <= 'd0;		//单位寄存
							   trail1_one		 <= 'd0;
							   trail2_one		 <= 'd0;
							   trail3_one		 <= 'd0;
							   trail4_one		 <= 'd0;
							   trail5_one		 <= 'd0;
							   trail6_one		 <= 'd0;
							   trail7_one		 <= 'd0;
							   trail8_one		 <= 'd0;
							   trail9_one		 <= 'd0;
							   trai20_one		 <= 'd0;
							   trai21_one		 <= 'd0;
							   trai22_one		 <= 'd0;
							   trai23_one		 <= 'd0;
							   trai24_one		 <= 'd0;
							   intpart   	     <= 'd0;
							   decpart   		 <= 'd0;
							   exponent_reg_d0   <= 'd0;
							   trail_reg_d0	     <= 'd0;
							   start			 <= 'd0;
							   etart			 <= 'd0;
							   sum				 <= 'd0;
							   count			 <= 'd0;
							   trail_reg_d1	     <= 'd0;
							   trail_reg_d2      <= 'd0;
							   trail_reg_d3	     <= 'd0;
							   trail_reg_d4	     <= 'd0;
							   cno_ride			 <= 'd0;
							   cno_ride_reg	     <= 'd0;
							   com				 <= 'd0;
							   state			 <= expo;
							   complete			 <= 'd0;
							   trai_ter			 <= 'd0;
							 end 
							else 
							 begin 
							   complete			 <= 'd0;
							   state			 <= idle;
							 end 
				end 
			expo:begin 
							if ( exponent >= 127 )
							 begin 
							    state			<= trai;
								exponent_reg_d0 <= exponent	- 127;
							 end 
							else 
								state			<= idle;
				end 
			trai:begin 
								start	<=	1'd1;
						    if (judge) 
							  begin //删除后面0
								trail_reg_d0	<=	trail>>cnt;
							    start           <= 1'd0;  
								state			<=	trai_1;
								sum				<=	23-cnt;
							  end 
							else 
								state			<=	trai;
				end 	
			trai_1:begin 
							if (trai_ter == 5)
								begin 
									trai_ter	<=	3'd0;
									state		<=	trai_2;
								end 
							else 
								begin 
									trai1_one	 <= 1'b1;
									trai2_one	 <= trail_reg_d0[22-cnt];
									trai3_one	 <= trail_reg_d0[21-cnt];
									trai4_one	 <= trail_reg_d0[20-cnt];
									trai5_one	 <= trail_reg_d0[19-cnt];
									trai6_one	 <= trail_reg_d0[18-cnt];
									trai7_one	 <= trail_reg_d0[17-cnt];
									trai8_one	 <= trail_reg_d0[16-cnt];
									trai9_one	 <= trail_reg_d0[15-cnt];
									trail0_one	 <= trail_reg_d0[14-cnt];
									trail1_one	 <= trail_reg_d0[13-cnt];
									trail2_one	 <= trail_reg_d0[12-cnt];
									trail3_one	 <= trail_reg_d0[11-cnt];
									trail4_one	 <= trail_reg_d0[10-cnt];
									trail5_one	 <= trail_reg_d0[9 -cnt];
									trail6_one	 <= trail_reg_d0[8 -cnt];
									trail7_one	 <= trail_reg_d0[7 -cnt];
									trail8_one	 <= trail_reg_d0[6 -cnt];
									trail9_one	 <= trail_reg_d0[5 -cnt];
									trai20_one	 <= trail_reg_d0[4 -cnt];
									trai21_one	 <= trail_reg_d0[3 -cnt];
									trai22_one	 <= trail_reg_d0[2 -cnt];
									trai23_one	 <= trail_reg_d0[1 -cnt];
									trai24_one	 <= trail_reg_d0[0 -cnt];
									state		 <=	trai_1;
									trai_ter	 <= trai_ter + 1'b1;
								end 
					end
			trai_2:begin 
							 if (count == exponent_reg_d0) begin 
								  trail_reg_d2 <= trail_reg_d1;
								 if (trail_reg_d2 == trail_reg_d1)
								  begin 
									state		 <= trai_3;
									count        <= count + 'd1;
								  end 
								 else 
								   trail_reg_d2 <= trail_reg_d1;
							 end 
							 else if (count <= exponent_reg_d0)  
								 count <= count + 'd1;
								  case (count)
									0 : trail_reg_d1[exponent_reg_d0 - count] <= trai1_one;
									1 : trail_reg_d1[exponent_reg_d0 - count] <= trai2_one;
									2 : trail_reg_d1[exponent_reg_d0 - count] <= trai3_one;
									3 : trail_reg_d1[exponent_reg_d0 - count] <= trai4_one;
									4 : trail_reg_d1[exponent_reg_d0 - count] <= trai5_one;
									5 : trail_reg_d1[exponent_reg_d0 - count] <= trai6_one;
									6 : trail_reg_d1[exponent_reg_d0 - count] <= trai7_one;
									7 : trail_reg_d1[exponent_reg_d0 - count] <= trai8_one;
									8 : trail_reg_d1[exponent_reg_d0 - count] <= trai9_one;
									9 : trail_reg_d1[exponent_reg_d0 - count] <= trail0_one;
									10: trail_reg_d1[exponent_reg_d0 - count] <= trail1_one;
									11: trail_reg_d1[exponent_reg_d0 - count] <= trail2_one;
									12: trail_reg_d1[exponent_reg_d0 - count] <= trail3_one;
									13: trail_reg_d1[exponent_reg_d0 - count] <= trail4_one;
									14: trail_reg_d1[exponent_reg_d0 - count] <= trail5_one;
									15: trail_reg_d1[exponent_reg_d0 - count] <= trail6_one;
									16: trail_reg_d1[exponent_reg_d0 - count] <= trail7_one;
									17: trail_reg_d1[exponent_reg_d0 - count] <= trail8_one;
									18: trail_reg_d1[exponent_reg_d0 - count] <= trail9_one;
									19: trail_reg_d1[exponent_reg_d0 - count] <= trai20_one;
									20: trail_reg_d1[exponent_reg_d0 - count] <= trai21_one;
									21: trail_reg_d1[exponent_reg_d0 - count] <= trai22_one;
									22: trail_reg_d1[exponent_reg_d0 - count] <= trai23_one;
									23: trail_reg_d1[exponent_reg_d0 - count] <= trai24_one;
								 default: ;
								endcase 
					end 		 
			trai_3:begin 
							if (count == sum+1)
								begin 
									state		 <=	decparti;
									trail_reg_d4 <= trail_reg_d3;
									com			 <= sum - exponent_reg_d0;
								end 
							else if ((count > exponent_reg_d0)&&(count<=sum))
							  count <= count + 'd1;
								case (count)
								0 : trail_reg_d3[sum]   <= trai1_one;
								1 : trail_reg_d3[sum-1] <= trai2_one;
								2 : trail_reg_d3[sum-2] <= trai3_one;
								3 : trail_reg_d3[sum-3] <= trai4_one;
								4 : trail_reg_d3[sum-4] <= trai5_one;
								5 : trail_reg_d3[sum-5] <= trai6_one;
								6 : trail_reg_d3[sum-6] <= trai7_one;
								7 : trail_reg_d3[sum-7] <= trai8_one;
								8 : trail_reg_d3[sum-8] <= trai9_one;
								9 : trail_reg_d3[sum-9] <= trail0_one;
								10: trail_reg_d3[sum-10]<= trail1_one;
								11: trail_reg_d3[sum-11]<= trail2_one;
								12: trail_reg_d3[sum-12]<= trail3_one;
								13: trail_reg_d3[sum-13]<= trail4_one;
								14: trail_reg_d3[sum-14]<= trail5_one;
								15: trail_reg_d3[sum-15]<= trail6_one;
								16: trail_reg_d3[sum-16]<= trail7_one;
								17: trail_reg_d3[sum-17]<= trail8_one;
								18: trail_reg_d3[sum-18]<= trail9_one;
								19: trail_reg_d3[sum-19]<= trai20_one;
								20: trail_reg_d3[sum-20]<= trai21_one;
								21: trail_reg_d3[sum-21]<= trai22_one;
								22: trail_reg_d3[sum-22]<= trai23_one;
								23: trail_reg_d3[sum-23]<= trai24_one;
							 default :;
							endcase 
					end 		
			decparti:begin 
							chic	<=	'd1;
							 if (ltate_done)
							  begin 
								chic     <= 'd0;
								cno_ride <= cno*1000; //扩展1000倍
								state    <= resu;
							  end 
							 else 
							    state    <= state;
					end 
			resu:begin 
							cno_ride_reg <= cno_ride>>20;
							state		 <= stop;
				end 
			stop:begin 
							count				<=	'd0;
							intpart				<=	trail_reg_d2;
							decpart				<= cno_ride_reg;
							complete			<= 'd1;
							state				<=	idle;
				end 
		default: ;
	endcase 
end 

always @ (*) begin 
	case (select)
		// 小数放大 33554432 倍   (25个)
		 0:	bucket	=	524288;			//0.5
		 1:	bucket	=	262144;			//0.25
		 2:	bucket	=	131072;			//0.125
		 3:	bucket	=	65536;			//0.0625
		 4:	bucket	=	32768;			//0.03125
		 5:	bucket	=	16384;			//0.015625
		 6:	bucket	=	8192;			//0.0078125
		 7:	bucket	=	4096;			//0.00390625
		 8:	bucket	=	2048;			//0.001953125
		 9:	bucket	=	1024;			//0.0009765625
		 10:bucket	=	512;			//0.00048828125
		 11:bucket  =   256;			//0.000244140625
		 12:bucket	=	128;			//0.0001220703125
		 13:bucket	=	64;				//0.00006103515625
		 14:bucket	=	32;				//0.000030517578125
		 15:bucket	=	16;				//0.0000152587890625
		 16:bucket	=	8;				//7.62939453125e-6
		 17:bucket	=	4;
		 18:bucket	=	2;
		 19:bucket	=	1;
		 20:bucket  =   0;
		default:;
	endcase 
end 
	
always @ (posedge clk or negedge rst_n) begin 
 if (!rst_n)
  begin 
	cno	       <= 'd0;
	select     <= 'd20;
	ltate      <= 'd0;
	a0         <= 'd0;
	a1         <= 'd0;
	a2         <= 'd0;
	a3         <= 'd0;
	a4         <= 'd0;
	a5         <= 'd0;
	a6         <= 'd0;
	a7         <= 'd0;
	a8         <= 'd0;
	a10        <= 'd0;
	a11        <= 'd0;
	a12        <= 'd0;
	a13        <= 'd0;
	a14        <= 'd0;
	a15        <= 'd0;
	a16        <= 'd0;
	a17        <= 'd0;
	a18	       <= 'd0;
	a19	       <= 'd0;
	ltate_done <= 'd0;
  end 
 else 
  case (ltate)
  //8'b1000_1100;
  0: begin 
			if (pos_chic)
			  begin 
				cno		<= 'd0;
				select	<= 'd20;
				ltate 	<= 'd1;
				a0    	<= 'd0;
				a1    	<= 'd0;
				a2    	<= 'd0;
				a3    	<= 'd0;
				a4    	<= 'd0;
				a5    	<= 'd0;
				a6    	<= 'd0;
				a7    	<= 'd0;
				a8    	<= 'd0;
				a10   	<= 'd0;
				a11   	<= 'd0;
				a12   	<= 'd0;
				a13   	<= 'd0;
				a14   	<= 'd0;
				a15   	<= 'd0;
				a16   	<= 'd0;
				a17   	<= 'd0;
				a18		<= 'd0;
				a19		<= 'd0;
			  end 
			else 
			  begin 
			   ltate_done <= 'd0;
			   ltate	  <= 'd0;
			  end 
	 end 
  1: if (trail_reg_d4[com-1] == 1) begin ltate <= 'd21; select <= 'd0; a0 <= 'd1; end  
	 else begin a0   <= 'd0; ltate <= 'd2; end 
  2: if (trail_reg_d4[com-2] == 1) begin ltate <= 'd21; select <= 'd1; a1 <= 'd1; end  
	 else begin a1   <= 'd0;ltate  <= 'd3; end 
  3: if (trail_reg_d4[com-3] == 1)begin  ltate <= 'd21; select <= 'd2; a2 <= 'd1; end  
	 else begin a2   <= 'd0;ltate  <= 'd4; end  
  4: if (trail_reg_d4[com-4] == 1) begin ltate <= 'd21; select <= 'd3; a3 <= 'd1; end  
	 else begin a3   <= 'd0;ltate  <= 'd5; end 
  5: if (trail_reg_d4[com-5] == 1)begin ltate  <= 'd21; select <= 'd4; a4 <= 'd1; end  
	 else begin a4   <= 'd0;ltate  <= 'd6; end  
  6: if (trail_reg_d4[com-6] == 1)begin ltate  <= 'd21; select <= 'd5; a5 <= 'd1; end  
	 else begin a5   <= 'd0;ltate  <= 'd7; end 
  7: if (trail_reg_d4[com-7] == 1)begin ltate  <= 'd21; select <= 'd6; a6 <= 'd1; end  
	 else begin a6   <= 'd0;ltate  <= 'd8; end  
  8: if (trail_reg_d4[com-8] == 1)begin ltate  <= 'd21; select <= 'd7; a7 <= 'd1; end  
	 else begin a7   <= 'd0;ltate  <= 'd9; end 
  9: if (trail_reg_d4[com-9] == 1)begin ltate  <= 'd21; select <= 'd8; a8 <= 'd1; end  
	 else begin a8   <= 'd0;ltate  <= 'd10;end 
  10: if (trail_reg_d4[com-10] == 1)begin ltate<= 'd21; select <= 'd9; a9 <= 'd1; end  
	  else begin a9  <= 'd0;ltate  <= 'd11;end 
  11: if (trail_reg_d4[com-11] == 1)begin ltate<= 'd21; select <= 'd10; a10<= 'd1;end  
	  else begin a10 <= 'd0;ltate <= 'd12; end 
  12: if (trail_reg_d4[com-12] == 1)begin ltate<= 'd21; select <= 'd11; a11<= 'd1;end  
	  else begin a11 <= 'd0;ltate <= 'd13; end 
  13: if (trail_reg_d4[com-13] == 1)begin ltate<= 'd21; select <= 'd12; a12<= 'd1;end  
	  else begin a12 <= 'd0;ltate <= 'd14; end 
  14: if (trail_reg_d4[com-14] == 1)begin ltate<= 'd21; select <= 'd13; a13<= 'd1;end  
	  else  begin a13<= 'd0;ltate <= 'd15; end 
  15: if (trail_reg_d4[com-15] == 1)begin ltate<= 'd21; select <= 'd14; a14<= 'd1;end  
	  else begin a14 <= 'd0;ltate <= 'd16; end 
  16: if (trail_reg_d4[com-16] == 1)begin ltate<= 'd21; select <= 'd15; a15<= 'd1;end  
	  else begin a15 <= 'd0;ltate <= 'd17; end  
  17: if (trail_reg_d4[com-17] == 1)begin ltate<= 'd21; select <= 'd16; a16<= 'd1;end  
	  else  begin a16<= 'd0;ltate <= 'd18; end  
  18: if (trail_reg_d4[com-18] == 1)begin ltate<= 'd21; select <= 'd17; a17<= 'd1;end  
	  else begin  a17<= 'd0;ltate <= 'd19; end 
  19: if (trail_reg_d4[com-19] == 1) begin ltate<= 'd21; select<= 'd18; a18<= 'd1;end  
	  else begin a18 <= 'd0;ltate <= 'd20; end 
  20: if (trail_reg_d4[com-20] == 1)begin ltate <= 'd21; select<= 'd19; a19<= 'd1;end  
	  else  begin a19<= 'd0;ltate <= 'd22; end 
  21: begin 
		if      (a0)  begin ltate <= 'd2; a0 <='d0;  cno <= cno + bucket; end 
		else if (a1)  begin ltate <= 'd3; a1 <='d0;	cno <= cno + bucket; end 
		else if (a2)  begin ltate <= 'd4; a2 <='d0;	cno <= cno + bucket; end 
		else if (a3)  begin ltate <= 'd5; a3 <='d0;	cno <= cno + bucket; end 
		else if (a4)  begin ltate <= 'd6; a4 <='d0;	cno <= cno + bucket; end 
		else if (a5)  begin ltate <= 'd7; a5 <='d0;	cno <= cno + bucket; end 
		else if (a6)  begin ltate <= 'd8; a6 <='d0;	cno <= cno + bucket; end 
		else if (a7)  begin ltate <= 'd9; a7 <='d0;	cno <= cno + bucket; end 
		else if (a8)  begin ltate <= 'd10;a8 <='d0;	cno <= cno + bucket; end 
		else if (a9)  begin ltate <= 'd11;a9 <='d0;	cno <= cno + bucket; end 
		else if (a10) begin ltate <= 'd12;a10<='d0;	cno <= cno + bucket; end 
		else if (a11) begin ltate <= 'd13;a11<='d0;	cno <= cno + bucket; end 
		else if (a12) begin ltate <= 'd14;a12<='d0;	cno <= cno + bucket; end 
		else if (a13) begin ltate <= 'd15;a13<='d0;	cno <= cno + bucket; end 
		else if (a14) begin ltate <= 'd16;a14<='d0;	cno <= cno + bucket; end 
		else if (a15) begin ltate <= 'd17;a15<='d0;	cno <= cno + bucket; end 
		else if (a16) begin ltate <= 'd18;a16<='d0;	cno <= cno + bucket; end 
		else if (a17) begin ltate <= 'd19;a17<='d0;	cno <= cno + bucket; end 
		else if (a18) begin ltate <= 'd20;a18<='d0;	cno <= cno + bucket; end 
		else if (a19) begin ltate <= 'd22;a19<='d0;	cno <= cno + bucket; end 
		else begin cno <= cno + bucket;  ltate <= ltate; end 
	  end 
  22: begin ltate_done <= 'd1; if (state    == resu) ltate<= 'd0; else ltate <= ltate; end 
	 
	default: ;
 endcase 
end 	


endmodule 
// 二进制数转浮点数 , A或B 使用一个即可
module floatingpoint32(
   input clk,
	input rst_n,
	input en,
   input[31:0] A,B,
   output reg [31:0] C,
	output reg			valid
    ); 

   reg[7:0] AE=0,BE=0,CE=0,DE=0;
   reg AS=0,BS=0,CS=0,DS=0;
   reg[24:0] AF,BF,CF;
   reg[7:0] index=0;
	reg drag_0,drag_1,drag_2;
	
	integer in;
	integer tem;
   always @(A or B)//上升沿触发
   begin
      if(en==1)
         begin
	         AE=A[30:23];      //  阶码8位   用的是移码表示   +127 01111111
	         AF={2'b01,A[22:0]};//把缺省的1给补上    同时考虑做加法时  有可能产生进位  所以补齐23+1+1  共25位
	         AS=A[31];         //   最高位  即符号位
	         BE=B[30:23];
	         BF={2'b01,B[22:0]};
	         BS=B[31];

            //如果某一方阶码大,则符号位一定是在大的一方
	         if (A[30:0]==0)                 //如果A是0,则结果就是B了
	             begin
		               CS=BS;
		               CE=BE;
		               CF=BF;
	             end
	         else if(B[30:0]==0)             //如果B是0,则结果就是A了
                begin
	               	CS=AS;
	               	CE=AE;
	               	CF=AF;
	             end
            else                             //A、B都不为0的情况下
	             begin
	                if(AE>BE)                  //A的阶码大于B的阶码
	                 	 begin
		               	CE=AE;                //对阶
			               DE=AE-BE;             //阶码做差
			               BF=(BF>>DE);         //右移
			               if (AS==BS)        //如果A、B符号位相同,就做加法
			                   begin
			                          	CS=AS;
			                        	CF=AF+BF;
		                      end
		                  else if (AS!=BS)        //如果A、B的符号位不相同,就做减法
		                      begin
			                         	CS=AS;
			                        	CF=AF-BF;
		                      end
	               	 end
	            	 else if(AE<BE)              //A的阶码小于B的阶码
		                begin
			                CE=BE;               //对阶
			                DE=BE-AE;            //阶码做差
			                AF=(AF>>DE);         //右移
			                if (AS==BS)          //如果A、B符号位相同,就做加法
			                    begin
				                     CS=AS;
			                     	CF=AF+BF;
			                    end
			                else if (AS!=BS)          //如果A、B的符号位不相同,就做减法
			                    begin
			                       	CS=BS;
			                     	CF=BF-AF;
			                    end
		                end
		            else if(AE==BE)             //A的阶码等于B阶码
		                begin
			                CE=AE;               //首先确定结果的阶码
			                if (AS==BS)          // 如果A、B符号位相同,就做加法
		                  	  begin
				                    CS=AS;
			                  	  CF=AF+BF;
		                       end
		              	    else                 //如果A、B的符号位不相同,就做减法
			                    begin
			                  	  CS=BS;
			                       CF=BF-AF;
			                    end
		                end
		            //归一化,寻找CF第一个1,并将其前移
		           in=24;
		           tem=0;
		           while(tem==0&&in>=0)
		              begin
			              if(CF[in]!=0)//如果尾数最高位CF[24]即尾数第25位不为0,则可以进行尾数规一化。
			                 begin
				                  tem=1;
				                  index=in;     //index指示的就是尾数为1的那位 就是CF[index]=1
			                 end
			              in=in-1;         //这里要注意在找到index之后  in还会减一次
		              end                   //while循环这里结束了
		    if(in<0)//相加得0或者相减的结果就是0  也是指尾数运算结果为25位的0
		        begin
			        CS=0;        //运算结果就是0了
			        CE=0;
			        CF=0;
		        end
		    else if(index==24)//如果在24位,则尾码右移,阶码加一
		        begin
			        CE=CE+8'b1;   //结果C的阶码要加1
			        CF=(CF>>1);   //尾数要向右移一位    1.xxxxxxxxxx
		        end
          else		       
			     begin             
		       	  index=8'd23-index;    //index指示的是需要移位数
			        CE=CE-index;          //阶码减去左移的尾数
			        CF=(CF<<index);        //结果C的尾数要向左移index位
		        end
	                    end//
	          C={CS,CE,CF[22:0]};// 最终结果为32位  cs符号位  ce阶码   cf位尾数   忽略尾数最高的1  
	
         end//   if(en==1)
   end   //always@(A or B)
	 
	 
	always @ (posedge clk or negedge rst_n) begin 
	 
		if (!rst_n)
		 begin 
			drag_0	<=	'd0;
			drag_1	<=	'd0;
			drag_2	<=	'd0;
		 end 
		else 
		 begin 
			drag_0	<=	en;
			drag_1	<=	drag_0;
			drag_2	<=	drag_1;
	    end 
	end 
	
	always @ (posedge clk or negedge rst_n) begin 
	 
		if (!rst_n)
			valid	<=	'd0;
		else 
			valid	<=	drag_2;
	
	end 

endmodule

实验结果

 结果对比

 使用eLinx工具进行开发

 

module FD(
	input	wire	clk
    );
	
	wire [31:0]	dataa = 520;
	wire [31:0]	result;
	
	fp_convert_1 u_fp_convert_1(
	.clock					(clk),
	.dataa					(dataa),
	.result					(result)
	);
endmodule

实验结果

 结果对比

 结论

        精度和范围:定点数的精度和范围是固定的,所以在进行浮点数到定点数转换时,需要确定合适的位宽和小数点位置,以满足应用的精度要求和数值范围。舍入方式:在进行浮点数到定点数转换时,需要选择适当的舍入方式。常见的舍入方式包括向上舍入、向下舍入、四舍五入等。选择合适的舍入方式可以在精度和性能之间进行权衡。运算延迟:定点数运算的延迟可能与浮点数运算有所不同。在进行浮点数到定点数转换后,需要对设计进行重新评估,以确保其满足时序要求。特别是在高速计算或实时系统中,这一点尤为重要。协议和接口:如果你的设计需要与其他模块或设备进行通信,确保你了解并遵循相应的协议和接口规范。有时,浮点数转换定点数可能涉及到数据格式的转换和解析,这需要与其他模块进行正确的数据交互。测试和验证:在进行浮点数转换定点数的设计时,确保进行充分的测试和验证。这包括对各种输入情况和边界条件进行测试,以确保转换的正确性和性能。优化和资源利用:定点数表示通常可以更好地利用 FPGA 的资源,但需要注意设计中的资源使用情况。在进行浮点数转换定点数时,考虑优化算法和数据结构,以提高资源利用率并满足性能要求。

参考资料

  1. 《数字信号处理》(Digital Signal Processing)一书,作者:John G. Proakis, Dimitris G. Manolakis。该书介绍了数字信号处理的基本概念和技术,包括浮点数和定点数表示以及相应的转换方法。

  2. 《嵌入式系统设计与优化》(Embedded Systems Design and Optimization)一书,作者:Wayne Wolf。该书讲解了在嵌入式系统中使用定点数进行计算的技术和优化方法,包括浮点数转换定点数的相关内容。

  3. 《FPGA原理与应用》(FPGA Prototyping by VHDL Examples)一书,作者:Pong P. Chu。该书介绍了在FPGA上进行数字系统设计的基本原理和方法,其中包括浮点数转换定点数的实现和应用。

  4. IEEE标准754-2008《二进制浮点算术标准》(IEEE Standard for Floating-Point Arithmetic)是关于二进制浮点数算术的国际标准。它详细描述了浮点数的表示方法、转换规则以及相应的运算规范。

  5. 学术论文和研究文章:可以通过学术搜索引擎如Google Scholar、IEEE Xplore等搜索相关的学术论文和研究文章,了解最新的浮点数转换定点数的研究成果和应用实践。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/869321.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

win10安装mysql和c++读取调用举例

一、下载mysql8.rar解压到C盘(也可以解压到其他位置) 在系统环境变量添加JAVA_HOMEC:\myslq8&#xff0c;并在path中添加%JAVA_HOME%\bin; 二、以管理员身份进入命令窗口 三、修改配置文件指定安装路径和数据库的存放路径 四、键入如下命令初始化并启动mysql服务,然后修改登录…

Linux下grep通配容易混淆的地方

先上一张图: 我希望找到某个版本为8的一个libXXX.8XXX.so ,那么应该怎么写呢? 先看这种写法对不对: 是不是结果出乎你的意料之外? 那么我们来看一下规则: 这里的 "*" 表示匹配前一个字符的零个或多个 于是我们就不难理解了: lib*8*.so 表示 包…

一篇文章搞懂如何使用JDBC操作数据库,还有JDBC进阶操作

目录 简介什么是JDBC如何使用JDBC1、获取连接2、操作数据3、关闭连接&#xff0c;释放资源使用技巧 查询操作创建表&#xff0c;插入模拟数据使用Java查询数据的数据SQL注入问题使用PreparedStatement查询 更新操作插入插入并获取主键更新删除 JDBC事务JDBC的批量操作JDBC连接池…

微服务与Nacos概述-5

引入OpenFeign 添加依赖&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><groupId>com.alibaba.cloud</groupId>…

【Linux】CentOS7.3环境下安装Mysql 8.0.30

CentOS7.3环境下安装Mysql 8.0.30 1.mysql官网下载安装包 下载地址&#xff1a;https://downloads.mysql.com/archives/community/ 2.上传安装包到linux服务器并解压 tar -vxf mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz修改名称 mv mysql-8.0.30-linux-glibc2.12-x86_6…

中心极限定理例题

关于大数定律的两个题目。 例1 注意牢记公式&#xff1a; P { X } P { ∑ i 1 n x i − n μ n σ < x } ∫ − ∞ x e − x 2 2 d x 2 π P\{ X\} P \{\frac { \sum_{i1}^{n} x_i - n \mu}{\sqrt {n} \sigma} < x \} \frac {\int _{-\infty} ^{x} e ^{- \frac {x^…

keil下载程序具体过程3:从jlink的log开始

引言 本篇文章开始&#xff0c;跟着jlink的log&#xff0c;我们将跟踪镜像文件具体的下载过程。 一、jlink的log 下面的是keil点击Download按钮输出的log。 FromELF: creating hex file... ".\Objects\Project.axf" - 0 Error(s), 0 Warning(s). Build Time Elapsed…

C++ 语言的输入与输出

C 标准 I/O 库包含 iostream、fstream 和 sstringstream。iostream、fstream 比较常用&#xff0c;一般操作于输入和输出&#xff0c;相较于前两者来说 sstringstream 的出现频率就低了许多&#xff0c;一般操作于数据的格式化。为了能更好的理解 C 语言的标准 I/O 库&#xff…

通过MATLAB自动产生Hamming编译码的verilog实现,包含testbench

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 1. 原理 1.1 编码规则 1.2 错误检测和纠正 2. 实现过程 2.1 编码过程 2.2 解码过程 3. 应用领域 3.1 数字通信 3.2 存储系统 3.3 ECC内存 3.4 数据传输 5.算法完整程序工程 1.算法…

MySQL多表关联查询

目录 1. inner join&#xff1a; 2. left join&#xff1a; 3. right join&#xff1a; 4.自连接 5.交叉连接&#xff1a; 6、联合查询 7、子查询 1. inner join&#xff1a; 代表选择的是两个表的交差部分。 内连接就是表间的主键与外键相连&#xff0c;只取得键值一致…

全国各地区数字经济工具变量-文本词频统计(2002-2023年)

数据简介&#xff1a;本数据使用全国各省工作报告&#xff0c;对其中数字经济相关的词汇进行词频统计&#xff0c;从而构建数字经济相关的工具变量。凭借数字经济政策供给与数字经济发展水平的相关系数的显著性作为二者匹配程度的划分依据&#xff0c;一定程度上规避了数字经济…

Vue.js2+Cesium1.103.0 九、淹没分析效果

Vue.js2Cesium1.103.0 九、淹没分析效果 Demo <template><divid"cesium-container"style"width: 100%; height: 100%;"><spanid"button"style"position: absolute; right: 50px; top: 50px; z-index: 999; font-size: 24px…

旅卦-火山旅

前言&#xff1a;人生就像一趟旅行&#xff0c;为谋生奔波也是旅&#xff0c;旅是人生的常态&#xff0c;我们看一下易经里的旅卦&#xff0c;分析下卦辞和爻辞以及自己的理解。 目录 卦辞 爻辞 总结 卦辞 旅&#xff1a;小亨&#xff0c;旅贞吉。 卦序&#xff1a;穷大者…

oracle 增加控制文件

oracle 增加控制文件 1、看control_file路径 SQL> show parameter controlNAME TYPE VALUE ------------------------------------ ----------- ------------------------------ control_file_record_keep_time integer …

Grafana展示k8s中pod的jvm监控面板/actuator/prometheus

场景 为保障java服务正常运行&#xff0c;对服务的jvm进行监控&#xff0c;通过使用actuator组件监控jvm情况&#xff0c;使用prometheus对数据进行采集&#xff0c;并在Grafana展现。 基于k8s场景 prometheus数据收集 配置service的lable&#xff0c;便于prometheus使用labl…

大数据课程I2——Kafka的架构

文章作者邮箱&#xff1a;yugongshiyesina.cn 地址&#xff1a;广东惠州 ▲ 本章节目的 ⚪ 掌握Kafka的架构&#xff1b; ⚪ 掌握Kafka的Topic与Partition&#xff1b; 一、Kafka核心概念及操作 1. producer生产者&#xff0c;可以是一个测试线程&#xff0c;也…

青大数据结构【2014】

一、单选 二、简答 为了解决顺序队列的假溢出问题,提出了循环队列,即把存储队列的表从逻辑上看成一个环 判别队列空和满有三种方法: 1)采用计数器判别,空时,计数器为0;满时,计数器为maxsize; 2)另设一个布尔变量以匹配队列的满和空; 3)少用一个元素的空间,约…

无向图邻接矩阵(C++ 代码)

#include<iostream>//无向图邻接矩阵 #define mvnum 100 using namespace std; typedef char Vertextype;//顶点数据类型 typedef int Arctype;//边权值类型 typedef struct {Vertextype vexs[mvnum];//顶点表Arctype arcs[mvnum][mvnum];//邻接矩阵int vexnum, arcnum;/…

解决macOS执行fastboot找不到设备的问题

背景 最近准备给我的备用机Redmi Note 11 5G刷个类原生的三方ROM&#xff0c;MIUI实在是用腻了。搜罗了一番&#xff0c;在XDA上找到了一个基于Pixel Experience开发的ROM&#xff1a;PixelExperience Plus for Redmi Note 11T/11S 5G/11 5G/POCO M4 Pro 5G (everpal)&#xf…

JavaScript激活严格模式

在JavaScript中&#xff0c;严格模式是一种特殊的模式&#xff0c;通过’use strict’;去激活严格模式&#xff01;在 JavaScript 中&#xff0c;“use strict” 是一种指令&#xff0c;表示在代码运行时启用严格模式&#xff0c;从而禁止使用一些不安全或者不规范的语法&#…