Skip to content

Commit 3437e52

Browse files
author
Daniel Kroening
committed
add constant_exprt variants of numeric_cast
numeric_cast is frequently used on constant_exprt typed arguments. The specialised version saves two branches.
1 parent 89d4fa7 commit 3437e52

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/util/arith_tools.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,20 @@ template <>
4545
struct numeric_castt<mp_integer> final
4646
{
4747
optionalt<mp_integer> operator()(const exprt &expr) const
48+
{
49+
if(expr.id() != ID_constant)
50+
return {};
51+
else
52+
return operator()(to_constant_expr(expr));
53+
}
54+
55+
optionalt<mp_integer> operator()(const constant_exprt &expr) const
4856
{
4957
mp_integer out;
50-
if(expr.id() != ID_constant || to_integer(to_constant_expr(expr), out))
58+
if(to_integer(expr, out))
5159
return {};
52-
return out;
60+
else
61+
return out;
5362
}
5463
};
5564

@@ -97,6 +106,15 @@ struct numeric_castt<T,
97106

98107
// Conversion from expression
99108
optionalt<T> operator()(const exprt &expr) const
109+
{
110+
if(expr.id() == ID_constant)
111+
return numeric_castt<T>{}(to_constant_expr(expr));
112+
else
113+
return {};
114+
}
115+
116+
// Conversion from expression
117+
optionalt<T> operator()(const constant_exprt &expr) const
100118
{
101119
if(auto mpi_opt = numeric_castt<mp_integer>{}(expr))
102120
return numeric_castt<T>{}(*mpi_opt);

0 commit comments

Comments
 (0)