1

If I try to use calculations with infinity results (in float32 representation) there are some unexpected results, as if the calculations were in double precision. Simple casts to shortreal doesn't work, only conversion to bits and back again.

module tb_inf;
  shortreal tmp1, tmp2, tmp3;
  initial begin
    tmp1 = 2e30;
    tmp2 = 1e30;
    tmp3 = shortreal'(tmp1*tmp2);
    $display("inf test1 %e ", shortreal'(tmp3));
    $display("inf test2 %e ", $bitstoshortreal($shortrealtobits(tmp3)));
  end
endmodule

In Vivado and modelsim I get:

inf test1 2e60
inf test2 inf

Is the second way of conversion is best for correct calculations with float?

Mogwaika
  • 46
  • 7
  • "*Is the second way of conversion is best for correct calculations with float?*" what does this mean? – Fra93 Jul 14 '22 at 15:09

1 Answers1

0

I tested out your code, and apparently the cast to shortreal does not work, and not even the initialization. I tried to init both of them to 1e60 and the result after the multiplcation was 1e120, which deos not make sense. The Verilog spec also refers to shortreal as "C float" but this is not the same. I think that the implementation is compiler specific, and you should use the bit casting everytime you do an operation.

// Code your testbench here
// or browse Examples
module tb_inf;
  
  shortreal tmp1;
  shortreal tmp2;
  shortreal tmp3;// tmp2, tmp3;
  initial begin
    tmp1 = 2e60; <-- should already be wrong...
    tmp2 = 1e60;
    $cast (tmp3,tmp1*tmp2);
    $display("inf test1 %e ",tmp3 );
    //$display("inf test2 %e ", $bitstoshortreal($shortrealtobits(tmp3)));
  end
endmodule

Fixed:

module tb_inf;
  
  shortreal tmp1;
  shortreal tmp2;
  shortreal tmp3;// tmp2, tmp3;
  initial begin
    tmp1 = $bitstoshortreal($shortrealtobits(2e30));
    tmp2 = $bitstoshortreal($shortrealtobits(1e30));
    tmp3 = $bitstoshortreal($shortrealtobits(tmp1*tmp2))
    $display("inf test1 %e ",tmp3 );
    //$display("inf test2 %e ",);
  end
endmodule

This shows inf.

Fra93
  • 1,992
  • 1
  • 9
  • 18