"Initial commit to Gerrit"
[profile/ivi/cogl.git] / cogl / cogl-util.h
1 /*
2  * Cogl
3  *
4  * An object oriented GL/GLES Abstraction/Utility Layer
5  *
6  * Copyright (C) 2007,2008,2009,2010 Intel Corporation.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  *
22  */
23
24 #ifndef __COGL_UTIL_H
25 #define __COGL_UTIL_H
26
27 #include <glib.h>
28 #include <math.h>
29
30 #include <cogl/cogl-defines.h>
31 #include "cogl-types.h"
32
33 #ifndef COGL_HAS_GLIB_SUPPORT
34 #include <stdio.h>
35 #endif
36
37 /* When compiling with Visual Studio, symbols that represent data that
38    are exported out of the DLL need to be marked with the dllexport
39    attribute. */
40 #ifdef _MSC_VER
41 #ifdef COGL_BUILD_EXP
42 #define COGL_EXPORT __declspec(dllexport)
43 #else
44 #define COGL_EXPORT __declspec(dllimport)
45 #endif
46 #else
47 #define COGL_EXPORT
48 #endif
49
50 int
51 _cogl_util_next_p2 (int a);
52
53 /* The signbit macro is defined by ISO C99 so it should be available,
54    however if it's not we can fallback to an evil hack */
55 #ifdef signbit
56 #define cogl_util_float_signbit(x) signbit(x)
57 #else
58 /* This trick was stolen from here:
59    http://lists.boost.org/Archives/boost/2006/08/108731.php
60
61    It xors the integer reinterpretations of -1.0f and 1.0f. In theory
62    they should only differ by the signbit so that gives a mask for the
63    sign which we can just test against the value */
64 static inline gboolean
65 cogl_util_float_signbit (float x)
66 {
67   static const union { float f; guint32 i; } negative_one = { -1.0f };
68   static const union { float f; guint32 i; } positive_one = { +1.0f };
69   union { float f; guint32 i; } value = { x };
70
71   return !!((negative_one.i ^ positive_one.i) & value.i);
72 }
73 #endif
74
75 /* This is a replacement for the nearbyint function which always
76    rounds to the nearest integer. nearbyint is apparently a C99
77    function so it might not always be available but also it seems in
78    glibc it is defined as a function call so this macro could end up
79    faster anyway. We can't just add 0.5f because it will break for
80    negative numbers. */
81 #define COGL_UTIL_NEARBYINT(x) ((int) ((x) < 0.0f ? (x) - 0.5f : (x) + 0.5f))
82
83 /* Returns whether the given integer is a power of two */
84 static inline gboolean
85 _cogl_util_is_pot (unsigned int num)
86 {
87   /* Make sure there is only one bit set */
88   return (num & (num - 1)) == 0;
89 }
90
91 /* Split Bob Jenkins' One-at-a-Time hash
92  *
93  * This uses the One-at-a-Time hash algorithm designed by Bob Jenkins
94  * but the mixing step is split out so the function can be used in a
95  * more incremental fashion.
96  */
97 static inline unsigned int
98 _cogl_util_one_at_a_time_hash (unsigned int hash,
99                                void *key,
100                                size_t bytes)
101 {
102   unsigned char *p = key;
103   int i;
104
105   for (i = 0; i < bytes; i++)
106     {
107       hash += p[i];
108       hash += (hash << 10);
109       hash ^= (hash >> 6);
110     }
111
112   return hash;
113 }
114
115 unsigned int
116 _cogl_util_one_at_a_time_mix (unsigned int hash);
117
118 /* These two builtins are available since GCC 3.4 */
119 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
120 #define COGL_UTIL_HAVE_BUILTIN_FFSL
121 #define COGL_UTIL_HAVE_BUILTIN_POPCOUNTL
122 #endif
123
124 /* The 'ffs' function is part of C99 so it isn't always available */
125 #ifdef HAVE_FFS
126 #define _cogl_util_ffs ffs
127 #else
128 int
129 _cogl_util_ffs (int num);
130 #endif
131
132 /* The 'ffsl' function is non-standard but GCC has a builtin for it
133    since 3.4 which we can use */
134 #ifdef COGL_UTIL_HAVE_BUILTIN_FFSL
135 #define _cogl_util_ffsl __builtin_ffsl
136 #else
137 /* If ints and longs are the same size we can just use ffs. Hopefully
138    the compiler will optimise away this conditional */
139 #define _cogl_util_ffsl(x)                                              \
140   (sizeof (long int) == sizeof (int) ? _cogl_util_ffs ((int) x) :       \
141    _cogl_util_ffsl_wrapper (x))
142 int
143 _cogl_util_ffsl_wrapper (long int num);
144 #endif /* COGL_UTIL_HAVE_BUILTIN_FFSL */
145
146 #ifdef COGL_UTIL_HAVE_BUILTIN_POPCOUNTL
147 #define _cogl_util_popcountl __builtin_popcountl
148 #else
149 extern const unsigned char _cogl_util_popcount_table[256];
150
151 /* There are many ways of doing popcount but doing a table lookup
152    seems to be the most robust against different sizes for long. Some
153    pages seem to claim it's the fastest method anyway. */
154 static inline int
155 _cogl_util_popcountl (unsigned long num)
156 {
157   int i;
158   int sum = 0;
159
160   /* Let's hope GCC will unroll this loop.. */
161   for (i = 0; i < sizeof (num); i++)
162     sum += _cogl_util_popcount_table[(num >> (i * 8)) & 0xff];
163
164   return sum;
165 }
166
167 #endif /* COGL_UTIL_HAVE_BUILTIN_POPCOUNTL */
168
169 #ifdef COGL_HAS_GLIB_SUPPORT
170 #define _COGL_RETURN_IF_FAIL(EXPR) g_return_if_fail(EXPR)
171 #define _COGL_RETURN_VAL_IF_FAIL(EXPR, VAL) g_return_val_if_fail(EXPR, VAL)
172 #else
173 #define _COGL_RETURN_IF_FAIL(EXPR) do {                             \
174    if (!(EXPR))                                                     \
175      {                                                              \
176        fprintf (stderr, "file %s: line %d: assertion `%s' failed",  \
177                 __FILE__,                                           \
178                 __LINE__,                                           \
179                 #EXPR);                                             \
180        return;                                                      \
181      };                                                             \
182   } while(0)
183 #define _COGL_RETURN_VAL_IF_FAIL(EXPR, VAL) do {                            \
184    if (!(EXPR))                                                     \
185      {                                                              \
186        fprintf (stderr, "file %s: line %d: assertion `%s' failed",  \
187                 __FILE__,                                           \
188                 __LINE__,                                           \
189                 #EXPR);                                             \
190        return (VAL);                                                \
191      };                                                             \
192   } while(0)
193 #endif /* COGL_HAS_GLIB_SUPPORT */
194
195 /* Match a CoglPixelFormat according to channel masks, color depth,
196  * bits per pixel and byte order. These information are provided by
197  * the Visual and XImage structures.
198  *
199  * If no specific pixel format could be found, COGL_PIXEL_FORMAT_ANY
200  * is returned.
201  */
202 CoglPixelFormat
203 _cogl_util_pixel_format_from_masks (unsigned long r_mask,
204                                     unsigned long g_mask,
205                                     unsigned long b_mask,
206                                     int depth, int bpp,
207                                     int byte_order);
208
209 #endif /* __COGL_UTIL_H */