马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
最近一个项目需要用到fp16乘法器,网上找到一段代码但是其中有些疑问,求大佬赐教
第一个问题是exponent = floatA[14:10] + floatB[14:10] - 5'd15 ;这里为什么是减15,不是减30
第二个问题是尾数规格化代码为啥还要考虑到后边的20到12位
- module floatMult (floatA,floatB,product);
-
- input [15:0] floatA, floatB;
- output reg [15:0] product;
-
- reg sign;
- reg signed [5:0] exponent; //6th bit is the sign
- reg [9:0] mantissa;
- reg [10:0] fractionA, fractionB; //fraction = {1,mantissa}
- reg [21:0] fraction;
-
-
- always [url=home.php?mod=space&uid=1801890]@[/url] (floatA or floatB) begin
- if (floatA == 0 || floatB == 0) begin
- product = 0;
- end else begin
- sign = floatA[15] ^ floatB[15];//异或,相异为1
- exponent = floatA[14:10] + floatB[14:10] - 5'd15 ;
-
- fractionA = {1'b1,floatA[9:0]};//显示出隐藏位1来计算
- fractionB = {1'b1,floatB[9:0]};
- fraction = fractionA * fractionB;
-
- if (fraction[21] == 1'b1) begin//规格化,将隐藏位再次隐藏起来
- fraction = fraction << 1;
- exponent = exponent + 1;
- end else if (fraction[20] == 1'b1) begin
- fraction = fraction << 2;
- exponent = exponent + 0;
- end else if (fraction[19] == 1'b1) begin
- fraction = fraction << 3;
- exponent = exponent - 1;
- end else if (fraction[18] == 1'b1) begin
- fraction = fraction << 4;
- exponent = exponent - 2;
- end else if (fraction[17] == 1'b1) begin
- fraction = fraction << 5;
- exponent = exponent - 3;
- end else if (fraction[16] == 1'b1) begin
- fraction = fraction << 6;
- exponent = exponent - 4;
- end else if (fraction[15] == 1'b1) begin
- fraction = fraction << 7;
- exponent = exponent - 5;
- end else if (fraction[14] == 1'b1) begin
- fraction = fraction << 8;
- exponent = exponent - 6;
- end else if (fraction[13] == 1'b1) begin
- fraction = fraction << 9;
- exponent = exponent - 7;
- end else if (fraction[12] == 1'b0) begin
- fraction = fraction << 10;
- exponent = exponent - 8;
- end
-
- mantissa = fraction[21:12];
- if(exponent[5]==1'b1) begin //exponent is negative 1.exponent太大了溢出;2.exponent太小了,忽略,直接等于0
- product=16'b0000000000000000;
- end
- else begin
- product = {sign,exponent[4:0],mantissa};
- end
- end
- end
-
- endmodule
复制代码
|