xref: /aosp_15_r20/external/deqp/framework/delibs/decpp/deDefs.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _DEDEFS_HPP
2 #define _DEDEFS_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements C++ Base Library
5  * -----------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Basic definitions.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "deDefs.h"
27 #include "deInt32.h"
28 
29 #if !defined(__cplusplus)
30 #error "C++ is required"
31 #endif
32 
33 #include <type_traits>
34 #include <utility>
35 
36 namespace de
37 {
38 
39 //! Compute absolute value of x.
40 template <typename T>
abs(T x)41 inline T abs(T x)
42 {
43     return x < T(0) ? -x : x;
44 }
45 
46 //! Get minimum of x and y.
47 template <typename T>
min(T x,T y)48 inline T min(T x, T y)
49 {
50     return x <= y ? x : y;
51 }
52 
53 //! Get maximum of x and y.
54 template <typename T>
max(T x,T y)55 inline T max(T x, T y)
56 {
57     return x >= y ? x : y;
58 }
59 
60 //! Clamp x in range a <= x <= b.
61 template <typename T>
clamp(T x,T a,T b)62 inline T clamp(T x, T a, T b)
63 {
64     DE_ASSERT(a <= b);
65     return x < a ? a : (x > b ? b : x);
66 }
67 
68 //! Test if x is in bounds a <= x < b.
69 template <typename T>
inBounds(T x,T a,T b)70 inline bool inBounds(T x, T a, T b)
71 {
72     return a <= x && x < b;
73 }
74 
75 //! Test if x is in range a <= x <= b.
76 template <typename T>
inRange(T x,T a,T b)77 inline bool inRange(T x, T a, T b)
78 {
79     return a <= x && x <= b;
80 }
81 
82 //! Return T with low n bits set
83 template <typename T>
rightSetMask(T n)84 inline T rightSetMask(T n)
85 {
86     DE_ASSERT(n < T(sizeof(T) * 8));
87     T one = T(1);
88     return T((one << n) - one);
89 }
90 
91 //! Return T with low n bits reset
92 template <typename T>
rightZeroMask(T n)93 inline T rightZeroMask(T n)
94 {
95     return T(~rightSetMask(n));
96 }
97 
98 //! Return T with high n bits set
99 template <typename T>
leftSetMask(T n)100 inline T leftSetMask(T n)
101 {
102     const T tlen = T(sizeof(T) * 8);
103     return T(~rightSetMask(tlen >= n ? tlen - n : T(0)));
104 }
105 
106 //! Return T with high n bits reset
107 template <typename T>
leftZeroMask(T n)108 inline T leftZeroMask(T n)
109 {
110     return T(~leftSetMask(n));
111 }
112 
113 //! Round x up to a multiple of y.
114 template <typename T>
roundUp(T x,T y)115 inline T roundUp(T x, T y)
116 {
117     DE_ASSERT(y != T(0));
118     const T mod = x % y;
119     return x + ((mod == T(0)) ? T(0) : (y - mod));
120 }
121 
122 //! Round x down to a multiple of y.
123 template <typename T>
roundDown(T x,T y)124 inline T roundDown(T x, T y)
125 {
126     DE_ASSERT(y != T(0));
127     return (x / y) * y;
128 }
129 
130 //! Find the greatest common divisor of x and y.
131 template <typename T>
gcd(T x,T y)132 T gcd(T x, T y)
133 {
134     DE_ASSERT(std::is_integral<T>::value && std::is_unsigned<T>::value);
135 
136     // Euclidean algorithm.
137     while (y != T{0})
138     {
139         T mod = x % y;
140         x     = y;
141         y     = mod;
142     }
143 
144     return x;
145 }
146 
147 //! Find the least common multiple of x and y.
148 template <typename T>
lcm(T x,T y)149 T lcm(T x, T y)
150 {
151     DE_ASSERT(std::is_integral<T>::value && std::is_unsigned<T>::value);
152 
153     T prod = x * y;
154     DE_ASSERT(x == 0 || prod / x == y); // Check overflow just in case.
155     return (prod) / gcd(x, y);
156 }
157 
158 //! Helper for DE_CHECK() macros.
159 void throwRuntimeError(const char *message, const char *expr, const char *file, int line);
160 
161 //! Default deleter.
162 template <typename T>
163 struct DefaultDeleter
164 {
DefaultDeleterde::DefaultDeleter165     inline DefaultDeleter(void)
166     {
167     }
168     template <typename U>
DefaultDeleterde::DefaultDeleter169     inline DefaultDeleter(const DefaultDeleter<U> &)
170     {
171     }
172     template <typename U>
operator =de::DefaultDeleter173     inline DefaultDeleter<T> &operator=(const DefaultDeleter<U> &)
174     {
175         return *this;
176     }
operator ()de::DefaultDeleter177     inline void operator()(T *ptr) const
178     {
179         delete ptr;
180     }
181 };
182 
183 //! A deleter for arrays
184 template <typename T>
185 struct ArrayDeleter
186 {
ArrayDeleterde::ArrayDeleter187     inline ArrayDeleter(void)
188     {
189     }
190     template <typename U>
ArrayDeleterde::ArrayDeleter191     inline ArrayDeleter(const ArrayDeleter<U> &)
192     {
193     }
194     template <typename U>
operator =de::ArrayDeleter195     inline ArrayDeleter<T> &operator=(const ArrayDeleter<U> &)
196     {
197         return *this;
198     }
operator ()de::ArrayDeleter199     inline void operator()(T *ptr) const
200     {
201         delete[] ptr;
202     }
203 };
204 
205 //! Get required memory alignment for type
206 template <typename T>
alignOf(void)207 size_t alignOf(void)
208 {
209     struct PaddingCheck
210     {
211         uint8_t b;
212         T t;
213     };
214     return (size_t)offsetof(PaddingCheck, t);
215 }
216 
217 //! Similar to DE_LENGTH_OF_ARRAY but constexpr and without auxiliar helpers.
218 template <typename T, size_t N>
arrayLength(const T (&)[N])219 constexpr size_t arrayLength(const T (&)[N])
220 {
221     return N;
222 }
223 
224 //! Get least significant bit index
findLSB(uint32_t value)225 inline int findLSB(uint32_t value)
226 {
227     return value ? deCtz32(value) : (-1);
228 }
229 
230 //! Get most significant bit index
findMSB(uint32_t value)231 inline int findMSB(uint32_t value)
232 {
233     return 31 - deClz32(value);
234 }
235 
236 //! Get most significant bit index
findMSB(int32_t value)237 inline int findMSB(int32_t value)
238 {
239     return (value < 0) ? findMSB(~(uint32_t)value) : findMSB((uint32_t)value);
240 }
241 
242 } // namespace de
243 
244 /*--------------------------------------------------------------------*//*!
245  * \brief Throw runtime error if condition is not met.
246  * \param X        Condition to check.
247  *
248  * This macro throws std::runtime_error if condition X is not met.
249  *//*--------------------------------------------------------------------*/
250 #define DE_CHECK_RUNTIME_ERR(X)                                       \
251     do                                                                \
252     {                                                                 \
253         if ((!false && (X)) ? false : true)                           \
254             ::de::throwRuntimeError(DE_NULL, #X, __FILE__, __LINE__); \
255     } while (false)
256 
257 /*--------------------------------------------------------------------*//*!
258  * \brief Throw runtime error if condition is not met.
259  * \param X        Condition to check.
260  * \param MSG    Additional message to include in the exception.
261  *
262  * This macro throws std::runtime_error with message MSG if condition X is
263  * not met.
264  *//*--------------------------------------------------------------------*/
265 #define DE_CHECK_RUNTIME_ERR_MSG(X, MSG)                          \
266     do                                                            \
267     {                                                             \
268         if ((!false && (X)) ? false : true)                       \
269             ::de::throwRuntimeError(MSG, #X, __FILE__, __LINE__); \
270     } while (false)
271 
272 //! Get array start pointer.
273 #define DE_ARRAY_BEGIN(ARR) (&(ARR)[0])
274 
275 //! Get array end pointer.
276 #define DE_ARRAY_END(ARR) (DE_ARRAY_BEGIN(ARR) + DE_LENGTH_OF_ARRAY(ARR))
277 
278 //! Empty C++ compilation unit silencing.
279 #if (DE_COMPILER == DE_COMPILER_MSC)
280 #define DE_EMPTY_CPP_FILE \
281     namespace             \
282     {                     \
283     uint8_t unused;       \
284     }
285 #else
286 #define DE_EMPTY_CPP_FILE
287 #endif
288 
289 // Warn if type is constructed, but left unused
290 //
291 // Used in types with non-trivial ctor/dtor but with ctor-dtor pair causing no (observable)
292 // side-effects.
293 //
294 // \todo add attribute for GCC
295 #if (DE_COMPILER == DE_COMPILER_CLANG) && defined(__has_attribute)
296 #if __has_attribute(warn_unused)
297 #define DE_WARN_UNUSED_TYPE __attribute__((warn_unused))
298 #else
299 #define DE_WARN_UNUSED_TYPE
300 #endif
301 #else
302 #define DE_WARN_UNUSED_TYPE
303 #endif
304 
305 #endif // _DEDEFS_HPP
306