doc: Redo the pages style, new logo, etc.
[gfxprim/pasky.git] / include / core / GP_Common.h
blob5de85469ed92acde152fc87dbc87ba92532a4f45
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #ifndef CORE_GP_COMMON_H
27 #define CORE_GP_COMMON_H
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <stddef.h>
35 #ifndef __cplusplus
38 * Returns a minimum of the two numbers.
40 #define GP_MIN(a, b) ({ \
41 typeof(a) _a = (a); \
42 typeof(b) _b = (b); \
43 _a < _b ? _a : _b; \
47 * Returns a maximum of the two numbers.
49 #define GP_MAX(a, b) ({ \
50 typeof(a) _a = (a); \
51 typeof(b) _b = (b); \
52 _a > _b ? _a : _b; \
56 * Returns maximum from three numbers.
58 #define GP_MAX3(a, b, c) ({ \
59 typeof(a) _a = (a); \
60 typeof(b) _b = (b); \
61 typeof(c) _c = (c); \
62 _a > _b ? (_a > _c ? _a : _c) : (_b > _c ? _b : _c); \
66 * Returns absolute value.
68 #define GP_ABS(a) ({ \
69 typeof(a) _a = a; \
70 _a > 0 ? _a : -_a; \
74 * Swap a and b using an intermediate variable
76 #define GP_SWAP(a, b) do { \
77 typeof(a) tmp = b; \
78 b = a; \
79 a = tmp; \
80 } while (0)
82 /* Determines the sign of the integer value; it is +1 if value is positive,
83 * -1 if negative, and 0 if it is zero.
85 #define GP_SIGN(a) ({ \
86 typeof(a) _a = a; \
87 (_a > 0) ? 1 : ((_a < 0) ? -1 : 0); \
90 #define GP_ARRAY_SIZE(array) (sizeof(array) / sizeof(*array))
92 #define GP_CONTAINER_OF(ptr, structure, member) \
93 ((structure *)((char *)(ptr) - offsetof(structure, member)))
95 #endif /* __cplusplus */
98 * The standard likely() and unlikely() used in Kernel
100 #ifndef likely
101 #ifdef __GNUC__
102 #define likely(x) __builtin_expect(!!(x),1)
103 #define unlikely(x) __builtin_expect(!!(x),0)
104 #else
105 #define likely(x) x
106 #define unlikely(x) x
107 #endif
108 #endif
110 #define GP_UNUSED(x) (x)__attribute__ ((unused))
113 * Internal macros with common code for GP_ABORT, GP_ASSERT and GP_CHECK.
114 * GP_INTERNAL_ABORT takes a message that may contain % (e.g. assert condition)
115 * and prints message and calls abort().
116 * GP_GENERAL_CHECK is a check with specified message prefix
117 * (for assert and check)
119 #define GP_INTERNAL_ABORT(...) do { \
120 GP_PrintAbortInfo(__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__); \
121 abort(); \
122 } while (0)
125 * Print as much trace info as possible. Currently, the (C) call stack and
126 * the Python stack if a Python interpreter is set up. In case more wrappers
127 * are written, it should print a trace for the currently active.
129 void GP_PrintAbortInfo(const char *file, const char *function, unsigned int line,
130 const char *msg, const char *fmt, ...)
131 __attribute__ ((format (printf, 5, 6)));
133 #define GP_GENERAL_CHECK(check_cond_, check_message_, ...) do { \
134 if (unlikely(!(check_cond_))) { \
135 if (#__VA_ARGS__ [0]) \
136 GP_INTERNAL_ABORT(check_message_ #check_cond_, \
137 "\n" __VA_ARGS__); \
138 else \
139 GP_INTERNAL_ABORT(check_message_ #check_cond_, " "); \
141 } while (0)
144 * Aborts and prints the message along with the location in code
145 * to stderr. Used for fatal errors.
147 * Use as either GP_ABORT(msg) or GP_ABORT(format, params...) where
148 * msg and format must be string constants.
150 #define GP_ABORT(...) \
151 GP_INTERNAL_ABORT("\n", __VA_ARGS__)
154 * Checks the condition and aborts immediately if it is not satisfied,
155 * printing the condition and location in the source.
156 * (Intended for checking for bugs within the library itself.)
158 * Use as either GP_ASSERT(cond), GP_ASSERT(cond, msg) or
159 * GP_ASSERT(cond, format, params...) where msg and format must be string
160 * constants.
162 #define GP_ASSERT(check_cond_, ...) \
163 GP_GENERAL_CHECK(check_cond_, "assertion failed: ", ##__VA_ARGS__)
166 * Perform a runtime check, on failure abort and print a message.
167 * (Intended for user-caused errors like invalid arguments.)
169 * Use as either GP_CHECK(cond), GP_CHECK(cond, msg) or
170 * GP_CHECK(cond, format, params...) where msg and format must be string
171 * constants.
173 #define GP_CHECK(check_cond_, ...) \
174 GP_GENERAL_CHECK(check_cond_, "check failed: ", ##__VA_ARGS__)
176 #endif /* CORE_GP_COMMON_H */