ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
python_math.h
Go to the documentation of this file.
1#ifndef CPROVER_PYTHON_FRONTEND_PYTHON_MATH_H
2#define CPROVER_PYTHON_FRONTEND_PYTHON_MATH_H
3
5#include <util/irep/expr.h>
7#include <nlohmann/json.hpp>
8#include <optional>
9#include <string>
10#include <string_view>
11#include <unordered_map>
12
14class type_handler;
15class contextt;
16class symbolt;
17
27{
28private:
29 python_converter &converter;
30 contextt &symbol_table;
31 type_handler &type_handler_;
32 mutable std::unordered_map<std::string_view, const symbolt *>
33 c_math_symbol_cache_;
34
40 exprt resolve_symbol(const exprt &operand) const;
41
49 std::optional<BigInt> try_fold_int_constant(const exprt &expr) const;
50
53 exprt promote_to_double_if_needed(exprt operand) const;
54
56 exprt build_unary_c_math_call(
57 const char *symbol_id,
58 const char *display_name,
59 exprt operand,
60 const nlohmann::json &element);
61
64 const symbolt &
65 get_c_math_symbol_cached(const char *symbol_id, const char *display_name);
66
67public:
70 std::optional<double> try_resolve_constant_double(const exprt &operand) const;
71
79
81
82 static bool
83 fits_in_width(const BigInt &value, unsigned width, bool is_signed);
84
86
99 const std::string &caller,
100 const std::string &func_name) const;
101
113 const std::string &caller,
114 const std::string &func_name);
115
117 bool is_unary_dispatch_function(std::string_view func_name) const;
118
120 bool is_binary_dispatch_function(std::string_view func_name) const;
121
129 std::string_view func_name,
130 exprt operand,
131 const nlohmann::json &element);
132
141 std::string_view func_name,
142 exprt lhs,
143 exprt rhs,
144 const nlohmann::json &element);
145
157 exprt compute_expr(const exprt &expr) const;
158
172 exprt handle_power(exprt lhs, exprt rhs);
173
189
202
217 exprt handle_modulo(exprt lhs, exprt rhs, const nlohmann::json &element);
218
232 exprt
233 handle_int_modulo(const exprt &lhs, const exprt &rhs, const exprt &bin_expr);
234
248 const exprt &lhs,
249 const exprt &rhs,
250 const exprt &bin_expr);
251
263 void handle_float_division(exprt &lhs, exprt &rhs, exprt &bin_expr) const;
264
277 void promote_int_to_float(exprt &op, const typet &target_type) const;
278
298 exprt handle_sqrt(exprt operand, const nlohmann::json &element);
299
318 exprt handle_sin(exprt operand, const nlohmann::json &element);
319
338 exprt handle_cos(exprt operand, const nlohmann::json &element);
339
372 exprt
373 handle_divmod(exprt dividend, exprt divisor, const nlohmann::json &element);
375 exprt dividend,
376 exprt divisor,
377 const nlohmann::json &element);
379 const typet &dividend_type,
380 const typet &divisor_type,
381 const nlohmann::json &element);
382
401 exprt handle_exp(exprt operand, const nlohmann::json &element);
402
426 exprt handle_log(exprt operand, const nlohmann::json &element);
427
431 exprt handle_acos(exprt operand, const nlohmann::json &element);
432
436 exprt handle_atan(exprt operand, const nlohmann::json &element);
437
441 exprt
442 handle_atan2(exprt y_operand, exprt x_operand, const nlohmann::json &element);
443
447 exprt handle_log2(exprt operand, const nlohmann::json &element);
448
452 exprt handle_pow(exprt base, exprt exp, const nlohmann::json &element);
453
457 exprt handle_fabs(exprt operand, const nlohmann::json &element);
458
462 exprt handle_trunc(exprt operand, const nlohmann::json &element);
463
467 exprt handle_fmod(exprt lhs, exprt rhs, const nlohmann::json &element);
468
472 exprt handle_copysign(exprt lhs, exprt rhs, const nlohmann::json &element);
473
477 exprt handle_tan(exprt operand, const nlohmann::json &element);
478
482 exprt handle_asin(exprt operand, const nlohmann::json &element);
483
487 exprt handle_sinh(exprt operand, const nlohmann::json &element);
488
492 exprt handle_cosh(exprt operand, const nlohmann::json &element);
493
497 exprt handle_tanh(exprt operand, const nlohmann::json &element);
498
502 exprt handle_log10(exprt operand, const nlohmann::json &element);
503
507 exprt handle_expm1(exprt operand, const nlohmann::json &element);
508
512 exprt handle_log1p(exprt operand, const nlohmann::json &element);
513
517 exprt handle_exp2(exprt operand, const nlohmann::json &element);
518
522 exprt handle_asinh(exprt operand, const nlohmann::json &element);
523
527 exprt handle_acosh(exprt operand, const nlohmann::json &element);
528
532 exprt handle_atanh(exprt operand, const nlohmann::json &element);
533
537 exprt handle_hypot(exprt lhs, exprt rhs, const nlohmann::json &element);
538
542 exprt handle_dist(exprt p, exprt q, const nlohmann::json &element);
543};
544
545#endif
static const twodig_t base
Definition bigint.cpp:28
Definition bigint.hh:58
Definition context.h:57
Definition expr.h:34
Main converter for transforming Python AST into ESBMC's intermediate representation.
Definition python_converter.h:76
exprt handle_fabs(exprt operand, const nlohmann::json &element)
Handle absolute value for floats (math.fabs).
Definition python_math.cpp:1322
exprt handle_hypot(exprt lhs, exprt rhs, const nlohmann::json &element)
Handle hypot function (math.hypot).
Definition python_math.cpp:1538
exprt handle_acosh(exprt operand, const nlohmann::json &element)
Handle acosh function (math.acosh).
Definition python_math.cpp:1516
exprt handle_cosh(exprt operand, const nlohmann::json &element)
Handle hyperbolic cosine function (math.cosh).
Definition python_math.cpp:1439
exprt handle_atan2(exprt y_operand, exprt x_operand, const nlohmann::json &element)
Handle two-argument arctangent function (math.atan2).
Definition python_math.cpp:1253
exprt handle_log10(exprt operand, const nlohmann::json &element)
Handle base-10 logarithm function (math.log10).
Definition python_math.cpp:1461
exprt handle_asin(exprt operand, const nlohmann::json &element)
Handle arcsine function (math.asin).
Definition python_math.cpp:1417
exprt handle_asinh(exprt operand, const nlohmann::json &element)
Handle asinh function (math.asinh).
Definition python_math.cpp:1505
static const BigInt kMaxConstantFoldExponent
Definition python_math.h:85
bool is_math_dispatch_target_cached(const std::string &caller, const std::string &func_name)
Cached variant of is_math_dispatch_target.
Definition python_math.cpp:206
exprt handle_fmod(exprt lhs, exprt rhs, const nlohmann::json &element)
Handle floating-point remainder (math.fmod).
Definition python_math.cpp:1356
python_math(python_converter &conv, contextt &ctx, type_handler &th)
Constructor.
Definition python_math.cpp:149
exprt handle_sinh(exprt operand, const nlohmann::json &element)
Handle hyperbolic sine function (math.sinh).
Definition python_math.cpp:1428
exprt handle_power_symbolic(exprt base, exprt exp)
Handle power operation symbolically using C's pow() function.
Definition python_math.cpp:511
exprt handle(std::string_view func_name, exprt operand, const nlohmann::json &element)
Dispatch supported unary math functions through a single entrypoint.
Definition python_math.cpp:244
exprt handle_dist(exprt p, exprt q, const nlohmann::json &element)
Handle dist function (math.dist) for tuple arguments.
Definition python_math.cpp:1562
exprt handle_pow(exprt base, exprt exp, const nlohmann::json &element)
Handle power function (math.pow).
Definition python_math.cpp:1298
exprt handle_acos(exprt operand, const nlohmann::json &element)
Handle arccosine function (math.acos).
Definition python_math.cpp:1231
exprt build_power_expression(const exprt &base, const BigInt &exp)
Build power expression using exponentiation by squaring.
Definition python_math.cpp:525
void promote_int_to_float(exprt &op, const typet &target_type) const
Promote integer expression to floating-point type.
Definition python_math.cpp:934
std::optional< double > try_resolve_constant_double(const exprt &operand) const
Resolve a constant-like expression (constant or constant-valued symbol) to a host double when possibl...
Definition python_math.cpp:395
exprt handle_trunc(exprt operand, const nlohmann::json &element)
Handle truncate to integer towards zero (math.trunc).
Definition python_math.cpp:1333
exprt handle_sqrt(exprt operand, const nlohmann::json &element)
Handle square root operation (math.sqrt).
Definition python_math.cpp:983
exprt handle_exp2(exprt operand, const nlohmann::json &element)
Handle exp2 function (math.exp2).
Definition python_math.cpp:1494
bool is_unary_dispatch_function(std::string_view func_name) const
Return true when func_name is handled by unary math dispatch.
Definition python_math.cpp:227
exprt handle_floor_division(const exprt &lhs, const exprt &rhs, const exprt &bin_expr)
Handle Python's floor division operator (//).
Definition python_math.cpp:744
exprt handle_divmod(exprt dividend, exprt divisor, const nlohmann::json &element)
Handle divmod() built-in function.
Definition python_math.cpp:1031
exprt handle_atan(exprt operand, const nlohmann::json &element)
Handle arctangent function (math.atan).
Definition python_math.cpp:1242
exprt handle_atanh(exprt operand, const nlohmann::json &element)
Handle atanh function (math.atanh).
Definition python_math.cpp:1527
static bool fits_in_width(const BigInt &value, unsigned width, bool is_signed)
Definition python_math.cpp:173
exprt handle_copysign(exprt lhs, exprt rhs, const nlohmann::json &element)
Handle copy sign (math.copysign).
Definition python_math.cpp:1380
exprt handle_tanh(exprt operand, const nlohmann::json &element)
Handle hyperbolic tangent function (math.tanh).
Definition python_math.cpp:1450
exprt handle_sin(exprt operand, const nlohmann::json &element)
Handle sine function (math.sin).
Definition python_math.cpp:1187
exprt handle_cos(exprt operand, const nlohmann::json &element)
Handle cosine function (math.cos).
Definition python_math.cpp:1198
exprt handle_log2(exprt operand, const nlohmann::json &element)
Handle base-2 logarithm function (math.log2).
Definition python_math.cpp:1279
exprt handle_log1p(exprt operand, const nlohmann::json &element)
Handle log1p function (math.log1p).
Definition python_math.cpp:1483
void handle_float_division(exprt &lhs, exprt &rhs, exprt &bin_expr) const
Handle Python's true division operator (/).
Definition python_math.cpp:877
exprt handle_power(exprt lhs, exprt rhs)
Handle Python's power operator (**).
Definition python_math.cpp:606
exprt handle_modulo(exprt lhs, exprt rhs, const nlohmann::json &element)
Handle Python's modulo operator (%).
Definition python_math.cpp:704
static BigInt pow_bigint_non_negative(BigInt base, BigInt exp)
Definition python_math.cpp:157
exprt handle_exp(exprt operand, const nlohmann::json &element)
Handle exponential function (math.exp).
Definition python_math.cpp:1209
exprt handle_log(exprt operand, const nlohmann::json &element)
Handle natural logarithm function (math.log).
Definition python_math.cpp:1220
exprt handle_tan(exprt operand, const nlohmann::json &element)
Handle tangent function (math.tan).
Definition python_math.cpp:1406
exprt compute_expr(const exprt &expr) const
Fold a +,-,*,/ expression to a constant when it is genuinely one.
Definition python_math.cpp:382
exprt handle_numeric_divmod(exprt dividend, exprt divisor, const nlohmann::json &element)
Definition python_math.cpp:1063
exprt handle_expm1(exprt operand, const nlohmann::json &element)
Handle expm1 function (math.expm1).
Definition python_math.cpp:1472
bool is_binary_dispatch_function(std::string_view func_name) const
Return true when func_name is handled by binary math dispatch.
Definition python_math.cpp:238
exprt handle_int_modulo(const exprt &lhs, const exprt &rhs, const exprt &bin_expr)
Handle Python's integer modulo operator (%).
Definition python_math.cpp:824
exprt raise_divmod_type_error(const typet &dividend_type, const typet &divisor_type, const nlohmann::json &element)
The TypeError is planted as a statement ahead of the call, and an (int, int) tuple stands in for the ...
Definition python_math.cpp:1011
exprt handle_power_symbolic_exponent(exprt lhs, exprt rhs)
An integer power whose exponent is not a constant: pow(), or the bits of 2.0 ** n for a base of 2.
Definition python_math.cpp:596
bool is_math_dispatch_target(const std::string &caller, const std::string &func_name) const
Classify whether a call should be handled by math dispatch.
Definition python_math.cpp:184
Definition symbol_id.h:6
Definition symbol.h:12
Definition type_handler.h:113
Definition type.h:9
Definition toml.hpp:1531
double exp(double x)
Definition exp.c:84