OpenSTA/liberty/Units.cc

187 lines
3.9 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
2020-03-07 03:50:37 +01:00
// Copyright (c) 2020, Parallax Software, Inc.
2018-09-28 17:54:21 +02: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
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-04-05 23:53:44 +02:00
#include "Units.hh"
2020-04-05 20:35:51 +02:00
2018-09-28 17:54:21 +02:00
#include <cmath> // abs
#include <limits>
2020-04-05 20:35:51 +02:00
2020-04-05 23:53:44 +02:00
#include "StringUtil.hh"
#include "MinMax.hh" // INF
2020-06-24 02:11:48 +02:00
#include "Fuzzy.hh"
2018-09-28 17:54:21 +02:00
namespace sta {
using std::abs;
2020-06-24 02:11:48 +02:00
Unit::Unit(const char *suffix) :
2018-09-28 17:54:21 +02:00
scale_(1.0),
2020-06-24 02:11:48 +02:00
suffix_(stringCopy(suffix)),
digits_(3)
2018-09-28 17:54:21 +02:00
{
}
Unit::Unit(float scale,
const char *suffix,
int digits) :
scale_(scale),
suffix_(stringCopy(suffix)),
digits_(digits)
{
}
Unit::~Unit()
{
stringDelete(suffix_);
}
void
2019-11-11 21:03:38 +01:00
Unit::operator=(const Unit &unit)
2018-09-28 17:54:21 +02:00
{
2019-11-11 21:03:38 +01:00
scale_ = unit.scale_;
stringDelete(suffix_);
suffix_ = stringCopy(unit.suffix_);
digits_ = unit.digits_;
2018-09-28 17:54:21 +02:00
}
void
Unit::setScale(float scale)
{
scale_ = scale;
}
2020-06-24 02:11:48 +02:00
const char *
Unit::scaleAbreviation()
{
if (fuzzyEqual(scale_, 1E+6))
return "M";
else if (fuzzyEqual(scale_, 1E+3))
return "k";
if (fuzzyEqual(scale_, 1.0))
return "";
else if (fuzzyEqual(scale_, 1E-3))
return "m";
else if (fuzzyEqual(scale_, 1E-6))
return "u";
else if (fuzzyEqual(scale_, 1E-9))
return "n";
else if (fuzzyEqual(scale_, 1E-12))
return "p";
else if (fuzzyEqual(scale_, 1E-15))
return "f";
else
return "?";
}
2018-09-28 17:54:21 +02:00
void
Unit::setSuffix(const char *suffix)
{
2019-11-11 21:03:38 +01:00
stringDelete(suffix_);
2018-09-28 17:54:21 +02:00
suffix_ = stringCopy(suffix);
}
void
Unit::setDigits(int digits)
{
digits_ = digits;
}
2018-11-26 18:15:52 +01:00
int
Unit::width() const
{
2020-06-24 02:11:48 +02:00
return digits_ + 2;
2018-11-26 18:15:52 +01:00
}
2018-09-28 17:54:21 +02:00
const char *
Unit::asString(float value) const
{
return asString(value, digits_);
}
const char *
Unit::asString(double value) const
{
return asString(static_cast<float>(value), digits_);
}
const char *
Unit::asString(float value,
int digits) const
{
// Special case INF because it blows up otherwise.
2018-10-03 01:20:18 +02:00
if (abs(value) >= INF * .1)
return (value > 0.0) ? "INF" : "-INF";
2018-09-28 17:54:21 +02:00
else {
float scaled_value = value / scale_;
// prevent "-0.00" on slowaris
if (abs(scaled_value) < 1E-6)
scaled_value = 0.0;
2020-06-24 02:11:48 +02:00
return stringPrintTmp("%.*f", digits, scaled_value);
2018-09-28 17:54:21 +02:00
}
}
////////////////////////////////////////////////////////////////
2020-06-24 02:11:48 +02:00
Units::Units() :
time_unit_("s"),
capacitance_unit_("F"),
voltage_unit_("v"),
resistance_unit_("ohm"),
pulling_resistance_unit_("ohm"),
current_unit_("A"),
power_unit_("W"),
distance_unit_("m"),
scalar_unit_("")
{
}
2018-09-28 17:54:21 +02:00
Unit *
Units::find(const char *unit_name)
{
if (stringEq(unit_name, "time"))
return &time_unit_;
else if (stringEq(unit_name, "capacitance"))
return &capacitance_unit_;
else if (stringEq(unit_name, "resistance"))
return &resistance_unit_;
else if (stringEq(unit_name, "voltage"))
return &voltage_unit_;
else if (stringEq(unit_name, "current"))
return &current_unit_;
else if (stringEq(unit_name, "power"))
return &power_unit_;
else if (stringEq(unit_name, "distance"))
return &distance_unit_;
else
2019-03-13 01:25:53 +01:00
return nullptr;
2018-09-28 17:54:21 +02:00
}
void
2019-11-11 21:03:38 +01:00
Units::operator=(const Units &units)
2018-09-28 17:54:21 +02:00
{
2019-11-11 21:03:38 +01:00
time_unit_ = *units.timeUnit();
capacitance_unit_ = *units.capacitanceUnit();
resistance_unit_ = *units.resistanceUnit();
voltage_unit_ = *units.voltageUnit();
current_unit_ = *units.currentUnit();
power_unit_ = *units.powerUnit();
distance_unit_ = *units.distanceUnit();
scalar_unit_ = *units.scalarUnit();
2018-09-28 17:54:21 +02:00
}
} // namespace