4 * An object oriented GL/GLES Abstraction/Utility Layer
6 * Copyright (C) 2007,2008,2009,2010 Intel Corporation.
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.
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.
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/>.
30 #include <cogl/cogl-defines.h>
31 #include "cogl-types.h"
33 #ifndef COGL_HAS_GLIB_SUPPORT
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
42 #define COGL_EXPORT __declspec(dllexport)
44 #define COGL_EXPORT __declspec(dllimport)
51 _cogl_util_next_p2 (int a);
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 */
56 #define cogl_util_float_signbit(x) signbit(x)
58 /* This trick was stolen from here:
59 http://lists.boost.org/Archives/boost/2006/08/108731.php
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)
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 };
71 return !!((negative_one.i ^ positive_one.i) & value.i);
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
81 #define COGL_UTIL_NEARBYINT(x) ((int) ((x) < 0.0f ? (x) - 0.5f : (x) + 0.5f))
83 /* Returns whether the given integer is a power of two */
84 static inline gboolean
85 _cogl_util_is_pot (unsigned int num)
87 /* Make sure there is only one bit set */
88 return (num & (num - 1)) == 0;
91 /* Split Bob Jenkins' One-at-a-Time hash
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.
97 static inline unsigned int
98 _cogl_util_one_at_a_time_hash (unsigned int hash,
102 unsigned char *p = key;
105 for (i = 0; i < bytes; i++)
108 hash += (hash << 10);
116 _cogl_util_one_at_a_time_mix (unsigned int hash);
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
124 /* The 'ffs' function is part of C99 so it isn't always available */
126 #define _cogl_util_ffs ffs
129 _cogl_util_ffs (int num);
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
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))
143 _cogl_util_ffsl_wrapper (long int num);
144 #endif /* COGL_UTIL_HAVE_BUILTIN_FFSL */
146 #ifdef COGL_UTIL_HAVE_BUILTIN_POPCOUNTL
147 #define _cogl_util_popcountl __builtin_popcountl
149 extern const unsigned char _cogl_util_popcount_table[256];
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. */
155 _cogl_util_popcountl (unsigned long num)
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];
167 #endif /* COGL_UTIL_HAVE_BUILTIN_POPCOUNTL */
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)
173 #define _COGL_RETURN_IF_FAIL(EXPR) do { \
176 fprintf (stderr, "file %s: line %d: assertion `%s' failed", \
183 #define _COGL_RETURN_VAL_IF_FAIL(EXPR, VAL) do { \
186 fprintf (stderr, "file %s: line %d: assertion `%s' failed", \
193 #endif /* COGL_HAS_GLIB_SUPPORT */
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.
199 * If no specific pixel format could be found, COGL_PIXEL_FORMAT_ANY
203 _cogl_util_pixel_format_from_masks (unsigned long r_mask,
204 unsigned long g_mask,
205 unsigned long b_mask,
209 #endif /* __COGL_UTIL_H */