Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verilog: add rounding mode to casts to real/shortreal #891

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 broken-smt-backend
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
Loading