summaryrefslogtreecommitdiff
path: root/fpoptimizer/valuerange.hh
blob: f2d0ab463c6f004a6e502c70fc2d7529f4c1ea83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef FPOptimizer_ValueRangeHH
#define FPOptimizer_ValueRangeHH

#include "fparser.hh"
#include "extrasrc/fpaux.hh"

namespace FPoptimizer_CodeTree
{
    namespace rangeutil
    {
        template<unsigned Compare> struct Comp { };
        template<>struct Comp<FUNCTIONPARSERTYPES::cLess> {
            template<typename Value_t>
            inline bool operator() (const Value_t& a, const Value_t& b) { return a<b; }
        };
        template<>struct Comp<FUNCTIONPARSERTYPES::cLessOrEq> {
            template<typename Value_t>
            inline bool operator() (const Value_t& a, const Value_t& b) { return a<=b; }
        };
        template<>struct Comp<FUNCTIONPARSERTYPES::cGreater> {
            template<typename Value_t>
            inline bool operator() (const Value_t& a, const Value_t& b) { return a>b; }
        };
        template<>struct Comp<FUNCTIONPARSERTYPES::cGreaterOrEq> {
            template<typename Value_t>
            inline bool operator() (const Value_t& a, const Value_t& b) { return a>=b; }
        };
        template<>struct Comp<FUNCTIONPARSERTYPES::cEqual> {
            template<typename Value_t>
            inline bool operator() (const Value_t& a, const Value_t& b) { return a==b; }
        };
        template<>struct Comp<FUNCTIONPARSERTYPES::cNEqual> {
            template<typename Value_t>
            inline bool operator() (const Value_t& a, const Value_t& b) { return a!=b; }
        };
    }

    template<typename Value_t>
    struct rangehalf
    {
        Value_t val;
        bool    known;

        rangehalf(): val(), known(false) { }
        rangehalf(const Value_t& v) : val(v), known(true) { }

        inline void set(const Value_t& v) { known=true; val=v; }

        /////////

        /* If value is known, refine it using func.
         * Otherwise, use the model.
         *
         * Call like this:
         *      range := param
         *      range.min.set(fp_floor)
         *      If param is known, sets minimum to floor(param.min)
         *      Otherwise, sets min to unknown
         * Or:
         *      range := param
         *      range.min.set(fp_atan, -pihalf)
         *      If param is known, sets minimum to floor(param.min)
         *      Otherwise, sets min to -pihalf
         */
        void set
            (Value_t (*const func)(Value_t),
             rangehalf<Value_t> model = rangehalf<Value_t>())
        {
            if(known) val = func(val); else *this = model;
        }

        void set
            (Value_t (*const func)(const Value_t&),
             rangehalf<Value_t> model = rangehalf<Value_t>())
        {
            if(known) val = func(val); else *this = model;
        }

        /* Call like this:
         *      range := param
         *      range.min.set_if<cGreater>(-1, fp_asin, -pihalf)
         *      If param is known AND param.min > -1, sets minimum to asin(param.min)
         *      Otherwise, sets min to -pihalf
         *      The purpose of the condition is to ensure that the function
         *      is not being called with illegal values.
         */
        template<unsigned Compare>
        void set_if
            (Value_t v,
             Value_t (*const func)(Value_t),
             rangehalf<Value_t> model = rangehalf<Value_t>())
        {
            if(known && rangeutil::Comp<Compare>() (val,v))
                val = func(val);
            else
                *this = model;
        }
        template<unsigned Compare>
        void set_if
            (const Value_t& v,
             Value_t (*const func)(const Value_t&),
             rangehalf<Value_t> model = rangehalf<Value_t>())
        {
            if(known && rangeutil::Comp<Compare>() (val,v))
                val = func(val);
            else
                *this = model;
        }
    };

    /* range expresses the range of values that an expression can take. */
    template<typename Value_t>
    struct range
    {
        rangehalf<Value_t> min, max;

        /* Initializations */
        range() : min(),max() { }
        range(Value_t mi,Value_t ma): min(mi),max(ma) { }
        range(bool,Value_t ma): min(),max(ma) { }
        range(Value_t mi,bool): min(mi),max() { }

        /* Apply the abs() function to the range,
         * i.e. +3..+5 becomes +3..+5;
         *      -3..+5 becomes  0..+5;
         *      -3..-1 becomes  0..+1
         */
        void set_abs();

        /* Negate the range, i.e. -3..+5 becomes -5..+3 */
        void set_neg();
    };

    /* Analysis functions for a range */
    template<typename Value_t>
    bool IsLogicalTrueValue(const range<Value_t>& p, bool abs);

    template<typename Value_t>
    bool IsLogicalFalseValue(const range<Value_t>& p, bool abs);
}

#endif