Skip to content

Commit 4e33bc6

Browse files
author
kroening
committed
(double)value REL const ---> value rel const if const can be represented in lower precision
git-svn-id: svn+ssh://svn.cprover.org/srv/svn/cbmc/trunk@3088 6afb6bc1-c8e4-404c-8f48-9ae832c5b171
1 parent 3bd3b3c commit 4e33bc6

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/util/simplify_expr.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,29 +1562,35 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
15621562
exprt op0=expr.op0();
15631563
exprt op1=expr.op1(); // rounding mode
15641564

1565-
// We can soundly re-write (float)(x op y)
1566-
// to (float)x op (float)y. True for any rounding mode!
1567-
1565+
// We can soundly re-write (float)((double)x op (double)y)
1566+
// to x op y. True for any rounding mode!
1567+
1568+
#if 0
15681569
if(op0.id()==ID_floatbv_div ||
15691570
op0.id()==ID_floatbv_mult ||
15701571
op0.id()==ID_floatbv_plus ||
15711572
op0.id()==ID_floatbv_minus)
15721573
{
1573-
if(op0.operands().size()==3)
1574+
if(op0.operands().size()==3 &&
1575+
op0.op0().id()==ID_typecast &&
1576+
op0.op1().id()==ID_typecast &&
1577+
op0.op0().operands().size()==1 &&
1578+
op0.op1().operands().size()==1 &&
1579+
ns.follow(op0.op0().type())==dest_type &&
1580+
ns.follow(op0.op1().type())==dest_type)
15741581
{
15751582
exprt result(op0.id(), expr.type());
15761583
result.operands().resize(3);
1577-
result.op0()=binary_exprt(op0.op0(), ID_floatbv_typecast, op1, expr.type());
1578-
result.op1()=binary_exprt(op0.op1(), ID_floatbv_typecast, op1, expr.type());
1579-
result.op2()=op1; // ?
1584+
result.op0()=op0.op0().op0();
1585+
result.op1()=op0.op1().op0();
1586+
result.op2()=op1;
15801587

1581-
simplify_node(result.op0());
1582-
simplify_node(result.op1());
15831588
simplify_node(result);
15841589
expr.swap(result);
15851590
return false;
15861591
}
15871592
}
1593+
#endif
15881594

15891595
// constant folding
15901596
if(op0.is_constant() && op1.is_constant())
@@ -1619,7 +1625,8 @@ bool simplify_exprt::simplify_floatbv_typecast(exprt &expr)
16191625

16201626
#if 0
16211627
// (T)(a?b:c) --> a?(T)b:(T)c
1622-
if(expr.op0().id()==ID_if && expr.op0().operands().size()==3)
1628+
if(expr.op0().id()==ID_if &&
1629+
expr.op0().operands().size()==3)
16231630
{
16241631
exprt tmp_op1=binary_exprt(expr.op0().op1(), ID_floatbv_typecast, expr.op1(), dest_type);
16251632
exprt tmp_op2=binary_exprt(expr.op0().op2(), ID_floatbv_typecast, expr.op1(), dest_type);
@@ -2554,7 +2561,7 @@ bool simplify_exprt::simplify_if(exprt &expr)
25542561
}
25552562
}
25562563

2557-
#if 0
2564+
#if 1
25582565
// a ? b : c --> a ? b[a/true] : c
25592566
exprt tmp_true=truevalue;
25602567
replace_expr(cond, true_exprt(), tmp_true);
@@ -3444,6 +3451,31 @@ bool simplify_exprt::simplify_inequality_constant(exprt &expr)
34443451
}
34453452
}
34463453
}
3454+
3455+
#if 1
3456+
// (double)value REL const ---> value rel const
3457+
// if 'const' can be represented exactly.
3458+
3459+
if(expr.op0().id()==ID_typecast &&
3460+
expr.op0().operands().size()==1 &&
3461+
ns.follow(expr.op0().type()).id()==ID_floatbv &&
3462+
ns.follow(expr.op0().op0().type()).id()==ID_floatbv)
3463+
{
3464+
ieee_floatt const_val(to_constant_expr(expr.op1()));
3465+
ieee_floatt const_val_converted=const_val;
3466+
const_val_converted.change_spec(to_floatbv_type(ns.follow(expr.op0().op0().type())));
3467+
ieee_floatt const_val_converted_back=const_val_converted;
3468+
const_val_converted_back.change_spec(to_floatbv_type(ns.follow(expr.op0().type())));
3469+
if(const_val_converted_back==const_val)
3470+
{
3471+
exprt result=expr;
3472+
result.op0()=expr.op0().op0();
3473+
result.op1()=const_val_converted.to_expr();
3474+
expr.swap(result);
3475+
return false;
3476+
}
3477+
}
3478+
#endif
34473479

34483480
// is the constant zero?
34493481

0 commit comments

Comments
 (0)