1 /*
2  * Cogl
3  *
4  * A Low Level GPU Graphics and Utilities API
5  *
6  * Copyright (C) 2007,2008,2009,2010 Intel Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use, copy,
12  * modify, merge, publish, distribute, sublicense, and/or sell copies
13  * of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  *
28  *
29  */
30 
31 #ifndef __COGL_UTIL_H
32 #define __COGL_UTIL_H
33 
34 #include <glib.h>
35 #include <math.h>
36 
37 #include <cogl/cogl-defines.h>
38 #include "cogl-types.h"
39 
40 #ifndef COGL_HAS_GLIB_SUPPORT
41 #include <stdio.h>
42 #endif
43 
44 /* Double check that config.h has been included */
45 #if !defined (GETTEXT_PACKAGE) && !defined (_COGL_IN_TEST_BITMASK)
46 #error "config.h must be included before including cogl-util.h"
47 #endif
48 
49 /* When compiling with Visual Studio, symbols that represent data that
50    are exported out of the DLL need to be marked with the dllexport
51    attribute. */
52 #ifdef _MSC_VER
53 #ifdef COGL_BUILD_EXP
54 #define COGL_EXPORT __declspec(dllexport)
55 #else
56 #define COGL_EXPORT __declspec(dllimport)
57 #endif
58 #else
59 #define COGL_EXPORT
60 #endif
61 
62 int
63 _cogl_util_next_p2 (int a);
64 
65 /* The signbit macro is defined by ISO C99 so it should be available,
66    however if it's not we can fallback to an evil hack */
67 #ifdef signbit
68 #define cogl_util_float_signbit(x) signbit(x)
69 #else
70 /* This trick was stolen from here:
71    http://lists.boost.org/Archives/boost/2006/08/108731.php
72 
73    It xors the integer reinterpretations of -1.0f and 1.0f. In theory
74    they should only differ by the signbit so that gives a mask for the
75    sign which we can just test against the value */
76 static inline CoglBool
cogl_util_float_signbit(float x)77 cogl_util_float_signbit (float x)
78 {
79   static const union { float f; uint32_t i; } negative_one = { -1.0f };
80   static const union { float f; uint32_t i; } positive_one = { +1.0f };
81   union { float f; uint32_t i; } value = { x };
82 
83   return !!((negative_one.i ^ positive_one.i) & value.i);
84 }
85 #endif
86 
87 /* This is a replacement for the nearbyint function which always
88    rounds to the nearest integer. nearbyint is apparently a C99
89    function so it might not always be available but also it seems in
90    glibc it is defined as a function call so this macro could end up
91    faster anyway. We can't just add 0.5f because it will break for
92    negative numbers. */
93 #define COGL_UTIL_NEARBYINT(x) ((int) ((x) < 0.0f ? (x) - 0.5f : (x) + 0.5f))
94 
95 /* Returns whether the given integer is a power of two */
96 static inline CoglBool
_cogl_util_is_pot(unsigned int num)97 _cogl_util_is_pot (unsigned int num)
98 {
99   /* Make sure there is only one bit set */
100   return (num & (num - 1)) == 0;
101 }
102 
103 /* Split Bob Jenkins' One-at-a-Time hash
104  *
105  * This uses the One-at-a-Time hash algorithm designed by Bob Jenkins
106  * but the mixing step is split out so the function can be used in a
107  * more incremental fashion.
108  */
109 static inline unsigned int
_cogl_util_one_at_a_time_hash(unsigned int hash,const void * key,size_t bytes)110 _cogl_util_one_at_a_time_hash (unsigned int hash,
111                                const void *key,
112                                size_t bytes)
113 {
114   const unsigned char *p = key;
115   int i;
116 
117   for (i = 0; i < bytes; i++)
118     {
119       hash += p[i];
120       hash += (hash << 10);
121       hash ^= (hash >> 6);
122     }
123 
124   return hash;
125 }
126 
127 unsigned int
128 _cogl_util_one_at_a_time_mix (unsigned int hash);
129 
130 /* These two builtins are available since GCC 3.4 */
131 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
132 #define COGL_UTIL_HAVE_BUILTIN_FFSL
133 #define COGL_UTIL_HAVE_BUILTIN_POPCOUNTL
134 #define COGL_UTIL_HAVE_BUILTIN_CLZ
135 #endif
136 
137 /* The 'ffs' function is part of C99 so it isn't always available */
138 #ifdef HAVE_FFS
139 #define _cogl_util_ffs ffs
140 #else
141 int
142 _cogl_util_ffs (int num);
143 #endif
144 
145 /* The 'ffsl' function is non-standard but GCC has a builtin for it
146    since 3.4 which we can use */
147 #ifdef COGL_UTIL_HAVE_BUILTIN_FFSL
148 #define _cogl_util_ffsl __builtin_ffsl
149 #else
150 /* If ints and longs are the same size we can just use ffs. Hopefully
151    the compiler will optimise away this conditional */
152 #define _cogl_util_ffsl(x)                                              \
153   (sizeof (long int) == sizeof (int) ? _cogl_util_ffs ((int) x) :       \
154    _cogl_util_ffsl_wrapper (x))
155 int
156 _cogl_util_ffsl_wrapper (long int num);
157 #endif /* COGL_UTIL_HAVE_BUILTIN_FFSL */
158 
159 static inline unsigned int
_cogl_util_fls(unsigned int n)160 _cogl_util_fls (unsigned int n)
161 {
162 #ifdef COGL_UTIL_HAVE_BUILTIN_CLZ
163    return n == 0 ? 0 : sizeof (unsigned int) * 8 - __builtin_clz (n);
164 #else
165    unsigned int v = 1;
166 
167    if (n == 0)
168       return 0;
169 
170    while (n >>= 1)
171        v++;
172 
173    return v;
174 #endif
175 }
176 
177 #ifdef COGL_UTIL_HAVE_BUILTIN_POPCOUNTL
178 #define _cogl_util_popcountl __builtin_popcountl
179 #else
180 extern const unsigned char _cogl_util_popcount_table[256];
181 
182 /* There are many ways of doing popcount but doing a table lookup
183    seems to be the most robust against different sizes for long. Some
184    pages seem to claim it's the fastest method anyway. */
185 static inline int
_cogl_util_popcountl(unsigned long num)186 _cogl_util_popcountl (unsigned long num)
187 {
188   int i;
189   int sum = 0;
190 
191   /* Let's hope GCC will unroll this loop.. */
192   for (i = 0; i < sizeof (num); i++)
193     sum += _cogl_util_popcount_table[(num >> (i * 8)) & 0xff];
194 
195   return sum;
196 }
197 
198 #endif /* COGL_UTIL_HAVE_BUILTIN_POPCOUNTL */
199 
200 #ifdef COGL_HAS_GLIB_SUPPORT
201 #define _COGL_RETURN_IF_FAIL(EXPR) g_return_if_fail(EXPR)
202 #define _COGL_RETURN_VAL_IF_FAIL(EXPR, VAL) g_return_val_if_fail(EXPR, VAL)
203 #else
204 #ifdef COGL_ENABLE_DEBUG
205 #define _COGL_RETURN_START do {
206 #define _COGL_RETURN_END } while (0)
207 #else /* COGL_ENABLE_DEBUG */
208 /* If debugging is disabled then we don't actually want to do the
209  * check but we still want the code for the expression to be generated
210  * so that it won't give loads of warnings about unused variables.
211  * Therefore we just surround the block with if(0) */
212 #define _COGL_RETURN_START do { if (0) {
213 #define _COGL_RETURN_END } } while (0)
214 #endif /* COGL_ENABLE_DEBUG */
215 #define _COGL_RETURN_IF_FAIL(EXPR) _COGL_RETURN_START {             \
216    if (!(EXPR))						            \
217      {							            \
218        fprintf (stderr, "file %s: line %d: assertion `%s' failed",  \
219                 __FILE__,					    \
220                 __LINE__,					    \
221                 #EXPR);						    \
222        return;						            \
223      };                                                             \
224   } _COGL_RETURN_END
225 #define _COGL_RETURN_VAL_IF_FAIL(EXPR, VAL) _COGL_RETURN_START {    \
226    if (!(EXPR))						            \
227      {							            \
228        fprintf (stderr, "file %s: line %d: assertion `%s' failed",  \
229                 __FILE__,					    \
230                 __LINE__,					    \
231                 #EXPR);						    \
232        return (VAL);						    \
233      };                                                             \
234   } _COGL_RETURN_END
235 #endif /* COGL_HAS_GLIB_SUPPORT */
236 
237 /* Match a CoglPixelFormat according to channel masks, color depth,
238  * bits per pixel and byte order. These information are provided by
239  * the Visual and XImage structures.
240  *
241  * If no specific pixel format could be found, COGL_PIXEL_FORMAT_ANY
242  * is returned.
243  */
244 CoglPixelFormat
245 _cogl_util_pixel_format_from_masks (unsigned long r_mask,
246                                     unsigned long g_mask,
247                                     unsigned long b_mask,
248                                     int depth, int bpp,
249                                     int byte_order);
250 
251 /* Since we can't rely on _Static_assert always being available for
252  * all compilers we have limited static assert that can be used in
253  * C code but not in headers.
254  */
255 #define _COGL_TYPEDEF_ASSERT(EXPRESSION) \
256   typedef struct { char Compile_Time_Assertion[(EXPRESSION) ? 1 : -1]; } \
257   G_PASTE (_GStaticAssert_, __LINE__)
258 
259 /* _COGL_STATIC_ASSERT:
260  * @expression: An expression to assert evaluates to true at compile
261  *              time.
262  * @message: A message to print to the console if the assertion fails
263  *           at compile time.
264  *
265  * Allows you to assert that an expression evaluates to true at
266  * compile time and aborts compilation if not. If possible message
267  * will also be printed if the assertion fails.
268  *
269  * Note: Only Gcc >= 4.6 supports the c11 _Static_assert which lets us
270  * print a nice message if the compile time assertion fails.
271  */
272 #ifdef HAVE_STATIC_ASSERT
273 #define _COGL_STATIC_ASSERT(EXPRESSION, MESSAGE) \
274   _Static_assert (EXPRESSION, MESSAGE);
275 #else
276 #define _COGL_STATIC_ASSERT(EXPRESSION, MESSAGE)
277 #endif
278 
279 #ifdef HAVE_MEMMEM
280 #define _cogl_util_memmem memmem
281 #else
282 char *
283 _cogl_util_memmem (const void *haystack,
284                    size_t haystack_len,
285                    const void *needle,
286                    size_t needle_len);
287 #endif
288 
289 static inline void
_cogl_util_scissor_intersect(int rect_x0,int rect_y0,int rect_x1,int rect_y1,int * scissor_x0,int * scissor_y0,int * scissor_x1,int * scissor_y1)290 _cogl_util_scissor_intersect (int rect_x0,
291                               int rect_y0,
292                               int rect_x1,
293                               int rect_y1,
294                               int *scissor_x0,
295                               int *scissor_y0,
296                               int *scissor_x1,
297                               int *scissor_y1)
298 {
299   *scissor_x0 = MAX (*scissor_x0, rect_x0);
300   *scissor_y0 = MAX (*scissor_y0, rect_y0);
301   *scissor_x1 = MIN (*scissor_x1, rect_x1);
302   *scissor_y1 = MIN (*scissor_y1, rect_y1);
303 }
304 
305 #endif /* __COGL_UTIL_H */
306