@@ -251,19 +251,44 @@ exprt verilog_lowering_cast(typecast_exprt expr)
251251 auto &src_type = expr.op ().type ();
252252 auto &dest_type = expr.type ();
253253
254+ // float to int
255+ if (
256+ (src_type.id () == ID_verilog_real ||
257+ src_type.id () == ID_verilog_shortreal ||
258+ src_type.id () == ID_verilog_realtime || src_type.id () == ID_floatbv) &&
259+ (dest_type.id () == ID_signedbv || dest_type.id () == ID_unsignedbv ||
260+ dest_type.id () == ID_bool))
261+ {
262+ // 1800-2017 6.12.2 requires rounding away from zero when converting
263+ // to integers.
264+ auto rm = floatbv_rounding_mode (ieee_floatt::rounding_modet::ROUND_TO_AWAY);
265+ auto rti =
266+ floatbv_round_to_integral_exprt{expr.op (), rm}.with_source_location (
267+ expr.source_location ());
268+ auto new_cast = floatbv_typecast_exprt{
269+ rti,
270+ ieee_floatt::rounding_mode_expr (
271+ ieee_floatt::rounding_modet::ROUND_TO_ZERO),
272+ verilog_lowering (dest_type)};
273+ return std::move (new_cast);
274+ }
275+
276+ // float to float, int to float
254277 if (
255278 dest_type.id () == ID_verilog_real ||
256279 dest_type.id () == ID_verilog_shortreal ||
257- dest_type.id () == ID_verilog_realtime || src_type. id () == ID_floatbv )
280+ dest_type.id () == ID_verilog_realtime)
258281 {
259- // This may require rounding, hence add rounding mode.
260- // 1800-2017 6.12.2 requires rounding away from zero,
261- // which we don't have.
282+ // The standard does not say how to round double precision
283+ // to single precision floating-point. It does not say how
284+ // to round integers to float. We simply use RNA,
285+ // given the explicit requirement to use RNA when converting
286+ // float to integer.
262287 expr.type () = verilog_lowering (dest_type);
263288 auto new_cast = floatbv_typecast_exprt{
264289 expr.op (),
265290 ieee_floatt::rounding_mode_expr (
266- ieee_floatt::rounding_modet::ROUND_TO_EVEN ),
291+ ieee_floatt::rounding_modet::ROUND_TO_AWAY ),
267292 verilog_lowering (dest_type)};
268293 return std::move (new_cast);
269294 }
0 commit comments