1 // OpenSTA, Static Timing Analyzer
2 // Copyright (c) 2020, Parallax Software, Inc.
3 //
4 // This program 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 // This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
16 
17 #pragma once
18 
19 #include <array>
20 #include <vector>
21 #include "DisallowCopyAssign.hh"
22 #include "Iterator.hh"
23 
24 namespace sta {
25 
26 class MinMax;
27 class MinMaxAll;
28 
29 // Use typedefs to make early/late functional equivalents to min/max.
30 typedef MinMax EarlyLate;
31 typedef MinMaxAll EarlyLateAll;
32 
33 // Large value used for min/max initial values.
34 extern const float INF;
35 
36 class MinMax
37 {
38 public:
39   static void init();
40   static void destroy();
41   // Singleton accessors.
min()42   static MinMax *min() { return &min_; }
max()43   static MinMax *max() { return &max_; }
early()44   static EarlyLate *early() { return &min_; }
late()45   static EarlyLate *late() { return &max_; }
minIndex()46   static int minIndex() { return min_.index_; }
earlyIndex()47   static int earlyIndex() { return min_.index_; }
maxIndex()48   static int maxIndex() { return max_.index_; }
lateIndex()49   static int lateIndex() { return max_.index_; }
asString() const50   const char *asString() const { return name_; }
index() const51   int index() const { return index_; }
initValue() const52   float initValue() const { return init_value_; }
53   // Max value1 > value2, Min value1 < value2.
54   bool compare(float value1,
55 	       float value2) const;
56   // min/max(value1, value2)
57   float minMax(float value1,
58 	       float value2) const;
59   MinMaxAll *asMinMaxAll() const;
60   MinMax *opposite() const;
61   // for range support.
62   // for (auto min_max : MinMax::range()) {}
range()63   static const std::array<MinMax*, 2> &range() { return range_; }
64   // for (auto mm_index : MinMax::rangeIndex()) {}
rangeIndex()65   static const std::array<int, 2> &rangeIndex() { return range_index_; }
66   // Find MinMax from name.
67   static MinMax *find(const char *min_max);
68   // Find MinMax from index.
69   static MinMax *find(int index);
70   static const int index_max = 1;
71   static const int index_count = 2;
72   static const int index_bit_count = 1;
73 
74 private:
75   DISALLOW_COPY_AND_ASSIGN(MinMax);
76   MinMax(const char *name,
77 	 int index,
78 	 float init_value,
79 	 bool (*compare)(float value1,
80 			 float value2));
81 
82   const char *name_;
83   int index_;
84   float init_value_;
85   bool (*compare_)(float value1,
86 		   float value2);
87 
88   static MinMax min_;
89   static MinMax max_;
90   static const std::array<MinMax*, 2> range_;
91   static const std::array<int, 2> range_index_;
92 };
93 
94 // Min/Max/All, where "All" means use both min and max.
95 class MinMaxAll
96 {
97 public:
98   // Singleton accessors.
min()99   static MinMaxAll *min() { return &min_; }
early()100   static MinMaxAll *early() { return &min_; }
max()101   static MinMaxAll *max() { return &max_; }
late()102   static MinMaxAll *late() { return &max_; }
all()103   static MinMaxAll *all() { return &all_; }
asString() const104   const char *asString() const { return name_; }
index() const105   int index() const { return index_; }
106   MinMax *asMinMax() const;
107   bool matches(const MinMax *min_max) const;
108   bool matches(const MinMaxAll *min_max) const;
109   static MinMaxAll *find(const char *min_max);
110   // for (auto min_max : min_max->range()) {}
range() const111   const std::vector<MinMax*> &range() const { return range_; }
112   // for (auto mm_index : min_max->rangeIndex()) {}
rangeIndex() const113   const std::vector<int> &rangeIndex() const { return range_index_; }
114 
115 private:
116   DISALLOW_COPY_AND_ASSIGN(MinMaxAll);
117   MinMaxAll(const char *name,
118 	    int index,
119 	    std::vector<MinMax*> range,
120 	    std::vector<int> range_index);
121 
122   const char *name_;
123   int index_;
124   const std::vector<MinMax*> range_;
125   const std::vector<int> range_index_;
126 
127   static MinMaxAll min_;
128   static MinMaxAll max_;
129   static MinMaxAll all_;
130 };
131 
132 ////////////////////////////////////////////////////////////////
133 
134 // Obsolete. Use range iteration.
135 class MinMaxIterator : public Iterator<MinMax*>
136 {
137 public:
MinMaxIterator()138   MinMaxIterator() : index_(0), index_max_(MinMax::index_max) {}
139   explicit MinMaxIterator(const MinMaxAll *min_max);
hasNext()140   bool hasNext() { return index_ <= index_max_; }
next()141   MinMax *next()
142   { return (index_++ == 0) ? MinMax::min() : MinMax::max(); }
143 
144 private:
145   DISALLOW_COPY_AND_ASSIGN(MinMaxIterator);
146 
147   int index_;
148   int index_max_;
149 };
150 
151 typedef MinMaxIterator EarlyLateIterator;
152 
153 } // namespace
154