2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2011,2012 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
48 /* We only use these two for debug output. However, the debug code is
49 * always seen by the compiler (and optimized out in non-debug builds.
50 * If including these becomes a problem, we can start thinking about
51 * someway around that. */
61 # define NULL ((void *) 0)
69 template <typename Type> static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
72 template <typename Type> static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
76 #define ARRAY_LENGTH(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
78 #define HB_STMT_START do
79 #define HB_STMT_END while (0)
81 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
82 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
83 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
85 #define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1]))
86 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
88 #define _PASTE1(a,b) a##b
89 #define PASTE(a,b) _PASTE1(a,b)
91 /* Lets assert int types. Saves trouble down the road. */
93 ASSERT_STATIC (sizeof (int8_t) == 1);
94 ASSERT_STATIC (sizeof (uint8_t) == 1);
95 ASSERT_STATIC (sizeof (int16_t) == 2);
96 ASSERT_STATIC (sizeof (uint16_t) == 2);
97 ASSERT_STATIC (sizeof (int32_t) == 4);
98 ASSERT_STATIC (sizeof (uint32_t) == 4);
99 ASSERT_STATIC (sizeof (int64_t) == 8);
100 ASSERT_STATIC (sizeof (uint64_t) == 8);
102 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
103 ASSERT_STATIC (sizeof (hb_position_t) == 4);
104 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
105 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
108 /* We like our types POD */
110 #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
111 #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
112 #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
115 # define _ASSERT_INSTANCE_POD1(_line, _instance) \
117 typedef __typeof__(_instance) _type_##_line; \
118 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
121 # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
123 # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
124 # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
126 /* Check _assertion in a method environment */
127 #define _ASSERT_POD1(_line) \
128 inline void _static_assertion_on_line_##_line (void) const \
129 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
130 # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
131 # define ASSERT_POD() _ASSERT_POD0 (__LINE__)
138 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
139 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
140 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
141 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
143 #define likely(expr) (expr)
144 #define unlikely(expr) (expr)
149 #define __attribute__(x)
153 #define HB_PURE_FUNC __attribute__((pure))
154 #define HB_CONST_FUNC __attribute__((const))
155 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
158 #define HB_CONST_FUNC
159 #define HB_PRINTF_FUNC(format_idx, arg_idx)
162 #define HB_UNUSED __attribute__((unused))
169 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
176 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
177 #define snprintf _snprintf
182 #define inline __inline
185 #ifdef __STRICT_ANSI__
187 #define inline __inline__
192 #define HB_FUNC __PRETTY_FUNCTION__
193 #elif defined(_MSC_VER)
194 #define HB_FUNC __FUNCSIG__
196 #define HB_FUNC __func__
200 /* Return the number of 1 bits in mask. */
201 static inline HB_CONST_FUNC unsigned int
202 _hb_popcount32 (uint32_t mask)
204 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
205 return __builtin_popcount (mask);
209 y = (mask >> 1) &033333333333;
210 y = mask - y - ((y >>1) & 033333333333);
211 return (((y + (y >> 3)) & 030707070707) % 077);
215 /* Returns the number of bits needed to store number */
216 static inline HB_CONST_FUNC unsigned int
217 _hb_bit_storage (unsigned int number)
219 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
220 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
222 register unsigned int n_bits = 0;
231 /* Returns the number of zero bits in the least significant side of number */
232 static inline HB_CONST_FUNC unsigned int
233 _hb_ctz (unsigned int number)
235 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
236 return likely (number) ? __builtin_ctz (number) : 0;
238 register unsigned int n_bits = 0;
239 if (unlikely (!number)) return 0;
240 while (!(number & 1)) {
249 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
251 return (size > 0) && (count >= ((unsigned int) -1) / size);
255 /* Type of bsearch() / qsort() compare function */
256 typedef int (*hb_compare_func_t) (const void *, const void *);
261 /* arrays and maps */
264 #define HB_PREALLOCED_ARRAY_INIT {0}
265 template <typename Type, unsigned int StaticSize>
266 struct hb_prealloced_array_t
269 unsigned int allocated;
271 Type static_array[StaticSize];
273 void init (void) { memset (this, 0, sizeof (*this)); }
275 inline Type& operator [] (unsigned int i) { return array[i]; }
276 inline const Type& operator [] (unsigned int i) const { return array[i]; }
278 inline Type *push (void)
281 array = static_array;
282 allocated = ARRAY_LENGTH (static_array);
284 if (likely (len < allocated))
285 return &array[len++];
287 /* Need to reallocate */
288 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
289 Type *new_array = NULL;
291 if (array == static_array) {
292 new_array = (Type *) calloc (new_allocated, sizeof (Type));
294 memcpy (new_array, array, len * sizeof (Type));
296 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
297 if (likely (!overflows)) {
298 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
302 if (unlikely (!new_array))
306 allocated = new_allocated;
307 return &array[len++];
310 inline void pop (void)
313 /* TODO: shrink array if needed */
316 inline void shrink (unsigned int l)
320 /* TODO: shrink array if needed */
323 template <typename T>
324 inline Type *find (T v) {
325 for (unsigned int i = 0; i < len; i++)
330 template <typename T>
331 inline const Type *find (T v) const {
332 for (unsigned int i = 0; i < len; i++)
338 inline void sort (void)
340 qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
343 inline void sort (unsigned int start, unsigned int end)
345 qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
348 template <typename T>
349 inline Type *bsearch (T *key)
351 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
353 template <typename T>
354 inline const Type *bsearch (T *key) const
356 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
359 inline void finish (void)
361 if (array != static_array)
369 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
370 template <typename item_t, typename lock_t>
371 struct hb_lockable_set_t
373 hb_prealloced_array_t <item_t, 2> items;
375 inline void init (void) { items.init (); }
377 template <typename T>
378 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
381 item_t *item = items.find (v);
394 item = items.push ();
402 template <typename T>
403 inline void remove (T v, lock_t &l)
406 item_t *item = items.find (v);
409 *item = items[items.len - 1];
418 template <typename T>
419 inline bool find (T v, item_t *i, lock_t &l)
422 item_t *item = items.find (v);
429 template <typename T>
430 inline item_t *find_or_insert (T v, lock_t &l)
433 item_t *item = items.find (v);
435 item = items.push ();
443 inline void finish (lock_t &l)
447 item_t old = items[items.len - 1];
462 /* Big-endian handling */
464 static inline uint16_t hb_be_uint16 (const uint16_t v)
466 const uint8_t *V = (const uint8_t *) &v;
467 return (uint16_t) (V[0] << 8) + V[1];
470 /* Note, of the following macros, uint16_get is the one called many many times.
471 * If there is any optimizations to be done, it's in that macro. However, I
472 * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which
473 * results in a single ror instruction, does NOT speed this up. In fact, it
474 * resulted in a minor slowdown. At any rate, note that v may not be correctly
475 * aligned, so I think the current implementation is optimal.
478 #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
479 #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
480 #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
482 #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
483 #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
484 #define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
487 /* ASCII tag/character handling */
489 static inline unsigned char ISALPHA (unsigned char c)
490 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
491 static inline unsigned char ISALNUM (unsigned char c)
492 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
493 static inline unsigned char TOUPPER (unsigned char c)
494 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
495 static inline unsigned char TOLOWER (unsigned char c)
496 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
498 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
499 ((const char *) s)[1], \
500 ((const char *) s)[2], \
501 ((const char *) s)[3]))
506 /* Makes class uncopyable. Use in private: section. */
509 T &operator = (const T &o)
520 _hb_debug (unsigned int level,
521 unsigned int max_level)
523 return level < max_level;
526 #define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
527 #define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
529 template <int max_level> inline void
530 _hb_debug_msg_va (const char *what,
539 if (!_hb_debug (level, max_level))
542 fprintf (stderr, "%-10s", what ? what : "");
545 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
547 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
550 static const char bars[] = "││││││││││││││││││││││││││││││││││││││││";
551 fprintf (stderr, "%2d %s├%s",
553 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), 3 * level),
554 level_dir ? (level_dir > 0 ? "╮" : "╯") : "╴");
556 fprintf (stderr, " ├╴");
559 /* If there's a class name, just write that. */
560 const char *dotdot = strstr (func, "::");
561 const char *space = strchr (func, ' ');
562 if (space && dotdot && space < dotdot)
564 unsigned int func_len = dotdot ? dotdot - func : strlen (func);
565 fprintf (stderr, "%.*s: ", func_len, func);
569 vfprintf (stderr, message, ap);
571 fprintf (stderr, "\n");
573 template <> inline void
574 _hb_debug_msg_va<0> (const char *what HB_UNUSED,
575 const void *obj HB_UNUSED,
576 const char *func HB_UNUSED,
577 bool indented HB_UNUSED,
578 unsigned int level HB_UNUSED,
579 int level_dir HB_UNUSED,
580 const char *message HB_UNUSED,
581 va_list ap HB_UNUSED) {}
583 template <int max_level> inline void
584 _hb_debug_msg (const char *what,
591 ...) HB_PRINTF_FUNC(7, 8);
592 template <int max_level> inline void
593 _hb_debug_msg (const char *what,
603 va_start (ap, message);
604 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
607 template <> inline void
608 _hb_debug_msg<0> (const char *what HB_UNUSED,
609 const void *obj HB_UNUSED,
610 const char *func HB_UNUSED,
611 bool indented HB_UNUSED,
612 unsigned int level HB_UNUSED,
613 int level_dir HB_UNUSED,
614 const char *message HB_UNUSED,
615 ...) HB_PRINTF_FUNC(7, 8);
616 template <> inline void
617 _hb_debug_msg<0> (const char *what HB_UNUSED,
618 const void *obj HB_UNUSED,
619 const char *func HB_UNUSED,
620 bool indented HB_UNUSED,
621 unsigned int level HB_UNUSED,
622 int level_dir HB_UNUSED,
623 const char *message HB_UNUSED,
626 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
627 #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
628 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
635 template <int max_level>
636 struct hb_auto_trace_t {
637 explicit inline hb_auto_trace_t (unsigned int *plevel_,
642 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
644 if (plevel) ++*plevel;
647 va_start (ap, message);
648 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
651 inline ~hb_auto_trace_t (void)
653 if (unlikely (!returned)) {
654 fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report. Level was %d.\n", plevel ? *plevel : -1);
655 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
659 if (plevel) --*plevel;
662 inline bool ret (bool v)
664 if (unlikely (returned)) {
665 fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n");
669 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, "return %s", v ? "true" : "false");
670 if (plevel) --*plevel;
677 unsigned int *plevel;
682 template <> /* Optimize when tracing is disabled */
683 struct hb_auto_trace_t<0> {
684 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
685 const char *what HB_UNUSED,
686 const void *obj HB_UNUSED,
687 const char *func HB_UNUSED,
688 const char *message HB_UNUSED,
691 template <typename T>
692 inline T ret (T v) { return v; }
695 #define TRACE_RETURN(RET) trace.ret (RET)
700 /* Pre-mature optimization:
701 * Checks for lo <= u <= hi but with an optimization if lo and hi
702 * are only different in a contiguous set of lower-most bits.
704 template <typename T> inline bool
705 hb_in_range (T u, T lo, T hi)
707 if ( ((lo^hi) & lo) == 0 &&
708 ((lo^hi) & hi) == (lo^hi) &&
709 ((lo^hi) & ((lo^hi) + 1)) == 0 )
710 return (u & ~(lo^hi)) == lo;
712 return lo <= u && u <= hi;
716 /* Useful for set-operations on small enums.
717 * For example, for testing "x ∈ {x1, x2, x3}" use:
718 * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
720 #define FLAG(x) (1<<(x))
723 template <typename T> inline void
724 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
729 unsigned int k = len - 1;
731 unsigned int new_k = 0;
733 for (unsigned int j = 0; j < k; j++)
734 if (compar (&array[j], &array[j+1]) > 0) {
737 array[j] = array[j + 1];
749 #endif /* HB_PRIVATE_HH */