gem5 v24.0.0.0
Loading...
Searching...
No Matches
mathexpr.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016, 2020 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include "sim/mathexpr.hh"
39
40#include <algorithm>
41#include <cmath>
42#include <regex>
43#include <string>
44
45#include "base/logging.hh"
46
47namespace gem5
48{
49
50MathExpr::MathExpr(std::string expr)
51 : ops(
52 std::array<OpSearch, uNeg + 1> {{
53 OpSearch {true, bAdd, 0, '+', [](double a, double b) { return a + b; }},
54 OpSearch {true, bSub, 0, '-', [](double a, double b) { return a - b; }},
55 OpSearch {true, bMul, 1, '*', [](double a, double b) { return a * b; }},
56 OpSearch {true, bDiv, 1, '/', [](double a, double b) { return a / b; }},
57 OpSearch {false,uNeg, 2, '-', [](double a, double b) { return -b; }},
58 OpSearch {true, bPow, 3, '^', [](double a, double b) {
59 return std::pow(a,b); }
60 }},
61 })
62{
63 // Cleanup
64 expr.erase(remove_if(expr.begin(), expr.end(), isspace), expr.end());
65
66 root = MathExpr::parse(expr);
67 panic_if(!root, "Invalid expression\n");
68}
69
77MathExpr::Node *
78MathExpr::parse(std::string expr) {
79 if (expr.size() == 0)
80 return NULL;
81
82 // From low to high priority
83 int par = 0;
84 for (unsigned p = 0; p < MAX_PRIO; p++) {
85 for (int i = expr.size() - 1; i >= 0; i--) {
86 if (expr[i] == ')')
87 par++;
88 if (expr[i] == '(')
89 par--;
90
91 if (par < 0) return NULL;
92 if (par > 0) continue;
93
94 for (unsigned opt = 0; opt < ops.size(); opt++) {
95 if (ops[opt].priority != p) continue;
96 if (ops[opt].c == expr[i]) {
97 // Try to parse each side
98 Node *l = NULL;
99 if (ops[opt].binary)
100 l = parse(expr.substr(0, i));
101 Node *r = parse(expr.substr(i + 1));
102 if ((l && r) || (!ops[opt].binary && r)) {
103 // Match!
104 Node *n = new Node();
105 n->op = ops[opt].op;
106 n->l = l;
107 n->r = r;
108 return n;
109 }
110 }
111 }
112 }
113 }
114
115 // Remove trivial parenthesis
116 if (expr.size() >= 2 && expr[0] == '(' && expr[expr.size() - 1] == ')')
117 return parse(expr.substr(1, expr.size() - 2));
118
119 // Match a number
120 {
121 char *sptr;
122 double v = strtod(expr.c_str(), &sptr);
123 if (sptr != expr.c_str()) {
124 Node *n = new Node();
125 n->op = sValue;
126 n->value = v;
127 return n;
128 }
129 }
130
131 // Match a variable
132 {
133 bool contains_non_alpha = false;
134 for (auto & c: expr)
135 contains_non_alpha = contains_non_alpha or
136 !( (c >= 'a' && c <= 'z') ||
137 (c >= 'A' && c <= 'Z') ||
138 (c >= '0' && c <= '9') ||
139 c == '$' || c == '\\' || c == '.' || c == '_');
140
141 if (!contains_non_alpha) {
142 Node * n = new Node();
143 n->op = sVariable;
144 n->variable = expr;
145 return n;
146 }
147 }
148
149 return NULL;
150}
151
152double
154 if (!n)
155 return 0;
156 else if (n->op == sValue)
157 return n->value;
158 else if (n->op == sVariable)
159 return fn(n->variable);
160
161 for (auto & opt : ops)
162 if (opt.op == n->op)
163 return opt.fn( eval(n->l, fn), eval(n->r, fn) );
164
165 panic("Invalid node!\n");
166 return 0;
167}
168
169std::string
170MathExpr::toStr(Node *n, std::string prefix) const {
171 std::string ret;
172 ret += prefix + "|-- " + n->toStr() + "\n";
173 if (n->r)
174 ret += toStr(n->r, prefix + "| ");
175 if (n->l)
176 ret += toStr(n->l, prefix + "| ");
177 return ret;
178}
179
180void
182 std::vector<std::string> &variables) const
183{
184 if (!n || n->op == sValue || n->op == nInvalid) {
185 return;
186 } else if (n->op == sVariable) {
187 variables.push_back(n->variable);
188 } else {
189 getVariables(n->l, variables);
190 getVariables(n->r, variables);
191 }
192}
193
194} // namespace gem5
double eval(EvalCallback fn) const
Evaluates the expression.
Definition mathexpr.hh:72
const int MAX_PRIO
Definition mathexpr.hh:97
Node * parse(std::string expr)
Parse and create nodes from string.
Definition mathexpr.cc:78
std::array< OpSearch, uNeg+1 > ops
Operator list.
Definition mathexpr.hh:109
MathExpr(std::string expr)
Definition mathexpr.cc:50
std::vector< std::string > getVariables() const
Return all variables in the this expression.
Definition mathexpr.hh:83
std::string toStr() const
Prints an ASCII representation of the expression tree.
Definition mathexpr.hh:63
std::function< double(std::string)> EvalCallback
Definition mathexpr.hh:56
STL vector class.
Definition stl.hh:37
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
Bitfield< 18, 16 > fn
Definition types.hh:149
Bitfield< 28 > v
Definition misc_types.hh:54
Bitfield< 31 > n
Bitfield< 7 > b
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 29 > c
Definition misc_types.hh:53
Bitfield< 8 > a
Definition misc_types.hh:66
Bitfield< 3, 0 > priority
Bitfield< 5 > l
Bitfield< 0 > p
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Overload hash function for BasicBlockRange type.
Definition binary32.hh:81

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0