blob: f4a31a69b49308d816f9ad717c52a16f0c268e73 [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
Arkady Shapkin6c103282018-02-15 23:17:46 +030054#if defined(_MSC_VER)
55#include <intrin.h>
56#endif
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040057
Behdad Esfahbod2a749682017-10-26 19:48:33 -060058#define HB_PASTE1(a,b) a##b
59#define HB_PASTE(a,b) HB_PASTE1(a,b)
60
Behdad Esfahbod52b41852015-10-03 13:20:55 +010061/* Compile-time custom allocator support. */
62
63#if defined(hb_malloc_impl) \
64 && defined(hb_calloc_impl) \
65 && defined(hb_realloc_impl) \
66 && defined(hb_free_impl)
Behdad Esfahbodcc6ea302015-10-12 17:21:52 -040067extern "C" void* hb_malloc_impl(size_t size);
68extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
69extern "C" void* hb_realloc_impl(void *ptr, size_t size);
70extern "C" void hb_free_impl(void *ptr);
Behdad Esfahbod52b41852015-10-03 13:20:55 +010071#define malloc hb_malloc_impl
72#define calloc hb_calloc_impl
73#define realloc hb_realloc_impl
74#define free hb_free_impl
75#endif
76
77
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040078/* Compiler attributes */
Behdad Esfahbodbc200452010-04-29 01:40:26 -040079
Behdad Esfahbodbc200452010-04-29 01:40:26 -040080
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +020081#if __cplusplus < 201103L
82
Behdad Esfahbod62e312e2017-10-27 09:29:20 -060083#ifndef nullptr
84#define nullptr NULL
85#endif
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +020086
87// Static assertions
88#ifndef static_assert
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +020089#define static_assert(e, msg) \
Behdad Esfahbod6f08b122017-10-26 18:23:03 -060090 HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +020091#endif // static_assert
92
93#endif // __cplusplus < 201103L
94
Ebrahim Byagowi76c48732017-06-02 21:53:10 +043095#if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
96#define likely(expr) (__builtin_expect (!!(expr), 1))
97#define unlikely(expr) (__builtin_expect (!!(expr), 0))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -040098#else
99#define likely(expr) (expr)
100#define unlikely(expr) (expr)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400101#endif
102
Steve Lhomme0b8f3ab2016-07-11 21:57:26 +0200103#if !defined(__GNUC__) && !defined(__clang__)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400104#undef __attribute__
105#define __attribute__(x)
106#endif
Behdad Esfahboda794ebf2009-08-06 12:32:35 -0400107
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400108#if __GNUC__ >= 3
109#define HB_PURE_FUNC __attribute__((pure))
110#define HB_CONST_FUNC __attribute__((const))
111#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
112#else
113#define HB_PURE_FUNC
114#define HB_CONST_FUNC
115#define HB_PRINTF_FUNC(format_idx, arg_idx)
116#endif
117#if __GNUC__ >= 4
118#define HB_UNUSED __attribute__((unused))
Behdad Esfahbodbe59f3c2017-12-05 09:01:28 -0800119#elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
120#define HB_UNUSED __pragma(warning(suppress: 4100 4101))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400121#else
122#define HB_UNUSED
123#endif
124
125#ifndef HB_INTERNAL
Behdad Esfahbod90d1c742018-02-11 19:08:59 -0800126# if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400127# define HB_INTERNAL __attribute__((__visibility__("hidden")))
128# else
129# define HB_INTERNAL
Behdad Esfahbodb87cbe22018-02-07 14:11:26 -0500130# define HB_NO_VISIBILITY 1
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400131# endif
132#endif
133
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400134#if __GNUC__ >= 3
135#define HB_FUNC __PRETTY_FUNCTION__
136#elif defined(_MSC_VER)
137#define HB_FUNC __FUNCSIG__
138#else
139#define HB_FUNC __func__
140#endif
141
Behdad Esfahbod8cef3a62018-02-09 16:04:23 -0600142#ifdef __SUNPRO_CC
143/* https://github.com/harfbuzz/harfbuzz/issues/630 */
144#define __restrict
145#endif
146
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200147/*
148 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
149 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
150 * cases that fall through without a break or return statement. HB_FALLTHROUGH
151 * is only needed on cases that have code:
152 *
153 * switch (foo) {
154 * case 1: // These cases have no code. No fallthrough annotations are needed.
155 * case 2:
156 * case 3:
157 * foo = 4; // This case has code, so a fallthrough annotation is needed:
158 * HB_FALLTHROUGH;
159 * default:
160 * return foo;
161 * }
162 */
163#if defined(__clang__) && __cplusplus >= 201103L
164 /* clang's fallthrough annotations are only available starting in C++11. */
165# define HB_FALLTHROUGH [[clang::fallthrough]]
Behdad Esfahbodf41b9212018-02-05 19:51:09 -0500166#elif __GNUC__ >= 7
167 /* GNU fallthrough attribute is available from GCC7 */
168# define HB_FALLTHROUGH __attribute__((fallthrough))
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200169#elif defined(_MSC_VER)
170 /*
171 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
172 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
173 */
174# include <sal.h>
175# define HB_FALLTHROUGH __fallthrough
176#else
177# define HB_FALLTHROUGH /* FALLTHROUGH */
178#endif
179
Behdad Esfahbod0fc0a102014-07-21 11:12:54 -0400180#if defined(_WIN32) || defined(__CYGWIN__)
Behdad Esfahboddb308282014-07-19 16:32:04 -0400181 /* We need Windows Vista for both Uniscribe backend and for
182 * MemoryBarrier. We don't support compiling on Windows XP,
183 * though we run on it fine. */
184# if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
185# undef _WIN32_WINNT
186# endif
187# ifndef _WIN32_WINNT
188# define _WIN32_WINNT 0x0600
189# endif
Behdad Esfahbod270971a2014-08-15 14:28:04 -0400190# ifndef WIN32_LEAN_AND_MEAN
191# define WIN32_LEAN_AND_MEAN 1
192# endif
193# ifndef STRICT
194# define STRICT 1
195# endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500196
Konstantin Rittf3537b62015-01-25 09:50:51 +0400197# if defined(_WIN32_WCE)
198 /* Some things not defined on Windows CE. */
Konstantin Ritt50690622016-04-26 12:02:26 +0400199# define vsnprintf _vsnprintf
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200200# define getenv(Name) nullptr
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400201# if _WIN32_WCE < 0x800
202# define setlocale(Category, Locale) "C"
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400203static int errno = 0; /* Use something better? */
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400204# endif
Konstantin Rittf3537b62015-01-25 09:50:51 +0400205# elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200206# define getenv(Name) nullptr
Konstantin Rittf3537b62015-01-25 09:50:51 +0400207# endif
Behdad Esfahbodce01ad72015-04-01 11:05:59 -0700208# if defined(_MSC_VER) && _MSC_VER < 1900
Konstantin Rittf3537b62015-01-25 09:50:51 +0400209# define snprintf _snprintf
Konstantin Rittf3537b62015-01-25 09:50:51 +0400210# endif
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400211#endif
212
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400213#if HAVE_ATEXIT
214/* atexit() is only safe to be called from shared libraries on certain
215 * platforms. Whitelist.
216 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
217# if defined(__linux) && defined(__GLIBC_PREREQ)
218# if __GLIBC_PREREQ(2,3)
219/* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
220# define HB_USE_ATEXIT 1
221# endif
222# elif defined(_MSC_VER) || defined(__MINGW32__)
223/* For MSVC:
224 * http://msdn.microsoft.com/en-ca/library/tze57ck3.aspx
225 * http://msdn.microsoft.com/en-ca/library/zk17ww08.aspx
226 * mingw32 headers say atexit is safe to use in shared libraries.
227 */
228# define HB_USE_ATEXIT 1
229# elif defined(__ANDROID__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
230/* This was fixed in Android NKD r8 or r8b:
231 * https://code.google.com/p/android/issues/detail?id=6455
232 * which introduced GCC 4.6:
233 * https://developer.android.com/tools/sdk/ndk/index.html
234 */
235# define HB_USE_ATEXIT 1
Bruce Mitchenere8859fc2018-02-04 01:26:57 +0700236# elif defined(__APPLE__)
237/* For macOS and related platforms, the atexit man page indicates
238 * that it will be invoked when the library is unloaded, not only
239 * at application exit.
240 */
241# define HB_USE_ATEXIT 1
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400242# endif
243#endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500244
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400245/* Basics */
246
247#undef MIN
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700248template <typename Type>
249static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400250
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500251#undef MAX
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700252template <typename Type>
253static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400254
Behdad Esfahbod16f175c2013-11-12 17:22:49 -0500255static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b)
256{ return (a + (b - 1)) / b; }
257
Behdad Esfahbod8a3511a2009-11-04 19:45:39 -0500258
Behdad Esfahbod45917532009-11-04 18:15:59 -0500259#undef ARRAY_LENGTH
Behdad Esfahbod25326c22012-08-04 16:43:18 -0700260template <typename Type, unsigned int n>
Behdad Esfahbod0beb66e2012-12-05 18:46:04 -0500261static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400262/* A const version, but does not detect erratically being called on pointers. */
263#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400264
Behdad Esfahbod35a73832009-08-01 19:30:31 -0400265#define HB_STMT_START do
266#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -0500267
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200268template <unsigned int cond> class hb_assert_constant_t;
269template <> class hb_assert_constant_t<1> {};
Behdad Esfahbod672ca3b2015-10-26 14:05:05 -0700270
271#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
Behdad Esfahboddcb70262011-04-21 16:34:22 -0400272
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400273/* Lets assert int types. Saves trouble down the road. */
274
Behdad Esfahbodc3448e82017-10-15 12:02:00 +0200275static_assert ((sizeof (int8_t) == 1), "");
276static_assert ((sizeof (uint8_t) == 1), "");
277static_assert ((sizeof (int16_t) == 2), "");
278static_assert ((sizeof (uint16_t) == 2), "");
279static_assert ((sizeof (int32_t) == 4), "");
280static_assert ((sizeof (uint32_t) == 4), "");
281static_assert ((sizeof (int64_t) == 8), "");
282static_assert ((sizeof (uint64_t) == 8), "");
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400283
Behdad Esfahbodc3448e82017-10-15 12:02:00 +0200284static_assert ((sizeof (hb_codepoint_t) == 4), "");
285static_assert ((sizeof (hb_position_t) == 4), "");
286static_assert ((sizeof (hb_mask_t) == 4), "");
287static_assert ((sizeof (hb_var_int_t) == 4), "");
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400288
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400289
290/* We like our types POD */
291
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400292#define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
293#define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
294#define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400295
296#ifdef __GNUC__
Behdad Esfahbod79e2b472012-06-06 11:27:17 -0400297# define _ASSERT_INSTANCE_POD1(_line, _instance) \
298 HB_STMT_START { \
299 typedef __typeof__(_instance) _type_##_line; \
300 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
301 } HB_STMT_END
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400302#else
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400303# define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400304#endif
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400305# define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
306# define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400307
308/* Check _assertion in a method environment */
309#define _ASSERT_POD1(_line) \
Behdad Esfahboddac86022014-06-03 17:57:00 -0400310 HB_UNUSED inline void _static_assertion_on_line_##_line (void) const \
Behdad Esfahbod73cb02d2012-06-06 11:29:25 -0400311 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
312# define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
313# define ASSERT_POD() _ASSERT_POD0 (__LINE__)
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400314
315
316
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400317/* Misc */
318
Behdad Esfahbodca42d962018-01-11 09:15:34 +0100319/*
320 * Void!
321 */
322typedef const struct _hb_void_t *hb_void_t;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200323#define HB_VOID ((const _hb_void_t *) nullptr)
Behdad Esfahbod7d3a1262010-04-29 13:54:01 -0400324
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100325/* Return the number of 1 bits in mask. */
Behdad Esfahbod978ace62018-02-16 16:06:17 -0800326template <typename T>
Behdad Esfahbod97e7f8f2010-05-11 00:11:36 -0400327static inline HB_CONST_FUNC unsigned int
Behdad Esfahbod978ace62018-02-16 16:06:17 -0800328_hb_popcount (T mask)
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400329{
Behdad Esfahbod978ace62018-02-16 16:06:17 -0800330#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) && defined(__OPTIMIZE__)
331 if (sizeof (unsigned int) >= sizeof (mask))
332 return __builtin_popcount (mask);
333
334 if (sizeof (unsigned long) >= sizeof (mask))
335 return __builtin_popcountl (mask);
336
337 if (sizeof (unsigned long long) >= sizeof (mask))
338 return __builtin_popcountll (mask);
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400339#endif
Behdad Esfahbod978ace62018-02-16 16:06:17 -0800340
341 if (sizeof (T) <= 4)
342 {
343 /* "HACKMEM 169" */
344 uint32_t y;
345 y = (mask >> 1) &033333333333;
346 y = mask - y - ((y >>1) & 033333333333);
347 return (((y + (y >> 3)) & 030707070707) % 077);
348 }
349
350 if (sizeof (T) == 8)
351 {
352 unsigned int shift = 32;
353 return _hb_popcount<uint32_t> ((uint32_t) mask) + _hb_popcount ((uint32_t) (mask >> shift));
354 }
355
356 if (sizeof (T) == 16)
357 {
358 unsigned int shift = 64;
359 return _hb_popcount<uint64_t> ((uint64_t) mask) + _hb_popcount ((uint64_t) (mask >> shift));
360 }
361
362 unsigned int count = 0;
363 while (mask)
364 {
365 count += _hb_popcount<uint64_t> ((uint64_t) mask);
366 unsigned int shift = 64;
367 mask = (T) (mask >> shift);
368 }
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400369}
370
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100371/* Returns the number of bits needed to store number */
372static inline HB_CONST_FUNC unsigned int
373_hb_bit_storage (unsigned int number)
374{
375#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100376 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
Arkady Shapkind37310a2018-02-16 21:45:08 +0300377#elif defined(_MSC_VER)
Arkady Shapkin6c103282018-02-15 23:17:46 +0300378 unsigned long where;
Arkady Shapkind37310a2018-02-16 21:45:08 +0300379 if (_BitScanReverse (&where, number)) return 1 + where;
Arkady Shapkin6c103282018-02-15 23:17:46 +0300380 return 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100381#else
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700382 unsigned int n_bits = 0;
Behdad Esfahbod9b602332010-05-20 15:31:12 +0100383 while (number) {
384 n_bits++;
385 number >>= 1;
386 }
387 return n_bits;
388#endif
389}
Behdad Esfahboddf660282009-08-01 20:46:02 -0400390
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100391/* Returns the number of zero bits in the least significant side of number */
392static inline HB_CONST_FUNC unsigned int
393_hb_ctz (unsigned int number)
394{
395#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
396 return likely (number) ? __builtin_ctz (number) : 0;
Arkady Shapkind37310a2018-02-16 21:45:08 +0300397#elif defined(_MSC_VER)
Arkady Shapkin6c103282018-02-15 23:17:46 +0300398 unsigned long where;
Arkady Shapkind37310a2018-02-16 21:45:08 +0300399 if (_BitScanForward (&where, number)) return where;
Arkady Shapkin6c103282018-02-15 23:17:46 +0300400 return 0;
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100401#else
Behdad Esfahboda949cd32014-03-16 20:22:42 -0700402 unsigned int n_bits = 0;
Behdad Esfahbodf7acd8d2010-05-20 17:26:35 +0100403 if (unlikely (!number)) return 0;
404 while (!(number & 1)) {
405 n_bits++;
406 number >>= 1;
407 }
408 return n_bits;
409#endif
410}
411
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400412static inline bool
413_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
414{
415 return (size > 0) && (count >= ((unsigned int) -1) / size);
416}
417
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600418static inline unsigned int
419_hb_ceil_to_4 (unsigned int v)
420{
Behdad Esfahbod931f8b72018-02-07 21:42:03 -0600421 return ((v - 1) | 3) + 1;
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600422}
423
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400424
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400425
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400426/* arrays and maps */
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400427
Behdad Esfahboda9f24c82011-04-21 17:18:22 -0400428
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200429#define HB_PREALLOCED_ARRAY_INIT {0, 0, nullptr}
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400430template <typename Type, unsigned int StaticSize=16>
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400431struct hb_prealloced_array_t
432{
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400433 unsigned int len;
434 unsigned int allocated;
435 Type *array;
436 Type static_array[StaticSize];
437
Behdad Esfahbod56ef4e02017-10-15 16:38:29 -0400438 void init (void)
439 {
440 len = 0;
441 allocated = ARRAY_LENGTH (static_array);
442 array = static_array;
443 }
Behdad Esfahbodefde8112011-08-23 00:04:57 +0200444
Behdad Esfahbod265ac612011-05-05 14:38:16 -0400445 inline Type& operator [] (unsigned int i) { return array[i]; }
446 inline const Type& operator [] (unsigned int i) const { return array[i]; }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400447
448 inline Type *push (void)
449 {
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200450 if (unlikely (!resize (len + 1)))
451 return nullptr;
452
453 return &array[len - 1];
454 }
455
Behdad Esfahbodaf274502018-02-10 13:25:49 -0600456 /* Allocate for size but don't adjust len. */
Rod Sheeter59c658c2018-02-08 19:22:47 -0800457 inline bool alloc(unsigned int size)
458 {
Behdad Esfahbodaf274502018-02-10 13:25:49 -0600459 if (likely (size <= allocated))
Rod Sheeter59c658c2018-02-08 19:22:47 -0800460 return true;
Behdad Esfahbodaf274502018-02-10 13:25:49 -0600461
462 /* Reallocate */
Rod Sheeter59c658c2018-02-08 19:22:47 -0800463
464 unsigned int new_allocated = allocated;
465 while (size >= new_allocated)
466 new_allocated += (new_allocated >> 1) + 8;
467
468 Type *new_array = nullptr;
469
470 if (array == static_array) {
471 new_array = (Type *) calloc (new_allocated, sizeof (Type));
472 if (new_array)
473 memcpy (new_array, array, len * sizeof (Type));
474 } else {
475 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
476 if (likely (!overflows)) {
477 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
478 }
479 }
480
481 if (unlikely (!new_array))
482 return false;
483
484 array = new_array;
Rod Sheeter48160642018-02-09 17:14:37 -0800485 allocated = new_allocated;
Behdad Esfahbodaf274502018-02-10 13:25:49 -0600486
Rod Sheeter48160642018-02-09 17:14:37 -0800487 return true;
Rod Sheeter59c658c2018-02-08 19:22:47 -0800488 }
489
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200490 inline bool resize (unsigned int size)
491 {
Behdad Esfahbodaf274502018-02-10 13:25:49 -0600492 if (!alloc (size))
Rod Sheeter59c658c2018-02-08 19:22:47 -0800493 return false;
Behdad Esfahbod5a503032011-05-02 19:54:29 -0400494
Behdad Esfahboddeed4a42017-10-15 16:53:09 +0200495 len = size;
496 return true;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400497 }
498
499 inline void pop (void)
500 {
501 len--;
Behdad Esfahbod639afdc2013-08-06 14:28:12 -0400502 }
503
504 inline void remove (unsigned int i)
505 {
506 if (unlikely (i >= len))
507 return;
508 memmove (static_cast<void *> (&array[i]),
509 static_cast<void *> (&array[i + 1]),
510 (len - i - 1) * sizeof (Type));
511 len--;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400512 }
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400513
514 inline void shrink (unsigned int l)
515 {
516 if (l < len)
517 len = l;
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400518 }
519
Behdad Esfahbod68435692011-05-05 14:12:37 -0400520 template <typename T>
521 inline Type *find (T v) {
522 for (unsigned int i = 0; i < len; i++)
523 if (array[i] == v)
524 return &array[i];
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200525 return nullptr;
Behdad Esfahbod68435692011-05-05 14:12:37 -0400526 }
527 template <typename T>
528 inline const Type *find (T v) const {
529 for (unsigned int i = 0; i < len; i++)
530 if (array[i] == v)
531 return &array[i];
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200532 return nullptr;
Behdad Esfahbod68435692011-05-05 14:12:37 -0400533 }
534
Rod Sheeter59c658c2018-02-08 19:22:47 -0800535 inline void qsort (int (*cmp)(const void*, const void*))
536 {
537 ::qsort (array, len, sizeof (Type), cmp);
538 }
539
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400540 inline void qsort (void)
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400541 {
Behdad Esfahbod0712e912017-10-29 17:01:47 -0600542 ::qsort (array, len, sizeof (Type), Type::cmp);
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400543 }
Behdad Esfahbod68435692011-05-05 14:12:37 -0400544
Behdad Esfahbodfb8cc862014-06-19 15:30:18 -0400545 inline void qsort (unsigned int start, unsigned int end)
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400546 {
Behdad Esfahbod0712e912017-10-29 17:01:47 -0600547 ::qsort (array + start, end - start, sizeof (Type), Type::cmp);
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400548 }
549
Behdad Esfahbod68435692011-05-05 14:12:37 -0400550 template <typename T>
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600551 inline Type *lsearch (const T &x)
Behdad Esfahbod6804b612018-02-07 13:47:35 -0500552 {
553 for (unsigned int i = 0; i < len; i++)
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600554 if (0 == this->array[i].cmp (&x))
Behdad Esfahbod6804b612018-02-07 13:47:35 -0500555 return &array[i];
556 return nullptr;
557 }
558
559 template <typename T>
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600560 inline Type *bsearch (const T &x)
Behdad Esfahbod68435692011-05-05 14:12:37 -0400561 {
Behdad Esfahbod5e740442017-10-15 16:15:06 +0200562 unsigned int i;
563 return bfind (x, &i) ? &array[i] : nullptr;
Behdad Esfahbod68435692011-05-05 14:12:37 -0400564 }
565 template <typename T>
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600566 inline const Type *bsearch (const T &x) const
Behdad Esfahbod68435692011-05-05 14:12:37 -0400567 {
Behdad Esfahbod5e740442017-10-15 16:15:06 +0200568 unsigned int i;
569 return bfind (x, &i) ? &array[i] : nullptr;
570 }
571 template <typename T>
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600572 inline bool bfind (const T &x, unsigned int *i) const
Behdad Esfahbod5e740442017-10-15 16:15:06 +0200573 {
Behdad Esfahboddb5f7ef2017-10-15 16:00:22 +0200574 int min = 0, max = (int) this->len - 1;
575 while (min <= max)
576 {
577 int mid = (min + max) / 2;
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600578 int c = this->array[mid].cmp (&x);
Behdad Esfahboddb5f7ef2017-10-15 16:00:22 +0200579 if (c < 0)
580 max = mid - 1;
581 else if (c > 0)
582 min = mid + 1;
583 else
Behdad Esfahbod5e740442017-10-15 16:15:06 +0200584 {
585 *i = mid;
586 return true;
587 }
Behdad Esfahboddb5f7ef2017-10-15 16:00:22 +0200588 }
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600589 if (max < 0 || (max < (int) this->len && this->array[max].cmp (&x) > 0))
Behdad Esfahbod5e740442017-10-15 16:15:06 +0200590 max++;
591 *i = max;
592 return false;
Behdad Esfahbod68435692011-05-05 14:12:37 -0400593 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400594
595 inline void finish (void)
596 {
597 if (array != static_array)
598 free (array);
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200599 array = nullptr;
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400600 allocated = len = 0;
601 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400602};
603
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500604template <typename Type>
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400605struct hb_auto_array_t : hb_prealloced_array_t <Type>
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500606{
Behdad Esfahbod546b1ad2014-06-26 19:10:21 -0400607 hb_auto_array_t (void) { hb_prealloced_array_t<Type>::init (); }
608 ~hb_auto_array_t (void) { hb_prealloced_array_t<Type>::finish (); }
Behdad Esfahbodc5d91f32013-03-09 04:34:21 -0500609};
610
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400611
Behdad Esfahbod0e253e92012-06-05 15:37:19 -0400612#define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400613template <typename item_t, typename lock_t>
614struct hb_lockable_set_t
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400615{
Behdad Esfahbod51f4d4d2017-10-27 15:09:22 -0600616 hb_prealloced_array_t <item_t, 1> items;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400617
Behdad Esfahbodbf93b632012-06-05 14:17:32 -0400618 inline void init (void) { items.init (); }
619
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400620 template <typename T>
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200621 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400622 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400623 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400624 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400625 if (item) {
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200626 if (replace) {
627 item_t old = *item;
628 *item = v;
629 l.unlock ();
630 old.finish ();
631 }
632 else {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200633 item = nullptr;
Behdad Esfahbod33ccc772011-08-09 00:43:24 +0200634 l.unlock ();
635 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400636 } else {
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400637 item = items.push ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400638 if (likely (item))
639 *item = v;
640 l.unlock ();
641 }
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400642 return item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400643 }
644
Behdad Esfahbod811482b2011-05-05 13:21:04 -0400645 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400646 inline void remove (T v, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400647 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400648 l.lock ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400649 item_t *item = items.find (v);
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400650 if (item) {
651 item_t old = *item;
652 *item = items[items.len - 1];
653 items.pop ();
654 l.unlock ();
655 old.finish ();
656 } else {
657 l.unlock ();
658 }
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400659 }
660
Behdad Esfahbod478a4252011-05-05 12:39:51 -0400661 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400662 inline bool find (T v, item_t *i, lock_t &l)
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400663 {
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400664 l.lock ();
665 item_t *item = items.find (v);
666 if (item)
667 *i = *item;
668 l.unlock ();
669 return !!item;
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400670 }
671
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400672 template <typename T>
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400673 inline item_t *find_or_insert (T v, lock_t &l)
674 {
675 l.lock ();
676 item_t *item = items.find (v);
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400677 if (!item) {
678 item = items.push ();
679 if (likely (item))
680 *item = v;
681 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400682 l.unlock ();
Behdad Esfahbodb45f32e2011-05-05 15:00:43 -0400683 return item;
684 }
685
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400686 inline void finish (lock_t &l)
687 {
Behdad Esfahbod3f4764b2012-07-30 10:06:42 -0400688 if (!items.len) {
689 /* No need for locking. */
690 items.finish ();
691 return;
692 }
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400693 l.lock ();
694 while (items.len) {
695 item_t old = items[items.len - 1];
696 items.pop ();
697 l.unlock ();
698 old.finish ();
699 l.lock ();
700 }
Behdad Esfahbod6a7ac792011-05-11 14:19:18 -0400701 items.finish ();
Behdad Esfahbod45bfa992011-05-10 19:12:49 -0400702 l.unlock ();
703 }
704
705};
706
Behdad Esfahbod852e08e2011-04-27 21:45:51 -0400707
Behdad Esfahbod41880962011-04-11 14:58:28 -0400708/* ASCII tag/character handling */
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400709
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500710static inline bool ISALPHA (unsigned char c)
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400711{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500712static inline bool ISALNUM (unsigned char c)
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400713{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
Behdad Esfahbod20b817a2013-02-27 18:39:37 -0500714static inline bool ISSPACE (unsigned char c)
715{ return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400716static inline unsigned char TOUPPER (unsigned char c)
717{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
718static inline unsigned char TOLOWER (unsigned char c)
719{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
Behdad Esfahboddb5227c2011-04-11 13:16:08 -0400720
721
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900722/* HB_NDEBUG disables some sanity checks that are very safe to disable and
723 * should be disabled in production systems. If NDEBUG is defined, enable
724 * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
725 * light-weight) to be enabled, then HB_DEBUG can be defined to disable
726 * the costlier checks. */
727#ifdef NDEBUG
728#define HB_NDEBUG
729#endif
730
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400731
732/* Misc */
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400733
Behdad Esfahboda8b89a02014-07-11 14:18:01 -0400734template <typename T> class hb_assert_unsigned_t;
735template <> class hb_assert_unsigned_t<unsigned char> {};
Dominik Röttsches9e7c7202014-07-17 14:40:34 +0300736template <> class hb_assert_unsigned_t<unsigned short> {};
Behdad Esfahboda8b89a02014-07-11 14:18:01 -0400737template <> class hb_assert_unsigned_t<unsigned int> {};
738template <> class hb_assert_unsigned_t<unsigned long> {};
Behdad Esfahbodf60f2162010-04-21 02:12:45 -0400739
Behdad Esfahbodb5aeb952012-07-13 09:45:54 -0400740template <typename T> static inline bool
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400741hb_in_range (T u, T lo, T hi)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400742{
Behdad Esfahbod385cf372014-07-17 18:22:07 -0400743 /* The sizeof() is here to force template instantiation.
744 * I'm sure there are better ways to do this but can't think of
745 * one right now. Declaring a variable won't work as HB_UNUSED
Behdad Esfahbod68e04af2015-02-21 16:30:28 +0300746 * is unusable on some platforms and unused types are less likely
Behdad Esfahbod385cf372014-07-17 18:22:07 -0400747 * to generate a warning than unused variables. */
Behdad Esfahbodc3448e82017-10-15 12:02:00 +0200748 static_assert ((sizeof (hb_assert_unsigned_t<T>) >= 0), "");
Behdad Esfahbod385cf372014-07-17 18:22:07 -0400749
Behdad Esfahbodc2b151d2014-08-10 18:52:07 -0400750 /* The casts below are important as if T is smaller than int,
751 * the subtract results will become a signed int! */
752 return (T)(u - lo) <= (T)(hi - lo);
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400753}
754
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400755template <typename T> static inline bool
Behdad Esfahbodc98b7182013-12-31 15:55:40 +0800756hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2)
757{
758 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2);
759}
760
761template <typename T> static inline bool
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400762hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
Behdad Esfahbod4a7f4f32012-07-23 13:15:33 -0400763{
Behdad Esfahbod093cd582012-07-23 14:04:42 -0400764 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 -0400765}
766
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400767
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800768/* Enable bitwise ops on enums marked as flags_t */
Behdad Esfahbod69862082015-11-04 18:46:22 -0800769/* To my surprise, looks like the function resolver is happy to silently cast
770 * one enum to another... So this doesn't provide the type-checking that I
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800771 * originally had in mind... :(.
772 *
ebraminio7c6937e2017-11-20 14:49:22 -0500773 * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800774 */
775#ifdef _MSC_VER
776# pragma warning(disable:4200)
777# pragma warning(disable:4800)
Chun-wei Fan167c3272015-11-09 17:17:56 +0800778#endif
Behdad Esfahbod1dc32ea2015-11-20 13:24:19 -0800779#define HB_MARK_AS_FLAG_T(T) \
780 extern "C++" { \
781 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
782 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
783 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
784 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
785 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
786 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
787 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
788 }
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800789
790
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400791/* Useful for set-operations on small enums.
792 * For example, for testing "x ∈ {x1, x2, x3}" use:
Behdad Esfahbod6058f982017-10-19 11:39:52 -0700793 * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400794 */
Behdad Esfahbodd8adaa92017-10-20 13:57:43 -0400795#define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned int)(x) < 32) + (1U << (unsigned int)(x)))
796#define FLAG_UNSAFE(x) ((unsigned int)(x) < 32 ? (1U << (unsigned int)(x)) : 0)
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400797#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400798
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400799
Behdad Esfahbodc1e87442014-10-01 11:07:08 -0400800template <typename T, typename T2> static inline void
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100801hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400802{
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100803 for (unsigned int i = 1; i < len; i++)
804 {
805 unsigned int j = i;
806 while (j && compar (&array[j - 1], &array[i]) > 0)
807 j--;
808 if (i == j)
809 continue;
810 /* Move item i to occupy place for item j, shift what's in between. */
811 {
Behdad Esfahbod93099742015-09-01 16:11:27 +0100812 T t = array[i];
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100813 memmove (&array[j + 1], &array[j], (i - j) * sizeof (T));
814 array[j] = t;
815 }
816 if (array2)
817 {
Behdad Esfahbod93099742015-09-01 16:11:27 +0100818 T2 t = array2[i];
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100819 memmove (&array2[j + 1], &array2[j], (i - j) * sizeof (T2));
820 array2[j] = t;
821 }
822 }
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400823}
824
Behdad Esfahbod250398b2014-10-01 11:28:01 -0400825template <typename T> static inline void
Behdad Esfahbod85846b32015-09-01 15:07:52 +0100826hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400827{
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200828 hb_stable_sort (array, len, compar, (int *) nullptr);
Behdad Esfahbod39b17832012-07-17 17:09:29 -0400829}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400830
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400831static inline hb_bool_t
832hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
833{
834 /* Pain because we don't know whether s is nul-terminated. */
835 char buf[64];
Behdad Esfahbod847794e2013-02-27 17:59:28 -0500836 len = MIN (ARRAY_LENGTH (buf) - 1, len);
837 strncpy (buf, s, len);
838 buf[len] = '\0';
Behdad Esfahbod6f3a3002012-08-07 22:13:25 -0400839
840 char *end;
841 errno = 0;
842 unsigned long v = strtoul (buf, &end, base);
843 if (errno) return false;
844 if (*end) return false;
845 *out = v;
846 return true;
847}
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400848
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400849
Behdad Esfahbod7737e872017-10-15 16:21:03 -0400850/* Vectorization */
851
852struct HbOpOr
853{
854 static const bool passthru_left = true;
855 static const bool passthru_right = true;
856 template <typename T> static void process (T &o, const T &a, const T &b) { o = a | b; }
857};
858struct HbOpAnd
859{
860 static const bool passthru_left = false;
861 static const bool passthru_right = false;
862 template <typename T> static void process (T &o, const T &a, const T &b) { o = a & b; }
863};
864struct HbOpMinus
865{
866 static const bool passthru_left = true;
867 static const bool passthru_right = false;
868 template <typename T> static void process (T &o, const T &a, const T &b) { o = a & ~b; }
869};
870struct HbOpXor
871{
872 static const bool passthru_left = true;
873 static const bool passthru_right = true;
874 template <typename T> static void process (T &o, const T &a, const T &b) { o = a ^ b; }
875};
876
877/* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))). */
878template <typename elt_t, unsigned int byte_size>
879struct hb_vector_size_t
880{
881 elt_t& operator [] (unsigned int i) { return v[i]; }
882 const elt_t& operator [] (unsigned int i) const { return v[i]; }
883
884 template <class Op>
885 inline hb_vector_size_t process (const hb_vector_size_t &o) const
886 {
887 hb_vector_size_t r;
888 for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++)
889 Op::process (r.v[i], v[i], o.v[i]);
890 return r;
891 }
892 inline hb_vector_size_t operator | (const hb_vector_size_t &o) const
893 { return process<HbOpOr> (o); }
894 inline hb_vector_size_t operator & (const hb_vector_size_t &o) const
895 { return process<HbOpAnd> (o); }
896 inline hb_vector_size_t operator ^ (const hb_vector_size_t &o) const
897 { return process<HbOpXor> (o); }
898 inline hb_vector_size_t operator ~ () const
899 {
900 hb_vector_size_t r;
901 for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++)
902 r.v[i] = ~v[i];
903 return r;
904 }
905
906 private:
Behdad Esfahbod221ce6c2017-10-15 17:58:58 -0400907 static_assert (byte_size / sizeof (elt_t) * sizeof (elt_t) == byte_size, "");
Behdad Esfahbod7737e872017-10-15 16:21:03 -0400908 elt_t v[byte_size / sizeof (elt_t)];
909};
910
911/* The `vector_size' attribute was introduced in gcc 3.1. */
912#if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
913#define HAVE_VECTOR_SIZE 1
914#endif
915
916
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500917/* Global runtime options. */
918
919struct hb_options_t
920{
Behdad Esfahbod5c871202014-10-14 20:07:31 -0700921 unsigned int initialized : 1;
922 unsigned int uniscribe_bug_compatible : 1;
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500923};
924
925union hb_options_union_t {
Behdad Esfahbod5c871202014-10-14 20:07:31 -0700926 unsigned int i;
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500927 hb_options_t opts;
928};
Behdad Esfahbodc3448e82017-10-15 12:02:00 +0200929static_assert ((sizeof (int) == sizeof (hb_options_union_t)), "");
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500930
931HB_INTERNAL void
932_hb_options_init (void);
933
934extern HB_INTERNAL hb_options_union_t _hb_options;
935
936static inline hb_options_t
937hb_options (void)
938{
939 if (unlikely (!_hb_options.i))
940 _hb_options_init ();
941
942 return _hb_options.opts;
943}
944
Steven R. Loomisa13b0232015-12-11 10:21:27 -0800945/* Size signifying variable-sized array */
946#define VAR 1
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500947
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600948
949/* String type. */
950
951struct hb_string_t
952{
953 inline hb_string_t (void) : bytes (nullptr), len (0) {}
954 inline hb_string_t (const char *bytes_, unsigned int len_) : bytes (bytes_), len (len_) {}
955
Behdad Esfahbode35a7632017-10-30 13:15:05 -0600956 inline int cmp (const hb_string_t &a) const
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600957 {
Behdad Esfahbode35a7632017-10-30 13:15:05 -0600958 if (len != a.len)
959 return (int) a.len - (int) len;
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600960
Behdad Esfahbode35a7632017-10-30 13:15:05 -0600961 return memcmp (a.bytes, bytes, len);
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600962 }
963 static inline int cmp (const void *pa, const void *pb)
964 {
965 hb_string_t *a = (hb_string_t *) pa;
966 hb_string_t *b = (hb_string_t *) pb;
Behdad Esfahbode35a7632017-10-30 13:15:05 -0600967 return b->cmp (*a);
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600968 }
969
970 const char *bytes;
971 unsigned int len;
972};
973
974
Behdad Esfahbodc57d4542011-04-20 18:50:27 -0400975#endif /* HB_PRIVATE_HH */