Files
OpenSTA/include/sta/MinMaxValues.hh
T

209 lines
5.3 KiB
C++
Raw Normal View History

2018-09-28 08:54:21 -07:00
// OpenSTA, Static Timing Analyzer
2026-03-10 13:21:17 -07:00
// Copyright (c) 2026, Parallax Software, Inc.
2018-09-28 08:54:21 -07:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
2022-01-04 10:17:08 -07:00
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2018-09-28 08:54:21 -07:00
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
2022-01-04 10:17:08 -07:00
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2025-01-21 18:54:33 -07:00
//
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.
//
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// This notice may not be removed or altered from any source distribution.
2018-09-28 08:54:21 -07:00
2020-02-15 17:13:16 -07:00
#pragma once
2018-09-28 08:54:21 -07:00
2020-04-05 14:53:44 -07:00
#include "Error.hh"
2026-04-15 09:38:10 -07:00
#include "MinMax.hh"
2018-09-28 08:54:21 -07:00
namespace sta {
template <class TYPE>
class MinMaxValues
{
public:
MinMaxValues()
{
clear();
}
MinMaxValues(TYPE init_value)
{
int mm_index;
mm_index = MinMax::minIndex();
values_[mm_index] = init_value;
exists_[mm_index] = true;
mm_index = MinMax::maxIndex();
values_[mm_index] = init_value;
exists_[mm_index] = true;
}
void
clear()
{
exists_[MinMax::minIndex()] = false;
exists_[MinMax::maxIndex()] = false;
}
void
clear(const MinMaxAll *min_max)
{
exists_[min_max->index()] = false;
}
bool
empty()
{
return !exists_[MinMax::minIndex()]
&& !exists_[MinMax::maxIndex()];
}
void
setValue(TYPE value)
{
2019-07-18 06:19:00 -07:00
for (auto mm_index : MinMax::rangeIndex()) {
2018-09-28 08:54:21 -07:00
values_[mm_index] = value;
exists_[mm_index] = true;
}
}
void
setValue(const MinMaxAll *min_max,
2026-01-03 16:59:35 -08:00
TYPE value)
2018-09-28 08:54:21 -07:00
{
2019-07-18 06:19:00 -07:00
for (auto mm_index : min_max->rangeIndex()) {
2018-09-28 08:54:21 -07:00
values_[mm_index] = value;
exists_[mm_index] = true;
}
}
void
setValue(const MinMax *min_max,
2026-01-03 16:59:35 -08:00
TYPE value)
2018-09-28 08:54:21 -07:00
{
int mm_index = min_max->index();
values_[mm_index] = value;
exists_[mm_index] = true;
}
void
mergeValue(const MinMax *min_max,
2026-01-03 16:59:35 -08:00
TYPE value)
2018-09-28 08:54:21 -07:00
{
int mm_index = min_max->index();
if (!exists_[mm_index]
2026-01-03 16:59:35 -08:00
|| min_max->compare(value, values_[mm_index])) {
2018-09-28 08:54:21 -07:00
values_[mm_index] = value;
exists_[mm_index] = true;
}
}
TYPE
value(const MinMax *min_max) const
{
int mm_index = min_max->index();
bool exists = exists_[mm_index];
if (exists)
return values_[mm_index];
2023-10-04 14:33:09 -07:00
else {
2020-12-13 18:21:35 -07:00
criticalError(226, "uninitialized value reference");
2023-10-04 14:33:09 -07:00
return 0.0;
}
2018-09-28 08:54:21 -07:00
}
void
value(const MinMax *min_max,
2026-01-03 16:59:35 -08:00
// Return values.
TYPE &value,
bool &exists) const
2018-09-28 08:54:21 -07:00
{
int mm_index = min_max->index();
exists = exists_[mm_index];
value = values_[mm_index];
}
bool
hasValue(const MinMax *min_max) const
{
int mm_index = min_max->index();
return exists_[mm_index];
}
void
removeValue(const MinMaxAll *min_max)
{
2019-07-18 06:19:00 -07:00
for (auto mm_index : min_max->rangeIndex())
2018-09-28 08:54:21 -07:00
exists_[mm_index] = false;
}
2025-09-14 11:43:59 -07:00
static bool equal(const MinMaxValues *values1,
2026-01-03 16:59:35 -08:00
const MinMaxValues *values2)
2018-09-28 08:54:21 -07:00
{
return ((!values1->exists_[MinMax::minIndex()]
2026-01-03 16:59:35 -08:00
&& !values2->exists_[MinMax::minIndex()])
|| (values1->exists_[MinMax::minIndex()]
&& values2->exists_[MinMax::minIndex()]
&& values1->values_[MinMax::minIndex()]
== values2->values_[MinMax::minIndex()]))
2018-09-28 08:54:21 -07:00
&& ((!values1->exists_[MinMax::maxIndex()]
2026-01-03 16:59:35 -08:00
&& !values2->exists_[MinMax::maxIndex()])
|| (values1->exists_[MinMax::maxIndex()]
&& values2->exists_[MinMax::maxIndex()]
&& values1->values_[MinMax::maxIndex()]
== values2->values_[MinMax::maxIndex()]));
2018-09-28 08:54:21 -07:00
}
2025-09-16 13:53:16 -07:00
static int cmp(const MinMaxValues *values1,
2026-01-03 16:59:35 -08:00
const MinMaxValues *values2)
2025-09-14 11:43:59 -07:00
{
if (!values1->exists_[MinMax::minIndex()]
2026-04-25 10:35:42 -07:00
&& !values2->exists_[MinMax::minIndex()]
&& !values1->exists_[MinMax::maxIndex()]
&& !values2->exists_[MinMax::maxIndex()])
return 0;
if (!values1->exists_[MinMax::minIndex()]
2026-01-03 16:59:35 -08:00
&& values2->exists_[MinMax::minIndex()])
2025-09-14 11:43:59 -07:00
return -1;
if (values1->exists_[MinMax::minIndex()]
2026-01-03 16:59:35 -08:00
&& !values2->exists_[MinMax::minIndex()])
2025-09-14 11:43:59 -07:00
return 1;
if (!values1->exists_[MinMax::maxIndex()]
2026-01-03 16:59:35 -08:00
&& values2->exists_[MinMax::maxIndex()])
2025-09-14 11:43:59 -07:00
return -1;
if (values1->exists_[MinMax::maxIndex()]
2026-01-03 16:59:35 -08:00
&& !values2->exists_[MinMax::maxIndex()])
2025-09-14 11:43:59 -07:00
return 1;
if (values1->values_[MinMax::minIndex()] < values2->values_[MinMax::minIndex()])
return -1;
if (values1->values_[MinMax::minIndex()] > values2->values_[MinMax::minIndex()])
return 1;
if (values1->values_[MinMax::maxIndex()] < values2->values_[MinMax::maxIndex()])
return -1;
if (values1->values_[MinMax::maxIndex()] > values2->values_[MinMax::maxIndex()])
return 1;
return 0;
}
2018-09-28 08:54:21 -07:00
private:
TYPE values_[MinMax::index_count];
bool exists_[MinMax::index_count];
};
2026-01-03 16:59:35 -08:00
using MinMaxFloatValues = MinMaxValues<float>;
using MinMaxIntValues = MinMaxValues<int>;
2018-09-28 08:54:21 -07:00
2026-04-13 14:58:16 -07:00
} // namespace sta