Skip to content

Commit 3d95eaa

Browse files
authored
Merge pull request #891 from diffblue/floatbv-typecast
Verilog: add rounding mode to casts to real/shortreal
2 parents f457b5e + 4fe641e commit 3d95eaa

File tree

7 files changed

+60
-1
lines changed

7 files changed

+60
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE broken-smt-backend
2+
cast_to_real1.sv
3+
--bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
--
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module main;
2+
3+
// no rounding
4+
p0: assert final (real'(0) == 0.0);
5+
p1: assert final (real'(1) == 1.0);
6+
7+
// rounding
8+
p2: assert final (real'('hffff_ffff_ffff_ffff) == real'('h1_0000_0000_0000_0000));
9+
p3: assert final (real'(-'sh0_ffff_ffff_ffff_ffff) == real'(-'sh1_0000_0000_0000_0000));
10+
11+
endmodule
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
KNOWNBUG
2+
cast_to_real2.sv
3+
--bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
--
9+
Typechecking yields inconsistent types.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module main;
2+
3+
p0: assert final (real'(-1) == -1.0);
4+
5+
endmodule
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
KNOWNBUG
2+
cast_to_real3.sv
3+
--bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
--
9+
The flattening solver fails with a width error.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module main;
2+
3+
p0: assert final (real'(1'b1) == 1);
4+
5+
endmodule

src/verilog/verilog_lowering.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Daniel Kroening, [email protected]
1111
#include <util/arith_tools.h>
1212
#include <util/bitvector_expr.h>
1313
#include <util/c_types.h>
14+
#include <util/floatbv_expr.h>
1415
#include <util/ieee_float.h>
1516

1617
#include "aval_bval_encoding.h"
@@ -272,7 +273,18 @@ exprt verilog_lowering(exprt expr)
272273
}
273274
}
274275

275-
return expr;
276+
// Cast to float? Turn into floatbv_typecast,
277+
// with rounding mode.
278+
if(typecast_expr.type().id() == ID_floatbv)
279+
{
280+
auto rm = ieee_floatt::rounding_mode_expr(
281+
ieee_floatt::rounding_modet::ROUND_TO_EVEN);
282+
auto floatbv_typecast =
283+
floatbv_typecast_exprt{typecast_expr.op(), rm, typecast_expr.type()};
284+
return std::move(floatbv_typecast);
285+
}
286+
else
287+
return expr;
276288
}
277289
else if(expr.id() == ID_verilog_explicit_type_cast)
278290
{

0 commit comments

Comments
 (0)