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