Skip to content

Commit 1ec10e8

Browse files
committed
Verilog: add rounding mode to casts to real/shortreal
Casts to real/shortreal may need to round, hence add the rounding mode during lowering.
1 parent 35fdabf commit 1ec10e8

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
KNOWNBUG
2+
cast_to_real1.sv
3+
--bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
--
9+
Typechecking is failing.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module main;
2+
3+
p0: assert final (real'(0) == 0.0);
4+
p1: assert final (real'(1) == 1.0);
5+
p2: assert final (real'(-1) == -1.0);
6+
p3: assert final (real'(1'b1) == 1);
7+
p4: assert final (real'('hffff_ffff_ffff_ffff) == real'('h1_0000_0000_0000_0000));
8+
p5: assert final (real'(-'sh0_ffff_ffff_ffff_ffff) == real'(-'sh1_0000_0000_0000_0000));
9+
10+
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)