Skip to content

Commit

Permalink
Verilog: add rounding mode to casts to real/shortreal
Browse files Browse the repository at this point in the history
Casts to real/shortreal may need to round, hence add the rounding mode
during lowering.
  • Loading branch information
kroening committed Dec 20, 2024
1 parent f457b5e commit 0e054f1
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 1 deletion.
8 changes: 8 additions & 0 deletions regression/verilog/expressions/cast_to_real1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
cast_to_real1.sv
--bound 0
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
--
11 changes: 11 additions & 0 deletions regression/verilog/expressions/cast_to_real1.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module main;

// no rounding
p0: assert final (real'(0) == 0.0);
p1: assert final (real'(1) == 1.0);

// rounding
p2: assert final (real'('hffff_ffff_ffff_ffff) == real'('h1_0000_0000_0000_0000));
p3: assert final (real'(-'sh0_ffff_ffff_ffff_ffff) == real'(-'sh1_0000_0000_0000_0000));

endmodule
9 changes: 9 additions & 0 deletions regression/verilog/expressions/cast_to_real2.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
KNOWNBUG
cast_to_real2.sv
--bound 0
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
--
Typechecking yields inconsistent types.
5 changes: 5 additions & 0 deletions regression/verilog/expressions/cast_to_real2.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module main;

p0: assert final (real'(-1) == -1.0);

endmodule
9 changes: 9 additions & 0 deletions regression/verilog/expressions/cast_to_real3.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
KNOWNBUG
cast_to_real3.sv
--bound 0
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
--
The flattening solver fails with a width error.
5 changes: 5 additions & 0 deletions regression/verilog/expressions/cast_to_real3.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module main;

p0: assert final (real'(1'b1) == 1);

endmodule
14 changes: 13 additions & 1 deletion src/verilog/verilog_lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected]
#include <util/arith_tools.h>
#include <util/bitvector_expr.h>
#include <util/c_types.h>
#include <util/floatbv_expr.h>
#include <util/ieee_float.h>

#include "aval_bval_encoding.h"
Expand Down Expand Up @@ -272,7 +273,18 @@ exprt verilog_lowering(exprt expr)
}
}

return expr;
// Cast to float? Turn into floatbv_typecast,
// with rounding mode.
if(typecast_expr.type().id() == ID_floatbv)
{
auto rm = ieee_floatt::rounding_mode_expr(
ieee_floatt::rounding_modet::ROUND_TO_EVEN);
auto floatbv_typecast =
floatbv_typecast_exprt{typecast_expr.op(), rm, typecast_expr.type()};
return std::move(floatbv_typecast);
}
else
return expr;
}
else if(expr.id() == ID_verilog_explicit_type_cast)
{
Expand Down

0 comments on commit 0e054f1

Please sign in to comment.