RDK Documentation (Open Sourced RDK Components)
uint33_t.h
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2021 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21  * @file uint33_t.h
22  * @brief Custom int variable to handle 33 bit
23  */
24 
25 #ifndef _UINT33_T_H
26 #define _UINT33_T_H
27 
28 #include <cassert>
29 #include <cstdint>
30 #include <limits>
31 
32 /**
33  * @struct uint33_t
34  * @brief Uint with size of 33 bits
35  */
36 struct uint33_t
37 {
38  uint64_t value : 33;
39  inline constexpr operator double() const { return static_cast<double>(value);}
40  inline constexpr operator bool() const { return static_cast<bool>(value);}
41  uint33_t& operator=(const uint64_t& i) { value = i; return *this;}
42  constexpr static uint33_t max_value() {return {0x1ffffffff};};
43  constexpr static uint33_t half_max() {return {uint33_t::max_value().value/2};}
44 };
45 
46 inline constexpr uint33_t operator+(const uint33_t& l, const uint33_t& r) { return {l.value + r.value}; }
47 inline constexpr uint33_t operator-(const uint33_t& l, const uint33_t& r) { return {l.value - r.value}; }
48 inline constexpr bool operator<(const uint33_t& l, const uint33_t& r) { return l.value < r.value; }
49 inline constexpr bool operator>(const uint33_t& l, const uint33_t& r) { return r < l; }
50 
51 inline constexpr bool operator==(const uint33_t& l, const uint33_t& r) { return l.value == r.value; }
52 inline constexpr bool operator!=(const uint33_t& l, const uint33_t& r) { return !(l == r);}
53 
54 template<typename Integer>
55 inline constexpr bool operator==(const uint33_t& l, const Integer& r) { return l == uint33_t{static_cast<uint64_t>(r)}; }
56 
57 template<typename Integer>
58 inline constexpr bool operator!=(const uint33_t& l, const Integer& r) { return !(l == r);}
59 
60 template<typename Integer>
61 inline constexpr bool operator==(const Integer& l, const uint33_t& r) { return r == l; }
62 
63 template<typename Integer>
64 inline constexpr bool operator!=(const Integer& l, const uint33_t& r) { return !(l == r);}
65 
66 namespace test_variables
67 {
68  constexpr uint33_t max_val{0x1ffffffff};
69 
70 #if defined(__clang__)
71 #pragma clang diagnostic push
72 #pragma clang diagnostic ignored "-Woverflow"
73 #elif defined(__GNUC__) || defined(__GNUG__)
74 #pragma GCC diagnostic push
75 #pragma GCC diagnostic ignored "-Woverflow"
76 #endif
77  //overflow on purpose to check the behaviour of the uint33_t in such scenario
78  constexpr uint33_t from_max_u64{std::numeric_limits<uint64_t>::max()};
79 #if defined(__clang__)
80 #pragma clang diagnostic pop
81 #elif defined(__GNUC__) || defined(__GNUG__)
82 #pragma GCC diagnostic pop
83 #endif
84 
85  constexpr uint33_t big_val{0x1fffffffc};
86  constexpr uint33_t two{2};
87  constexpr uint33_t three{3};
88  constexpr uint33_t four{4};
89  constexpr uint33_t seven{7};
90  constexpr uint33_t zero{0};
91 
92  static_assert(four.value == 4, "value representation");
93  static_assert(two.value == 2, "value representation");
94  static_assert(zero.value == 0, "value representation");
95  static_assert(max_val.value == 0x1ffffffff, "value representation");
96  static_assert(from_max_u64.value == 0x1ffffffff, "value representation");
97 
98  static_assert((double)four == 4.0, "implicit double operator");
99  static_assert((bool)four, "implicit bool operator - positive gives true");
100  static_assert(!!four, "implicit bool operator - positive gives true");
101  static_assert(!((bool)zero), "implicit bool operator - zero gives false");
102  static_assert(!zero, "implicit bool operator - zero gives false");
103 
104  static_assert(three + four == seven, "addition");
105 
106  static_assert(max_val + four == three, "addition");
107  static_assert(four + max_val == three, "addition");
108 
109  static_assert(seven - four == three, "substraction");
110  static_assert(three - seven == (max_val - three), "substraction");
111  static_assert(three - seven == (zero - four), "substraction");
112 
113  static_assert(three - big_val == seven, "substraction");
114 
115 
116  static_assert(three == three, "equality comparison");
117  static_assert(!(three == four), "equality comparison");
118  static_assert(three != four, "equality comparison");
119  static_assert(!(three != three), "equality comparison");
120  static_assert(from_max_u64 == max_val, "equality comparison");
121 
122 
123  static_assert(three == 3, "int equality comparison");
124  static_assert(three != 4, "int equality comparison");
125  static_assert(!(three == 4), "int equality comparison");
126 
127  static_assert(three < four, "lt comparison");
128  static_assert(!(three < three), "lt comparison");
129  static_assert(!(three < two), "lt comparison");
130 
131  static_assert(four > three, "gt comparison");
132  static_assert(!(three > three), "gt comparison");
133  static_assert(!(two > three), "gt comparison");
134 
135 
136 #ifndef NDEBUG
137 namespace
138 {
139  //C++11 does not support complex contexpr expressions, hence moving to runtime test
140  int test()
141  {
142  uint33_t x{3};
143  x = 4;
144  assert(x == four && "int assignment");
145  return 42;
146  }
147  int test_ = test();
148 }
149 #endif
150 }
151 
152 #endif
uint33_t
Uint with size of 33 bits.
Definition: uint33_t.h:36