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.
abs(T x)40 template<typename T> inline T abs (T x) { return x < T(0) ? -x : x; }
41
42 //! Get minimum of x and y.
min(T x, T y)43 template<typename T> inline T min (T x, T y) { return x <= y ? x : y; }
44
45 //! Get maximum of x and y.
max(T x, T y)46 template<typename T> inline T max (T x, T y) { return x >= y ? x : y; }
47
48 //! Clamp x in range a <= x <= b.
clamp(T x, T a, T b)49 template<typename T> inline T clamp (T x, T a, T b) { DE_ASSERT(a <= b); return x < a ? a : (x > b ? b : x); }
50
51 //! Test if x is in bounds a <= x < b.
inBounds(T x, T a, T b)52 template<typename T> inline bool inBounds (T x, T a, T b) { return a <= x && x < b; }
53
54 //! Test if x is in range a <= x <= b.
inRange(T x, T a, T b)55 template<typename T> inline bool inRange (T x, T a, T b) { return a <= x && x <= b; }
56
57 //! Return T with low n bits set
rightSetMask(T n)58 template<typename T> inline T rightSetMask (T n) { DE_ASSERT(n < T(sizeof(T) * 8)); T one = T(1); return T((one << n) - one); }
59
60 //! Return T with low n bits reset
61 template<typename T> inline T rightZeroMask (T n) { return T(~rightSetMask(n)); }
62
63 //! Return T with high n bits set
64 template<typename T> inline T leftSetMask (T n) { const T tlen = T(sizeof(T) * 8); return T(~rightSetMask(tlen >= n ? tlen - n : T(0))); }
65
66 //! Return T with high n bits reset
67 template<typename T> inline T leftZeroMask (T n) { return T(~leftSetMask(n)); }
68
69 //! Round x up to a multiple of y.
70 template<typename T> inline T roundUp (T x, T y) { DE_ASSERT(y != T(0)); const T mod = x % y; return x + ((mod == T(0)) ? T(0) : (y - mod)); }
71
72 //! Round x down to a multiple of y.
73 template<typename T> inline T roundDown (T x, T y) { DE_ASSERT(y != T(0)); return (x / y) * y; }
74
75 //! Find the greatest common divisor of x and y.
76 template<typename T>
77 T gcd (T x, T y)
78 {
79 DE_ASSERT(std::is_integral<T>::value && std::is_unsigned<T>::value);
80
81 // Euclidean algorithm.
82 while (y != T{0})
83 {
84 T mod = x % y;
85 x = y;
86 y = mod;
87 }
88
89 return x;
90 }
91
92 //! Find the least common multiple of x and y.
93 template<typename T>
94 T lcm (T x, T y)
95 {
96 DE_ASSERT(std::is_integral<T>::value && std::is_unsigned<T>::value);
97
98 T prod = x * y;
99 DE_ASSERT(x == 0 || prod / x == y); // Check overflow just in case.
100 return (prod) / gcd(x, y);
101 }
102
103 //! Helper for DE_CHECK() macros.
104 void throwRuntimeError (const char* message, const char* expr, const char* file, int line);
105
106 //! Default deleter.
107 template<typename T> struct DefaultDeleter
108 {
109 inline DefaultDeleter (void) {}
110 template<typename U> inline DefaultDeleter (const DefaultDeleter<U>&) {}
111 template<typename U> inline DefaultDeleter<T>& operator= (const DefaultDeleter<U>&) { return *this; }
112 inline void operator() (T* ptr) const { delete ptr; }
113 };
114
115 //! A deleter for arrays
116 template<typename T> struct ArrayDeleter
117 {
118 inline ArrayDeleter (void) {}
119 template<typename U> inline ArrayDeleter (const ArrayDeleter<U>&) {}
120 template<typename U> inline ArrayDeleter<T>& operator= (const ArrayDeleter<U>&) { return *this; }
121 inline void operator() (T* ptr) const { delete[] ptr; }
122 };
123
124 //! Get required memory alignment for type
125 template<typename T>
126 size_t alignOf (void)
127 {
128 struct PaddingCheck { deUint8 b; T t; };
129 return (size_t)DE_OFFSET_OF(PaddingCheck, t);
130 }
131
132 //! Similar to DE_LENGTH_OF_ARRAY but constexpr and without auxiliar helpers.
133 template <typename T, size_t N>
134 constexpr size_t arrayLength (const T (&)[N]) { return N; }
135
136 //! Get least significant bit index
137 inline int findLSB (deUint32 value)
138 {
139 return value ? deCtz32(value) : (-1);
140 }
141
142 //! Get most significant bit index
143 inline int findMSB (deUint32 value)
144 {
145 return 31 - deClz32(value);
146 }
147
148 //! Get most significant bit index
149 inline int findMSB (deInt32 value)
150 {
151 return (value < 0) ? findMSB(~(deUint32)value) : findMSB((deUint32)value);
152 }
153
154 } // de
155
156 /*--------------------------------------------------------------------*//*!
157 * \brief Throw runtime error if condition is not met.
158 * \param X Condition to check.
159 *
160 * This macro throws std::runtime_error if condition X is not met.
161 *//*--------------------------------------------------------------------*/
162 #define DE_CHECK_RUNTIME_ERR(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) ::de::throwRuntimeError(DE_NULL, #X, __FILE__, __LINE__); } while(deGetFalse())
163
164 /*--------------------------------------------------------------------*//*!
165 * \brief Throw runtime error if condition is not met.
166 * \param X Condition to check.
167 * \param MSG Additional message to include in the exception.
168 *
169 * This macro throws std::runtime_error with message MSG if condition X is
170 * not met.
171 *//*--------------------------------------------------------------------*/
172 #define DE_CHECK_RUNTIME_ERR_MSG(X, MSG) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) ::de::throwRuntimeError(MSG, #X, __FILE__, __LINE__); } while(deGetFalse())
173
174 //! Get array start pointer.
175 #define DE_ARRAY_BEGIN(ARR) (&(ARR)[0])
176
177 //! Get array end pointer.
178 #define DE_ARRAY_END(ARR) (DE_ARRAY_BEGIN(ARR) + DE_LENGTH_OF_ARRAY(ARR))
179
180 //! Empty C++ compilation unit silencing.
181 #if (DE_COMPILER == DE_COMPILER_MSC)
182 # define DE_EMPTY_CPP_FILE namespace { deUint8 unused; }
183 #else
184 # define DE_EMPTY_CPP_FILE
185 #endif
186
187 // Warn if type is constructed, but left unused
188 //
189 // Used in types with non-trivial ctor/dtor but with ctor-dtor pair causing no (observable)
190 // side-effects.
191 //
192 // \todo add attribute for GCC
193 #if (DE_COMPILER == DE_COMPILER_CLANG) && defined(__has_attribute)
194 # if __has_attribute(warn_unused)
195 # define DE_WARN_UNUSED_TYPE __attribute__((warn_unused))
196 # else
197 # define DE_WARN_UNUSED_TYPE
198 # endif
199 #else
200 # define DE_WARN_UNUSED_TYPE
201 #endif
202
203 #endif // _DEDEFS_HPP
204