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