1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6
7 #include <cstdint>
8 #include <string>
9 #include <ostream>
10 #include <sstream>
11
12 namespace arm
13 {
14
15 namespace pipe
16 {
17
EncodeVersion(uint32_t major,uint32_t minor,uint32_t patch)18 constexpr inline uint32_t EncodeVersion(uint32_t major, uint32_t minor, uint32_t patch)
19 {
20 return (major << 22) | (minor << 12) | patch;
21 }
22
23 // Encodes a semantic version https://semver.org/ into a 32 bit integer in the following fashion
24 //
25 // bits 22:31 major: Unsigned 10-bit integer. Major component of the schema version number.
26 // bits 12:21 minor: Unsigned 10-bit integer. Minor component of the schema version number.
27 // bits 0:11 patch: Unsigned 12-bit integer. Patch component of the schema version number.
28 //
29 class Version
30 {
31 public:
Version(uint32_t encodedValue)32 Version(uint32_t encodedValue)
33 {
34 m_Major = (encodedValue >> 22) & 1023;
35 m_Minor = (encodedValue >> 12) & 1023;
36 m_Patch = encodedValue & 4095;
37 }
38
Version(uint32_t major,uint32_t minor,uint32_t patch)39 Version(uint32_t major, uint32_t minor, uint32_t patch) :
40 m_Major(major),
41 m_Minor(minor),
42 m_Patch(patch)
43 {}
44
GetEncodedValue()45 uint32_t GetEncodedValue()
46 {
47 return EncodeVersion(m_Major, m_Minor, m_Patch);
48 }
49
GetMajor()50 uint32_t GetMajor() { return m_Major; }
GetMinor()51 uint32_t GetMinor() { return m_Minor; }
GetPatch()52 uint32_t GetPatch() { return m_Patch; }
53
operator ==(const Version & other) const54 bool operator==(const Version& other) const
55 {
56 return m_Major == other.m_Major && m_Minor == other.m_Minor && m_Patch == other.m_Patch;
57 }
58
ToString() const59 std::string ToString() const
60 {
61 constexpr char separator = '.';
62
63 std::stringstream stringStream;
64 stringStream << m_Major << separator << m_Minor << separator << m_Patch;
65
66 return stringStream.str();
67 }
68
69 private:
70 uint32_t m_Major;
71 uint32_t m_Minor;
72 uint32_t m_Patch;
73 };
74
operator <<(std::ostream & os,const Version & version)75 inline std::ostream& operator<<(std::ostream& os, const Version& version)
76 {
77 os << version.ToString();
78 return os;
79 }
80
81 } // namespace pipe
82
83 } // namespace arm
84