/src/solidity/libsmtutil/Helpers.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | |
19 | | #pragma once |
20 | | |
21 | | #include <libsmtutil/SolverInterface.h> |
22 | | |
23 | | namespace solidity::smtutil |
24 | | { |
25 | | |
26 | | /// Signed division in SMTLIB2 rounds differently than EVM. |
27 | | /// This does not check for division by zero! |
28 | | inline Expression signedDivisionEVM(Expression _left, Expression _right) |
29 | 0 | { |
30 | 0 | return Expression::ite( |
31 | 0 | _left >= 0, |
32 | 0 | Expression::ite(_right >= 0, _left / _right, 0 - (_left / (0 - _right))), |
33 | 0 | Expression::ite(_right >= 0, 0 - ((0 - _left) / _right), (0 - _left) / (0 - _right)) |
34 | 0 | ); |
35 | 0 | } |
36 | | |
37 | | inline Expression abs(Expression _value) |
38 | 0 | { |
39 | 0 | return Expression::ite(_value >= 0, _value, 0 - _value); |
40 | 0 | } |
41 | | |
42 | | /// Signed modulo in SMTLIB2 behaves differently with regards |
43 | | /// to the sign than EVM. |
44 | | /// This does not check for modulo by zero! |
45 | | inline Expression signedModuloEVM(Expression _left, Expression _right) |
46 | 0 | { |
47 | 0 | return Expression::ite( |
48 | 0 | _left >= 0, |
49 | 0 | _left % _right, |
50 | 0 | Expression::ite( |
51 | 0 | (_left % _right) == 0, |
52 | 0 | 0, |
53 | 0 | (_left % _right) - abs(_right) |
54 | 0 | ) |
55 | 0 | ); |
56 | 0 | } |
57 | | |
58 | | } |