我需要在有符号定点乘法器中提取哪些位?

问题描述 投票:0回答:1

我需要在 Verilog 中设计一个定点乘法器,它采用 16 个 bif,格式为 1 个符号位、6 个整数位和 7 个小数位。我只是不知道要提取哪些位以确保准确的答案。

module signed_fixed_point_multiplier(
    input signed [15:0] a,
    input signed [15:0] b,
    output signed [15:0] result
);
    wire sign_bit;
    wire [6:0] a_dec, b_dec;
    wire [7:0] a_frac, b_frac;
    wire signed [31:0] temp_result;

    
    assign sign_bit = a[15] ^ b[15]; // XOR the sign bits to get the sign
    assign signed temp_result = a * b; // Multiply the fixed-point numbers
    always @(*) begin
        if (temp_result[31] != temp_result[30]) begin
            // Overflow occurred if the sign bit of the result is not the same as the bit before it
            overflow = 1;
        end else begin
            overflow = 0;
        end
    end
endmodule
verilog fixed-point
1个回答
0
投票

该帖子在位数方面存在内部问题,1 个符号位 + 6 个整数位 + 7 个小数位 = 14 位,但是您有一个 16 位总线。我将根据 16 位、带符号、8 位小数给出答案。

如果 16 位乘以 16 位,结果就是 32 位。您可以搜索“定点乘法”或查看我的Zip Cpu Bit Grownt

在输出中使用少于 32 位会导致精度损失。

使用32位输出时不会溢出。

如果您使用 32 位输出的切片,则 LSB 的精度会损失,并且 MSB 可能会饱和。可以做出相关的设计选择(MSB 处的饱和或翻转、LSB 处的舍入或截断)。

这是一个示例,我计算全精度 mul(输出 c),并抓取全精度的切片,然后通过按 2^NUMBER_FRACTIONAL_BITS 缩放将结果解释为定点数。我选择了一个向左对齐的切片,这样就不可能出现饱和或翻转,并且我在 LSB 处截断了。

RTL

module sfpm
  (
    input  signed [15:0] a,
    input  signed [15:0] b,
    output signed [31:0] c,
    output signed [15:0] result
  );

  assign c = a * b;
  assign result = c[31:16];
  
endmodule

测试台

module tb ();
  
  logic signed [15:0] a,b,result;
  logic signed [31:0] c;
  
  localparam NUM_FRACT_BITS_INPUT = 8;
  localparam NUM_FRACT_BITS_OUTPUT = NUM_FRACT_BITS_INPUT * 2;
  
  initial begin
    $dumpfile("dump.vcd"); $dumpvars;
    // a vector
    a = 1.5*(2**8);
    b = 1.5*(2**8);
    #1;
    // scale and print
    $display("a b c result-> %f %f %f %f", 
             $itor(a)/(2**8),$itor(b)/(2**8),$itor(c)/(2**16),$itor(result));
    $finish;
  end
  
  // design under test instance
  sfpm dut(.*);

结果

a b c result-> 1.500000 1.500000 2.250000 2.000000

您可以看到当我们截断到小数点右侧时所导致的精度损失。


您似乎不清楚如何解释输出答案。 如果有 N 个小数位 * M 个小数位,则结果将有 M + N 个小数位。在我的示例中,有 16 个小数位。

回到帖子文本中表达的数字,

16 bif 格式为 1 个符号位、6 个整数位和 7 个小数位 全精度结果将有

© www.soinside.com 2019 - 2024. All rights reserved.