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>
70 static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
73 template <typename Type>
74 static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
78 template <typename Type, unsigned int n>
79 static inline unsigned int ARRAY_LENGTH (const Type (&a)[n]) { return n; }
81 #define HB_STMT_START do
82 #define HB_STMT_END while (0)
84 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
85 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
86 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
88 #define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1]))
89 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
91 #define _PASTE1(a,b) a##b
92 #define PASTE(a,b) _PASTE1(a,b)
94 /* Lets assert int types. Saves trouble down the road. */
96 ASSERT_STATIC (sizeof (int8_t) == 1);
97 ASSERT_STATIC (sizeof (uint8_t) == 1);
98 ASSERT_STATIC (sizeof (int16_t) == 2);
99 ASSERT_STATIC (sizeof (uint16_t) == 2);
100 ASSERT_STATIC (sizeof (int32_t) == 4);
101 ASSERT_STATIC (sizeof (uint32_t) == 4);
102 ASSERT_STATIC (sizeof (int64_t) == 8);
103 ASSERT_STATIC (sizeof (uint64_t) == 8);
105 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
106 ASSERT_STATIC (sizeof (hb_position_t) == 4);
107 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
108 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
111 /* We like our types POD */
113 #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
114 #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
115 #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
118 # define _ASSERT_INSTANCE_POD1(_line, _instance) \
120 typedef __typeof__(_instance) _type_##_line; \
121 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
124 # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
126 # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
127 # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
129 /* Check _assertion in a method environment */
130 #define _ASSERT_POD1(_line) \
131 inline void _static_assertion_on_line_##_line (void) const \
132 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
133 # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
134 # define ASSERT_POD() _ASSERT_POD0 (__LINE__)
141 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
142 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
143 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
144 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
146 #define likely(expr) (expr)
147 #define unlikely(expr) (expr)
152 #define __attribute__(x)
156 #define HB_PURE_FUNC __attribute__((pure))
157 #define HB_CONST_FUNC __attribute__((const))
158 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
161 #define HB_CONST_FUNC
162 #define HB_PRINTF_FUNC(format_idx, arg_idx)
165 #define HB_UNUSED __attribute__((unused))
172 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
179 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
180 #define snprintf _snprintf
185 #define inline __inline
188 #ifdef __STRICT_ANSI__
190 #define inline __inline__
195 #define HB_FUNC __PRETTY_FUNCTION__
196 #elif defined(_MSC_VER)
197 #define HB_FUNC __FUNCSIG__
199 #define HB_FUNC __func__
203 /* Return the number of 1 bits in mask. */
204 static inline HB_CONST_FUNC unsigned int
205 _hb_popcount32 (uint32_t mask)
207 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
208 return __builtin_popcount (mask);
212 y = (mask >> 1) &033333333333;
213 y = mask - y - ((y >>1) & 033333333333);
214 return (((y + (y >> 3)) & 030707070707) % 077);
218 /* Returns the number of bits needed to store number */
219 static inline HB_CONST_FUNC unsigned int
220 _hb_bit_storage (unsigned int number)
222 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
223 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
225 register unsigned int n_bits = 0;
234 /* Returns the number of zero bits in the least significant side of number */
235 static inline HB_CONST_FUNC unsigned int
236 _hb_ctz (unsigned int number)
238 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
239 return likely (number) ? __builtin_ctz (number) : 0;
241 register unsigned int n_bits = 0;
242 if (unlikely (!number)) return 0;
243 while (!(number & 1)) {
252 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
254 return (size > 0) && (count >= ((unsigned int) -1) / size);
258 /* Type of bsearch() / qsort() compare function */
259 typedef int (*hb_compare_func_t) (const void *, const void *);
264 /* arrays and maps */
267 #define HB_PREALLOCED_ARRAY_INIT {0}
268 template <typename Type, unsigned int StaticSize>
269 struct hb_prealloced_array_t
272 unsigned int allocated;
274 Type static_array[StaticSize];
276 void init (void) { memset (this, 0, sizeof (*this)); }
278 inline Type& operator [] (unsigned int i) { return array[i]; }
279 inline const Type& operator [] (unsigned int i) const { return array[i]; }
281 inline Type *push (void)
284 array = static_array;
285 allocated = ARRAY_LENGTH (static_array);
287 if (likely (len < allocated))
288 return &array[len++];
290 /* Need to reallocate */
291 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
292 Type *new_array = NULL;
294 if (array == static_array) {
295 new_array = (Type *) calloc (new_allocated, sizeof (Type));
297 memcpy (new_array, array, len * sizeof (Type));
299 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
300 if (likely (!overflows)) {
301 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
305 if (unlikely (!new_array))
309 allocated = new_allocated;
310 return &array[len++];
313 inline void pop (void)
316 /* TODO: shrink array if needed */
319 inline void shrink (unsigned int l)
323 /* TODO: shrink array if needed */
326 template <typename T>
327 inline Type *find (T v) {
328 for (unsigned int i = 0; i < len; i++)
333 template <typename T>
334 inline const Type *find (T v) const {
335 for (unsigned int i = 0; i < len; i++)
341 inline void sort (void)
343 qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
346 inline void sort (unsigned int start, unsigned int end)
348 qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
351 template <typename T>
352 inline Type *bsearch (T *key)
354 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
356 template <typename T>
357 inline const Type *bsearch (T *key) const
359 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
362 inline void finish (void)
364 if (array != static_array)
372 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
373 template <typename item_t, typename lock_t>
374 struct hb_lockable_set_t
376 hb_prealloced_array_t <item_t, 2> items;
378 inline void init (void) { items.init (); }
380 template <typename T>
381 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
384 item_t *item = items.find (v);
397 item = items.push ();
405 template <typename T>
406 inline void remove (T v, lock_t &l)
409 item_t *item = items.find (v);
412 *item = items[items.len - 1];
421 template <typename T>
422 inline bool find (T v, item_t *i, lock_t &l)
425 item_t *item = items.find (v);
432 template <typename T>
433 inline item_t *find_or_insert (T v, lock_t &l)
436 item_t *item = items.find (v);
438 item = items.push ();
446 inline void finish (lock_t &l)
449 /* No need for locking. */
455 item_t old = items[items.len - 1];
470 /* Big-endian handling */
472 static inline uint16_t hb_be_uint16 (const uint16_t v)
474 const uint8_t *V = (const uint8_t *) &v;
475 return (V[0] << 8) | V[1];
478 static inline uint16_t hb_uint16_swap (const uint16_t v)
480 return (v >> 8) | (v << 8);
483 static inline uint32_t hb_uint32_swap (const uint32_t v)
485 return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16);
488 /* Note, of the following macros, uint16_get is the one called many many times.
489 * If there is any optimizations to be done, it's in that macro. However, I
490 * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which
491 * results in a single ror instruction, does NOT speed this up. In fact, it
492 * resulted in a minor slowdown. At any rate, note that v may not be correctly
493 * aligned, so I think the current implementation is optimal.
496 #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
497 #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
498 #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
500 #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
501 #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
502 #define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
505 /* ASCII tag/character handling */
507 static inline unsigned char ISALPHA (unsigned char c)
508 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
509 static inline unsigned char ISALNUM (unsigned char c)
510 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
511 static inline unsigned char TOUPPER (unsigned char c)
512 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
513 static inline unsigned char TOLOWER (unsigned char c)
514 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
516 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
517 ((const char *) s)[1], \
518 ((const char *) s)[2], \
519 ((const char *) s)[3]))
524 /* Makes class uncopyable. Use in private: section. */
527 T &operator = (const T &o)
538 _hb_debug (unsigned int level,
539 unsigned int max_level)
541 return level < max_level;
544 #define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
545 #define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
547 template <int max_level> inline void
548 _hb_debug_msg_va (const char *what,
557 if (!_hb_debug (level, max_level))
560 fprintf (stderr, "%-10s", what ? what : "");
563 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
565 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
568 /* One may want to add ASCII version of these. See:
569 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */
570 #define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
571 #define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
572 #define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
573 #define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
574 #define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
575 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;
576 fprintf (stderr, "%2d %s" VRBAR "%s",
578 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level),
579 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
581 fprintf (stderr, " " VRBAR LBAR);
584 /* If there's a class name, just write that. */
585 const char *dotdot = strstr (func, "::");
586 const char *space = strchr (func, ' ');
587 if (space && dotdot && space < dotdot)
589 unsigned int func_len = dotdot ? dotdot - func : strlen (func);
590 fprintf (stderr, "%.*s: ", func_len, func);
594 vfprintf (stderr, message, ap);
596 fprintf (stderr, "\n");
598 template <> inline void
599 _hb_debug_msg_va<0> (const char *what HB_UNUSED,
600 const void *obj HB_UNUSED,
601 const char *func HB_UNUSED,
602 bool indented HB_UNUSED,
603 unsigned int level HB_UNUSED,
604 int level_dir HB_UNUSED,
605 const char *message HB_UNUSED,
606 va_list ap HB_UNUSED) {}
608 template <int max_level> inline void
609 _hb_debug_msg (const char *what,
616 ...) HB_PRINTF_FUNC(7, 8);
617 template <int max_level> inline void
618 _hb_debug_msg (const char *what,
628 va_start (ap, message);
629 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
632 template <> inline void
633 _hb_debug_msg<0> (const char *what HB_UNUSED,
634 const void *obj HB_UNUSED,
635 const char *func HB_UNUSED,
636 bool indented HB_UNUSED,
637 unsigned int level HB_UNUSED,
638 int level_dir HB_UNUSED,
639 const char *message HB_UNUSED,
640 ...) HB_PRINTF_FUNC(7, 8);
641 template <> inline void
642 _hb_debug_msg<0> (const char *what HB_UNUSED,
643 const void *obj HB_UNUSED,
644 const char *func HB_UNUSED,
645 bool indented HB_UNUSED,
646 unsigned int level HB_UNUSED,
647 int level_dir HB_UNUSED,
648 const char *message HB_UNUSED,
651 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
652 #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
653 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
660 template <int max_level>
661 struct hb_auto_trace_t {
662 explicit inline hb_auto_trace_t (unsigned int *plevel_,
667 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
669 if (plevel) ++*plevel;
672 va_start (ap, message);
673 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
676 inline ~hb_auto_trace_t (void)
678 if (unlikely (!returned)) {
679 fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report. Level was %d.\n", plevel ? *plevel : -1);
680 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
684 if (plevel) --*plevel;
687 inline bool ret (bool v, unsigned int line = 0)
689 if (unlikely (returned)) {
690 fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n");
694 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, "return %s (line %d)", v ? "true" : "false", line);
695 if (plevel) --*plevel;
702 unsigned int *plevel;
707 template <> /* Optimize when tracing is disabled */
708 struct hb_auto_trace_t<0> {
709 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
710 const char *what HB_UNUSED,
711 const void *obj HB_UNUSED,
712 const char *func HB_UNUSED,
713 const char *message HB_UNUSED,
716 template <typename T>
717 inline T ret (T v, unsigned int line = 0) { return v; }
720 #define TRACE_RETURN(RET) trace.ret (RET, __LINE__)
725 /* Pre-mature optimization:
726 * Checks for lo <= u <= hi but with an optimization if lo and hi
727 * are only different in a contiguous set of lower-most bits.
729 template <typename T> static inline bool
730 hb_in_range (T u, T lo, T hi)
732 if ( ((lo^hi) & lo) == 0 &&
733 ((lo^hi) & hi) == (lo^hi) &&
734 ((lo^hi) & ((lo^hi) + 1)) == 0 )
735 return (u & ~(lo^hi)) == lo;
737 return lo <= u && u <= hi;
740 template <typename T> static inline bool
741 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
743 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3);
747 /* Useful for set-operations on small enums.
748 * For example, for testing "x ∈ {x1, x2, x3}" use:
749 * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
751 #define FLAG(x) (1<<(x))
752 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
755 template <typename T, typename T2> inline void
756 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
761 unsigned int k = len - 1;
763 unsigned int new_k = 0;
765 for (unsigned int j = 0; j < k; j++)
766 if (compar (&array[j], &array[j+1]) > 0)
771 array[j] = array[j + 1];
778 array2[j] = array2[j + 1];
788 template <typename T> inline void
789 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
791 hb_bubble_sort (array, len, compar, (int *) NULL);
794 static inline hb_bool_t
795 hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
797 /* Pain because we don't know whether s is nul-terminated. */
799 strncpy (buf, s, MIN (ARRAY_LENGTH (buf) - 1, len));
800 buf[MIN (ARRAY_LENGTH (buf) - 1, len)] = '\0';
804 unsigned long v = strtoul (buf, &end, base);
805 if (errno) return false;
806 if (*end) return false;
812 #endif /* HB_PRIVATE_HH */