Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2007,2008,2009 Red Hat, Inc. |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 3 | * Copyright © 2011,2012 Google, Inc. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 4 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame] | 5 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 6 | * |
| 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 Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 26 | * Google Author(s): Behdad Esfahbod |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 27 | */ |
| 28 | |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 29 | #ifndef HB_PRIVATE_HH |
| 30 | #define HB_PRIVATE_HH |
Behdad Esfahbod | 5b3f770 | 2006-12-28 06:42:37 -0500 | [diff] [blame] | 31 | |
Ebrahim Byagowi | 5dbbd0f | 2018-01-11 12:33:22 +0330 | [diff] [blame] | 32 | #define _GNU_SOURCE 1 |
| 33 | |
Behdad Esfahbod | 5ddd9cc | 2011-09-16 16:40:44 -0400 | [diff] [blame] | 34 | #ifdef HAVE_CONFIG_H |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 35 | #include "config.h" |
| 36 | #endif |
Behdad Esfahbod | 12360f7 | 2008-01-23 15:50:38 -0500 | [diff] [blame] | 37 | |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 38 | #include "hb.h" |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 39 | #define HB_H_IN |
Behdad Esfahbod | 3e32cd9 | 2012-04-23 13:20:52 -0400 | [diff] [blame] | 40 | #ifdef HAVE_OT |
| 41 | #include "hb-ot.h" |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 42 | #define HB_OT_H_IN |
Behdad Esfahbod | 3e32cd9 | 2012-04-23 13:20:52 -0400 | [diff] [blame] | 43 | #endif |
Behdad Esfahbod | b28815c | 2009-08-04 22:35:36 -0400 | [diff] [blame] | 44 | |
Behdad Esfahbod | 7cdd6ab | 2018-01-10 04:33:08 +0100 | [diff] [blame] | 45 | #include <math.h> |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 46 | #include <stdlib.h> |
Behdad Esfahbod | b65c060 | 2011-07-28 16:48:43 -0400 | [diff] [blame] | 47 | #include <stddef.h> |
Behdad Esfahbod | f0954d1 | 2009-07-30 15:33:57 -0400 | [diff] [blame] | 48 | #include <string.h> |
| 49 | #include <assert.h> |
Behdad Esfahbod | 7acb389 | 2009-08-05 15:20:34 -0400 | [diff] [blame] | 50 | #include <errno.h> |
Behdad Esfahbod | 40ec3bb | 2017-11-03 16:57:30 -0400 | [diff] [blame] | 51 | #include <stdio.h> |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 52 | #include <stdarg.h> |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 53 | |
Tor Andersson | 8f4c123 | 2018-04-13 23:01:54 +0430 | [diff] [blame] | 54 | #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__) |
Arkady Shapkin | 6c10328 | 2018-02-15 23:17:46 +0300 | [diff] [blame] | 55 | #include <intrin.h> |
| 56 | #endif |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 57 | |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 58 | #define HB_PASTE1(a,b) a##b |
| 59 | #define HB_PASTE(a,b) HB_PASTE1(a,b) |
| 60 | |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 61 | |
Behdad Esfahbod | 52b4185 | 2015-10-03 13:20:55 +0100 | [diff] [blame] | 62 | /* Compile-time custom allocator support. */ |
| 63 | |
| 64 | #if defined(hb_malloc_impl) \ |
| 65 | && defined(hb_calloc_impl) \ |
| 66 | && defined(hb_realloc_impl) \ |
| 67 | && defined(hb_free_impl) |
Behdad Esfahbod | cc6ea30 | 2015-10-12 17:21:52 -0400 | [diff] [blame] | 68 | extern "C" void* hb_malloc_impl(size_t size); |
| 69 | extern "C" void* hb_calloc_impl(size_t nmemb, size_t size); |
| 70 | extern "C" void* hb_realloc_impl(void *ptr, size_t size); |
| 71 | extern "C" void hb_free_impl(void *ptr); |
Behdad Esfahbod | 52b4185 | 2015-10-03 13:20:55 +0100 | [diff] [blame] | 72 | #define malloc hb_malloc_impl |
| 73 | #define calloc hb_calloc_impl |
| 74 | #define realloc hb_realloc_impl |
| 75 | #define free hb_free_impl |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 76 | |
Behdad Esfahbod | 763f878 | 2018-07-10 13:47:41 +0200 | [diff] [blame] | 77 | #if defined(hb_memalign_impl) |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 78 | extern "C" int hb_memalign_impl(void **memptr, size_t alignment, size_t size); |
| 79 | #define posix_memalign hb_memalign_impl |
| 80 | #else |
| 81 | #undef HAVE_POSIX_MEMALIGN |
| 82 | #endif |
| 83 | |
Behdad Esfahbod | 52b4185 | 2015-10-03 13:20:55 +0100 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 87 | /* Compiler attributes */ |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 88 | |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 89 | |
Behdad Esfahbod | 292c100 | 2018-07-10 13:16:52 +0200 | [diff] [blame] | 90 | template <typename T> |
| 91 | struct _hb_alignof |
| 92 | { |
| 93 | struct s |
| 94 | { |
| 95 | char c; |
| 96 | T t; |
| 97 | }; |
| 98 | static constexpr unsigned int value = offsetof (s, t); |
| 99 | }; |
| 100 | |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 101 | #if __cplusplus < 201103L |
| 102 | |
Behdad Esfahbod | 62e312e | 2017-10-27 09:29:20 -0600 | [diff] [blame] | 103 | #ifndef nullptr |
| 104 | #define nullptr NULL |
| 105 | #endif |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 106 | |
Behdad Esfahbod | bca8361 | 2018-07-10 12:58:13 +0200 | [diff] [blame] | 107 | #ifndef constexpr |
| 108 | #define constexpr const |
| 109 | #endif |
| 110 | |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 111 | // Static assertions |
| 112 | #ifndef static_assert |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 113 | #define static_assert(e, msg) \ |
Behdad Esfahbod | 6f08b12 | 2017-10-26 18:23:03 -0600 | [diff] [blame] | 114 | HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1] |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 115 | #endif // static_assert |
| 116 | |
Behdad Esfahbod | f83e992 | 2018-05-24 11:46:57 -0700 | [diff] [blame] | 117 | #ifdef __GNUC__ |
| 118 | #if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)) |
| 119 | #define thread_local __thread |
| 120 | #endif |
| 121 | #else |
| 122 | #define thread_local |
| 123 | #endif |
| 124 | |
Behdad Esfahbod | bca8361 | 2018-07-10 12:58:13 +0200 | [diff] [blame] | 125 | #ifndef alignof |
Behdad Esfahbod | 27c5317 | 2018-07-10 13:03:42 +0200 | [diff] [blame] | 126 | #define alignof(x) (_hb_alignof<x>::value) |
Behdad Esfahbod | bca8361 | 2018-07-10 12:58:13 +0200 | [diff] [blame] | 127 | #endif // alignof |
| 128 | |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 129 | #endif // __cplusplus < 201103L |
| 130 | |
Ebrahim Byagowi | 76c4873 | 2017-06-02 21:53:10 +0430 | [diff] [blame] | 131 | #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__) |
| 132 | #define likely(expr) (__builtin_expect (!!(expr), 1)) |
| 133 | #define unlikely(expr) (__builtin_expect (!!(expr), 0)) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 134 | #else |
| 135 | #define likely(expr) (expr) |
| 136 | #define unlikely(expr) (expr) |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 137 | #endif |
| 138 | |
Steve Lhomme | 0b8f3ab | 2016-07-11 21:57:26 +0200 | [diff] [blame] | 139 | #if !defined(__GNUC__) && !defined(__clang__) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 140 | #undef __attribute__ |
| 141 | #define __attribute__(x) |
| 142 | #endif |
Behdad Esfahbod | a794ebf | 2009-08-06 12:32:35 -0400 | [diff] [blame] | 143 | |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 144 | #if __GNUC__ >= 3 |
| 145 | #define HB_PURE_FUNC __attribute__((pure)) |
| 146 | #define HB_CONST_FUNC __attribute__((const)) |
| 147 | #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx))) |
| 148 | #else |
| 149 | #define HB_PURE_FUNC |
| 150 | #define HB_CONST_FUNC |
| 151 | #define HB_PRINTF_FUNC(format_idx, arg_idx) |
| 152 | #endif |
| 153 | #if __GNUC__ >= 4 |
| 154 | #define HB_UNUSED __attribute__((unused)) |
Behdad Esfahbod | be59f3c | 2017-12-05 09:01:28 -0800 | [diff] [blame] | 155 | #elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */ |
| 156 | #define HB_UNUSED __pragma(warning(suppress: 4100 4101)) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 157 | #else |
| 158 | #define HB_UNUSED |
| 159 | #endif |
| 160 | |
| 161 | #ifndef HB_INTERNAL |
Behdad Esfahbod | 90d1c74 | 2018-02-11 19:08:59 -0800 | [diff] [blame] | 162 | # if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 163 | # define HB_INTERNAL __attribute__((__visibility__("hidden"))) |
| 164 | # else |
| 165 | # define HB_INTERNAL |
Behdad Esfahbod | b87cbe2 | 2018-02-07 14:11:26 -0500 | [diff] [blame] | 166 | # define HB_NO_VISIBILITY 1 |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 167 | # endif |
| 168 | #endif |
| 169 | |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 170 | #if __GNUC__ >= 3 |
| 171 | #define HB_FUNC __PRETTY_FUNCTION__ |
| 172 | #elif defined(_MSC_VER) |
| 173 | #define HB_FUNC __FUNCSIG__ |
| 174 | #else |
| 175 | #define HB_FUNC __func__ |
| 176 | #endif |
| 177 | |
prrace | 498e437 | 2018-06-09 16:04:28 -0700 | [diff] [blame] | 178 | #if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140) |
Behdad Esfahbod | 8cef3a6 | 2018-02-09 16:04:23 -0600 | [diff] [blame] | 179 | /* https://github.com/harfbuzz/harfbuzz/issues/630 */ |
| 180 | #define __restrict |
| 181 | #endif |
| 182 | |
Behdad Esfahbod | 305d2fb | 2015-10-21 11:04:28 -0200 | [diff] [blame] | 183 | /* |
| 184 | * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411 |
| 185 | * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch |
| 186 | * cases that fall through without a break or return statement. HB_FALLTHROUGH |
| 187 | * is only needed on cases that have code: |
| 188 | * |
| 189 | * switch (foo) { |
| 190 | * case 1: // These cases have no code. No fallthrough annotations are needed. |
| 191 | * case 2: |
| 192 | * case 3: |
| 193 | * foo = 4; // This case has code, so a fallthrough annotation is needed: |
| 194 | * HB_FALLTHROUGH; |
| 195 | * default: |
| 196 | * return foo; |
| 197 | * } |
| 198 | */ |
| 199 | #if defined(__clang__) && __cplusplus >= 201103L |
| 200 | /* clang's fallthrough annotations are only available starting in C++11. */ |
| 201 | # define HB_FALLTHROUGH [[clang::fallthrough]] |
Behdad Esfahbod | f41b921 | 2018-02-05 19:51:09 -0500 | [diff] [blame] | 202 | #elif __GNUC__ >= 7 |
| 203 | /* GNU fallthrough attribute is available from GCC7 */ |
| 204 | # define HB_FALLTHROUGH __attribute__((fallthrough)) |
Behdad Esfahbod | 305d2fb | 2015-10-21 11:04:28 -0200 | [diff] [blame] | 205 | #elif defined(_MSC_VER) |
| 206 | /* |
| 207 | * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis): |
| 208 | * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx |
| 209 | */ |
| 210 | # include <sal.h> |
| 211 | # define HB_FALLTHROUGH __fallthrough |
| 212 | #else |
| 213 | # define HB_FALLTHROUGH /* FALLTHROUGH */ |
| 214 | #endif |
| 215 | |
Behdad Esfahbod | 0fc0a10 | 2014-07-21 11:12:54 -0400 | [diff] [blame] | 216 | #if defined(_WIN32) || defined(__CYGWIN__) |
Behdad Esfahbod | db30828 | 2014-07-19 16:32:04 -0400 | [diff] [blame] | 217 | /* We need Windows Vista for both Uniscribe backend and for |
| 218 | * MemoryBarrier. We don't support compiling on Windows XP, |
| 219 | * though we run on it fine. */ |
| 220 | # if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600 |
| 221 | # undef _WIN32_WINNT |
| 222 | # endif |
| 223 | # ifndef _WIN32_WINNT |
| 224 | # define _WIN32_WINNT 0x0600 |
| 225 | # endif |
Behdad Esfahbod | 270971a | 2014-08-15 14:28:04 -0400 | [diff] [blame] | 226 | # ifndef WIN32_LEAN_AND_MEAN |
| 227 | # define WIN32_LEAN_AND_MEAN 1 |
| 228 | # endif |
| 229 | # ifndef STRICT |
| 230 | # define STRICT 1 |
| 231 | # endif |
Behdad Esfahbod | dabe698 | 2012-11-23 14:21:35 -0500 | [diff] [blame] | 232 | |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 233 | # if defined(_WIN32_WCE) |
| 234 | /* Some things not defined on Windows CE. */ |
Konstantin Ritt | 5069062 | 2016-04-26 12:02:26 +0400 | [diff] [blame] | 235 | # define vsnprintf _vsnprintf |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 236 | # define getenv(Name) nullptr |
Konstantin Ritt | 855a5d7 | 2015-04-10 17:18:01 +0400 | [diff] [blame] | 237 | # if _WIN32_WCE < 0x800 |
| 238 | # define setlocale(Category, Locale) "C" |
Behdad Esfahbod | 26a963b | 2014-08-10 18:04:50 -0400 | [diff] [blame] | 239 | static int errno = 0; /* Use something better? */ |
Konstantin Ritt | 855a5d7 | 2015-04-10 17:18:01 +0400 | [diff] [blame] | 240 | # endif |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 241 | # elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 242 | # define getenv(Name) nullptr |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 243 | # endif |
Behdad Esfahbod | ce01ad7 | 2015-04-01 11:05:59 -0700 | [diff] [blame] | 244 | # if defined(_MSC_VER) && _MSC_VER < 1900 |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 245 | # define snprintf _snprintf |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 246 | # endif |
Behdad Esfahbod | 26a963b | 2014-08-10 18:04:50 -0400 | [diff] [blame] | 247 | #endif |
| 248 | |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 249 | #if HAVE_ATEXIT |
| 250 | /* atexit() is only safe to be called from shared libraries on certain |
| 251 | * platforms. Whitelist. |
| 252 | * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */ |
| 253 | # if defined(__linux) && defined(__GLIBC_PREREQ) |
| 254 | # if __GLIBC_PREREQ(2,3) |
| 255 | /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */ |
| 256 | # define HB_USE_ATEXIT 1 |
| 257 | # endif |
| 258 | # elif defined(_MSC_VER) || defined(__MINGW32__) |
| 259 | /* For MSVC: |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 260 | * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx |
| 261 | * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 262 | * mingw32 headers say atexit is safe to use in shared libraries. |
| 263 | */ |
| 264 | # define HB_USE_ATEXIT 1 |
Ebrahim Byagowi | 632713b | 2018-04-12 14:17:03 +0430 | [diff] [blame] | 265 | # elif defined(__ANDROID__) |
| 266 | /* This is available since Android NKD r8 or r8b: |
| 267 | * https://issuetracker.google.com/code/p/android/issues/detail?id=6455 |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 268 | */ |
| 269 | # define HB_USE_ATEXIT 1 |
Bruce Mitchener | e8859fc | 2018-02-04 01:26:57 +0700 | [diff] [blame] | 270 | # elif defined(__APPLE__) |
| 271 | /* For macOS and related platforms, the atexit man page indicates |
| 272 | * that it will be invoked when the library is unloaded, not only |
| 273 | * at application exit. |
| 274 | */ |
| 275 | # define HB_USE_ATEXIT 1 |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 276 | # endif |
| 277 | #endif |
Behdad Esfahbod | c14b24f | 2018-03-26 10:44:54 -0700 | [diff] [blame] | 278 | #ifdef HB_NO_ATEXIT |
| 279 | # undef HB_USE_ATEXIT |
| 280 | #endif |
Behdad Esfahbod | dabe698 | 2012-11-23 14:21:35 -0500 | [diff] [blame] | 281 | |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 282 | /* Basics */ |
| 283 | |
| 284 | #undef MIN |
Behdad Esfahbod | 25326c2 | 2012-08-04 16:43:18 -0700 | [diff] [blame] | 285 | template <typename Type> |
| 286 | static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 287 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 288 | #undef MAX |
Behdad Esfahbod | 25326c2 | 2012-08-04 16:43:18 -0700 | [diff] [blame] | 289 | template <typename Type> |
| 290 | static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 291 | |
Behdad Esfahbod | 16f175c | 2013-11-12 17:22:49 -0500 | [diff] [blame] | 292 | static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b) |
| 293 | { return (a + (b - 1)) / b; } |
| 294 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 295 | |
Behdad Esfahbod | 4591753 | 2009-11-04 18:15:59 -0500 | [diff] [blame] | 296 | #undef ARRAY_LENGTH |
Behdad Esfahbod | 25326c2 | 2012-08-04 16:43:18 -0700 | [diff] [blame] | 297 | template <typename Type, unsigned int n> |
Behdad Esfahbod | 0beb66e | 2012-12-05 18:46:04 -0500 | [diff] [blame] | 298 | static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; } |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 299 | /* A const version, but does not detect erratically being called on pointers. */ |
| 300 | #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 301 | |
Behdad Esfahbod | 35a7383 | 2009-08-01 19:30:31 -0400 | [diff] [blame] | 302 | #define HB_STMT_START do |
| 303 | #define HB_STMT_END while (0) |
Behdad Esfahbod | 5b3f770 | 2006-12-28 06:42:37 -0500 | [diff] [blame] | 304 | |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 305 | template <unsigned int cond> class hb_assert_constant_t; |
| 306 | template <> class hb_assert_constant_t<1> {}; |
Behdad Esfahbod | 672ca3b | 2015-10-26 14:05:05 -0700 | [diff] [blame] | 307 | |
| 308 | #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>)) |
Behdad Esfahbod | dcb7026 | 2011-04-21 16:34:22 -0400 | [diff] [blame] | 309 | |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 310 | /* Lets assert int types. Saves trouble down the road. */ |
| 311 | |
Behdad Esfahbod | c3448e8 | 2017-10-15 12:02:00 +0200 | [diff] [blame] | 312 | static_assert ((sizeof (int8_t) == 1), ""); |
| 313 | static_assert ((sizeof (uint8_t) == 1), ""); |
| 314 | static_assert ((sizeof (int16_t) == 2), ""); |
| 315 | static_assert ((sizeof (uint16_t) == 2), ""); |
| 316 | static_assert ((sizeof (int32_t) == 4), ""); |
| 317 | static_assert ((sizeof (uint32_t) == 4), ""); |
| 318 | static_assert ((sizeof (int64_t) == 8), ""); |
| 319 | static_assert ((sizeof (uint64_t) == 8), ""); |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 320 | |
Behdad Esfahbod | c3448e8 | 2017-10-15 12:02:00 +0200 | [diff] [blame] | 321 | static_assert ((sizeof (hb_codepoint_t) == 4), ""); |
| 322 | static_assert ((sizeof (hb_position_t) == 4), ""); |
| 323 | static_assert ((sizeof (hb_mask_t) == 4), ""); |
| 324 | static_assert ((sizeof (hb_var_int_t) == 4), ""); |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 325 | |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 326 | |
| 327 | /* We like our types POD */ |
| 328 | |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 329 | #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; } |
| 330 | #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type) |
| 331 | #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type) |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 332 | |
| 333 | #ifdef __GNUC__ |
Behdad Esfahbod | 79e2b47 | 2012-06-06 11:27:17 -0400 | [diff] [blame] | 334 | # define _ASSERT_INSTANCE_POD1(_line, _instance) \ |
| 335 | HB_STMT_START { \ |
| 336 | typedef __typeof__(_instance) _type_##_line; \ |
| 337 | _ASSERT_TYPE_POD1 (_line, _type_##_line); \ |
| 338 | } HB_STMT_END |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 339 | #else |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 340 | # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 341 | #endif |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 342 | # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance) |
| 343 | # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance) |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 344 | |
| 345 | /* Check _assertion in a method environment */ |
| 346 | #define _ASSERT_POD1(_line) \ |
Behdad Esfahbod | dac8602 | 2014-06-03 17:57:00 -0400 | [diff] [blame] | 347 | HB_UNUSED inline void _static_assertion_on_line_##_line (void) const \ |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 348 | { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ } |
| 349 | # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line) |
| 350 | # define ASSERT_POD() _ASSERT_POD0 (__LINE__) |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 351 | |
| 352 | |
| 353 | |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 354 | /* Tiny functions */ |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 355 | |
Behdad Esfahbod | ca42d96 | 2018-01-11 09:15:34 +0100 | [diff] [blame] | 356 | /* |
| 357 | * Void! |
| 358 | */ |
| 359 | typedef const struct _hb_void_t *hb_void_t; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 360 | #define HB_VOID ((const _hb_void_t *) nullptr) |
Behdad Esfahbod | 7d3a126 | 2010-04-29 13:54:01 -0400 | [diff] [blame] | 361 | |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 362 | /* Return the number of 1 bits in v. */ |
Behdad Esfahbod | 978ace6 | 2018-02-16 16:06:17 -0800 | [diff] [blame] | 363 | template <typename T> |
Behdad Esfahbod | 97e7f8f | 2010-05-11 00:11:36 -0400 | [diff] [blame] | 364 | static inline HB_CONST_FUNC unsigned int |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 365 | _hb_popcount (T v) |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 366 | { |
Behdad Esfahbod | 978ace6 | 2018-02-16 16:06:17 -0800 | [diff] [blame] | 367 | #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) && defined(__OPTIMIZE__) |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 368 | if (sizeof (T) <= sizeof (unsigned int)) |
| 369 | return __builtin_popcount (v); |
Behdad Esfahbod | 978ace6 | 2018-02-16 16:06:17 -0800 | [diff] [blame] | 370 | |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 371 | if (sizeof (T) <= sizeof (unsigned long)) |
| 372 | return __builtin_popcountl (v); |
Behdad Esfahbod | 978ace6 | 2018-02-16 16:06:17 -0800 | [diff] [blame] | 373 | |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 374 | if (sizeof (T) <= sizeof (unsigned long long)) |
| 375 | return __builtin_popcountll (v); |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 376 | #endif |
Behdad Esfahbod | 978ace6 | 2018-02-16 16:06:17 -0800 | [diff] [blame] | 377 | |
| 378 | if (sizeof (T) <= 4) |
| 379 | { |
| 380 | /* "HACKMEM 169" */ |
| 381 | uint32_t y; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 382 | y = (v >> 1) &033333333333; |
| 383 | y = v - y - ((y >>1) & 033333333333); |
Behdad Esfahbod | 978ace6 | 2018-02-16 16:06:17 -0800 | [diff] [blame] | 384 | return (((y + (y >> 3)) & 030707070707) % 077); |
| 385 | } |
| 386 | |
| 387 | if (sizeof (T) == 8) |
| 388 | { |
| 389 | unsigned int shift = 32; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 390 | return _hb_popcount<uint32_t> ((uint32_t) v) + _hb_popcount ((uint32_t) (v >> shift)); |
Behdad Esfahbod | 978ace6 | 2018-02-16 16:06:17 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | if (sizeof (T) == 16) |
| 394 | { |
| 395 | unsigned int shift = 64; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 396 | return _hb_popcount<uint64_t> ((uint64_t) v) + _hb_popcount ((uint64_t) (v >> shift)); |
Behdad Esfahbod | 978ace6 | 2018-02-16 16:06:17 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 399 | assert (0); |
Behdad Esfahbod | 676b19f | 2018-06-06 15:23:35 -0700 | [diff] [blame] | 400 | return 0; /* Shut up stupid compiler. */ |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 403 | /* Returns the number of bits needed to store number */ |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 404 | template <typename T> |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 405 | static inline HB_CONST_FUNC unsigned int |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 406 | _hb_bit_storage (T v) |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 407 | { |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 408 | if (unlikely (!v)) return 0; |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 409 | |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 410 | #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 411 | if (sizeof (T) <= sizeof (unsigned int)) |
| 412 | return sizeof (unsigned int) * 8 - __builtin_clz (v); |
| 413 | |
| 414 | if (sizeof (T) <= sizeof (unsigned long)) |
Behdad Esfahbod | 6a91a2e | 2018-02-16 17:49:41 -0800 | [diff] [blame] | 415 | return sizeof (unsigned long) * 8 - __builtin_clzl (v); |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 416 | |
| 417 | if (sizeof (T) <= sizeof (unsigned long long)) |
Behdad Esfahbod | 6a91a2e | 2018-02-16 17:49:41 -0800 | [diff] [blame] | 418 | return sizeof (unsigned long long) * 8 - __builtin_clzll (v); |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 419 | #endif |
| 420 | |
Tor Andersson | 8f4c123 | 2018-04-13 23:01:54 +0430 | [diff] [blame] | 421 | #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__) |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 422 | if (sizeof (T) <= sizeof (unsigned int)) |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 423 | { |
| 424 | unsigned long where; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 425 | _BitScanReverse (&where, v); |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 426 | return 1 + where; |
| 427 | } |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 428 | # if _WIN64 |
| 429 | if (sizeof (T) <= 8) |
| 430 | { |
| 431 | unsigned long where; |
| 432 | _BitScanReverse64 (&where, v); |
| 433 | return 1 + where; |
| 434 | } |
| 435 | # endif |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 436 | #endif |
| 437 | |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 438 | if (sizeof (T) <= 4) |
| 439 | { |
| 440 | /* "bithacks" */ |
| 441 | const unsigned int b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}; |
| 442 | const unsigned int S[] = {1, 2, 4, 8, 16}; |
Behdad Esfahbod | 4e517ec | 2018-02-16 18:20:12 -0800 | [diff] [blame] | 443 | unsigned int r = 0; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 444 | for (int i = 4; i >= 0; i--) |
| 445 | if (v & b[i]) |
| 446 | { |
| 447 | v >>= S[i]; |
| 448 | r |= S[i]; |
| 449 | } |
Behdad Esfahbod | 4e517ec | 2018-02-16 18:20:12 -0800 | [diff] [blame] | 450 | return r + 1; |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 451 | } |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 452 | if (sizeof (T) <= 8) |
| 453 | { |
| 454 | /* "bithacks" */ |
Behdad Esfahbod | 7ec3ba2 | 2018-03-24 14:12:12 -0700 | [diff] [blame] | 455 | const uint64_t b[] = {0x2ULL, 0xCULL, 0xF0ULL, 0xFF00ULL, 0xFFFF0000ULL, 0xFFFFFFFF00000000ULL}; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 456 | const unsigned int S[] = {1, 2, 4, 8, 16, 32}; |
Behdad Esfahbod | 4e517ec | 2018-02-16 18:20:12 -0800 | [diff] [blame] | 457 | unsigned int r = 0; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 458 | for (int i = 5; i >= 0; i--) |
| 459 | if (v & b[i]) |
| 460 | { |
| 461 | v >>= S[i]; |
| 462 | r |= S[i]; |
| 463 | } |
Behdad Esfahbod | 4e517ec | 2018-02-16 18:20:12 -0800 | [diff] [blame] | 464 | return r + 1; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 465 | } |
| 466 | if (sizeof (T) == 16) |
| 467 | { |
| 468 | unsigned int shift = 64; |
Behdad Esfahbod | 78d92e0 | 2018-06-06 15:24:43 -0700 | [diff] [blame] | 469 | return (v >> shift) ? _hb_bit_storage<uint64_t> ((uint64_t) (v >> shift)) + shift : |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 470 | _hb_bit_storage<uint64_t> ((uint64_t) v); |
| 471 | } |
| 472 | |
| 473 | assert (0); |
Behdad Esfahbod | 676b19f | 2018-06-06 15:23:35 -0700 | [diff] [blame] | 474 | return 0; /* Shut up stupid compiler. */ |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 475 | } |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 476 | |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 477 | /* Returns the number of zero bits in the least significant side of v */ |
| 478 | template <typename T> |
Behdad Esfahbod | f7acd8d | 2010-05-20 17:26:35 +0100 | [diff] [blame] | 479 | static inline HB_CONST_FUNC unsigned int |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 480 | _hb_ctz (T v) |
Behdad Esfahbod | f7acd8d | 2010-05-20 17:26:35 +0100 | [diff] [blame] | 481 | { |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 482 | if (unlikely (!v)) return 0; |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 483 | |
| 484 | #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 485 | if (sizeof (T) <= sizeof (unsigned int)) |
| 486 | return __builtin_ctz (v); |
| 487 | |
| 488 | if (sizeof (T) <= sizeof (unsigned long)) |
| 489 | return __builtin_ctzl (v); |
| 490 | |
| 491 | if (sizeof (T) <= sizeof (unsigned long long)) |
| 492 | return __builtin_ctzll (v); |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 493 | #endif |
| 494 | |
Tor Andersson | 8f4c123 | 2018-04-13 23:01:54 +0430 | [diff] [blame] | 495 | #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__) |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 496 | if (sizeof (T) <= sizeof (unsigned int)) |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 497 | { |
| 498 | unsigned long where; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 499 | _BitScanForward (&where, v); |
Behdad Esfahbod | 97a7110 | 2018-02-18 10:50:24 -0800 | [diff] [blame] | 500 | return where; |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 501 | } |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 502 | # if _WIN64 |
| 503 | if (sizeof (T) <= 8) |
| 504 | { |
| 505 | unsigned long where; |
| 506 | _BitScanForward64 (&where, v); |
Behdad Esfahbod | 97a7110 | 2018-02-18 10:50:24 -0800 | [diff] [blame] | 507 | return where; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 508 | } |
| 509 | # endif |
Behdad Esfahbod | 864a2dd | 2018-02-16 14:21:40 -0800 | [diff] [blame] | 510 | #endif |
| 511 | |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 512 | if (sizeof (T) <= 4) |
| 513 | { |
| 514 | /* "bithacks" */ |
| 515 | unsigned int c = 32; |
Behdad Esfahbod | cd11107 | 2018-02-16 18:28:58 -0800 | [diff] [blame] | 516 | v &= - (int32_t) v; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 517 | if (v) c--; |
| 518 | if (v & 0x0000FFFF) c -= 16; |
| 519 | if (v & 0x00FF00FF) c -= 8; |
| 520 | if (v & 0x0F0F0F0F) c -= 4; |
| 521 | if (v & 0x33333333) c -= 2; |
| 522 | if (v & 0x55555555) c -= 1; |
| 523 | return c; |
Behdad Esfahbod | f7acd8d | 2010-05-20 17:26:35 +0100 | [diff] [blame] | 524 | } |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 525 | if (sizeof (T) <= 8) |
| 526 | { |
| 527 | /* "bithacks" */ |
| 528 | unsigned int c = 64; |
Behdad Esfahbod | cd11107 | 2018-02-16 18:28:58 -0800 | [diff] [blame] | 529 | v &= - (int64_t) (v); |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 530 | if (v) c--; |
Behdad Esfahbod | ebccd01 | 2018-03-24 17:51:55 -0700 | [diff] [blame] | 531 | if (v & 0x00000000FFFFFFFFULL) c -= 32; |
| 532 | if (v & 0x0000FFFF0000FFFFULL) c -= 16; |
| 533 | if (v & 0x00FF00FF00FF00FFULL) c -= 8; |
| 534 | if (v & 0x0F0F0F0F0F0F0F0FULL) c -= 4; |
| 535 | if (v & 0x3333333333333333ULL) c -= 2; |
| 536 | if (v & 0x5555555555555555ULL) c -= 1; |
Behdad Esfahbod | 82eb1da | 2018-02-16 16:52:09 -0800 | [diff] [blame] | 537 | return c; |
| 538 | } |
| 539 | if (sizeof (T) == 16) |
| 540 | { |
| 541 | unsigned int shift = 64; |
| 542 | return (uint64_t) v ? _hb_bit_storage<uint64_t> ((uint64_t) v) : |
| 543 | _hb_bit_storage<uint64_t> ((uint64_t) v >> shift) + shift; |
| 544 | } |
| 545 | |
| 546 | assert (0); |
Behdad Esfahbod | 676b19f | 2018-06-06 15:23:35 -0700 | [diff] [blame] | 547 | return 0; /* Shut up stupid compiler. */ |
Behdad Esfahbod | f7acd8d | 2010-05-20 17:26:35 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Behdad Esfahbod | 080a0eb | 2011-04-28 16:01:01 -0400 | [diff] [blame] | 550 | static inline bool |
| 551 | _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size) |
| 552 | { |
| 553 | return (size > 0) && (count >= ((unsigned int) -1) / size); |
| 554 | } |
| 555 | |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 556 | static inline unsigned int |
| 557 | _hb_ceil_to_4 (unsigned int v) |
| 558 | { |
Behdad Esfahbod | 931f8b7 | 2018-02-07 21:42:03 -0600 | [diff] [blame] | 559 | return ((v - 1) | 3) + 1; |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 560 | } |
| 561 | |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 562 | static inline bool _hb_ispow2 (unsigned int v) |
| 563 | { |
| 564 | return 0 == (v & (v - 1)); |
| 565 | } |
Behdad Esfahbod | 080a0eb | 2011-04-28 16:01:01 -0400 | [diff] [blame] | 566 | |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 567 | |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 568 | /* |
| 569 | * |
| 570 | * Utility types |
| 571 | * |
| 572 | */ |
| 573 | |
Behdad Esfahbod | a7dd90f | 2018-06-01 16:07:55 -0700 | [diff] [blame] | 574 | #define HB_DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
Behdad Esfahbod | fb07d1a | 2018-06-01 17:32:07 -0700 | [diff] [blame] | 575 | TypeName(const TypeName&); \ |
| 576 | void operator=(const TypeName&) |
Behdad Esfahbod | 6c22276 | 2018-05-25 16:21:27 -0700 | [diff] [blame] | 577 | |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 578 | /* |
Behdad Esfahbod | 5d80129 | 2018-05-24 11:33:15 -0700 | [diff] [blame] | 579 | * Static pools |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 580 | */ |
| 581 | |
| 582 | /* Global nul-content Null pool. Enlarge as necessary. */ |
| 583 | |
Behdad Esfahbod | 4ca211b | 2018-06-01 17:18:57 -0700 | [diff] [blame] | 584 | #define HB_NULL_POOL_SIZE 264 |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 585 | static_assert (HB_NULL_POOL_SIZE % sizeof (void *) == 0, "Align HB_NULL_POOL_SIZE."); |
| 586 | |
| 587 | #ifdef HB_NO_VISIBILITY |
| 588 | static |
| 589 | #else |
| 590 | extern HB_INTERNAL |
| 591 | #endif |
Behdad Esfahbod | 7f7b137 | 2018-05-24 14:09:04 -0700 | [diff] [blame] | 592 | void * const _hb_NullPool[HB_NULL_POOL_SIZE / sizeof (void *)] |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 593 | #ifdef HB_NO_VISIBILITY |
| 594 | = {} |
| 595 | #endif |
| 596 | ; |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 597 | /* Generic nul-content Null objects. */ |
| 598 | template <typename Type> |
Behdad Esfahbod | 7f7b137 | 2018-05-24 14:09:04 -0700 | [diff] [blame] | 599 | static inline Type const & Null (void) { |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 600 | static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); |
Behdad Esfahbod | 7f7b137 | 2018-05-24 14:09:04 -0700 | [diff] [blame] | 601 | return *reinterpret_cast<Type const *> (_hb_NullPool); |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 602 | } |
| 603 | #define Null(Type) Null<Type>() |
| 604 | |
| 605 | /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ |
| 606 | #define DEFINE_NULL_DATA(Namespace, Type, data) \ |
| 607 | } /* Close namespace. */ \ |
| 608 | static const char _Null##Type[sizeof (Namespace::Type) + 1] = data; /* +1 is for nul-termination in data */ \ |
| 609 | template <> \ |
| 610 | /*static*/ inline const Namespace::Type& Null<Namespace::Type> (void) { \ |
| 611 | return *reinterpret_cast<const Namespace::Type *> (_Null##Type); \ |
| 612 | } \ |
| 613 | namespace Namespace { \ |
| 614 | /* The following line really exists such that we end in a place needing semicolon */ \ |
| 615 | static_assert (Namespace::Type::min_size + 1 <= sizeof (_Null##Type), "Null pool too small. Enlarge.") |
| 616 | |
| 617 | |
Behdad Esfahbod | 5d80129 | 2018-05-24 11:33:15 -0700 | [diff] [blame] | 618 | /* Global writable pool. Enlarge as necessary. */ |
| 619 | |
Behdad Esfahbod | f83e992 | 2018-05-24 11:46:57 -0700 | [diff] [blame] | 620 | /* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool |
| 621 | * for correct operation. It only exist to catch and divert program logic bugs instead of |
| 622 | * causing bad memory access. So, races there are not actually introducing incorrectness |
Behdad Esfahbod | efbab6b | 2018-05-29 18:21:55 -0700 | [diff] [blame] | 623 | * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */ |
Behdad Esfahbod | 5d80129 | 2018-05-24 11:33:15 -0700 | [diff] [blame] | 624 | #ifdef HB_NO_VISIBILITY |
| 625 | static |
| 626 | #else |
| 627 | extern HB_INTERNAL |
| 628 | #endif |
Behdad Esfahbod | efbab6b | 2018-05-29 18:21:55 -0700 | [diff] [blame] | 629 | /*thread_local*/ void * _hb_CrapPool[HB_NULL_POOL_SIZE / sizeof (void *)] |
Behdad Esfahbod | 5d80129 | 2018-05-24 11:33:15 -0700 | [diff] [blame] | 630 | #ifdef HB_NO_VISIBILITY |
| 631 | = {} |
| 632 | #endif |
| 633 | ; |
| 634 | /* CRAP pool: Common Region for Access Protection. */ |
| 635 | template <typename Type> |
| 636 | static inline Type& Crap (void) { |
| 637 | static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); |
| 638 | Type *obj = reinterpret_cast<Type *> (_hb_CrapPool); |
| 639 | *obj = Null(Type); |
| 640 | return *obj; |
| 641 | } |
| 642 | #define Crap(Type) Crap<Type>() |
| 643 | |
Behdad Esfahbod | d600e84 | 2018-05-30 16:25:46 -0700 | [diff] [blame] | 644 | template <typename Type> |
| 645 | struct CrapOrNull { |
| 646 | static inline Type & get (void) { return Crap(Type); } |
| 647 | }; |
| 648 | template <typename Type> |
| 649 | struct CrapOrNull<const Type> { |
| 650 | static inline Type const & get (void) { return Null(Type); } |
| 651 | }; |
| 652 | #define CrapOrNull(Type) CrapOrNull<Type>::get () |
| 653 | |
| 654 | |
Behdad Esfahbod | 673b764 | 2018-05-23 20:12:23 -0700 | [diff] [blame] | 655 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 656 | /* arrays and maps */ |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 657 | |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 658 | |
Behdad Esfahbod | 39f11d8 | 2018-07-10 14:01:39 +0200 | [diff] [blame^] | 659 | #define HB_VECTOR_INIT {0, 0, false, nullptr} |
Behdad Esfahbod | 975bdd5 | 2018-06-01 17:37:13 -0700 | [diff] [blame] | 660 | template <typename Type, unsigned int StaticSize=8> |
Behdad Esfahbod | 5c3112a | 2018-05-01 19:07:04 -0400 | [diff] [blame] | 661 | struct hb_vector_t |
Behdad Esfahbod | 0e253e9 | 2012-06-05 15:37:19 -0400 | [diff] [blame] | 662 | { |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 663 | unsigned int len; |
| 664 | unsigned int allocated; |
Behdad Esfahbod | 975bdd5 | 2018-06-01 17:37:13 -0700 | [diff] [blame] | 665 | bool successful; |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 666 | Type *arrayZ; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 667 | Type static_array[StaticSize]; |
| 668 | |
Behdad Esfahbod | 56ef4e0 | 2017-10-15 16:38:29 -0400 | [diff] [blame] | 669 | void init (void) |
| 670 | { |
| 671 | len = 0; |
| 672 | allocated = ARRAY_LENGTH (static_array); |
Behdad Esfahbod | 975bdd5 | 2018-06-01 17:37:13 -0700 | [diff] [blame] | 673 | successful = true; |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 674 | arrayZ = static_array; |
Behdad Esfahbod | 56ef4e0 | 2017-10-15 16:38:29 -0400 | [diff] [blame] | 675 | } |
Behdad Esfahbod | efde811 | 2011-08-23 00:04:57 +0200 | [diff] [blame] | 676 | |
Behdad Esfahbod | 5d80129 | 2018-05-24 11:33:15 -0700 | [diff] [blame] | 677 | inline Type& operator [] (unsigned int i) |
| 678 | { |
| 679 | if (unlikely (i >= len)) |
| 680 | return Crap (Type); |
| 681 | return arrayZ[i]; |
| 682 | } |
| 683 | inline const Type& operator [] (unsigned int i) const |
| 684 | { |
| 685 | if (unlikely (i >= len)) |
Behdad Esfahbod | 353f4d2 | 2018-05-31 19:52:16 -0700 | [diff] [blame] | 686 | return Null(Type); |
Behdad Esfahbod | 5d80129 | 2018-05-24 11:33:15 -0700 | [diff] [blame] | 687 | return arrayZ[i]; |
| 688 | } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 689 | |
| 690 | inline Type *push (void) |
| 691 | { |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 692 | if (unlikely (!resize (len + 1))) |
Behdad Esfahbod | f751576 | 2018-06-01 17:48:37 -0700 | [diff] [blame] | 693 | return &Crap(Type); |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 694 | return &arrayZ[len - 1]; |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 695 | } |
Behdad Esfahbod | 65aeabd | 2018-05-23 16:15:28 -0700 | [diff] [blame] | 696 | inline Type *push (const Type& v) |
| 697 | { |
Behdad Esfahbod | f751576 | 2018-06-01 17:48:37 -0700 | [diff] [blame] | 698 | Type *p = push (); |
| 699 | *p = v; |
| 700 | return p; |
Behdad Esfahbod | 65aeabd | 2018-05-23 16:15:28 -0700 | [diff] [blame] | 701 | } |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 702 | |
Behdad Esfahbod | af27450 | 2018-02-10 13:25:49 -0600 | [diff] [blame] | 703 | /* Allocate for size but don't adjust len. */ |
Behdad Esfahbod | 65aeabd | 2018-05-23 16:15:28 -0700 | [diff] [blame] | 704 | inline bool alloc (unsigned int size) |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 705 | { |
Behdad Esfahbod | 975bdd5 | 2018-06-01 17:37:13 -0700 | [diff] [blame] | 706 | if (unlikely (!successful)) |
| 707 | return false; |
| 708 | |
Behdad Esfahbod | af27450 | 2018-02-10 13:25:49 -0600 | [diff] [blame] | 709 | if (likely (size <= allocated)) |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 710 | return true; |
Behdad Esfahbod | af27450 | 2018-02-10 13:25:49 -0600 | [diff] [blame] | 711 | |
| 712 | /* Reallocate */ |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 713 | |
| 714 | unsigned int new_allocated = allocated; |
| 715 | while (size >= new_allocated) |
| 716 | new_allocated += (new_allocated >> 1) + 8; |
| 717 | |
| 718 | Type *new_array = nullptr; |
| 719 | |
Behdad Esfahbod | 1ab3c3e | 2018-06-01 17:34:24 -0700 | [diff] [blame] | 720 | if (arrayZ == static_array) |
| 721 | { |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 722 | new_array = (Type *) calloc (new_allocated, sizeof (Type)); |
| 723 | if (new_array) |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 724 | memcpy (new_array, arrayZ, len * sizeof (Type)); |
Behdad Esfahbod | 1ab3c3e | 2018-06-01 17:34:24 -0700 | [diff] [blame] | 725 | } |
| 726 | else |
| 727 | { |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 728 | bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type)); |
Behdad Esfahbod | 1ab3c3e | 2018-06-01 17:34:24 -0700 | [diff] [blame] | 729 | if (likely (!overflows)) |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 730 | new_array = (Type *) realloc (arrayZ, new_allocated * sizeof (Type)); |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | if (unlikely (!new_array)) |
Behdad Esfahbod | 975bdd5 | 2018-06-01 17:37:13 -0700 | [diff] [blame] | 734 | { |
| 735 | successful = false; |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 736 | return false; |
Behdad Esfahbod | 975bdd5 | 2018-06-01 17:37:13 -0700 | [diff] [blame] | 737 | } |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 738 | |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 739 | arrayZ = new_array; |
Rod Sheeter | 4816064 | 2018-02-09 17:14:37 -0800 | [diff] [blame] | 740 | allocated = new_allocated; |
Behdad Esfahbod | af27450 | 2018-02-10 13:25:49 -0600 | [diff] [blame] | 741 | |
Rod Sheeter | 4816064 | 2018-02-09 17:14:37 -0800 | [diff] [blame] | 742 | return true; |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Behdad Esfahbod | 31c4236 | 2018-05-24 13:38:46 -0700 | [diff] [blame] | 745 | inline bool resize (int size_) |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 746 | { |
Behdad Esfahbod | 31c4236 | 2018-05-24 13:38:46 -0700 | [diff] [blame] | 747 | unsigned int size = size_ < 0 ? 0u : (unsigned int) size_; |
Behdad Esfahbod | af27450 | 2018-02-10 13:25:49 -0600 | [diff] [blame] | 748 | if (!alloc (size)) |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 749 | return false; |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 750 | |
Behdad Esfahbod | c4d0d11 | 2018-06-11 22:11:45 -0400 | [diff] [blame] | 751 | if (size > len) |
| 752 | memset (arrayZ + len, 0, (size - len) * sizeof (*arrayZ)); |
| 753 | |
Behdad Esfahbod | deed4a4 | 2017-10-15 16:53:09 +0200 | [diff] [blame] | 754 | len = size; |
| 755 | return true; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | inline void pop (void) |
| 759 | { |
Behdad Esfahbod | 31c4236 | 2018-05-24 13:38:46 -0700 | [diff] [blame] | 760 | if (!len) return; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 761 | len--; |
Behdad Esfahbod | 639afdc | 2013-08-06 14:28:12 -0400 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | inline void remove (unsigned int i) |
| 765 | { |
| 766 | if (unlikely (i >= len)) |
| 767 | return; |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 768 | memmove (static_cast<void *> (&arrayZ[i]), |
| 769 | static_cast<void *> (&arrayZ[i + 1]), |
Behdad Esfahbod | 639afdc | 2013-08-06 14:28:12 -0400 | [diff] [blame] | 770 | (len - i - 1) * sizeof (Type)); |
| 771 | len--; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 772 | } |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 773 | |
Behdad Esfahbod | 31c4236 | 2018-05-24 13:38:46 -0700 | [diff] [blame] | 774 | inline void shrink (int size_) |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 775 | { |
Behdad Esfahbod | 31c4236 | 2018-05-24 13:38:46 -0700 | [diff] [blame] | 776 | unsigned int size = size_ < 0 ? 0u : (unsigned int) size_; |
| 777 | if (size < len) |
| 778 | len = size; |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 779 | } |
| 780 | |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 781 | template <typename T> |
| 782 | inline Type *find (T v) { |
| 783 | for (unsigned int i = 0; i < len; i++) |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 784 | if (arrayZ[i] == v) |
| 785 | return &arrayZ[i]; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 786 | return nullptr; |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 787 | } |
| 788 | template <typename T> |
| 789 | inline const Type *find (T v) const { |
| 790 | for (unsigned int i = 0; i < len; i++) |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 791 | if (arrayZ[i] == v) |
| 792 | return &arrayZ[i]; |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 793 | return nullptr; |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 794 | } |
| 795 | |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 796 | inline void qsort (int (*cmp)(const void*, const void*)) |
| 797 | { |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 798 | ::qsort (arrayZ, len, sizeof (Type), cmp); |
Rod Sheeter | 59c658c | 2018-02-08 19:22:47 -0800 | [diff] [blame] | 799 | } |
| 800 | |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 801 | inline void qsort (void) |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 802 | { |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 803 | ::qsort (arrayZ, len, sizeof (Type), Type::cmp); |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 804 | } |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 805 | |
Behdad Esfahbod | fb8cc86 | 2014-06-19 15:30:18 -0400 | [diff] [blame] | 806 | inline void qsort (unsigned int start, unsigned int end) |
Behdad Esfahbod | b70c96d | 2011-07-07 21:07:41 -0400 | [diff] [blame] | 807 | { |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 808 | ::qsort (arrayZ + start, end - start, sizeof (Type), Type::cmp); |
Behdad Esfahbod | b70c96d | 2011-07-07 21:07:41 -0400 | [diff] [blame] | 809 | } |
| 810 | |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 811 | template <typename T> |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 812 | inline Type *lsearch (const T &x) |
Behdad Esfahbod | 6804b61 | 2018-02-07 13:47:35 -0500 | [diff] [blame] | 813 | { |
| 814 | for (unsigned int i = 0; i < len; i++) |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 815 | if (0 == this->arrayZ[i].cmp (&x)) |
| 816 | return &arrayZ[i]; |
Behdad Esfahbod | 6804b61 | 2018-02-07 13:47:35 -0500 | [diff] [blame] | 817 | return nullptr; |
| 818 | } |
| 819 | |
| 820 | template <typename T> |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 821 | inline Type *bsearch (const T &x) |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 822 | { |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 823 | unsigned int i; |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 824 | return bfind (x, &i) ? &arrayZ[i] : nullptr; |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 825 | } |
| 826 | template <typename T> |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 827 | inline const Type *bsearch (const T &x) const |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 828 | { |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 829 | unsigned int i; |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 830 | return bfind (x, &i) ? &arrayZ[i] : nullptr; |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 831 | } |
| 832 | template <typename T> |
Behdad Esfahbod | c479a59 | 2018-02-07 21:13:10 -0600 | [diff] [blame] | 833 | inline bool bfind (const T &x, unsigned int *i) const |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 834 | { |
Behdad Esfahbod | db5f7ef | 2017-10-15 16:00:22 +0200 | [diff] [blame] | 835 | int min = 0, max = (int) this->len - 1; |
| 836 | while (min <= max) |
| 837 | { |
| 838 | int mid = (min + max) / 2; |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 839 | int c = this->arrayZ[mid].cmp (&x); |
Behdad Esfahbod | db5f7ef | 2017-10-15 16:00:22 +0200 | [diff] [blame] | 840 | if (c < 0) |
| 841 | max = mid - 1; |
| 842 | else if (c > 0) |
| 843 | min = mid + 1; |
| 844 | else |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 845 | { |
| 846 | *i = mid; |
| 847 | return true; |
| 848 | } |
Behdad Esfahbod | db5f7ef | 2017-10-15 16:00:22 +0200 | [diff] [blame] | 849 | } |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 850 | if (max < 0 || (max < (int) this->len && this->arrayZ[max].cmp (&x) > 0)) |
Behdad Esfahbod | 5e74044 | 2017-10-15 16:15:06 +0200 | [diff] [blame] | 851 | max++; |
| 852 | *i = max; |
| 853 | return false; |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 854 | } |
Behdad Esfahbod | 6a7ac79 | 2011-05-11 14:19:18 -0400 | [diff] [blame] | 855 | |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 856 | inline void fini (void) |
Behdad Esfahbod | 6a7ac79 | 2011-05-11 14:19:18 -0400 | [diff] [blame] | 857 | { |
Behdad Esfahbod | 63f57f4 | 2018-05-08 16:56:11 -0700 | [diff] [blame] | 858 | if (arrayZ != static_array) |
| 859 | free (arrayZ); |
| 860 | arrayZ = nullptr; |
Behdad Esfahbod | 6a7ac79 | 2011-05-11 14:19:18 -0400 | [diff] [blame] | 861 | allocated = len = 0; |
| 862 | } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 863 | }; |
| 864 | |
Behdad Esfahbod | c5d91f3 | 2013-03-09 04:34:21 -0500 | [diff] [blame] | 865 | template <typename Type> |
Behdad Esfahbod | 4a01eb1 | 2018-05-01 19:05:58 -0400 | [diff] [blame] | 866 | struct hb_auto_t : Type |
Behdad Esfahbod | c5d91f3 | 2013-03-09 04:34:21 -0500 | [diff] [blame] | 867 | { |
Behdad Esfahbod | 4a01eb1 | 2018-05-01 19:05:58 -0400 | [diff] [blame] | 868 | hb_auto_t (void) { Type::init (); } |
| 869 | ~hb_auto_t (void) { Type::fini (); } |
| 870 | private: /* Hide */ |
| 871 | void init (void) {} |
| 872 | void fini (void) {} |
Behdad Esfahbod | c5d91f3 | 2013-03-09 04:34:21 -0500 | [diff] [blame] | 873 | }; |
Behdad Esfahbod | 4a01eb1 | 2018-05-01 19:05:58 -0400 | [diff] [blame] | 874 | template <typename Type> |
Behdad Esfahbod | 5c3112a | 2018-05-01 19:07:04 -0400 | [diff] [blame] | 875 | struct hb_auto_array_t : hb_auto_t <hb_vector_t <Type> > {}; |
Behdad Esfahbod | c5d91f3 | 2013-03-09 04:34:21 -0500 | [diff] [blame] | 876 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 877 | |
Behdad Esfahbod | 39f11d8 | 2018-07-10 14:01:39 +0200 | [diff] [blame^] | 878 | #define HB_LOCKABLE_SET_INIT {HB_VECTOR_INIT} |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 879 | template <typename item_t, typename lock_t> |
| 880 | struct hb_lockable_set_t |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 881 | { |
Behdad Esfahbod | 5c3112a | 2018-05-01 19:07:04 -0400 | [diff] [blame] | 882 | hb_vector_t <item_t, 1> items; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 883 | |
Behdad Esfahbod | bf93b63 | 2012-06-05 14:17:32 -0400 | [diff] [blame] | 884 | inline void init (void) { items.init (); } |
| 885 | |
Behdad Esfahbod | 478a425 | 2011-05-05 12:39:51 -0400 | [diff] [blame] | 886 | template <typename T> |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 887 | inline item_t *replace_or_insert (T v, lock_t &l, bool replace) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 888 | { |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 889 | l.lock (); |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 890 | item_t *item = items.find (v); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 891 | if (item) { |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 892 | if (replace) { |
| 893 | item_t old = *item; |
| 894 | *item = v; |
| 895 | l.unlock (); |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 896 | old.fini (); |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 897 | } |
| 898 | else { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 899 | item = nullptr; |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 900 | l.unlock (); |
| 901 | } |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 902 | } else { |
Behdad Esfahbod | 65aeabd | 2018-05-23 16:15:28 -0700 | [diff] [blame] | 903 | item = items.push (v); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 904 | l.unlock (); |
| 905 | } |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 906 | return item; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 907 | } |
| 908 | |
Behdad Esfahbod | 811482b | 2011-05-05 13:21:04 -0400 | [diff] [blame] | 909 | template <typename T> |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 910 | inline void remove (T v, lock_t &l) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 911 | { |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 912 | l.lock (); |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 913 | item_t *item = items.find (v); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 914 | if (item) { |
| 915 | item_t old = *item; |
| 916 | *item = items[items.len - 1]; |
| 917 | items.pop (); |
| 918 | l.unlock (); |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 919 | old.fini (); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 920 | } else { |
| 921 | l.unlock (); |
| 922 | } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 923 | } |
| 924 | |
Behdad Esfahbod | 478a425 | 2011-05-05 12:39:51 -0400 | [diff] [blame] | 925 | template <typename T> |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 926 | inline bool find (T v, item_t *i, lock_t &l) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 927 | { |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 928 | l.lock (); |
| 929 | item_t *item = items.find (v); |
| 930 | if (item) |
| 931 | *i = *item; |
| 932 | l.unlock (); |
| 933 | return !!item; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 934 | } |
| 935 | |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 936 | template <typename T> |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 937 | inline item_t *find_or_insert (T v, lock_t &l) |
| 938 | { |
| 939 | l.lock (); |
| 940 | item_t *item = items.find (v); |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 941 | if (!item) { |
Behdad Esfahbod | 65aeabd | 2018-05-23 16:15:28 -0700 | [diff] [blame] | 942 | item = items.push (v); |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 943 | } |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 944 | l.unlock (); |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 945 | return item; |
| 946 | } |
| 947 | |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 948 | inline void fini (lock_t &l) |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 949 | { |
Behdad Esfahbod | 3f4764b | 2012-07-30 10:06:42 -0400 | [diff] [blame] | 950 | if (!items.len) { |
| 951 | /* No need for locking. */ |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 952 | items.fini (); |
Behdad Esfahbod | 3f4764b | 2012-07-30 10:06:42 -0400 | [diff] [blame] | 953 | return; |
| 954 | } |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 955 | l.lock (); |
| 956 | while (items.len) { |
| 957 | item_t old = items[items.len - 1]; |
| 958 | items.pop (); |
| 959 | l.unlock (); |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 960 | old.fini (); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 961 | l.lock (); |
| 962 | } |
Behdad Esfahbod | a60ba79 | 2018-05-01 19:01:25 -0400 | [diff] [blame] | 963 | items.fini (); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 964 | l.unlock (); |
| 965 | } |
| 966 | |
| 967 | }; |
| 968 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 969 | |
Behdad Esfahbod | 4188096 | 2011-04-11 14:58:28 -0400 | [diff] [blame] | 970 | /* ASCII tag/character handling */ |
Behdad Esfahbod | db5227c | 2011-04-11 13:16:08 -0400 | [diff] [blame] | 971 | |
Behdad Esfahbod | 20b817a | 2013-02-27 18:39:37 -0500 | [diff] [blame] | 972 | static inline bool ISALPHA (unsigned char c) |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 973 | { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } |
Behdad Esfahbod | 20b817a | 2013-02-27 18:39:37 -0500 | [diff] [blame] | 974 | static inline bool ISALNUM (unsigned char c) |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 975 | { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } |
Behdad Esfahbod | 20b817a | 2013-02-27 18:39:37 -0500 | [diff] [blame] | 976 | static inline bool ISSPACE (unsigned char c) |
| 977 | { return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; } |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 978 | static inline unsigned char TOUPPER (unsigned char c) |
| 979 | { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; } |
| 980 | static inline unsigned char TOLOWER (unsigned char c) |
| 981 | { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; } |
Behdad Esfahbod | db5227c | 2011-04-11 13:16:08 -0400 | [diff] [blame] | 982 | |
| 983 | |
Behdad Esfahbod | 91dd115 | 2016-02-25 13:56:47 +0900 | [diff] [blame] | 984 | /* HB_NDEBUG disables some sanity checks that are very safe to disable and |
| 985 | * should be disabled in production systems. If NDEBUG is defined, enable |
| 986 | * HB_NDEBUG; but if it's desirable that normal assert()s (which are very |
| 987 | * light-weight) to be enabled, then HB_DEBUG can be defined to disable |
| 988 | * the costlier checks. */ |
| 989 | #ifdef NDEBUG |
Behdad Esfahbod | bd021a6 | 2018-05-08 01:50:30 -0700 | [diff] [blame] | 990 | #define HB_NDEBUG 1 |
Behdad Esfahbod | 91dd115 | 2016-02-25 13:56:47 +0900 | [diff] [blame] | 991 | #endif |
| 992 | |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 993 | |
| 994 | /* Misc */ |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 995 | |
Behdad Esfahbod | a8b89a0 | 2014-07-11 14:18:01 -0400 | [diff] [blame] | 996 | template <typename T> class hb_assert_unsigned_t; |
| 997 | template <> class hb_assert_unsigned_t<unsigned char> {}; |
Dominik Röttsches | 9e7c720 | 2014-07-17 14:40:34 +0300 | [diff] [blame] | 998 | template <> class hb_assert_unsigned_t<unsigned short> {}; |
Behdad Esfahbod | a8b89a0 | 2014-07-11 14:18:01 -0400 | [diff] [blame] | 999 | template <> class hb_assert_unsigned_t<unsigned int> {}; |
| 1000 | template <> class hb_assert_unsigned_t<unsigned long> {}; |
Behdad Esfahbod | f60f216 | 2010-04-21 02:12:45 -0400 | [diff] [blame] | 1001 | |
Behdad Esfahbod | b5aeb95 | 2012-07-13 09:45:54 -0400 | [diff] [blame] | 1002 | template <typename T> static inline bool |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 1003 | hb_in_range (T u, T lo, T hi) |
Behdad Esfahbod | 7b08b0a | 2011-07-20 23:59:07 -0400 | [diff] [blame] | 1004 | { |
Behdad Esfahbod | 385cf37 | 2014-07-17 18:22:07 -0400 | [diff] [blame] | 1005 | /* The sizeof() is here to force template instantiation. |
| 1006 | * I'm sure there are better ways to do this but can't think of |
| 1007 | * one right now. Declaring a variable won't work as HB_UNUSED |
Behdad Esfahbod | 68e04af | 2015-02-21 16:30:28 +0300 | [diff] [blame] | 1008 | * is unusable on some platforms and unused types are less likely |
Behdad Esfahbod | 385cf37 | 2014-07-17 18:22:07 -0400 | [diff] [blame] | 1009 | * to generate a warning than unused variables. */ |
Behdad Esfahbod | c3448e8 | 2017-10-15 12:02:00 +0200 | [diff] [blame] | 1010 | static_assert ((sizeof (hb_assert_unsigned_t<T>) >= 0), ""); |
Behdad Esfahbod | 385cf37 | 2014-07-17 18:22:07 -0400 | [diff] [blame] | 1011 | |
Behdad Esfahbod | c2b151d | 2014-08-10 18:52:07 -0400 | [diff] [blame] | 1012 | /* The casts below are important as if T is smaller than int, |
| 1013 | * the subtract results will become a signed int! */ |
| 1014 | return (T)(u - lo) <= (T)(hi - lo); |
Behdad Esfahbod | 7b08b0a | 2011-07-20 23:59:07 -0400 | [diff] [blame] | 1015 | } |
| 1016 | |
Behdad Esfahbod | 4a7f4f3 | 2012-07-23 13:15:33 -0400 | [diff] [blame] | 1017 | template <typename T> static inline bool |
Behdad Esfahbod | c98b718 | 2013-12-31 15:55:40 +0800 | [diff] [blame] | 1018 | hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2) |
| 1019 | { |
| 1020 | return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2); |
| 1021 | } |
| 1022 | |
| 1023 | template <typename T> static inline bool |
Behdad Esfahbod | 093cd58 | 2012-07-23 14:04:42 -0400 | [diff] [blame] | 1024 | hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3) |
Behdad Esfahbod | 4a7f4f3 | 2012-07-23 13:15:33 -0400 | [diff] [blame] | 1025 | { |
Behdad Esfahbod | 093cd58 | 2012-07-23 14:04:42 -0400 | [diff] [blame] | 1026 | return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3); |
Behdad Esfahbod | 4a7f4f3 | 2012-07-23 13:15:33 -0400 | [diff] [blame] | 1027 | } |
| 1028 | |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 1029 | |
Behdad Esfahbod | aa7044d | 2015-11-04 16:25:57 -0800 | [diff] [blame] | 1030 | /* Enable bitwise ops on enums marked as flags_t */ |
Behdad Esfahbod | 6986208 | 2015-11-04 18:46:22 -0800 | [diff] [blame] | 1031 | /* To my surprise, looks like the function resolver is happy to silently cast |
| 1032 | * one enum to another... So this doesn't provide the type-checking that I |
Behdad Esfahbod | e0082ae | 2015-11-17 18:42:13 -0800 | [diff] [blame] | 1033 | * originally had in mind... :(. |
| 1034 | * |
ebraminio | 7c6937e | 2017-11-20 14:49:22 -0500 | [diff] [blame] | 1035 | * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163 |
Behdad Esfahbod | e0082ae | 2015-11-17 18:42:13 -0800 | [diff] [blame] | 1036 | */ |
| 1037 | #ifdef _MSC_VER |
| 1038 | # pragma warning(disable:4200) |
| 1039 | # pragma warning(disable:4800) |
Chun-wei Fan | 167c327 | 2015-11-09 17:17:56 +0800 | [diff] [blame] | 1040 | #endif |
Behdad Esfahbod | 1dc32ea | 2015-11-20 13:24:19 -0800 | [diff] [blame] | 1041 | #define HB_MARK_AS_FLAG_T(T) \ |
| 1042 | extern "C++" { \ |
| 1043 | static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \ |
| 1044 | static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \ |
| 1045 | static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \ |
| 1046 | static inline T operator ~ (T r) { return T (~(unsigned int) r); } \ |
| 1047 | static inline T& operator |= (T &l, T r) { l = l | r; return l; } \ |
| 1048 | static inline T& operator &= (T& l, T r) { l = l & r; return l; } \ |
| 1049 | static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \ |
| 1050 | } |
Behdad Esfahbod | aa7044d | 2015-11-04 16:25:57 -0800 | [diff] [blame] | 1051 | |
| 1052 | |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 1053 | /* Useful for set-operations on small enums. |
| 1054 | * For example, for testing "x ∈ {x1, x2, x3}" use: |
Behdad Esfahbod | 6058f98 | 2017-10-19 11:39:52 -0700 | [diff] [blame] | 1055 | * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 1056 | */ |
Behdad Esfahbod | d8adaa9 | 2017-10-20 13:57:43 -0400 | [diff] [blame] | 1057 | #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned int)(x) < 32) + (1U << (unsigned int)(x))) |
| 1058 | #define FLAG_UNSAFE(x) ((unsigned int)(x) < 32 ? (1U << (unsigned int)(x)) : 0) |
Behdad Esfahbod | 2c372b8 | 2012-07-20 13:37:48 -0400 | [diff] [blame] | 1059 | #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x)) |
Behdad Esfahbod | 7b08b0a | 2011-07-20 23:59:07 -0400 | [diff] [blame] | 1060 | |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 1061 | |
Behdad Esfahbod | c1e8744 | 2014-10-01 11:07:08 -0400 | [diff] [blame] | 1062 | template <typename T, typename T2> static inline void |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 1063 | hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2) |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 1064 | { |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 1065 | for (unsigned int i = 1; i < len; i++) |
| 1066 | { |
| 1067 | unsigned int j = i; |
| 1068 | while (j && compar (&array[j - 1], &array[i]) > 0) |
| 1069 | j--; |
| 1070 | if (i == j) |
| 1071 | continue; |
| 1072 | /* Move item i to occupy place for item j, shift what's in between. */ |
| 1073 | { |
Behdad Esfahbod | 9309974 | 2015-09-01 16:11:27 +0100 | [diff] [blame] | 1074 | T t = array[i]; |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 1075 | memmove (&array[j + 1], &array[j], (i - j) * sizeof (T)); |
| 1076 | array[j] = t; |
| 1077 | } |
| 1078 | if (array2) |
| 1079 | { |
Behdad Esfahbod | 9309974 | 2015-09-01 16:11:27 +0100 | [diff] [blame] | 1080 | T2 t = array2[i]; |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 1081 | memmove (&array2[j + 1], &array2[j], (i - j) * sizeof (T2)); |
| 1082 | array2[j] = t; |
| 1083 | } |
| 1084 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 1085 | } |
| 1086 | |
Behdad Esfahbod | 250398b | 2014-10-01 11:28:01 -0400 | [diff] [blame] | 1087 | template <typename T> static inline void |
Behdad Esfahbod | 85846b3 | 2015-09-01 15:07:52 +0100 | [diff] [blame] | 1088 | hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *)) |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 1089 | { |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 1090 | hb_stable_sort (array, len, compar, (int *) nullptr); |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 1091 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 1092 | |
Behdad Esfahbod | 6f3a300 | 2012-08-07 22:13:25 -0400 | [diff] [blame] | 1093 | static inline hb_bool_t |
| 1094 | hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out) |
| 1095 | { |
| 1096 | /* Pain because we don't know whether s is nul-terminated. */ |
| 1097 | char buf[64]; |
Behdad Esfahbod | 847794e | 2013-02-27 17:59:28 -0500 | [diff] [blame] | 1098 | len = MIN (ARRAY_LENGTH (buf) - 1, len); |
| 1099 | strncpy (buf, s, len); |
| 1100 | buf[len] = '\0'; |
Behdad Esfahbod | 6f3a300 | 2012-08-07 22:13:25 -0400 | [diff] [blame] | 1101 | |
| 1102 | char *end; |
| 1103 | errno = 0; |
| 1104 | unsigned long v = strtoul (buf, &end, base); |
| 1105 | if (errno) return false; |
| 1106 | if (*end) return false; |
| 1107 | *out = v; |
| 1108 | return true; |
| 1109 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 1110 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 1111 | |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 1112 | /* Vectorization */ |
| 1113 | |
| 1114 | struct HbOpOr |
| 1115 | { |
| 1116 | static const bool passthru_left = true; |
| 1117 | static const bool passthru_right = true; |
| 1118 | template <typename T> static void process (T &o, const T &a, const T &b) { o = a | b; } |
| 1119 | }; |
| 1120 | struct HbOpAnd |
| 1121 | { |
| 1122 | static const bool passthru_left = false; |
| 1123 | static const bool passthru_right = false; |
| 1124 | template <typename T> static void process (T &o, const T &a, const T &b) { o = a & b; } |
| 1125 | }; |
| 1126 | struct HbOpMinus |
| 1127 | { |
| 1128 | static const bool passthru_left = true; |
| 1129 | static const bool passthru_right = false; |
| 1130 | template <typename T> static void process (T &o, const T &a, const T &b) { o = a & ~b; } |
| 1131 | }; |
| 1132 | struct HbOpXor |
| 1133 | { |
| 1134 | static const bool passthru_left = true; |
| 1135 | static const bool passthru_right = true; |
| 1136 | template <typename T> static void process (T &o, const T &a, const T &b) { o = a ^ b; } |
| 1137 | }; |
| 1138 | |
Behdad Esfahbod | b995b50 | 2018-05-22 21:06:22 -0700 | [diff] [blame] | 1139 | |
| 1140 | /* Compiler-assisted vectorization. */ |
| 1141 | |
Behdad Esfahbod | 1ebaa09 | 2018-07-05 14:04:13 +0430 | [diff] [blame] | 1142 | /* |
| 1143 | * Disable vectorization for now. To correctly use them, we should |
| 1144 | * use posix_memalign() to allocate them. Otherwise, can cause |
| 1145 | * misaligned access. |
| 1146 | * |
| 1147 | * https://bugs.chromium.org/p/chromium/issues/detail?id=860184 |
| 1148 | */ |
| 1149 | #if !defined(HB_VECTOR_SIZE) |
| 1150 | # define HB_VECTOR_SIZE 0 |
| 1151 | #endif |
| 1152 | |
| 1153 | |
Behdad Esfahbod | b995b50 | 2018-05-22 21:06:22 -0700 | [diff] [blame] | 1154 | /* The `vector_size' attribute was introduced in gcc 3.1. */ |
Behdad Esfahbod | 058708a | 2018-07-05 13:16:00 +0430 | [diff] [blame] | 1155 | #if !defined(HB_VECTOR_SIZE) |
| 1156 | # if defined( __GNUC__ ) && ( __GNUC__ >= 4 ) |
| 1157 | # define HB_VECTOR_SIZE 128 |
| 1158 | # else |
| 1159 | # define HB_VECTOR_SIZE 0 |
| 1160 | # endif |
Behdad Esfahbod | b995b50 | 2018-05-22 21:06:22 -0700 | [diff] [blame] | 1161 | #endif |
| 1162 | |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 1163 | /* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))). */ |
| 1164 | template <typename elt_t, unsigned int byte_size> |
| 1165 | struct hb_vector_size_t |
| 1166 | { |
Behdad Esfahbod | f56a250 | 2018-05-22 21:36:07 -0700 | [diff] [blame] | 1167 | elt_t& operator [] (unsigned int i) { return u.v[i]; } |
| 1168 | const elt_t& operator [] (unsigned int i) const { return u.v[i]; } |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 1169 | |
| 1170 | template <class Op> |
| 1171 | inline hb_vector_size_t process (const hb_vector_size_t &o) const |
| 1172 | { |
| 1173 | hb_vector_size_t r; |
Behdad Esfahbod | 6c818c5 | 2018-05-22 22:00:05 -0700 | [diff] [blame] | 1174 | #if HB_VECTOR_SIZE |
Behdad Esfahbod | f56a250 | 2018-05-22 21:36:07 -0700 | [diff] [blame] | 1175 | if (HB_VECTOR_SIZE && 0 == (byte_size * 8) % HB_VECTOR_SIZE) |
| 1176 | for (unsigned int i = 0; i < ARRAY_LENGTH (u.vec); i++) |
| 1177 | Op::process (r.u.vec[i], u.vec[i], o.u.vec[i]); |
Behdad Esfahbod | b995b50 | 2018-05-22 21:06:22 -0700 | [diff] [blame] | 1178 | else |
Behdad Esfahbod | f56a250 | 2018-05-22 21:36:07 -0700 | [diff] [blame] | 1179 | #endif |
| 1180 | for (unsigned int i = 0; i < ARRAY_LENGTH (u.v); i++) |
| 1181 | Op::process (r.u.v[i], u.v[i], o.u.v[i]); |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 1182 | return r; |
| 1183 | } |
| 1184 | inline hb_vector_size_t operator | (const hb_vector_size_t &o) const |
| 1185 | { return process<HbOpOr> (o); } |
| 1186 | inline hb_vector_size_t operator & (const hb_vector_size_t &o) const |
| 1187 | { return process<HbOpAnd> (o); } |
| 1188 | inline hb_vector_size_t operator ^ (const hb_vector_size_t &o) const |
| 1189 | { return process<HbOpXor> (o); } |
| 1190 | inline hb_vector_size_t operator ~ () const |
| 1191 | { |
| 1192 | hb_vector_size_t r; |
Behdad Esfahbod | f56a250 | 2018-05-22 21:36:07 -0700 | [diff] [blame] | 1193 | #if HB_VECTOR_SIZE && 0 |
| 1194 | if (HB_VECTOR_SIZE && 0 == (byte_size * 8) % HB_VECTOR_SIZE) |
| 1195 | for (unsigned int i = 0; i < ARRAY_LENGTH (u.vec); i++) |
| 1196 | r.u.vec[i] = ~u.vec[i]; |
| 1197 | else |
| 1198 | #endif |
| 1199 | for (unsigned int i = 0; i < ARRAY_LENGTH (u.v); i++) |
| 1200 | r.u.v[i] = ~u.v[i]; |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 1201 | return r; |
| 1202 | } |
| 1203 | |
| 1204 | private: |
Behdad Esfahbod | 221ce6c | 2017-10-15 17:58:58 -0400 | [diff] [blame] | 1205 | static_assert (byte_size / sizeof (elt_t) * sizeof (elt_t) == byte_size, ""); |
Behdad Esfahbod | b995b50 | 2018-05-22 21:06:22 -0700 | [diff] [blame] | 1206 | union { |
| 1207 | elt_t v[byte_size / sizeof (elt_t)]; |
Behdad Esfahbod | f56a250 | 2018-05-22 21:36:07 -0700 | [diff] [blame] | 1208 | #if HB_VECTOR_SIZE |
Behdad Esfahbod | 4ca211b | 2018-06-01 17:18:57 -0700 | [diff] [blame] | 1209 | typedef unsigned long vec_t __attribute__((vector_size (HB_VECTOR_SIZE / 8))); |
Behdad Esfahbod | f56a250 | 2018-05-22 21:36:07 -0700 | [diff] [blame] | 1210 | vec_t vec[byte_size / sizeof (vec_t)]; |
| 1211 | #endif |
| 1212 | } u; |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 1213 | }; |
| 1214 | |
Behdad Esfahbod | 7737e87 | 2017-10-15 16:21:03 -0400 | [diff] [blame] | 1215 | |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 1216 | /* Global runtime options. */ |
| 1217 | |
| 1218 | struct hb_options_t |
| 1219 | { |
Behdad Esfahbod | 5c87120 | 2014-10-14 20:07:31 -0700 | [diff] [blame] | 1220 | unsigned int initialized : 1; |
| 1221 | unsigned int uniscribe_bug_compatible : 1; |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 1222 | }; |
| 1223 | |
| 1224 | union hb_options_union_t { |
Behdad Esfahbod | 5c87120 | 2014-10-14 20:07:31 -0700 | [diff] [blame] | 1225 | unsigned int i; |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 1226 | hb_options_t opts; |
| 1227 | }; |
Behdad Esfahbod | c3448e8 | 2017-10-15 12:02:00 +0200 | [diff] [blame] | 1228 | static_assert ((sizeof (int) == sizeof (hb_options_union_t)), ""); |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 1229 | |
| 1230 | HB_INTERNAL void |
| 1231 | _hb_options_init (void); |
| 1232 | |
| 1233 | extern HB_INTERNAL hb_options_union_t _hb_options; |
| 1234 | |
| 1235 | static inline hb_options_t |
| 1236 | hb_options (void) |
| 1237 | { |
| 1238 | if (unlikely (!_hb_options.i)) |
| 1239 | _hb_options_init (); |
| 1240 | |
| 1241 | return _hb_options.opts; |
| 1242 | } |
| 1243 | |
Steven R. Loomis | a13b023 | 2015-12-11 10:21:27 -0800 | [diff] [blame] | 1244 | /* Size signifying variable-sized array */ |
| 1245 | #define VAR 1 |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 1246 | |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 1247 | |
| 1248 | /* String type. */ |
| 1249 | |
Behdad Esfahbod | 08e280f | 2018-05-08 01:57:27 -0700 | [diff] [blame] | 1250 | struct hb_bytes_t |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 1251 | { |
Behdad Esfahbod | 08e280f | 2018-05-08 01:57:27 -0700 | [diff] [blame] | 1252 | inline hb_bytes_t (void) : bytes (nullptr), len (0) {} |
| 1253 | inline hb_bytes_t (const char *bytes_, unsigned int len_) : bytes (bytes_), len (len_) {} |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 1254 | |
Behdad Esfahbod | 08e280f | 2018-05-08 01:57:27 -0700 | [diff] [blame] | 1255 | inline int cmp (const hb_bytes_t &a) const |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 1256 | { |
Behdad Esfahbod | e35a763 | 2017-10-30 13:15:05 -0600 | [diff] [blame] | 1257 | if (len != a.len) |
| 1258 | return (int) a.len - (int) len; |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 1259 | |
Behdad Esfahbod | e35a763 | 2017-10-30 13:15:05 -0600 | [diff] [blame] | 1260 | return memcmp (a.bytes, bytes, len); |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 1261 | } |
| 1262 | static inline int cmp (const void *pa, const void *pb) |
| 1263 | { |
Behdad Esfahbod | 08e280f | 2018-05-08 01:57:27 -0700 | [diff] [blame] | 1264 | hb_bytes_t *a = (hb_bytes_t *) pa; |
| 1265 | hb_bytes_t *b = (hb_bytes_t *) pb; |
Behdad Esfahbod | e35a763 | 2017-10-30 13:15:05 -0600 | [diff] [blame] | 1266 | return b->cmp (*a); |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | const char *bytes; |
| 1270 | unsigned int len; |
| 1271 | }; |
| 1272 | |
| 1273 | |
Chun-wei Fan | 19256be | 2018-03-12 13:33:03 +0800 | [diff] [blame] | 1274 | /* fallback for round() */ |
Chun-wei Fan | 19256be | 2018-03-12 13:33:03 +0800 | [diff] [blame] | 1275 | static inline double |
Behdad Esfahbod | 01dff1e | 2018-06-26 18:00:58 -0400 | [diff] [blame] | 1276 | _hb_round (double x) |
Chun-wei Fan | 19256be | 2018-03-12 13:33:03 +0800 | [diff] [blame] | 1277 | { |
| 1278 | if (x >= 0) |
| 1279 | return floor (x + 0.5); |
| 1280 | else |
| 1281 | return ceil (x - 0.5); |
| 1282 | } |
Behdad Esfahbod | 292c100 | 2018-07-10 13:16:52 +0200 | [diff] [blame] | 1283 | #if !defined (HAVE_ROUND) && !defined (HAVE_DECL_ROUND) |
Behdad Esfahbod | 01dff1e | 2018-06-26 18:00:58 -0400 | [diff] [blame] | 1284 | #define round(x) _hb_round(x) |
Chun-wei Fan | 19256be | 2018-03-12 13:33:03 +0800 | [diff] [blame] | 1285 | #endif |
| 1286 | |
| 1287 | |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 1288 | /* fallback for posix_memalign() */ |
| 1289 | static inline int |
| 1290 | _hb_memalign(void **memptr, size_t alignment, size_t size) |
| 1291 | { |
| 1292 | if (unlikely (!_hb_ispow2 (alignment) || |
| 1293 | !alignment || |
| 1294 | 0 != (alignment & (sizeof (void *) - 1)))) |
| 1295 | return EINVAL; |
| 1296 | |
| 1297 | char *p = (char *) malloc (size + alignment - 1); |
| 1298 | if (unlikely (!p)) |
| 1299 | return ENOMEM; |
| 1300 | |
| 1301 | size_t off = (size_t) p & (alignment - 1); |
| 1302 | if (off) |
| 1303 | p += alignment - off; |
| 1304 | |
| 1305 | *memptr = (void *) p; |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | #if !defined(posix_memalign) && !defined(HAVE_POSIX_MEMALIGN) |
| 1310 | #define posix_memalign _hb_memalign |
| 1311 | #endif |
| 1312 | |
| 1313 | |
| 1314 | |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 1315 | #endif /* HB_PRIVATE_HH */ |