Files
OpenSTA/util/MinMax.cc
T

186 lines
4.4 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-04-05 14:53:44 -07:00
#include "MinMax.hh"
2020-04-05 11:35:51 -07:00
2018-09-28 08:54:21 -07:00
#include <algorithm>
2024-03-20 12:29:58 -07:00
#include <limits>
2020-04-05 11:35:51 -07:00
2020-04-05 14:53:44 -07:00
#include "StringUtil.hh"
2018-09-28 08:54:21 -07:00
namespace sta {
const float INF = 1E+30F;
static bool
compareMin(float value1,
2026-01-03 16:59:35 -08:00
float value2)
2018-09-28 08:54:21 -07:00
{
return value1 < value2;
}
static bool
compareMax(float value1,
2026-01-03 16:59:35 -08:00
float value2)
2018-09-28 08:54:21 -07:00
{
return value1 > value2;
}
////////////////////////////////////////////////////////////////
2025-03-30 15:27:53 -07:00
const MinMax MinMax::min_("min", 0, INF, std::numeric_limits<int>::max(), compareMin);
const MinMax MinMax::max_("max", 1, -INF, std::numeric_limits<int>::min(), compareMax);
const std::array<const MinMax*, 2> MinMax::range_{&min_, &max_};
2026-04-13 14:58:16 -07:00
const std::array<size_t, 2> MinMax::range_index_{min_.index(), max_.index()};
2018-09-28 08:54:21 -07:00
2026-04-13 14:58:16 -07:00
MinMax::MinMax(std::string_view name,
size_t index,
2026-01-03 16:59:35 -08:00
float init_value,
2024-03-20 12:29:58 -07:00
int init_value_int,
2026-01-03 16:59:35 -08:00
bool (*compare)(float value1, float value2)) :
2018-09-28 08:54:21 -07:00
name_(name),
index_(index),
init_value_(init_value),
2024-03-20 12:29:58 -07:00
init_value_int_(init_value_int),
2018-09-28 08:54:21 -07:00
compare_(compare)
{
}
2025-03-30 15:27:53 -07:00
const MinMaxAll *
2018-09-28 08:54:21 -07:00
MinMax::asMinMaxAll() const
{
2019-07-18 06:19:00 -07:00
if (this == &min_)
2018-09-28 08:54:21 -07:00
return MinMaxAll::min();
else
return MinMaxAll::max();
}
2025-03-30 15:27:53 -07:00
const MinMax *
2018-09-28 08:54:21 -07:00
MinMax::opposite() const
{
2019-07-18 06:19:00 -07:00
if (this == &max_)
return &min_;
2018-09-28 08:54:21 -07:00
else
2019-07-18 06:19:00 -07:00
return &max_;
2018-09-28 08:54:21 -07:00
}
2025-03-30 15:27:53 -07:00
const MinMax *
2026-04-13 14:58:16 -07:00
MinMax::find(std::string_view min_max)
2018-09-28 08:54:21 -07:00
{
2026-03-28 19:13:35 -07:00
if (stringEqual(min_max, "min")
|| stringEqual(min_max, "early"))
2019-07-18 06:19:00 -07:00
return &min_;
2026-03-28 19:13:35 -07:00
else if (stringEqual(min_max, "max")
|| stringEqual(min_max, "late"))
2019-07-18 06:19:00 -07:00
return &max_;
2018-09-28 08:54:21 -07:00
else
2019-03-12 17:25:53 -07:00
return nullptr;
2018-09-28 08:54:21 -07:00
}
2025-03-30 15:27:53 -07:00
const MinMax *
2026-04-13 14:58:16 -07:00
MinMax::find(size_t index)
2018-09-28 08:54:21 -07:00
{
2019-07-18 06:19:00 -07:00
if (index == min_.index())
return &min_;
else if (index == max_.index())
return &max_;
2018-09-28 08:54:21 -07:00
else
2019-03-12 17:25:53 -07:00
return nullptr;
2018-09-28 08:54:21 -07:00
}
bool
MinMax::compare(float value1,
2026-01-03 16:59:35 -08:00
float value2) const
2018-09-28 08:54:21 -07:00
{
return compare_(value1, value2);
}
float
MinMax::minMax(float value1,
2026-01-03 16:59:35 -08:00
float value2) const
{
if (compare_(value1, value2))
return value1;
else
return value2;
}
2018-09-28 08:54:21 -07:00
////////////////////////////////////////////////////////////////
2025-03-30 15:27:53 -07:00
const MinMaxAll MinMaxAll::min_("min", 0, {MinMax::min()}, {MinMax::min()->index()});
const MinMaxAll MinMaxAll::max_("max", 1, {MinMax::max()}, {MinMax::max()->index()});
const MinMaxAll MinMaxAll::all_("all", 2, {MinMax::min(), MinMax::max()},
2026-01-03 16:59:35 -08:00
{MinMax::min()->index(), MinMax::max()->index()});
2018-09-28 08:54:21 -07:00
2026-04-13 14:58:16 -07:00
MinMaxAll::MinMaxAll(std::string_view name,
size_t index,
const std::vector<const MinMax*> &range,
const std::vector<size_t> &range_index) :
2018-09-28 08:54:21 -07:00
name_(name),
2019-07-18 06:19:00 -07:00
index_(index),
range_(range),
range_index_(range_index)
2018-09-28 08:54:21 -07:00
{
}
2025-03-30 15:27:53 -07:00
const MinMax *
2018-09-28 08:54:21 -07:00
MinMaxAll::asMinMax() const
{
2019-07-18 06:19:00 -07:00
if (this == &min_)
2018-09-28 08:54:21 -07:00
return MinMax::min();
else
return MinMax::max();
}
bool
MinMaxAll::matches(const MinMax *min_max) const
{
2019-07-18 06:19:00 -07:00
return this == &all_ || asMinMax() == min_max;
2018-09-28 08:54:21 -07:00
}
bool
MinMaxAll::matches(const MinMaxAll *min_max) const
{
2019-07-18 06:19:00 -07:00
return this == &all_ || this == min_max;
2018-09-28 08:54:21 -07:00
}
2025-03-30 15:27:53 -07:00
const MinMaxAll *
2018-09-28 08:54:21 -07:00
MinMaxAll::find(const char *min_max)
{
2026-03-28 19:13:35 -07:00
if (stringEqual(min_max, "min")
|| stringEqual(min_max, "early"))
2019-07-18 06:19:00 -07:00
return &min_;
2026-03-28 19:13:35 -07:00
else if (stringEqual(min_max, "max")
|| stringEqual(min_max, "late"))
2019-07-18 06:19:00 -07:00
return &max_;
2026-03-28 19:13:35 -07:00
else if (stringEqual(min_max, "all")
|| stringEqual(min_max, "min_max")
|| stringEqual(min_max, "minmax"))
2019-07-18 06:19:00 -07:00
return &all_;
2018-09-28 08:54:21 -07:00
else
2019-03-12 17:25:53 -07:00
return nullptr;
2018-09-28 08:54:21 -07:00
}
2026-04-13 14:58:16 -07:00
} // namespace sta