Skip to content

Commit 92396e4

Browse files
committed
add utils for magnitudes
1 parent 3e782e0 commit 92396e4

File tree

1 file changed

+32
-0
lines changed
  • IntelPresentMon/CommonUtilities

1 file changed

+32
-0
lines changed

IntelPresentMon/CommonUtilities/Math.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,36 @@ namespace pmon::util
2121
{
2222
return (a - b) < CommonEpsilon(a, b);
2323
}
24+
enum class MagnitudePrefix
25+
{
26+
Base,
27+
Kilo,
28+
Kibi,
29+
Mega,
30+
Mebi,
31+
Giga,
32+
Gibi,
33+
};
34+
constexpr double GetMagnitudeFactor(MagnitudePrefix prefix)
35+
{
36+
switch (prefix) {
37+
case MagnitudePrefix::Base: return 1.;
38+
case MagnitudePrefix::Kilo: return 1'000.;
39+
case MagnitudePrefix::Kibi: return 1'024.;
40+
case MagnitudePrefix::Mega: return 1'000'000.;
41+
case MagnitudePrefix::Mebi: return 1'048'576.;
42+
case MagnitudePrefix::Giga: return 1'000'000'000.;
43+
case MagnitudePrefix::Gibi: return 1'073'741'824.;
44+
default: return 0.;
45+
}
46+
}
47+
template<typename From, typename To = From>
48+
To ConvertMagnitudePrefix(From from, MagnitudePrefix fromPrefix, MagnitudePrefix toPrefix)
49+
{
50+
auto fromExtended = double(from);
51+
const auto srcFactor = GetMagnitudeFactor(fromPrefix);
52+
const auto dstFactor = GetMagnitudeFactor(toPrefix);
53+
const auto conversionFactor = srcFactor / dstFactor;
54+
return To(fromExtended * conversionFactor);
55+
}
2456
}

0 commit comments

Comments
 (0)