2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2011 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
36 #include "hb-common.h"
42 /* We only use these two for debug output. However, the debug code is
43 * always seen by the compiler (and optimized out in non-debug builds.
44 * If including these becomes a problem, we can start thinking about
45 * someway around that. */
55 # define NULL ((void *) 0)
70 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
73 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
78 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
80 #define HB_STMT_START do
81 #define HB_STMT_END while (0)
83 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
84 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
85 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
87 #define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
90 /* Lets assert int types. Saves trouble down the road. */
92 ASSERT_STATIC (sizeof (int8_t) == 1);
93 ASSERT_STATIC (sizeof (uint8_t) == 1);
94 ASSERT_STATIC (sizeof (int16_t) == 2);
95 ASSERT_STATIC (sizeof (uint16_t) == 2);
96 ASSERT_STATIC (sizeof (int32_t) == 4);
97 ASSERT_STATIC (sizeof (uint32_t) == 4);
98 ASSERT_STATIC (sizeof (int64_t) == 8);
99 ASSERT_STATIC (sizeof (uint64_t) == 8);
101 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
102 ASSERT_STATIC (sizeof (hb_position_t) == 4);
103 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
104 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
109 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
110 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
111 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
112 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
114 #define likely(expr) (expr)
115 #define unlikely(expr) (expr)
120 #define __attribute__(x)
124 #define HB_PURE_FUNC __attribute__((pure))
125 #define HB_CONST_FUNC __attribute__((const))
128 #define HB_CONST_FUNC
131 #define HB_UNUSED __attribute__((unused))
137 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
141 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
142 #define snprintf _snprintf
147 #define inline __inline
150 #ifdef __STRICT_ANSI__
152 #define inline __inline__
157 #define HB_FUNC __PRETTY_FUNCTION__
158 #elif defined(_MSC_VER)
159 #define HB_FUNC __FUNCSIG__
161 #define HB_FUNC __func__
165 /* Return the number of 1 bits in mask. */
166 static inline HB_CONST_FUNC unsigned int
167 _hb_popcount32 (uint32_t mask)
169 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
170 return __builtin_popcount (mask);
174 y = (mask >> 1) &033333333333;
175 y = mask - y - ((y >>1) & 033333333333);
176 return (((y + (y >> 3)) & 030707070707) % 077);
180 /* Returns the number of bits needed to store number */
181 static inline HB_CONST_FUNC unsigned int
182 _hb_bit_storage (unsigned int number)
184 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
185 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
187 register unsigned int n_bits = 0;
196 /* Returns the number of zero bits in the least significant side of number */
197 static inline HB_CONST_FUNC unsigned int
198 _hb_ctz (unsigned int number)
200 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
201 return likely (number) ? __builtin_ctz (number) : 0;
203 register unsigned int n_bits = 0;
204 if (unlikely (!number)) return 0;
205 while (!(number & 1)) {
214 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
216 return (size > 0) && (count >= ((unsigned int) -1) / size);
220 /* Type of bsearch() / qsort() compare function */
221 typedef int (*hb_compare_func_t) (const void *, const void *);
224 /* We need external help for these */
230 typedef volatile int hb_atomic_int_t;
231 #define hb_atomic_int_fetch_and_add(AI, V) g_atomic_int_exchange_and_add (&(AI), V)
232 #define hb_atomic_int_get(AI) g_atomic_int_get (&(AI))
233 #define hb_atomic_int_set(AI, V) g_atomic_int_set (&(AI), V)
235 typedef GStaticMutex hb_mutex_t;
236 #define HB_MUTEX_INIT G_STATIC_MUTEX_INIT
237 #define hb_mutex_init(M) g_static_mutex_init (&M)
238 #define hb_mutex_lock(M) g_static_mutex_lock (&M)
239 #define hb_mutex_trylock(M) g_static_mutex_trylock (&M)
240 #define hb_mutex_unlock(M) g_static_mutex_unlock (&M)
245 #define _HB__STR2__(x) #x
246 #define _HB__STR1__(x) _HB__STR2__(x)
247 #define _HB__LOC__ __FILE__ "("_HB__STR1__(__LINE__)") : Warning Msg: "
248 #pragma message(_HB__LOC__"Could not find any system to define platform macros, library will NOT be thread-safe")
250 #warning "Could not find any system to define platform macros, library will NOT be thread-safe"
253 typedef volatile int hb_atomic_int_t;
254 #define hb_atomic_int_fetch_and_add(AI, V) ((AI) += (V), (AI) - (V))
255 #define hb_atomic_int_get(AI) (AI)
256 #define hb_atomic_int_set(AI, V) ((void) ((AI) = (V)))
258 typedef volatile int hb_mutex_t;
259 #define HB_MUTEX_INIT 0
260 #define hb_mutex_init(M) HB_STMT_START { (M) = 0; } HB_STMT_END
261 #define hb_mutex_lock(M) HB_STMT_START { (M) = 1; } HB_STMT_END
262 #define hb_mutex_trylock(M) ((M) = 1, 1)
263 #define hb_mutex_unlock(M) HB_STMT_START { (M) = 0; } HB_STMT_END
271 /* arrays and maps */
274 template <typename Type, unsigned int StaticSize>
275 struct hb_static_array_t {
278 unsigned int allocated;
280 Type static_array[StaticSize];
282 void finish (void) { for (unsigned i = 0; i < len; i++) array[i].finish (); }
284 inline Type& operator [] (unsigned int i)
289 inline Type *push (void)
292 array = static_array;
293 allocated = ARRAY_LENGTH (static_array);
295 if (likely (len < allocated))
296 return &array[len++];
298 /* Need to reallocate */
299 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
300 Type *new_array = NULL;
302 if (array == static_array) {
303 new_array = (Type *) calloc (new_allocated, sizeof (Type));
305 memcpy (new_array, array, len * sizeof (Type));
307 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
308 if (likely (!overflows)) {
309 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
313 if (unlikely (!new_array))
317 allocated = new_allocated;
318 return &array[len++];
321 inline void pop (void)
324 /* TODO: shrink array if needed */
328 template <typename Type>
329 struct hb_array_t : hb_static_array_t<Type, 2> {};
332 template <typename Key, typename Value>
337 /* unsigned int hash; */
340 void finish (void) { value.finish (); }
343 hb_array_t <item_t> items;
347 inline item_t *find (Key key) {
348 if (unlikely (!key)) return NULL;
349 for (unsigned int i = 0; i < items.len; i++)
350 if (key == items[i].key)
357 inline bool set (Key key,
360 if (unlikely (!key)) return NULL;
366 item = items.push ();
367 if (unlikely (!item)) return false;
373 inline void unset (Key &key)
380 *item = items[items.len - 1];
384 inline Value *get (Key key)
386 item_t *item = find (key);
387 return item ? &item->value : NULL;
390 void finish (void) { items.finish (); }
397 /* Big-endian handling */
399 static inline uint16_t hb_be_uint16 (const uint16_t v)
401 const uint8_t *V = (const uint8_t *) &v;
402 return (uint16_t) (V[0] << 8) + V[1];
405 #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
406 #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
407 #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
409 #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
410 #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
411 #define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
414 /* ASCII tag/character handling */
416 static inline unsigned char ISALPHA (unsigned char c)
417 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
418 static inline unsigned char ISALNUM (unsigned char c)
419 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
420 static inline unsigned char TOUPPER (unsigned char c)
421 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
422 static inline unsigned char TOLOWER (unsigned char c)
423 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
425 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
426 ((const char *) s)[1], \
427 ((const char *) s)[2], \
428 ((const char *) s)[3]))
437 static inline bool /* always returns TRUE */
438 _hb_trace (const char *what,
439 const char *function,
442 unsigned int max_depth)
444 (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function));
451 #endif /* HB_PRIVATE_HH */