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"
43 /* We only use these two for debug output. However, the debug code is
44 * always seen by the compiler (and optimized out in non-debug builds.
45 * If including these becomes a problem, we can start thinking about
46 * someway around that. */
57 # define NULL ((void *) 0)
72 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
75 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
80 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
82 #define HB_STMT_START do
83 #define HB_STMT_END while (0)
85 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
86 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
87 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
89 #define ASSERT_STATIC_EXPR(_cond) ((void) sizeof (char[(_cond) ? 1 : -1]))
90 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
93 /* Lets assert int types. Saves trouble down the road. */
95 ASSERT_STATIC (sizeof (int8_t) == 1);
96 ASSERT_STATIC (sizeof (uint8_t) == 1);
97 ASSERT_STATIC (sizeof (int16_t) == 2);
98 ASSERT_STATIC (sizeof (uint16_t) == 2);
99 ASSERT_STATIC (sizeof (int32_t) == 4);
100 ASSERT_STATIC (sizeof (uint32_t) == 4);
101 ASSERT_STATIC (sizeof (int64_t) == 8);
102 ASSERT_STATIC (sizeof (uint64_t) == 8);
104 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
105 ASSERT_STATIC (sizeof (hb_position_t) == 4);
106 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
107 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
112 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
113 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
114 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
115 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
117 #define likely(expr) (expr)
118 #define unlikely(expr) (expr)
123 #define __attribute__(x)
127 #define HB_PURE_FUNC __attribute__((pure))
128 #define HB_CONST_FUNC __attribute__((const))
129 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
132 #define HB_CONST_FUNC
133 #define HB_PRINTF_FUCN(format_idx, arg_idx)
136 #define HB_UNUSED __attribute__((unused))
142 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
146 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
147 #define snprintf _snprintf
152 #define inline __inline
155 #ifdef __STRICT_ANSI__
157 #define inline __inline__
162 #define HB_FUNC __PRETTY_FUNCTION__
163 #elif defined(_MSC_VER)
164 #define HB_FUNC __FUNCSIG__
166 #define HB_FUNC __func__
170 /* Return the number of 1 bits in mask. */
171 static inline HB_CONST_FUNC unsigned int
172 _hb_popcount32 (uint32_t mask)
174 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
175 return __builtin_popcount (mask);
179 y = (mask >> 1) &033333333333;
180 y = mask - y - ((y >>1) & 033333333333);
181 return (((y + (y >> 3)) & 030707070707) % 077);
185 /* Returns the number of bits needed to store number */
186 static inline HB_CONST_FUNC unsigned int
187 _hb_bit_storage (unsigned int number)
189 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
190 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
192 register unsigned int n_bits = 0;
201 /* Returns the number of zero bits in the least significant side of number */
202 static inline HB_CONST_FUNC unsigned int
203 _hb_ctz (unsigned int number)
205 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
206 return likely (number) ? __builtin_ctz (number) : 0;
208 register unsigned int n_bits = 0;
209 if (unlikely (!number)) return 0;
210 while (!(number & 1)) {
219 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
221 return (size > 0) && (count >= ((unsigned int) -1) / size);
225 /* Type of bsearch() / qsort() compare function */
226 typedef int (*hb_compare_func_t) (const void *, const void *);
232 /* arrays and maps */
235 template <typename Type, unsigned int StaticSize>
236 struct hb_prealloced_array_t {
239 unsigned int allocated;
241 Type static_array[StaticSize];
243 inline Type& operator [] (unsigned int i) { return array[i]; }
244 inline const Type& operator [] (unsigned int i) const { return array[i]; }
246 inline Type *push (void)
249 array = static_array;
250 allocated = ARRAY_LENGTH (static_array);
252 if (likely (len < allocated))
253 return &array[len++];
255 /* Need to reallocate */
256 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
257 Type *new_array = NULL;
259 if (array == static_array) {
260 new_array = (Type *) calloc (new_allocated, sizeof (Type));
262 memcpy (new_array, array, len * sizeof (Type));
264 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
265 if (likely (!overflows)) {
266 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
270 if (unlikely (!new_array))
274 allocated = new_allocated;
275 return &array[len++];
278 inline void pop (void)
281 /* TODO: shrink array if needed */
284 inline void shrink (unsigned int l)
288 /* TODO: shrink array if needed */
291 template <typename T>
292 inline Type *find (T v) {
293 for (unsigned int i = 0; i < len; i++)
298 template <typename T>
299 inline const Type *find (T v) const {
300 for (unsigned int i = 0; i < len; i++)
306 inline void sort (void)
308 qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
311 inline void sort (unsigned int start, unsigned int end)
313 qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
316 template <typename T>
317 inline Type *bsearch (T *key)
319 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
321 template <typename T>
322 inline const Type *bsearch (T *key) const
324 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
327 inline void finish (void)
329 if (array != static_array)
336 template <typename Type>
337 struct hb_array_t : hb_prealloced_array_t<Type, 2> {};
340 template <typename item_t, typename lock_t>
341 struct hb_lockable_set_t
343 hb_array_t <item_t> items;
345 template <typename T>
346 inline item_t *replace_or_insert (T v, lock_t &l)
349 item_t *item = items.find (v);
356 item = items.push ();
364 template <typename T>
365 inline void remove (T v, lock_t &l)
368 item_t *item = items.find (v);
371 *item = items[items.len - 1];
380 template <typename T>
381 inline bool find (T v, item_t *i, lock_t &l)
384 item_t *item = items.find (v);
391 template <typename T>
392 inline item_t *find_or_insert (T v, lock_t &l)
395 item_t *item = items.find (v);
397 item = items.push ();
405 inline void finish (lock_t &l)
409 item_t old = items[items.len - 1];
425 /* Big-endian handling */
427 static inline uint16_t hb_be_uint16 (const uint16_t v)
429 const uint8_t *V = (const uint8_t *) &v;
430 return (uint16_t) (V[0] << 8) + V[1];
433 #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
434 #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
435 #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
437 #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
438 #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
439 #define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
442 /* ASCII tag/character handling */
444 static inline unsigned char ISALPHA (unsigned char c)
445 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
446 static inline unsigned char ISALNUM (unsigned char c)
447 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
448 static inline unsigned char TOUPPER (unsigned char c)
449 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
450 static inline unsigned char TOLOWER (unsigned char c)
451 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
453 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
454 ((const char *) s)[1], \
455 ((const char *) s)[2], \
456 ((const char *) s)[3]))
461 /* Makes class uncopyable. Use in private: section. */
464 T &operator = (const T &o)
476 _hb_debug (unsigned int level,
477 unsigned int max_level)
479 return level < max_level;
482 #define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
483 #define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
485 template <int max_level> inline bool /* always returns TRUE */
486 _hb_debug_msg (const char *what,
492 ...) HB_PRINTF_FUNC(6, 7);
493 template <int max_level> inline bool /* always returns TRUE */
494 _hb_debug_msg (const char *what,
503 va_start (ap, message);
505 (void) (_hb_debug (level, max_level) &&
506 fprintf (stderr, "%s(%p): ", what, obj) &&
507 (func && fprintf (stderr, "%s: ", func), TRUE) &&
508 (indented && fprintf (stderr, "%-*d-> ", level + 1, level), TRUE) &&
509 vfprintf (stderr, message, ap) &&
510 fprintf (stderr, "\n"));
516 template <> inline bool /* always returns TRUE */
517 _hb_debug_msg<0> (const char *what,
523 ...) HB_PRINTF_FUNC(6, 7);
524 template <> inline bool /* always returns TRUE */
525 _hb_debug_msg<0> (const char *what,
536 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, FALSE, (LEVEL), __VA_ARGS__)
537 #define DEBUG_MSG(WHAT, OBJ, ...) DEBUG_MSG_LEVEL (WHAT, OBJ, 0, __VA_ARGS__)
538 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, FALSE, 0, __VA_ARGS__)
545 template <int max_level>
546 struct hb_auto_trace_t {
547 explicit inline hb_auto_trace_t (unsigned int *plevel_,
551 const char *message) : plevel(plevel_)
553 if (max_level) ++*plevel;
554 /* TODO support variadic args here */
555 _hb_debug_msg<max_level> (what, obj, func, TRUE, *plevel, "%s", message);
557 ~hb_auto_trace_t (void) { if (max_level) --*plevel; }
560 unsigned int *plevel;
562 template <> /* Optimize when tracing is disabled */
563 struct hb_auto_trace_t<0> {
564 explicit inline hb_auto_trace_t (unsigned int *plevel_,
568 const char *message) {}
575 /* Pre-mature optimization:
576 * Checks for lo <= u <= hi but with an optimization if lo and hi
577 * are only different in a contiguous set of lower-most bits.
579 template <typename T> inline bool
580 hb_in_range (T u, T lo, T hi)
582 if ( ((lo^hi) & lo) == 0 &&
583 ((lo^hi) & hi) == (lo^hi) &&
584 ((lo^hi) & ((lo^hi) + 1)) == 0 )
585 return (u & ~(lo^hi)) == lo;
587 return lo <= u && u <= hi;
591 /* Useful for set-operations on small enums.
592 * For example, for testing "x ∈ {x1, x2, x3}" use:
593 * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
595 #define FLAG(x) (1<<(x))
598 template <typename T> inline void
599 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
604 unsigned int k = len - 1;
606 unsigned int new_k = 0;
608 for (unsigned int j = 0; j < k; j++)
609 if (compar (&array[j], &array[j+1]) > 0) {
612 array[j] = array[j + 1];
626 #endif /* HB_PRIVATE_HH */