typedefs.h
1 /*************************************************************************/
2 /* typedefs.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef TYPEDEFS_H
30 #define TYPEDEFS_H
31 
32 #include <stddef.h>
37 #include "platform_config.h"
38 
39 #ifndef _STR
40 #define _STR(m_x) #m_x
41 #define _MKSTR(m_x) _STR(m_x)
42 #endif
43 // have to include version.h for this to work, include it in the .cpp not the .h
44 #ifdef VERSION_PATCH
45 #define VERSION_MKSTRING _MKSTR(VERSION_MAJOR)"." _MKSTR(VERSION_MINOR)"." _MKSTR(VERSION_PATCH)"." _MKSTR(VERSION_STATUS)"." _MKSTR(VERSION_REVISION)
46 #else
47 #define VERSION_MKSTRING _MKSTR(VERSION_MAJOR)"." _MKSTR(VERSION_MINOR)"." _MKSTR(VERSION_STATUS)"." _MKSTR(VERSION_REVISION)
48 #endif // VERSION_PATCH
49 #define VERSION_FULL_NAME _MKSTR(VERSION_NAME)" v" VERSION_MKSTRING
50 
51 
52 #ifndef _ALWAYS_INLINE_
53 
54 #if defined(__GNUC__) && (__GNUC__ >= 4 )
55 # define _ALWAYS_INLINE_ __attribute__((always_inline)) inline
56 #elif defined(__llvm__)
57 # define _ALWAYS_INLINE_ __attribute__((always_inline)) inline
58 #elif defined(_MSC_VER)
59 # define _ALWAYS_INLINE_ __forceinline
60 #else
61 # define _ALWAYS_INLINE_ inline
62 #endif
63 
64 #endif
65 
66 #ifndef _FORCE_INLINE_
67 
68 #ifdef DEBUG_ENABLED
69 
70 #define _FORCE_INLINE_ inline
71 
72 #else
73 
74 #define _FORCE_INLINE_ _ALWAYS_INLINE_
75 
76 #endif
77 
78 #endif
79 
80 #ifndef DEFAULT_ALIGNMENT
81 #define DEFAULT_ALIGNMENT 1
82 #endif
83 
84 
85 //custom, gcc-safe offsetof, because gcc complains a lot.
86 template<class T>
87 T *_nullptr() { T*t=NULL; return t; }
88 
89 #define OFFSET_OF(st, m) \
90 ((size_t) ( (char *)&(_nullptr<st>()->m) - (char *)0 ))
91 
95 #ifndef NULL
96 #define NULL 0
97 #endif
98 
103 #ifdef _WIN32
104 # undef min // override standard definition
105 # undef max // override standard definition
106 # undef ERROR // override (really stupid) wingdi.h standard definition
107 # undef DELETE // override (another really stupid) winnt.h standard definition
108 # undef MessageBox // override winuser.h standard definition
109 # undef MIN // override standard definition
110 # undef MAX // override standard definition
111 # undef CLAMP // override standard definition
112 # undef Error
113 # undef OK
114 #endif
115 
116 #include "error_macros.h"
117 #include "error_list.h"
118 
119 #include "int_types.h"
120 
123 #ifndef ABS
124 #define ABS(m_v) ((m_v<0)?(-(m_v)):(m_v))
125 #endif
126 
127 #ifndef SGN
128 #define SGN(m_v) ((m_v<0)?(-1.0):(+1.0))
129 #endif
130 
131 #ifndef MIN
132 #define MIN(m_a,m_b) (((m_a)<(m_b))?(m_a):(m_b))
133 #endif
134 
135 #ifndef MAX
136 #define MAX(m_a,m_b) (((m_a)>(m_b))?(m_a):(m_b))
137 #endif
138 
139 #ifndef CLAMP
140 #define CLAMP(m_a,m_min,m_max) (((m_a)<(m_min))?(m_min):(((m_a)>(m_max))?m_max:m_a))
141 #endif
142 
144 #ifndef SWAP
145 
146 #define SWAP(m_x,m_y) __swap_tmpl(m_x,m_y)
147 template<class T>
148 inline void __swap_tmpl(T &x, T &y ) {
149 
150  T aux=x;
151  x=y;
152  y=aux;
153 }
154 
155 #endif //swap
156 
157 #define HEX2CHR( m_hex ) ( (m_hex>='0' && m_hex<='9')?(m_hex-'0'):\
158  ((m_hex>='A' && m_hex<='F')?(10+m_hex-'A'):\
159  ((m_hex>='a' && m_hex<='f')?(10+m_hex-'a'):0)))
160 
161 // Macro to check whether we are compiled by clang
162 // and we have a specific builtin
163 #if defined(__llvm__) && defined(__has_builtin)
164  #define _llvm_has_builtin(x) __has_builtin(x)
165 #else
166  #define _llvm_has_builtin(x) 0
167 #endif
168 
169 #if (defined(__GNUC__) && (__GNUC__ >= 5)) || _llvm_has_builtin(__builtin_mul_overflow)
170 # define _mul_overflow __builtin_mul_overflow
171 #endif
172 
173 #if (defined(__GNUC__) && (__GNUC__ >= 5)) || _llvm_has_builtin(__builtin_add_overflow)
174 # define _add_overflow __builtin_add_overflow
175 #endif
176 
177 
178 
179 
180 
183 static _FORCE_INLINE_ unsigned int nearest_power_of_2(unsigned int x) {
184 
185  --x;
186  x |= x >> 1;
187  x |= x >> 2;
188  x |= x >> 4;
189  x |= x >> 8;
190  x |= x >> 16;
191 
192  return ++x;
193 }
194 
195 template<class T>
196 static _FORCE_INLINE_ T nearest_power_of_2_templated(T x) {
197 
198  --x;
199  // If the compiler is smart, it unrolls this loop
200  // If its dumb, this is a bit slow.
201  for (size_t i = 0; i < sizeof(T); i++)
202  x |= x >> (1 << i);
203 
204  return ++x;
205 }
206 
209 static inline unsigned int nearest_shift(unsigned int p_number) {
210 
211  for (int i=30;i>=0;i--) {
212 
213  if (p_number&(1<<i))
214  return i+1;
215  }
216 
217  return 0;
218 }
219 
221 static inline int get_shift_from_power_of_2( unsigned int p_pixel ) {
222  // return a GL_TEXTURE_SIZE_ENUM
223 
224 
225  for (unsigned int i=0;i<32;i++) {
226 
227  if (p_pixel==(unsigned int)(1<<i))
228  return i;
229  }
230 
231  return -1;
232 }
233 
235 static inline uint16_t BSWAP16(uint16_t x) {
236  return (x>>8)|(x<<8);
237 }
239 static inline uint32_t BSWAP32(uint32_t x) {
240  return((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24));
241 }
244 static inline uint64_t BSWAP64(uint64_t x) {
245  x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
246  x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
247  x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
248  return x;
249 }
250 
256 template<class T>
257 struct Comparator {
258 
259  inline bool operator()(const T& p_a, const T& p_b) const { return (p_a<p_b); }
260 
261 };
262 
263 
264 void _global_lock();
265 void _global_unlock();
266 
267 struct _GlobalLock {
268 
269  _GlobalLock() { _global_lock(); }
270  ~_GlobalLock() { _global_unlock(); }
271 };
272 
273 #define GLOBAL_LOCK_FUNCTION _GlobalLock _global_lock_;
274 #ifdef NO_SAFE_CAST
275 
276 #define SAFE_CAST static_cast
277 
278 #else
279 
280 #define SAFE_CAST dynamic_cast
281 
282 #endif
283 
284 #define MT_SAFE
285 
286 #define __STRX(m_index) #m_index
287 #define __STR(m_index) __STRX(m_index)
288 
289 
290 #endif /* typedefs.h */
291 
Definition: typedefs.h:267
Definition: typedefs.h:257