IVGCVSW-3429 Add a utility Version class
[platform/upstream/armnn.git] / src / profiling / EncodeVersion.hpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6
7 #include <cstddef>
8
9 namespace mlutil
10 {
11
12 namespace Impl
13 {
14
15     constexpr uint32_t EncodeVersion(uint32_t major, uint32_t minor, uint32_t patch)
16     {
17         return (major << 22) | (minor << 12) | patch;
18     }
19
20 } // namespace Impl
21
22 // Encodes a semantic version https://semver.org/ into a 32 bit integer in the following fashion
23 //
24 // bits 22:31 major: Unsigned 10-bit integer. Major component of the schema version number.
25 // bits 12:21 minor: Unsigned 10-bit integer. Minor component of the schema version number.
26 // bits 0:11  patch: Unsigned 12-bit integer. Patch component of the schema version number.
27 //
28 class Version
29 {
30 public:
31     Version(uint32_t encodedValue)
32     {
33         m_Major = (encodedValue >> 22) & 1023;
34         m_Minor = (encodedValue >> 12) & 1023;
35         m_Patch = encodedValue & 4095;
36     }
37
38     Version(uint32_t major, uint32_t minor, uint32_t patch)
39     : m_Major(major), m_Minor(minor), m_Patch(patch) {}
40
41     uint32_t GetEncodedValue()
42     {
43         return mlutil::Impl::EncodeVersion(m_Major, m_Minor, m_Patch);
44     }
45
46     uint32_t GetMajor() {return m_Major;}
47     uint32_t GetMinor() {return m_Minor;}
48     uint32_t GetPatch() {return m_Patch;}
49
50 private:
51     uint32_t m_Major;
52     uint32_t m_Minor;
53     uint32_t m_Patch;
54 };
55
56 } // namespace mlutil