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)
67 typedef const _hb_void_t &hb_void_t;
68 #define HB_VOID (* (const _hb_void_t *) NULL)
75 template <typename Type>
76 static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
79 template <typename Type>
80 static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
84 template <typename Type, unsigned int n>
85 static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
86 /* A const version, but does not detect erratically being called on pointers. */
87 #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
89 #define HB_STMT_START do
90 #define HB_STMT_END while (0)
92 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
93 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
94 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
96 #define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1]))
97 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
99 #define _PASTE1(a,b) a##b
100 #define PASTE(a,b) _PASTE1(a,b)
102 /* Lets assert int types. Saves trouble down the road. */
104 ASSERT_STATIC (sizeof (int8_t) == 1);
105 ASSERT_STATIC (sizeof (uint8_t) == 1);
106 ASSERT_STATIC (sizeof (int16_t) == 2);
107 ASSERT_STATIC (sizeof (uint16_t) == 2);
108 ASSERT_STATIC (sizeof (int32_t) == 4);
109 ASSERT_STATIC (sizeof (uint32_t) == 4);
110 ASSERT_STATIC (sizeof (int64_t) == 8);
111 ASSERT_STATIC (sizeof (uint64_t) == 8);
113 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
114 ASSERT_STATIC (sizeof (hb_position_t) == 4);
115 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
116 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
119 /* We like our types POD */
121 #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
122 #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
123 #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
126 # define _ASSERT_INSTANCE_POD1(_line, _instance) \
128 typedef __typeof__(_instance) _type_##_line; \
129 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
132 # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
134 # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
135 # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
137 /* Check _assertion in a method environment */
138 #define _ASSERT_POD1(_line) \
139 inline void _static_assertion_on_line_##_line (void) const \
140 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
141 # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
142 # define ASSERT_POD() _ASSERT_POD0 (__LINE__)
149 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
150 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
151 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
152 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
154 #define likely(expr) (expr)
155 #define unlikely(expr) (expr)
160 #define __attribute__(x)
164 #define HB_PURE_FUNC __attribute__((pure))
165 #define HB_CONST_FUNC __attribute__((const))
166 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
169 #define HB_CONST_FUNC
170 #define HB_PRINTF_FUNC(format_idx, arg_idx)
173 #define HB_UNUSED __attribute__((unused))
180 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
187 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
188 #define snprintf _snprintf
193 #define inline __inline
196 #ifdef __STRICT_ANSI__
198 #define inline __inline__
203 #define HB_FUNC __PRETTY_FUNCTION__
204 #elif defined(_MSC_VER)
205 #define HB_FUNC __FUNCSIG__
207 #define HB_FUNC __func__
211 /* Return the number of 1 bits in mask. */
212 static inline HB_CONST_FUNC unsigned int
213 _hb_popcount32 (uint32_t mask)
215 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
216 return __builtin_popcount (mask);
220 y = (mask >> 1) &033333333333;
221 y = mask - y - ((y >>1) & 033333333333);
222 return (((y + (y >> 3)) & 030707070707) % 077);
226 /* Returns the number of bits needed to store number */
227 static inline HB_CONST_FUNC unsigned int
228 _hb_bit_storage (unsigned int number)
230 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
231 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
233 register unsigned int n_bits = 0;
242 /* Returns the number of zero bits in the least significant side of number */
243 static inline HB_CONST_FUNC unsigned int
244 _hb_ctz (unsigned int number)
246 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
247 return likely (number) ? __builtin_ctz (number) : 0;
249 register unsigned int n_bits = 0;
250 if (unlikely (!number)) return 0;
251 while (!(number & 1)) {
260 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
262 return (size > 0) && (count >= ((unsigned int) -1) / size);
266 /* Type of bsearch() / qsort() compare function */
267 typedef int (*hb_compare_func_t) (const void *, const void *);
272 /* arrays and maps */
275 #define HB_PREALLOCED_ARRAY_INIT {0}
276 template <typename Type, unsigned int StaticSize>
277 struct hb_prealloced_array_t
280 unsigned int allocated;
282 Type static_array[StaticSize];
284 void init (void) { memset (this, 0, sizeof (*this)); }
286 inline Type& operator [] (unsigned int i) { return array[i]; }
287 inline const Type& operator [] (unsigned int i) const { return array[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 */
327 inline void shrink (unsigned int l)
331 /* TODO: shrink array if needed */
334 template <typename T>
335 inline Type *find (T v) {
336 for (unsigned int i = 0; i < len; i++)
341 template <typename T>
342 inline const Type *find (T v) const {
343 for (unsigned int i = 0; i < len; i++)
349 inline void sort (void)
351 qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
354 inline void sort (unsigned int start, unsigned int end)
356 qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
359 template <typename T>
360 inline Type *bsearch (T *key)
362 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
364 template <typename T>
365 inline const Type *bsearch (T *key) const
367 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
370 inline void finish (void)
372 if (array != static_array)
380 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
381 template <typename item_t, typename lock_t>
382 struct hb_lockable_set_t
384 hb_prealloced_array_t <item_t, 2> items;
386 inline void init (void) { items.init (); }
388 template <typename T>
389 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
392 item_t *item = items.find (v);
405 item = items.push ();
413 template <typename T>
414 inline void remove (T v, lock_t &l)
417 item_t *item = items.find (v);
420 *item = items[items.len - 1];
429 template <typename T>
430 inline bool find (T v, item_t *i, lock_t &l)
433 item_t *item = items.find (v);
440 template <typename T>
441 inline item_t *find_or_insert (T v, lock_t &l)
444 item_t *item = items.find (v);
446 item = items.push ();
454 inline void finish (lock_t &l)
457 /* No need for locking. */
463 item_t old = items[items.len - 1];
478 /* Big-endian handling */
480 static inline uint16_t hb_be_uint16 (const uint16_t v)
482 const uint8_t *V = (const uint8_t *) &v;
483 return (V[0] << 8) | V[1];
486 static inline uint16_t hb_uint16_swap (const uint16_t v)
488 return (v >> 8) | (v << 8);
491 static inline uint32_t hb_uint32_swap (const uint32_t v)
493 return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16);
496 /* Note, of the following macros, uint16_get is the one called many many times.
497 * If there is any optimizations to be done, it's in that macro. However, I
498 * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which
499 * results in a single ror instruction, does NOT speed this up. In fact, it
500 * resulted in a minor slowdown. At any rate, note that v may not be correctly
501 * aligned, so I think the current implementation is optimal.
504 #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
505 #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
506 #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
508 #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
509 #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
510 #define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
512 #define hb_be_uint24_put(v,V) HB_STMT_START { v[0] = (V>>16); v[1] = (V>>8); v[2] (V); } HB_STMT_END
513 #define hb_be_uint24_get(v) (uint32_t) ((v[0] << 16) + (v[1] << 8) + v[2])
514 #define hb_be_uint24_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2])
517 /* ASCII tag/character handling */
519 static inline unsigned char ISALPHA (unsigned char c)
520 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
521 static inline unsigned char ISALNUM (unsigned char c)
522 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
523 static inline unsigned char TOUPPER (unsigned char c)
524 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
525 static inline unsigned char TOLOWER (unsigned char c)
526 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
528 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
529 ((const char *) s)[1], \
530 ((const char *) s)[2], \
531 ((const char *) s)[3]))
536 /* Makes class uncopyable. Use in private: section. */
539 T &operator = (const T &o)
550 _hb_debug (unsigned int level,
551 unsigned int max_level)
553 return level < max_level;
556 #define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
557 #define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
559 template <int max_level> static inline void
560 _hb_debug_msg_va (const char *what,
569 if (!_hb_debug (level, max_level))
572 fprintf (stderr, "%-10s", what ? what : "");
575 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
577 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
580 /* One may want to add ASCII version of these. See:
581 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */
582 #define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
583 #define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
584 #define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
585 #define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
586 #define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
587 static const char bars[] = VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR;
588 fprintf (stderr, "%2d %s" VRBAR "%s",
590 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level),
591 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
593 fprintf (stderr, " " VRBAR LBAR);
597 /* Skip "typename" */
598 if (0 == strncmp (func, "typename ", 9))
600 /* Skip return type */
601 const char *space = strchr (func, ' ');
604 /* Skip parameter list */
605 const char *paren = strchr (func, '(');
606 unsigned int func_len = paren ? paren - func : strlen (func);
607 fprintf (stderr, "%.*s: ", func_len, func);
611 vfprintf (stderr, message, ap);
613 fprintf (stderr, "\n");
615 template <> inline void
616 _hb_debug_msg_va<0> (const char *what HB_UNUSED,
617 const void *obj HB_UNUSED,
618 const char *func HB_UNUSED,
619 bool indented HB_UNUSED,
620 unsigned int level HB_UNUSED,
621 int level_dir HB_UNUSED,
622 const char *message HB_UNUSED,
623 va_list ap HB_UNUSED) {}
625 template <int max_level> static inline void
626 _hb_debug_msg (const char *what,
633 ...) HB_PRINTF_FUNC(7, 8);
634 template <int max_level> static inline void
635 _hb_debug_msg (const char *what,
645 va_start (ap, message);
646 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
649 template <> inline void
650 _hb_debug_msg<0> (const char *what HB_UNUSED,
651 const void *obj HB_UNUSED,
652 const char *func HB_UNUSED,
653 bool indented HB_UNUSED,
654 unsigned int level HB_UNUSED,
655 int level_dir HB_UNUSED,
656 const char *message HB_UNUSED,
657 ...) HB_PRINTF_FUNC(7, 8);
658 template <> inline void
659 _hb_debug_msg<0> (const char *what HB_UNUSED,
660 const void *obj HB_UNUSED,
661 const char *func HB_UNUSED,
662 bool indented HB_UNUSED,
663 unsigned int level HB_UNUSED,
664 int level_dir HB_UNUSED,
665 const char *message HB_UNUSED,
668 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
669 #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
670 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
677 template <typename T>
678 struct hb_printer_t {};
681 struct hb_printer_t<bool> {
682 const char *print (bool v) { return v ? "true" : "false"; }
686 struct hb_printer_t<hb_void_t> {
687 const char *print (hb_void_t) { return ""; }
695 template <typename T>
696 static inline void _hb_warn_no_return (bool returned)
698 if (unlikely (!returned)) {
699 fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n");
703 inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED)
706 template <int max_level, typename ret_t>
707 struct hb_auto_trace_t {
708 explicit inline hb_auto_trace_t (unsigned int *plevel_,
713 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
715 if (plevel) ++*plevel;
718 va_start (ap, message);
719 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
722 inline ~hb_auto_trace_t (void)
724 _hb_warn_no_return<ret_t> (returned);
726 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
728 if (plevel) --*plevel;
731 inline ret_t ret (ret_t v, unsigned int line = 0)
733 if (unlikely (returned)) {
734 fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n");
738 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1,
739 "return %s (line %d)",
740 hb_printer_t<ret_t>().print (v), line);
741 if (plevel) --*plevel;
748 unsigned int *plevel;
753 template <typename ret_t> /* Optimize when tracing is disabled */
754 struct hb_auto_trace_t<0, ret_t> {
755 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
756 const char *what HB_UNUSED,
757 const void *obj HB_UNUSED,
758 const char *func HB_UNUSED,
759 const char *message HB_UNUSED,
762 inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; }
765 #define TRACE_RETURN(RET) trace.ret (RET, __LINE__)
770 /* Pre-mature optimization:
771 * Checks for lo <= u <= hi but with an optimization if lo and hi
772 * are only different in a contiguous set of lower-most bits.
774 template <typename T> static inline bool
775 hb_in_range (T u, T lo, T hi)
777 if ( ((lo^hi) & lo) == 0 &&
778 ((lo^hi) & hi) == (lo^hi) &&
779 ((lo^hi) & ((lo^hi) + 1)) == 0 )
780 return (u & ~(lo^hi)) == lo;
782 return lo <= u && u <= hi;
785 template <typename T> static inline bool
786 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
788 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3);
792 /* Useful for set-operations on small enums.
793 * For example, for testing "x ∈ {x1, x2, x3}" use:
794 * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
796 #define FLAG(x) (1<<(x))
797 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
800 template <typename T, typename T2> inline void
801 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
806 unsigned int k = len - 1;
808 unsigned int new_k = 0;
810 for (unsigned int j = 0; j < k; j++)
811 if (compar (&array[j], &array[j+1]) > 0)
816 array[j] = array[j + 1];
823 array2[j] = array2[j + 1];
833 template <typename T> inline void
834 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
836 hb_bubble_sort (array, len, compar, (int *) NULL);
839 static inline hb_bool_t
840 hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
842 /* Pain because we don't know whether s is nul-terminated. */
844 strncpy (buf, s, MIN (ARRAY_LENGTH (buf) - 1, len));
845 buf[MIN (ARRAY_LENGTH (buf) - 1, len)] = '\0';
849 unsigned long v = strtoul (buf, &end, base);
850 if (errno) return false;
851 if (*end) return false;
857 #endif /* HB_PRIVATE_HH */