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]))
88 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
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);
110 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
111 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
112 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
113 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
115 #define likely(expr) (expr)
116 #define unlikely(expr) (expr)
121 #define __attribute__(x)
125 #define HB_PURE_FUNC __attribute__((pure))
126 #define HB_CONST_FUNC __attribute__((const))
129 #define HB_CONST_FUNC
132 #define HB_UNUSED __attribute__((unused))
138 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
142 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
143 #define snprintf _snprintf
148 #define inline __inline
151 #ifdef __STRICT_ANSI__
153 #define inline __inline__
158 #define HB_FUNC __PRETTY_FUNCTION__
159 #elif defined(_MSC_VER)
160 #define HB_FUNC __FUNCSIG__
162 #define HB_FUNC __func__
166 /* Return the number of 1 bits in mask. */
167 static inline HB_CONST_FUNC unsigned int
168 _hb_popcount32 (uint32_t mask)
170 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
171 return __builtin_popcount (mask);
175 y = (mask >> 1) &033333333333;
176 y = mask - y - ((y >>1) & 033333333333);
177 return (((y + (y >> 3)) & 030707070707) % 077);
181 /* Returns the number of bits needed to store number */
182 static inline HB_CONST_FUNC unsigned int
183 _hb_bit_storage (unsigned int number)
185 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
186 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
188 register unsigned int n_bits = 0;
197 /* Returns the number of zero bits in the least significant side of number */
198 static inline HB_CONST_FUNC unsigned int
199 _hb_ctz (unsigned int number)
201 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
202 return likely (number) ? __builtin_ctz (number) : 0;
204 register unsigned int n_bits = 0;
205 if (unlikely (!number)) return 0;
206 while (!(number & 1)) {
215 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
217 return (size > 0) && (count >= ((unsigned int) -1) / size);
221 /* Type of bsearch() / qsort() compare function */
222 typedef int (*hb_compare_func_t) (const void *, const void *);
228 /* arrays and maps */
231 template <typename Type, unsigned int StaticSize>
232 struct hb_prealloced_array_t {
235 unsigned int allocated;
237 Type static_array[StaticSize];
239 inline Type& operator [] (unsigned int i) { return array[i]; }
240 inline const Type& operator [] (unsigned int i) const { return array[i]; }
242 inline Type *push (void)
245 array = static_array;
246 allocated = ARRAY_LENGTH (static_array);
248 if (likely (len < allocated))
249 return &array[len++];
251 /* Need to reallocate */
252 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
253 Type *new_array = NULL;
255 if (array == static_array) {
256 new_array = (Type *) calloc (new_allocated, sizeof (Type));
258 memcpy (new_array, array, len * sizeof (Type));
260 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
261 if (likely (!overflows)) {
262 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
266 if (unlikely (!new_array))
270 allocated = new_allocated;
271 return &array[len++];
274 inline void pop (void)
277 /* TODO: shrink array if needed */
280 inline void shrink (unsigned int l)
284 /* TODO: shrink array if needed */
287 template <typename T>
288 inline Type *find (T v) {
289 for (unsigned int i = 0; i < len; i++)
294 template <typename T>
295 inline const Type *find (T v) const {
296 for (unsigned int i = 0; i < len; i++)
302 inline void sort (void)
304 qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
307 inline void sort (unsigned int start, unsigned int end)
309 qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
312 template <typename T>
313 inline Type *bsearch (T *key)
315 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
317 template <typename T>
318 inline const Type *bsearch (T *key) const
320 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
323 inline void finish (void)
325 if (array != static_array)
332 template <typename Type>
333 struct hb_array_t : hb_prealloced_array_t<Type, 2> {};
336 template <typename item_t, typename lock_t>
337 struct hb_lockable_set_t
339 hb_array_t <item_t> items;
341 template <typename T>
342 inline item_t *replace_or_insert (T v, lock_t &l)
345 item_t *item = items.find (v);
352 item = items.push ();
360 template <typename T>
361 inline void remove (T v, lock_t &l)
364 item_t *item = items.find (v);
367 *item = items[items.len - 1];
376 template <typename T>
377 inline bool find (T v, item_t *i, lock_t &l)
380 item_t *item = items.find (v);
387 template <typename T>
388 inline item_t *find_or_insert (T v, lock_t &l)
391 item_t *item = items.find (v);
393 item = items.push ();
401 inline void finish (lock_t &l)
405 item_t old = items[items.len - 1];
421 /* Big-endian handling */
423 static inline uint16_t hb_be_uint16 (const uint16_t v)
425 const uint8_t *V = (const uint8_t *) &v;
426 return (uint16_t) (V[0] << 8) + V[1];
429 #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
430 #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
431 #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
433 #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
434 #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
435 #define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
438 /* ASCII tag/character handling */
440 static inline unsigned char ISALPHA (unsigned char c)
441 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
442 static inline unsigned char ISALNUM (unsigned char c)
443 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
444 static inline unsigned char TOUPPER (unsigned char c)
445 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
446 static inline unsigned char TOLOWER (unsigned char c)
447 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
449 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
450 ((const char *) s)[1], \
451 ((const char *) s)[2], \
452 ((const char *) s)[3]))
457 /* Makes class uncopyable. Use in private: section. */
460 T &operator = (const T &o)
469 static inline bool /* always returns TRUE */
470 _hb_trace (const char *what,
471 const char *function,
474 unsigned int max_depth)
476 (void) ((depth < max_depth) && fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, depth, depth, function));
481 /* Pre-mature optimization:
482 * Checks for lo <= u <= hi but with an optimization if lo and hi
483 * are only different in a contiguous set of lower-most bits.
486 hb_codepoint_in_range (hb_codepoint_t u, hb_codepoint_t lo, hb_codepoint_t hi)
488 if ( ((lo^hi) & lo) == 0 &&
489 ((lo^hi) & hi) == (lo^hi) &&
490 ((lo^hi) & ((lo^hi) + 1)) == 0 )
491 return (u & ~(lo^hi)) == lo;
493 return lo <= u && u <= hi;
497 /* Useful for set-operations on small enums */
498 #define FLAG(x) (1<<(x))
502 #endif /* HB_PRIVATE_HH */