blob: eba797cfce9c7d909504304f08a01e4d6a90559e [file] [log] [blame]
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2007,2008,2009 Red Hat, Inc.
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02003 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05006 *
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.
12 *
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
17 * DAMAGE.
18 *
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.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -040026 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -050027 */
28
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040029#ifndef HB_PRIVATE_HH
30#define HB_PRIVATE_HH
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -050031
Ebrahim Byagowi5dbbd0f2018-01-11 12:33:22 +033032#define _GNU_SOURCE 1
33
Behdad Esfahbod5ddd9cc2011-09-16 16:40:44 -040034#ifdef HAVE_CONFIG_H
Behdad Esfahboddf660282009-08-01 20:46:02 -040035#include "config.h"
36#endif
Behdad Esfahbod12360f72008-01-23 15:50:38 -050037
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040038#include "hb.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040039#define HB_H_IN
Behdad Esfahbod3e32cd92012-04-23 13:20:52 -040040#ifdef HAVE_OT
41#include "hb-ot.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040042#define HB_OT_H_IN
Behdad Esfahbod3e32cd92012-04-23 13:20:52 -040043#endif
Behdad Esfahbodb28815c2009-08-04 22:35:36 -040044
Behdad Esfahbod7cdd6ab2018-01-10 04:33:08 +010045#include <math.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040046#include <stdlib.h>
Behdad Esfahbodb65c0602011-07-28 16:48:43 -040047#include <stddef.h>
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040048#include <string.h>
49#include <assert.h>
Behdad Esfahbod7acb3892009-08-05 15:20:34 -040050#include <errno.h>
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -040051#include <stdio.h>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -040052#include <stdarg.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040053
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040054
Behdad Esfahbod2a749682017-10-26 19:48:33 -060055#define HB_PASTE1(a,b) a##b
56#define HB_PASTE(a,b) HB_PASTE1(a,b)
57
Behdad Esfahbod52b41852015-10-03 13:20:55 +010058/* Compile-time custom allocator support. */
59
60#if defined(hb_malloc_impl) \
61 && defined(hb_calloc_impl) \
62 && defined(hb_realloc_impl) \
63 && defined(hb_free_impl)
Behdad Esfahbodcc6ea302015-10-12 17:21:52 -040064extern "C" void* hb_malloc_impl(size_t size);
65extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
66extern "C" void* hb_realloc_impl(void *ptr, size_t size);
67extern "C" void hb_free_impl(void *ptr);
Behdad Esfahbod52b41852015-10-03 13:20:55 +010068#define malloc hb_malloc_impl
69#define calloc hb_calloc_impl
70#define realloc hb_realloc_impl
71#define free hb_free_impl
72#endif
73
74
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040075/* Compiler attributes */
Behdad Esfahbodbc200452010-04-29 01:40:26 -040076
Behdad Esfahbodbc200452010-04-29 01:40:26 -040077
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +020078#if __cplusplus < 201103L
79
Behdad Esfahbod62e312e2017-10-27 09:29:20 -060080#ifndef nullptr
81#define nullptr NULL
82#endif
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +020083
84// Static assertions
85#ifndef static_assert
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +020086#define static_assert(e, msg) \
Behdad Esfahbod6f08b122017-10-26 18:23:03 -060087 HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +020088#endif // static_assert
89
90#endif // __cplusplus < 201103L
91
Ebrahim Byagowi76c48732017-06-02 21:53:10 +043092#if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
93#define likely(expr) (__builtin_expect (!!(expr), 1))
94#define unlikely(expr) (__builtin_expect (!!(expr), 0))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040095#else
96#define likely(expr) (expr)
97#define unlikely(expr) (expr)
Behdad Esfahbodbc200452010-04-29 01:40:26 -040098#endif
99
Steve Lhomme0b8f3ab2016-07-11 21:57:26 +0200100#if !defined(__GNUC__) && !defined(__clang__)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400101#undef __attribute__
102#define __attribute__(x)
103#endif
Behdad Esfahboda794ebf2009-08-06 12:32:35 -0400104
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400105#if __GNUC__ >= 3
106#define HB_PURE_FUNC __attribute__((pure))
107#define HB_CONST_FUNC __attribute__((const))
108#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
109#else
110#define HB_PURE_FUNC
111#define HB_CONST_FUNC
112#define HB_PRINTF_FUNC(format_idx, arg_idx)
113#endif
114#if __GNUC__ >= 4
115#define HB_UNUSED __attribute__((unused))
Behdad Esfahbodbe59f3c2017-12-05 09:01:28 -0800116#elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
117#define HB_UNUSED __pragma(warning(suppress: 4100 4101))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400118#else
119#define HB_UNUSED
120#endif
121
122#ifndef HB_INTERNAL
Behdad Esfahbodf1a8d502014-07-19 16:52:32 -0400123# if !defined(__MINGW32__) && !defined(__CYGWIN__)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400124# define HB_INTERNAL __attribute__((__visibility__("hidden")))
125# else
126# define HB_INTERNAL
127# endif
128#endif
129
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400130#if __GNUC__ >= 3
131#define HB_FUNC __PRETTY_FUNCTION__
132#elif defined(_MSC_VER)
133#define HB_FUNC __FUNCSIG__
134#else
135#define HB_FUNC __func__
136#endif
137
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200138/*
139 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
140 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
141 * cases that fall through without a break or return statement. HB_FALLTHROUGH
142 * is only needed on cases that have code:
143 *
144 * switch (foo) {
145 * case 1: // These cases have no code. No fallthrough annotations are needed.
146 * case 2:
147 * case 3:
148 * foo = 4; // This case has code, so a fallthrough annotation is needed:
149 * HB_FALLTHROUGH;
150 * default:
151 * return foo;
152 * }
153 */
154#if defined(__clang__) && __cplusplus >= 201103L
155 /* clang's fallthrough annotations are only available starting in C++11. */
156# define HB_FALLTHROUGH [[clang::fallthrough]]
157#elif defined(_MSC_VER)
158 /*
159 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
160 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
161 */
162# include <sal.h>
163# define HB_FALLTHROUGH __fallthrough
164#else
165# define HB_FALLTHROUGH /* FALLTHROUGH */
166#endif
167
Behdad Esfahbod0fc0a102014-07-21 11:12:54 -0400168#if defined(_WIN32) || defined(__CYGWIN__)
Behdad Esfahboddb308282014-07-19 16:32:04 -0400169 /* We need Windows Vista for both Uniscribe backend and for
170 * MemoryBarrier. We don't support compiling on Windows XP,
171 * though we run on it fine. */
172# if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
173# undef _WIN32_WINNT
174# endif
175# ifndef _WIN32_WINNT
176# define _WIN32_WINNT 0x0600
177# endif
Behdad Esfahbod270971a2014-08-15 14:28:04 -0400178# ifndef WIN32_LEAN_AND_MEAN
179# define WIN32_LEAN_AND_MEAN 1
180# endif
181# ifndef STRICT
182# define STRICT 1
183# endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500184
Konstantin Rittf3537b62015-01-25 09:50:51 +0400185# if defined(_WIN32_WCE)
186 /* Some things not defined on Windows CE. */
Konstantin Ritt50690622016-04-26 12:02:26 +0400187# define vsnprintf _vsnprintf
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200188# define getenv(Name) nullptr
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400189# if _WIN32_WCE < 0x800
190# define setlocale(Category, Locale) "C"
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400191static int errno = 0; /* Use something better? */
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400192# endif
Konstantin Rittf3537b62015-01-25 09:50:51 +0400193# elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200194# define getenv(Name) nullptr
Konstantin Rittf3537b62015-01-25 09:50:51 +0400195# endif
Behdad Esfahbodce01ad72015-04-01 11:05:59 -0700196# if defined(_MSC_VER) && _MSC_VER < 1900
Konstantin Rittf3537b62015-01-25 09:50:51 +0400197# define snprintf _snprintf
Konstantin Rittf3537b62015-01-25 09:50:51 +0400198# endif
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400199#endif
200
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400201#if HAVE_ATEXIT
202/* atexit() is only safe to be called from shared libraries on certain
203 * platforms. Whitelist.
204 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
205# if defined(__linux) && defined(__GLIBC_PREREQ)
206# if __GLIBC_PREREQ(2,3)
207/* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
208# define HB_USE_ATEXIT 1
209# endif
210# elif defined(_MSC_VER) || defined(__MINGW32__)
211/* For MSVC:
212 * http://msdn.microsoft.com/en-ca/library/tze57ck3.aspx
213 * http://msdn.microsoft.com/en-ca/library/zk17ww08.aspx
214 * mingw32 headers say atexit is safe to use in shared libraries.
215 */
216# define HB_USE_ATEXIT 1
217# elif defined(__ANDROID__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
218/* This was fixed in Android NKD r8 or r8b:
219 * https://code.google.com/p/android/issues/detail?id=6455
220 * which introduced GCC 4.6:
221 * https://developer.android.com/tools/sdk/ndk/index.html
222 */
223# define HB_USE_ATEXIT 1
Bruce Mitchenere8859fc2018-02-04 01:26:57 +0700224# elif defined(__APPLE__)
225/* For macOS and related platforms, the atexit man page indicates
226 * that it will be invoked when the library is unloaded, not only
227 * at application exit.
228 */
229# define HB_USE_ATEXIT 1
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400230# endif
231#endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500232
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400233/* Basics */
234
235#undef MIN
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700236template <typename Type>
237static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400238
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500239#undef MAX
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700240template <typename Type>
241static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400242
Behdad Esfahbod16f175c2013-11-12 17:22:49 -0500243static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b)
244{ return (a + (b - 1)) / b; }
245
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500246
Behdad Esfahbod45917532009-11-04 18:15:59 -0500247#undef ARRAY_LENGTH
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700248template <typename Type, unsigned int n>
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500249static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400250/* A const version, but does not detect erratically being called on pointers. */
251#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400252
Behdad Esfahbod35a73832009-08-01 19:30:31 -0400253#define HB_STMT_START do
254#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -0500255
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200256template <unsigned int cond> class hb_assert_constant_t;
257template <> class hb_assert_constant_t<1> {};
Behdad Esfahbod672ca3b2015-10-26 14:05:05 -0700258
259#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
Behdad Esfahboddcb70262011-04-21 16:34:22 -0400260
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400261/* Lets assert int types. Saves trouble down the road. */
262
Behdad Esfahbodc3448e82017-10-15 12:02:00 +0200263static_assert ((sizeof (int8_t) == 1), "");
264static_assert ((sizeof (uint8_t) == 1), "");
265static_assert ((sizeof (int16_t) == 2), "");
266static_assert ((sizeof (uint16_t) == 2), "");
267static_assert ((sizeof (int32_t) == 4), "");
268static_assert ((sizeof (uint32_t) == 4), "");
269static_assert ((sizeof (int64_t) == 8), "");
270static_assert ((sizeof (uint64_t) == 8), "");
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400271
Behdad Esfahbodc3448e82017-10-15 12:02:00 +0200272static_assert ((sizeof (hb_codepoint_t) == 4), "");
273static_assert ((sizeof (hb_position_t) == 4), "");
274static_assert ((sizeof (hb_mask_t) == 4), "");
275static_assert ((sizeof (hb_var_int_t) == 4), "");
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400276
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400277
278/* We like our types POD */
279
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400280#define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
281#define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
282#define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400283
284#ifdef __GNUC__
Behdad Esfahbod79e2b472012-06-06 11:27:17 -0400285# define _ASSERT_INSTANCE_POD1(_line, _instance) \
286 HB_STMT_START { \
287 typedef __typeof__(_instance) _type_##_line; \
288 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
289 } HB_STMT_END
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400290#else
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400291# define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400292#endif
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400293# define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
294# define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400295
296/* Check _assertion in a method environment */
297#define _ASSERT_POD1(_line) \
Behdad Esfahboddac86022014-06-03 17:57:00 -0400298 HB_UNUSED inline void _static_assertion_on_line_##_line (void) const \
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400299 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
300# define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
301# define ASSERT_POD() _ASSERT_POD0 (__LINE__)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400302
303
304
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400305/* Misc */
306
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100307/*
308 * Void!
309 */
310typedef const struct _hb_void_t *hb_void_t;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200311#define HB_VOID ((const _hb_void_t *) nullptr)
Behdad Esfahbod7d3a1262010-04-29 13:54:01 -0400312
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100313/* Return the number of 1 bits in mask. */
Behdad Esfahbod97e7f8f2010-05-11 00:11:36 -0400314static inline HB_CONST_FUNC unsigned int
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400315_hb_popcount32 (uint32_t mask)
316{
317#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100318 return __builtin_popcount (mask);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400319#else
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100320 /* "HACKMEM 169" */
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700321 uint32_t y;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100322 y = (mask >> 1) &033333333333;
323 y = mask - y - ((y >>1) & 033333333333);
324 return (((y + (y >> 3)) & 030707070707) % 077);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400325#endif
326}
Behdad Esfahbod6fb4ac72017-10-15 16:00:09 +0200327static inline HB_CONST_FUNC unsigned int
328_hb_popcount64 (uint64_t mask)
329{
330#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
Behdad Esfahbodced86da2017-10-22 17:03:36 -0400331 if (sizeof (long) >= sizeof (mask))
332 return __builtin_popcountl (mask);
Behdad Esfahbod6fb4ac72017-10-15 16:00:09 +0200333#endif
Behdad Esfahbod2ee710e2017-10-23 08:37:48 -0400334 return _hb_popcount32 (mask & 0xFFFFFFFF) + _hb_popcount32 (mask >> 32);
Behdad Esfahbod6fb4ac72017-10-15 16:00:09 +0200335}
336template <typename T> static inline unsigned int _hb_popcount (T mask);
337template <> inline unsigned int _hb_popcount<uint32_t> (uint32_t mask) { return _hb_popcount32 (mask); }
338template <> inline unsigned int _hb_popcount<uint64_t> (uint64_t mask) { return _hb_popcount64 (mask); }
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400339
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100340/* Returns the number of bits needed to store number */
341static inline HB_CONST_FUNC unsigned int
342_hb_bit_storage (unsigned int number)
343{
344#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100345 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100346#else
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700347 unsigned int n_bits = 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100348 while (number) {
349 n_bits++;
350 number >>= 1;
351 }
352 return n_bits;
353#endif
354}
Behdad Esfahboddf660282009-08-01 20:46:02 -0400355
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100356/* Returns the number of zero bits in the least significant side of number */
357static inline HB_CONST_FUNC unsigned int
358_hb_ctz (unsigned int number)
359{
360#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
361 return likely (number) ? __builtin_ctz (number) : 0;
362#else
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700363 unsigned int n_bits = 0;
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100364 if (unlikely (!number)) return 0;
365 while (!(number & 1)) {
366 n_bits++;
367 number >>= 1;
368 }
369 return n_bits;
370#endif
371}
372
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400373static inline bool
374_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
375{
376 return (size > 0) && (count >= ((unsigned int) -1) / size);
377}
378
379
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400380
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400381/* arrays and maps */
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400382
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400383
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200384#define HB_PREALLOCED_ARRAY_INIT {0, 0, nullptr}
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400385template <typename Type, unsigned int StaticSize=16>
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400386struct hb_prealloced_array_t
387{
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400388 unsigned int len;
389 unsigned int allocated;
390 Type *array;
391 Type static_array[StaticSize];
392
Behdad Esfahbod56ef4e02017-10-15 16:38:29 -0400393 void init (void)
394 {
395 len = 0;
396 allocated = ARRAY_LENGTH (static_array);
397 array = static_array;
398 }
Behdad Esfahbodefde8112011-08-23 00:04:57 +0200399
Behdad Esfahbod265ac612011-05-05 14:38:16 -0400400 inline Type& operator [] (unsigned int i) { return array[i]; }
401 inline const Type& operator [] (unsigned int i) const { return array[i]; }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400402
403 inline Type *push (void)
404 {
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200405 if (unlikely (!resize (len + 1)))
406 return nullptr;
407
408 return &array[len - 1];
409 }
410
411 inline bool resize (unsigned int size)
412 {
Behdad Esfahbod56ef4e02017-10-15 16:38:29 -0400413 if (unlikely (size > allocated))
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200414 {
415 /* Need to reallocate */
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400416
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200417 unsigned int new_allocated = allocated;
418 while (size >= new_allocated)
419 new_allocated += (new_allocated >> 1) + 8;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400420
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200421 Type *new_array = nullptr;
422
423 if (array == static_array) {
424 new_array = (Type *) calloc (new_allocated, sizeof (Type));
425 if (new_array)
426 memcpy (new_array, array, len * sizeof (Type));
427 } else {
428 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
429 if (likely (!overflows)) {
430 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
431 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400432 }
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200433
434 if (unlikely (!new_array))
435 return false;
436
437 array = new_array;
438 allocated = new_allocated;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400439 }
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400440
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200441 len = size;
442 return true;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400443 }
444
445 inline void pop (void)
446 {
447 len--;
Behdad Esfahbod639afdc2013-08-06 14:28:12 -0400448 }
449
450 inline void remove (unsigned int i)
451 {
452 if (unlikely (i >= len))
453 return;
454 memmove (static_cast<void *> (&array[i]),
455 static_cast<void *> (&array[i + 1]),
456 (len - i - 1) * sizeof (Type));
457 len--;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400458 }
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400459
460 inline void shrink (unsigned int l)
461 {
462 if (l < len)
463 len = l;
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400464 }
465
Behdad Esfahbod68435692011-05-05 14:12:37 -0400466 template <typename T>
467 inline Type *find (T v) {
468 for (unsigned int i = 0; i < len; i++)
469 if (array[i] == v)
470 return &array[i];
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200471 return nullptr;
Behdad Esfahbod68435692011-05-05 14:12:37 -0400472 }
473 template <typename T>
474 inline const Type *find (T v) const {
475 for (unsigned int i = 0; i < len; i++)
476 if (array[i] == v)
477 return &array[i];
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200478 return nullptr;
Behdad Esfahbod68435692011-05-05 14:12:37 -0400479 }
480
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400481 inline void qsort (void)
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400482 {
Behdad Esfahbod0712e912017-10-29 17:01:47 -0600483 ::qsort (array, len, sizeof (Type), Type::cmp);
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400484 }
Behdad Esfahbod68435692011-05-05 14:12:37 -0400485
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400486 inline void qsort (unsigned int start, unsigned int end)
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400487 {
Behdad Esfahbod0712e912017-10-29 17:01:47 -0600488 ::qsort (array + start, end - start, sizeof (Type), Type::cmp);
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400489 }
490
Behdad Esfahbod68435692011-05-05 14:12:37 -0400491 template <typename T>
Behdad Esfahboddb5f7ef2017-10-15 16:00:22 +0200492 inline Type *bsearch (T *x)
Behdad Esfahbod68435692011-05-05 14:12:37 -0400493 {
Behdad Esfahbod5e740442017-10-15 16:15:06 +0200494 unsigned int i;
495 return bfind (x, &i) ? &array[i] : nullptr;
Behdad Esfahbod68435692011-05-05 14:12:37 -0400496 }
497 template <typename T>
Behdad Esfahboddb5f7ef2017-10-15 16:00:22 +0200498 inline const Type *bsearch (T *x) const
Behdad Esfahbod68435692011-05-05 14:12:37 -0400499 {
Behdad Esfahbod5e740442017-10-15 16:15:06 +0200500 unsigned int i;
501 return bfind (x, &i) ? &array[i] : nullptr;
502 }
503 template <typename T>
504 inline bool bfind (T *x, unsigned int *i) const
505 {
Behdad Esfahboddb5f7ef2017-10-15 16:00:22 +0200506 int min = 0, max = (int) this->len - 1;
507 while (min <= max)
508 {
509 int mid = (min + max) / 2;
510 int c = this->array[mid].cmp (x);
511 if (c < 0)
512 max = mid - 1;
513 else if (c > 0)
514 min = mid + 1;
515 else
Behdad Esfahbod5e740442017-10-15 16:15:06 +0200516 {
517 *i = mid;
518 return true;
519 }
Behdad Esfahboddb5f7ef2017-10-15 16:00:22 +0200520 }
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200521 if (max < 0 || (max < (int) this->len && this->array[max].cmp (x) > 0))
Behdad Esfahbod5e740442017-10-15 16:15:06 +0200522 max++;
523 *i = max;
524 return false;
Behdad Esfahbod68435692011-05-05 14:12:37 -0400525 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400526
527 inline void finish (void)
528 {
529 if (array != static_array)
530 free (array);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200531 array = nullptr;
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400532 allocated = len = 0;
533 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400534};
535
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500536template <typename Type>
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400537struct hb_auto_array_t : hb_prealloced_array_t <Type>
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500538{
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400539 hb_auto_array_t (void) { hb_prealloced_array_t<Type>::init (); }
540 ~hb_auto_array_t (void) { hb_prealloced_array_t<Type>::finish (); }
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500541};
542
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400543
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400544#define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400545template <typename item_t, typename lock_t>
546struct hb_lockable_set_t
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400547{
Behdad Esfahbod51f4d4d2017-10-27 15:09:22 -0600548 hb_prealloced_array_t <item_t, 1> items;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400549
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400550 inline void init (void) { items.init (); }
551
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400552 template <typename T>
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200553 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400554 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400555 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400556 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400557 if (item) {
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200558 if (replace) {
559 item_t old = *item;
560 *item = v;
561 l.unlock ();
562 old.finish ();
563 }
564 else {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200565 item = nullptr;
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200566 l.unlock ();
567 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400568 } else {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400569 item = items.push ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400570 if (likely (item))
571 *item = v;
572 l.unlock ();
573 }
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400574 return item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400575 }
576
Behdad Esfahbod811482b2011-05-05 13:21:04 -0400577 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400578 inline void remove (T v, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400579 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400580 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400581 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400582 if (item) {
583 item_t old = *item;
584 *item = items[items.len - 1];
585 items.pop ();
586 l.unlock ();
587 old.finish ();
588 } else {
589 l.unlock ();
590 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400591 }
592
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400593 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400594 inline bool find (T v, item_t *i, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400595 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400596 l.lock ();
597 item_t *item = items.find (v);
598 if (item)
599 *i = *item;
600 l.unlock ();
601 return !!item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400602 }
603
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400604 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400605 inline item_t *find_or_insert (T v, lock_t &l)
606 {
607 l.lock ();
608 item_t *item = items.find (v);
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400609 if (!item) {
610 item = items.push ();
611 if (likely (item))
612 *item = v;
613 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400614 l.unlock ();
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400615 return item;
616 }
617
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400618 inline void finish (lock_t &l)
619 {
Behdad Esfahbod3f4764b2012-07-30 10:06:42 -0400620 if (!items.len) {
621 /* No need for locking. */
622 items.finish ();
623 return;
624 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400625 l.lock ();
626 while (items.len) {
627 item_t old = items[items.len - 1];
628 items.pop ();
629 l.unlock ();
630 old.finish ();
631 l.lock ();
632 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400633 items.finish ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400634 l.unlock ();
635 }
636
637};
638
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400639
Behdad Esfahbod41880962011-04-11 14:58:28 -0400640/* ASCII tag/character handling */
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400641
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500642static inline bool ISALPHA (unsigned char c)
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400643{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500644static inline bool ISALNUM (unsigned char c)
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400645{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500646static inline bool ISSPACE (unsigned char c)
647{ return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400648static inline unsigned char TOUPPER (unsigned char c)
649{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
650static inline unsigned char TOLOWER (unsigned char c)
651{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400652
653
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900654/* HB_NDEBUG disables some sanity checks that are very safe to disable and
655 * should be disabled in production systems. If NDEBUG is defined, enable
656 * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
657 * light-weight) to be enabled, then HB_DEBUG can be defined to disable
658 * the costlier checks. */
659#ifdef NDEBUG
660#define HB_NDEBUG
661#endif
662
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400663
664/* Misc */
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400665
Behdad Esfahboda8b89a02014-07-11 14:18:01 -0400666template <typename T> class hb_assert_unsigned_t;
667template <> class hb_assert_unsigned_t<unsigned char> {};
Dominik Röttsches9e7c7202014-07-17 14:40:34 +0300668template <> class hb_assert_unsigned_t<unsigned short> {};
Behdad Esfahboda8b89a02014-07-11 14:18:01 -0400669template <> class hb_assert_unsigned_t<unsigned int> {};
670template <> class hb_assert_unsigned_t<unsigned long> {};
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400671
Behdad Esfahbodb5aeb952012-07-13 09:45:54 -0400672template <typename T> static inline bool
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400673hb_in_range (T u, T lo, T hi)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400674{
Behdad Esfahbod385cf372014-07-17 18:22:07 -0400675 /* The sizeof() is here to force template instantiation.
676 * I'm sure there are better ways to do this but can't think of
677 * one right now. Declaring a variable won't work as HB_UNUSED
Behdad Esfahbod68e04af2015-02-21 16:30:28 +0300678 * is unusable on some platforms and unused types are less likely
Behdad Esfahbod385cf372014-07-17 18:22:07 -0400679 * to generate a warning than unused variables. */
Behdad Esfahbodc3448e82017-10-15 12:02:00 +0200680 static_assert ((sizeof (hb_assert_unsigned_t<T>) >= 0), "");
Behdad Esfahbod385cf372014-07-17 18:22:07 -0400681
Behdad Esfahbodc2b151d2014-08-10 18:52:07 -0400682 /* The casts below are important as if T is smaller than int,
683 * the subtract results will become a signed int! */
684 return (T)(u - lo) <= (T)(hi - lo);
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400685}
686
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400687template <typename T> static inline bool
Behdad Esfahbodc98b7182013-12-31 15:55:40 +0800688hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2)
689{
690 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2);
691}
692
693template <typename T> static inline bool
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400694hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400695{
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400696 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3);
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400697}
698
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400699
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800700/* Enable bitwise ops on enums marked as flags_t */
Behdad Esfahbod69862082015-11-04 18:46:22 -0800701/* To my surprise, looks like the function resolver is happy to silently cast
702 * one enum to another... So this doesn't provide the type-checking that I
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800703 * originally had in mind... :(.
704 *
ebraminio7c6937e2017-11-20 14:49:22 -0500705 * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800706 */
707#ifdef _MSC_VER
708# pragma warning(disable:4200)
709# pragma warning(disable:4800)
Chun-wei Fan167c3272015-11-09 17:17:56 +0800710#endif
Behdad Esfahbod1dc32ea2015-11-20 13:24:19 -0800711#define HB_MARK_AS_FLAG_T(T) \
712 extern "C++" { \
713 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
714 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
715 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
716 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
717 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
718 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
719 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
720 }
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800721
722
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400723/* Useful for set-operations on small enums.
724 * For example, for testing "x ∈ {x1, x2, x3}" use:
Behdad Esfahbod6058f982017-10-19 11:39:52 -0700725 * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400726 */
Behdad Esfahbodd8adaa92017-10-20 13:57:43 -0400727#define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned int)(x) < 32) + (1U << (unsigned int)(x)))
728#define FLAG_UNSAFE(x) ((unsigned int)(x) < 32 ? (1U << (unsigned int)(x)) : 0)
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400729#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400730
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400731
Behdad Esfahbodc1e87442014-10-01 11:07:08 -0400732template <typename T, typename T2> static inline void
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100733hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400734{
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100735 for (unsigned int i = 1; i < len; i++)
736 {
737 unsigned int j = i;
738 while (j && compar (&array[j - 1], &array[i]) > 0)
739 j--;
740 if (i == j)
741 continue;
742 /* Move item i to occupy place for item j, shift what's in between. */
743 {
Behdad Esfahbod93099742015-09-01 16:11:27 +0100744 T t = array[i];
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100745 memmove (&array[j + 1], &array[j], (i - j) * sizeof (T));
746 array[j] = t;
747 }
748 if (array2)
749 {
Behdad Esfahbod93099742015-09-01 16:11:27 +0100750 T2 t = array2[i];
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100751 memmove (&array2[j + 1], &array2[j], (i - j) * sizeof (T2));
752 array2[j] = t;
753 }
754 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400755}
756
Behdad Esfahbod250398b2014-10-01 11:28:01 -0400757template <typename T> static inline void
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100758hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400759{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200760 hb_stable_sort (array, len, compar, (int *) nullptr);
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400761}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400762
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400763static inline hb_bool_t
764hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
765{
766 /* Pain because we don't know whether s is nul-terminated. */
767 char buf[64];
Behdad Esfahbod847794e2013-02-27 17:59:28 -0500768 len = MIN (ARRAY_LENGTH (buf) - 1, len);
769 strncpy (buf, s, len);
770 buf[len] = '\0';
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400771
772 char *end;
773 errno = 0;
774 unsigned long v = strtoul (buf, &end, base);
775 if (errno) return false;
776 if (*end) return false;
777 *out = v;
778 return true;
779}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400780
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400781
Behdad Esfahbod7737e872017-10-15 16:21:03 -0400782/* Vectorization */
783
784struct HbOpOr
785{
786 static const bool passthru_left = true;
787 static const bool passthru_right = true;
788 template <typename T> static void process (T &o, const T &a, const T &b) { o = a | b; }
789};
790struct HbOpAnd
791{
792 static const bool passthru_left = false;
793 static const bool passthru_right = false;
794 template <typename T> static void process (T &o, const T &a, const T &b) { o = a & b; }
795};
796struct HbOpMinus
797{
798 static const bool passthru_left = true;
799 static const bool passthru_right = false;
800 template <typename T> static void process (T &o, const T &a, const T &b) { o = a & ~b; }
801};
802struct HbOpXor
803{
804 static const bool passthru_left = true;
805 static const bool passthru_right = true;
806 template <typename T> static void process (T &o, const T &a, const T &b) { o = a ^ b; }
807};
808
809/* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))). */
810template <typename elt_t, unsigned int byte_size>
811struct hb_vector_size_t
812{
813 elt_t& operator [] (unsigned int i) { return v[i]; }
814 const elt_t& operator [] (unsigned int i) const { return v[i]; }
815
816 template <class Op>
817 inline hb_vector_size_t process (const hb_vector_size_t &o) const
818 {
819 hb_vector_size_t r;
820 for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++)
821 Op::process (r.v[i], v[i], o.v[i]);
822 return r;
823 }
824 inline hb_vector_size_t operator | (const hb_vector_size_t &o) const
825 { return process<HbOpOr> (o); }
826 inline hb_vector_size_t operator & (const hb_vector_size_t &o) const
827 { return process<HbOpAnd> (o); }
828 inline hb_vector_size_t operator ^ (const hb_vector_size_t &o) const
829 { return process<HbOpXor> (o); }
830 inline hb_vector_size_t operator ~ () const
831 {
832 hb_vector_size_t r;
833 for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++)
834 r.v[i] = ~v[i];
835 return r;
836 }
837
838 private:
Behdad Esfahbod221ce6c2017-10-15 17:58:58 -0400839 static_assert (byte_size / sizeof (elt_t) * sizeof (elt_t) == byte_size, "");
Behdad Esfahbod7737e872017-10-15 16:21:03 -0400840 elt_t v[byte_size / sizeof (elt_t)];
841};
842
843/* The `vector_size' attribute was introduced in gcc 3.1. */
844#if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
845#define HAVE_VECTOR_SIZE 1
846#endif
847
848
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500849/* Global runtime options. */
850
851struct hb_options_t
852{
Behdad Esfahbod5c871202014-10-14 20:07:31 -0700853 unsigned int initialized : 1;
854 unsigned int uniscribe_bug_compatible : 1;
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500855};
856
857union hb_options_union_t {
Behdad Esfahbod5c871202014-10-14 20:07:31 -0700858 unsigned int i;
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500859 hb_options_t opts;
860};
Behdad Esfahbodc3448e82017-10-15 12:02:00 +0200861static_assert ((sizeof (int) == sizeof (hb_options_union_t)), "");
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500862
863HB_INTERNAL void
864_hb_options_init (void);
865
866extern HB_INTERNAL hb_options_union_t _hb_options;
867
868static inline hb_options_t
869hb_options (void)
870{
871 if (unlikely (!_hb_options.i))
872 _hb_options_init ();
873
874 return _hb_options.opts;
875}
876
Steven R. Loomisa13b0232015-12-11 10:21:27 -0800877/* Size signifying variable-sized array */
878#define VAR 1
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500879
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600880
881/* String type. */
882
883struct hb_string_t
884{
885 inline hb_string_t (void) : bytes (nullptr), len (0) {}
886 inline hb_string_t (const char *bytes_, unsigned int len_) : bytes (bytes_), len (len_) {}
887
Behdad Esfahbode35a7632017-10-30 13:15:05 -0600888 inline int cmp (const hb_string_t &a) const
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600889 {
Behdad Esfahbode35a7632017-10-30 13:15:05 -0600890 if (len != a.len)
891 return (int) a.len - (int) len;
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600892
Behdad Esfahbode35a7632017-10-30 13:15:05 -0600893 return memcmp (a.bytes, bytes, len);
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600894 }
895 static inline int cmp (const void *pa, const void *pb)
896 {
897 hb_string_t *a = (hb_string_t *) pa;
898 hb_string_t *b = (hb_string_t *) pb;
Behdad Esfahbode35a7632017-10-30 13:15:05 -0600899 return b->cmp (*a);
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600900 }
901
902 const char *bytes;
903 unsigned int len;
904};
905
906
Behdad Esfahbodc57d4542011-04-20 18:50:27 -0400907#endif /* HB_PRIVATE_HH */