2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Red Hat Author(s): Behdad Esfahbod
34 #include "hb-common.h"
40 /* We only use these two for debug output. However, the debug code is
41 * always seen by the compiler (and optimized out in non-debug builds.
42 * If including these becomes a problem, we can start thinking about
43 * someway around that. */
53 # define NULL ((void *) 0)
66 #define MIN(a,b) ((a) < (b) ? (a) : (b))
69 #define MAX(a,b) ((a) > (b) ? (a) : (b))
72 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
74 #define HB_STMT_START do
75 #define HB_STMT_END while (0)
77 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
78 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
79 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
85 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
86 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
87 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
88 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
90 #define likely(expr) (expr)
91 #define unlikely(expr) (expr)
96 #define __attribute__(x)
100 #define HB_PURE_FUNC __attribute__((pure))
101 #define HB_CONST_FUNC __attribute__((const))
104 #define HB_CONST_FUNC
107 #define HB_UNUSED __attribute__((unused))
113 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
117 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
118 #define snprintf _snprintf
123 #define inline __inline
126 #ifdef __STRICT_ANSI__
128 #define inline __inline__
133 #define HB_FUNC __PRETTY_FUNCTION__
134 #elif defined(_MSC_VER)
135 #define HB_FUNC __FUNCSIG__
137 #define HB_FUNC __func__
141 /* Return the number of 1 bits in mask. */
142 static inline HB_CONST_FUNC unsigned int
143 _hb_popcount32 (uint32_t mask)
145 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
146 return __builtin_popcount (mask);
150 y = (mask >> 1) &033333333333;
151 y = mask - y - ((y >>1) & 033333333333);
152 return (((y + (y >> 3)) & 030707070707) % 077);
156 /* Returns the number of bits needed to store number */
157 static inline HB_CONST_FUNC unsigned int
158 _hb_bit_storage (unsigned int number)
160 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
161 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
163 register unsigned int n_bits = 0;
172 /* Returns the number of zero bits in the least significant side of number */
173 static inline HB_CONST_FUNC unsigned int
174 _hb_ctz (unsigned int number)
176 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
177 return likely (number) ? __builtin_ctz (number) : 0;
179 register unsigned int n_bits = 0;
180 if (unlikely (!number)) return 0;
181 while (!(number & 1)) {
189 /* We need external help for these */
195 typedef int hb_atomic_int_t;
196 #define hb_atomic_int_fetch_and_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V)
197 #define hb_atomic_int_get(AI) g_atomic_int_get (&(AI))
198 #define hb_atomic_int_set(AI, V) g_atomic_int_set (&(AI), V)
200 typedef GStaticMutex hb_mutex_t;
201 #define HB_MUTEX_INIT G_STATIC_MUTEX_INIT
202 #define hb_mutex_init(M) g_static_mutex_init (&M)
203 #define hb_mutex_lock(M) g_static_mutex_lock (&M)
204 #define hb_mutex_trylock(M) g_static_mutex_trylock (&M)
205 #define hb_mutex_unlock(M) g_static_mutex_unlock (&M)
210 #pragma message(__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe")
212 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
215 typedef int hb_atomic_int_t;
216 #define hb_atomic_int_fetch_and_add(AI, V) ((AI) += (V), (AI) - (V))
217 #define hb_atomic_int_get(AI) (AI)
218 #define hb_atomic_int_set(AI, V) HB_STMT_START { (AI) = (V); } HB_STMT_END
220 typedef int hb_mutex_t;
221 #define HB_MUTEX_INIT 0
222 #define hb_mutex_init(M) HB_STMT_START { (M) = 0; } HB_STMT_END
223 #define hb_mutex_lock(M) HB_STMT_START { (M) = 1; } HB_STMT_END
224 #define hb_mutex_trylock(M) ((M) = 1, 1)
225 #define hb_mutex_unlock(M) HB_STMT_START { (M) = 0; } HB_STMT_END
230 /* Big-endian handling */
232 #define hb_be_uint16(v) ((uint16_t) ((((const uint8_t *)&(v))[0] << 8) + (((const uint8_t *)&(v))[1])))
234 #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
235 #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
236 #define hb_be_uint16_cmp(a,b) (a[0] == b[0] && a[1] == b[1])
238 #define hb_be_uint32_put(v,V) HB_STMT_START { v[0] = (V>>24); v[1] = (V>>16); v[2] = (V>>8); v[3] = (V); } HB_STMT_END
239 #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
240 #define hb_be_uint32_cmp(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
249 static inline hb_bool_t /* always returns TRUE */
250 _hb_trace (const char *what,
251 const char *function,
254 unsigned int max_depth)
256 if (depth < max_depth)
257 fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function);
262 #include "hb-object-private.h"
267 #endif /* HB_PRIVATE_H */