v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
macros.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * Sifteo SDK
4  *
5  * Copyright <c> 2012 Sifteo, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #pragma once
27 #ifdef NOT_USERSPACE
28 # error This is a userspace-only header, not allowed by the current build.
29 #endif
30 
31 #include <sifteo/abi.h>
32 
59 #define NOINLINE __attribute__ ((noinline))
60 
76 #define ALWAYS_INLINE __attribute__ ((always_inline))
77 
80 #define STRINGIFY(_x) #_x
81 
90 #define TOSTRING(_x) STRINGIFY(_x)
91 
94 #define SRCLINE __FILE__ ":" TOSTRING(__LINE__)
95 
117 #define DEBUG_ONLY(_x) \
118 do { \
119  if (_SYS_lti_isDebug()) { \
120  _x \
121  } \
122 } while (0)
123 
124 #ifdef NO_LOG
125 # define LOG(...)
126 #else
127 
179 # define LOG(...) \
180 do { \
181  if (_SYS_lti_isDebug()) \
182  _SYS_lti_log(__VA_ARGS__); \
183 } while (0)
184 #endif
185 
186 #ifdef NO_ASSERT
187 # define ASSERT(_x)
188 #else
189 
205 # define ASSERT(_x) \
206 do { \
207  if (_SYS_lti_isDebug() && !(_x)) { \
208  _SYS_lti_log("ASSERT failure at %s:%d, (%s)\n", __FILE__, __LINE__, #_x); \
209  _SYS_abort(); \
210  } \
211 } while (0)
212 #endif
213 
227 #define SCRIPT_TYPE(_type) \
228 do { \
229  _SYS_log((_SYS_SCRIPT_ ## _type) | (_SYS_LOGTYPE_SCRIPT << 27), \
230  0,0,0,0,0,0,0); \
231 } while (0)
232 
266 #define SCRIPT(_type, _code) \
267 do { \
268  if (_SYS_lti_isDebug()) { \
269  SCRIPT_TYPE(_type); \
270  _SYS_lti_log("%s", #_code); \
271  SCRIPT_TYPE(NONE); \
272  } \
273 } while (0)
274 
303 #define SCRIPT_FMT(_type, ...) \
304 do { \
305  if (_SYS_lti_isDebug()) { \
306  SCRIPT_TYPE(_type); \
307  _SYS_lti_log(__VA_ARGS__); \
308  SCRIPT_TYPE(NONE); \
309  } \
310 } while (0)
311 
314 #define LOG_INT(_x) LOG("%s = %d\n", #_x, (_x));
315 
318 #define LOG_HEX(_x) LOG("%s = 0x%08x\n", #_x, (_x));
319 
322 #define LOG_FLOAT(_x) LOG("%s = %f\n", #_x, (_x));
323 
326 #define LOG_STR(_x) LOG("%s = \"%s\"\n", #_x, (const char*)(_x));
327 
330 #define LOG_INT2(_x) LOG("%s = (%d, %d)\n", #_x, (_x).x, (_x).y);
331 
334 #define LOG_INT3(_x) LOG("%s = (%d, %d, %d)\n", #_x, (_x).x, (_x).y, (_x).z);
335 
338 #define LOG_FLOAT2(_x) LOG("%s = (%f, %f)\n", #_x, (_x).x, (_x).y);
339 
342 #define STATIC_ASSERT(_x) ((void)sizeof(char[1 - 2*!(_x)]))
343 
344 #ifndef MIN
345 #define MIN(a,b) ((a) < (b) ? (a) : (b))
348 #define MAX(a,b) ((a) > (b) ? (a) : (b))
351 #endif
352 
353 #ifndef NULL
354 #define NULL 0
357 #endif
358 
359 #ifndef arraysize
360 #define arraysize(a) (sizeof(a) / sizeof((a)[0]))
363 #endif
364 
365 #ifndef offsetof
366 #define offsetof(t,m) ((uintptr_t)(uint8_t*)&(((t*)0)->m))
369 #endif
370