mathexpr.hh revision 11531
1298SN/A/*
211270Sandreas.sandberg@arm.com * Copyright (c) 2016 ARM Limited
38142SAli.Saidi@ARM.com * All rights reserved
48142SAli.Saidi@ARM.com *
58142SAli.Saidi@ARM.com * The license below extends only to copyright in the software and shall
68142SAli.Saidi@ARM.com * not be construed as granting a license to any other intellectual
78142SAli.Saidi@ARM.com * property including but not limited to intellectual property relating
88142SAli.Saidi@ARM.com * to a hardware implementation of the functionality of the software
98142SAli.Saidi@ARM.com * licensed hereunder.  You may use the software subject to the license
108142SAli.Saidi@ARM.com * terms below provided that you ensure that this notice is replicated
118142SAli.Saidi@ARM.com * unmodified and in its entirety in all distributions of the software,
128142SAli.Saidi@ARM.com * modified or unmodified, in source code or in binary form.
138142SAli.Saidi@ARM.com *
148580Ssteve.reinhardt@amd.com * Redistribution and use in source and binary forms, with or without
152188SN/A * modification, are permitted provided that the following conditions are
16298SN/A * met: redistributions of source code must retain the above copyright
17298SN/A * notice, this list of conditions and the following disclaimer;
18298SN/A * redistributions in binary form must reproduce the above copyright
19298SN/A * notice, this list of conditions and the following disclaimer in the
20298SN/A * documentation and/or other materials provided with the distribution;
21298SN/A * neither the name of the copyright holders nor the names of its
22298SN/A * contributors may be used to endorse or promote products derived from
23298SN/A * this software without specific prior written permission.
24298SN/A *
25298SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26298SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27298SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28298SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29298SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30298SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31298SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32298SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33298SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34298SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35298SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36298SN/A *
37298SN/A * Authors: David Guillen Fandos
38298SN/A */
39298SN/A
402665Ssaidi@eecs.umich.edu#ifndef __SIM_MATHEXPR_HH__
412665Ssaidi@eecs.umich.edu#define __SIM_MATHEXPR_HH__
42298SN/A
43298SN/A#include <algorithm>
44954SN/A#include <functional>
45956SN/A#include <string>
46956SN/A
478229Snate@binkert.orgclass MathExpr {
484078Sbinkertn@umich.edu  public:
49299SN/A
509659SAndreas.Sandberg@ARM.com    MathExpr(std::string expr);
51299SN/A
528777Sgblack@eecs.umich.edu    typedef std::function<double(std::string)> EvalCallback;
539659SAndreas.Sandberg@ARM.com
542170SN/A    /**
5510553Salexandru.dutu@amd.com     * Prints an ASCII representation of the expression tree
565882Snate@binkert.org     *
578734Sdam.sunwoo@arm.com     * @return A string containing the ASCII representation of the expression
586658Snate@binkert.org     */
591717SN/A    std::string toStr() const { return toStr(root, ""); }
608229Snate@binkert.org
612680Sktlim@umich.edu    /**
628232Snate@binkert.org     * Evaluates the expression
639733Sandreas@sandberg.pp.se     *
648232Snate@binkert.org     * @param fn A callback funcion to evaluate variables
658232Snate@binkert.org     *
6611290Sgabor.dozsa@arm.com     * @return The value for this expression
675529Snate@binkert.org     */
688784Sgblack@eecs.umich.edu    double eval(EvalCallback fn) const { return eval(root, fn); }
6911290Sgabor.dozsa@arm.com
7010553Salexandru.dutu@amd.com  private:
713565Sgblack@eecs.umich.edu    enum Operator {
72298SN/A        bAdd, bSub, bMul, bDiv, bPow, uNeg, sValue, sVariable, nInvalid
735606Snate@binkert.org    };
74298SN/A
75695SN/A    // Match operators
76695SN/A    const int MAX_PRIO = 4;
77954SN/A    typedef double (*binOp)(double, double);
782080SN/A    struct OpSearch {
79298SN/A        bool binary;
80299SN/A        Operator op;
811052SN/A        int priority;
82729SN/A        char c;
832107SN/A        binOp fn;
84298SN/A    };
855504Snate@binkert.org
865504Snate@binkert.org    /** Operator list */
878784Sgblack@eecs.umich.edu    std::array<OpSearch, uNeg + 1> ops;
888784Sgblack@eecs.umich.edu
898784Sgblack@eecs.umich.edu    class Node {
908784Sgblack@eecs.umich.edu      public:
918784Sgblack@eecs.umich.edu        Node() : op(nInvalid), l(0), r(0), value(0) {}
925780Ssteve.reinhardt@amd.com        std::string toStr() const {
939659SAndreas.Sandberg@ARM.com            const char opStr[] = {'+', '-', '*', '/', '^', '-'};
949659SAndreas.Sandberg@ARM.com            switch (op) {
959659SAndreas.Sandberg@ARM.com              case nInvalid:
969659SAndreas.Sandberg@ARM.com                return "INVALID";
979659SAndreas.Sandberg@ARM.com              case sVariable:
989733Sandreas@sandberg.pp.se                return variable;
999733Sandreas@sandberg.pp.se              case sValue:
1009659SAndreas.Sandberg@ARM.com                return std::to_string(value);
1019659SAndreas.Sandberg@ARM.com              default:
1029659SAndreas.Sandberg@ARM.com                return std::string(1, opStr[op]);
1039659SAndreas.Sandberg@ARM.com            };
1049659SAndreas.Sandberg@ARM.com        }
1059879Sandreas@sandberg.pp.se
1069879Sandreas@sandberg.pp.se        Operator op;
1079879Sandreas@sandberg.pp.se        Node *l, *r;
1089879Sandreas@sandberg.pp.se        double value;
1099659SAndreas.Sandberg@ARM.com        std::string variable;
1109659SAndreas.Sandberg@ARM.com    };
1119659SAndreas.Sandberg@ARM.com
1129659SAndreas.Sandberg@ARM.com    /** Root node */
1139659SAndreas.Sandberg@ARM.com    Node * root;
1149659SAndreas.Sandberg@ARM.com
1159659SAndreas.Sandberg@ARM.com    /** Parse and create nodes from string */
1169659SAndreas.Sandberg@ARM.com    Node *parse(std::string expr);
1179659SAndreas.Sandberg@ARM.com
1189659SAndreas.Sandberg@ARM.com    /** Print tree as string */
1199659SAndreas.Sandberg@ARM.com    std::string toStr(Node *n, std::string prefix) const;
1209659SAndreas.Sandberg@ARM.com
1219659SAndreas.Sandberg@ARM.com    /** Eval a node */
1229659SAndreas.Sandberg@ARM.com    double eval(const Node *n, EvalCallback fn) const;
1239659SAndreas.Sandberg@ARM.com};
1249659SAndreas.Sandberg@ARM.com
1259659SAndreas.Sandberg@ARM.com#endif
1269659SAndreas.Sandberg@ARM.com
1279659SAndreas.Sandberg@ARM.com
1289659SAndreas.Sandberg@ARM.com