Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012,2017 Google, Inc. |
| 3 | * Copyright © 2021 Behdad Esfahbod |
| 4 | * |
| 5 | * This is part of HarfBuzz, a text shaping library. |
| 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 | * Google Author(s): Behdad Esfahbod |
| 26 | */ |
| 27 | |
| 28 | #ifndef HB_BIT_PAGE_HH |
| 29 | #define HB_BIT_PAGE_HH |
| 30 | |
| 31 | #include "hb.hh" |
| 32 | |
Behdad Esfahbod | 43a4028 | 2022-11-18 15:54:34 -0700 | [diff] [blame] | 33 | |
| 34 | /* Compiler-assisted vectorization. */ |
| 35 | |
| 36 | /* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))), |
Behdad Esfahbod | 3e1c524 | 2023-01-10 17:18:34 -0700 | [diff] [blame] | 37 | * basically a fixed-size bitset. We can't use the compiler type because hb_vector_t cannot |
| 38 | * guarantee alignment requirements. */ |
Behdad Esfahbod | 43a4028 | 2022-11-18 15:54:34 -0700 | [diff] [blame] | 39 | template <typename elt_t, unsigned int byte_size> |
| 40 | struct hb_vector_size_t |
| 41 | { |
| 42 | elt_t& operator [] (unsigned int i) { return v[i]; } |
| 43 | const elt_t& operator [] (unsigned int i) const { return v[i]; } |
| 44 | |
Behdad Esfahbod | a180ae4 | 2023-01-16 13:06:30 -0700 | [diff] [blame] | 45 | void init0 () |
| 46 | { |
| 47 | for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++) |
| 48 | v[i] = 0; |
| 49 | } |
| 50 | void init1 () |
| 51 | { |
| 52 | for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++) |
| 53 | v[i] = (elt_t) -1; |
| 54 | } |
Behdad Esfahbod | 43a4028 | 2022-11-18 15:54:34 -0700 | [diff] [blame] | 55 | |
| 56 | template <typename Op> |
| 57 | hb_vector_size_t process (const Op& op) const |
| 58 | { |
| 59 | hb_vector_size_t r; |
| 60 | for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++) |
| 61 | r.v[i] = op (v[i]); |
| 62 | return r; |
| 63 | } |
| 64 | template <typename Op> |
| 65 | hb_vector_size_t process (const Op& op, const hb_vector_size_t &o) const |
| 66 | { |
| 67 | hb_vector_size_t r; |
| 68 | for (unsigned int i = 0; i < ARRAY_LENGTH (v); i++) |
| 69 | r.v[i] = op (v[i], o.v[i]); |
| 70 | return r; |
| 71 | } |
| 72 | hb_vector_size_t operator | (const hb_vector_size_t &o) const |
| 73 | { return process (hb_bitwise_or, o); } |
| 74 | hb_vector_size_t operator & (const hb_vector_size_t &o) const |
| 75 | { return process (hb_bitwise_and, o); } |
| 76 | hb_vector_size_t operator ^ (const hb_vector_size_t &o) const |
| 77 | { return process (hb_bitwise_xor, o); } |
| 78 | hb_vector_size_t operator ~ () const |
| 79 | { return process (hb_bitwise_neg); } |
| 80 | |
Behdad Esfahbod | 744eb6b | 2022-11-18 15:56:06 -0700 | [diff] [blame] | 81 | hb_array_t<const elt_t> iter () const |
| 82 | { return hb_array (v); } |
| 83 | |
Behdad Esfahbod | 43a4028 | 2022-11-18 15:54:34 -0700 | [diff] [blame] | 84 | private: |
| 85 | static_assert (0 == byte_size % sizeof (elt_t), ""); |
| 86 | elt_t v[byte_size / sizeof (elt_t)]; |
| 87 | }; |
| 88 | |
| 89 | |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 90 | struct hb_bit_page_t |
| 91 | { |
Behdad Esfahbod | 347c1f7 | 2023-07-09 09:33:04 -0600 | [diff] [blame] | 92 | void init0 () { v.init0 (); population = 0; } |
| 93 | void init1 () { v.init1 (); population = PAGE_BITS; } |
| 94 | |
| 95 | void dirty () { population = UINT_MAX; } |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 96 | |
Behdad Esfahbod | 92d5ec2 | 2023-01-17 05:59:19 -0700 | [diff] [blame] | 97 | static inline constexpr unsigned len () |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 98 | { return ARRAY_LENGTH_CONST (v); } |
| 99 | |
Behdad Esfahbod | 0340ba1 | 2023-09-05 14:56:09 +0300 | [diff] [blame] | 100 | operator bool () const { return !is_empty (); } |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 101 | bool is_empty () const |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 102 | { |
Behdad Esfahbod | 9b9a9c6 | 2023-07-09 09:55:00 -0600 | [diff] [blame] | 103 | if (has_population ()) return !population; |
Behdad Esfahbod | 744eb6b | 2022-11-18 15:56:06 -0700 | [diff] [blame] | 104 | return |
| 105 | + hb_iter (v) |
| 106 | | hb_none |
| 107 | ; |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 108 | } |
Behdad Esfahbod | 58f848d | 2022-05-19 15:42:54 -0600 | [diff] [blame] | 109 | uint32_t hash () const |
Behdad Esfahbod | 124f9ae | 2022-05-19 12:58:02 -0600 | [diff] [blame] | 110 | { |
Behdad Esfahbod | a58bbe5 | 2023-05-09 12:06:35 -0600 | [diff] [blame] | 111 | return hb_bytes_t ((const char *) &v, sizeof (v)).hash (); |
Behdad Esfahbod | 124f9ae | 2022-05-19 12:58:02 -0600 | [diff] [blame] | 112 | } |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 113 | |
Behdad Esfahbod | 347c1f7 | 2023-07-09 09:33:04 -0600 | [diff] [blame] | 114 | void add (hb_codepoint_t g) { elt (g) |= mask (g); dirty (); } |
| 115 | void del (hb_codepoint_t g) { elt (g) &= ~mask (g); dirty (); } |
Behdad Esfahbod | 53fd4c9 | 2022-07-20 13:33:49 -0600 | [diff] [blame] | 116 | void set (hb_codepoint_t g, bool value) { if (value) add (g); else del (g); } |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 117 | bool get (hb_codepoint_t g) const { return elt (g) & mask (g); } |
| 118 | |
| 119 | void add_range (hb_codepoint_t a, hb_codepoint_t b) |
| 120 | { |
| 121 | elt_t *la = &elt (a); |
| 122 | elt_t *lb = &elt (b); |
| 123 | if (la == lb) |
| 124 | *la |= (mask (b) << 1) - mask(a); |
| 125 | else |
| 126 | { |
Behdad Esfahbod | 0980e2b | 2023-07-09 15:13:57 -0600 | [diff] [blame] | 127 | *la |= ~(mask (a) - 1llu); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 128 | la++; |
| 129 | |
Behdad Esfahbod | ac0efaf | 2022-11-22 12:50:36 -0700 | [diff] [blame] | 130 | hb_memset (la, 0xff, (char *) lb - (char *) la); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 131 | |
Behdad Esfahbod | 0980e2b | 2023-07-09 15:13:57 -0600 | [diff] [blame] | 132 | *lb |= ((mask (b) << 1) - 1llu); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 133 | } |
Behdad Esfahbod | 347c1f7 | 2023-07-09 09:33:04 -0600 | [diff] [blame] | 134 | dirty (); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 135 | } |
| 136 | void del_range (hb_codepoint_t a, hb_codepoint_t b) |
| 137 | { |
| 138 | elt_t *la = &elt (a); |
| 139 | elt_t *lb = &elt (b); |
| 140 | if (la == lb) |
Behdad Esfahbod | 0980e2b | 2023-07-09 15:13:57 -0600 | [diff] [blame] | 141 | *la &= ~((mask (b) << 1llu) - mask(a)); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 142 | else |
| 143 | { |
| 144 | *la &= mask (a) - 1; |
| 145 | la++; |
| 146 | |
Behdad Esfahbod | ac0efaf | 2022-11-22 12:50:36 -0700 | [diff] [blame] | 147 | hb_memset (la, 0, (char *) lb - (char *) la); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 148 | |
Behdad Esfahbod | 0980e2b | 2023-07-09 15:13:57 -0600 | [diff] [blame] | 149 | *lb &= ~((mask (b) << 1) - 1llu); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 150 | } |
Behdad Esfahbod | 347c1f7 | 2023-07-09 09:33:04 -0600 | [diff] [blame] | 151 | dirty (); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 152 | } |
| 153 | void set_range (hb_codepoint_t a, hb_codepoint_t b, bool v) |
| 154 | { if (v) add_range (a, b); else del_range (a, b); } |
| 155 | |
Andrew John | 0182988 | 2022-03-25 08:36:44 -0700 | [diff] [blame] | 156 | |
| 157 | // Writes out page values to the array p. Returns the number of values |
| 158 | // written. At most size codepoints will be written. |
| 159 | unsigned int write (uint32_t base, |
| 160 | unsigned int start_value, |
| 161 | hb_codepoint_t *p, |
| 162 | unsigned int size) const |
| 163 | { |
Behdad Esfahbod | 33165f4 | 2022-11-29 15:14:15 -0700 | [diff] [blame] | 164 | unsigned int start_v = start_value / ELT_BITS; |
Andrew John | 0182988 | 2022-03-25 08:36:44 -0700 | [diff] [blame] | 165 | unsigned int start_bit = start_value & ELT_MASK; |
| 166 | unsigned int count = 0; |
| 167 | for (unsigned i = start_v; i < len () && count < size; i++) |
| 168 | { |
| 169 | elt_t bits = v[i]; |
Behdad Esfahbod | 33165f4 | 2022-11-29 15:14:15 -0700 | [diff] [blame] | 170 | uint32_t v_base = base | (i * ELT_BITS); |
Andrew John | 0182988 | 2022-03-25 08:36:44 -0700 | [diff] [blame] | 171 | for (unsigned int j = start_bit; j < ELT_BITS && count < size; j++) |
| 172 | { |
| 173 | if ((elt_t(1) << j) & bits) { |
| 174 | *p++ = v_base | j; |
| 175 | count++; |
| 176 | } |
| 177 | } |
| 178 | start_bit = 0; |
| 179 | } |
| 180 | return count; |
| 181 | } |
| 182 | |
| 183 | // Writes out the values NOT in this page to the array p. Returns the |
| 184 | // number of values written. At most size codepoints will be written. |
| 185 | // Returns the number of codepoints written. next_value holds the next value |
| 186 | // that should be written (if not present in this page). This is used to fill |
| 187 | // any missing value gaps between this page and the previous page, if any. |
| 188 | // next_value is updated to one more than the last value present in this page. |
| 189 | unsigned int write_inverted (uint32_t base, |
| 190 | unsigned int start_value, |
| 191 | hb_codepoint_t *p, |
| 192 | unsigned int size, |
| 193 | hb_codepoint_t *next_value) const |
| 194 | { |
Behdad Esfahbod | 33165f4 | 2022-11-29 15:14:15 -0700 | [diff] [blame] | 195 | unsigned int start_v = start_value / ELT_BITS; |
Andrew John | 0182988 | 2022-03-25 08:36:44 -0700 | [diff] [blame] | 196 | unsigned int start_bit = start_value & ELT_MASK; |
| 197 | unsigned int count = 0; |
| 198 | for (unsigned i = start_v; i < len () && count < size; i++) |
| 199 | { |
| 200 | elt_t bits = v[i]; |
Behdad Esfahbod | 33165f4 | 2022-11-29 15:14:15 -0700 | [diff] [blame] | 201 | uint32_t v_offset = i * ELT_BITS; |
Andrew John | 0182988 | 2022-03-25 08:36:44 -0700 | [diff] [blame] | 202 | for (unsigned int j = start_bit; j < ELT_BITS && count < size; j++) |
| 203 | { |
| 204 | if ((elt_t(1) << j) & bits) |
| 205 | { |
| 206 | hb_codepoint_t value = base | v_offset | j; |
| 207 | // Emit all the missing values from next_value up to value - 1. |
| 208 | for (hb_codepoint_t k = *next_value; k < value && count < size; k++) |
| 209 | { |
| 210 | *p++ = k; |
| 211 | count++; |
| 212 | } |
| 213 | // Skip over this value; |
| 214 | *next_value = value + 1; |
| 215 | } |
| 216 | } |
| 217 | start_bit = 0; |
| 218 | } |
| 219 | return count; |
| 220 | } |
| 221 | |
Behdad Esfahbod | 0340ba1 | 2023-09-05 14:56:09 +0300 | [diff] [blame] | 222 | bool operator == (const hb_bit_page_t &other) const { return is_equal (other); } |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 223 | bool is_equal (const hb_bit_page_t &other) const |
| 224 | { |
Behdad Esfahbod | a331e91 | 2022-11-26 14:59:37 -0700 | [diff] [blame] | 225 | for (unsigned i = 0; i < len (); i++) |
| 226 | if (v[i] != other.v[i]) |
| 227 | return false; |
| 228 | return true; |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 229 | } |
Behdad Esfahbod | 0340ba1 | 2023-09-05 14:56:09 +0300 | [diff] [blame] | 230 | bool operator <= (const hb_bit_page_t &larger_page) const { return is_subset (larger_page); } |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 231 | bool is_subset (const hb_bit_page_t &larger_page) const |
| 232 | { |
Behdad Esfahbod | 2f4ed5e | 2023-07-09 09:45:46 -0600 | [diff] [blame] | 233 | if (has_population () && larger_page.has_population () && |
| 234 | population > larger_page.population) |
| 235 | return false; |
| 236 | |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 237 | for (unsigned i = 0; i < len (); i++) |
| 238 | if (~larger_page.v[i] & v[i]) |
| 239 | return false; |
| 240 | return true; |
| 241 | } |
| 242 | |
Behdad Esfahbod | 347c1f7 | 2023-07-09 09:33:04 -0600 | [diff] [blame] | 243 | bool has_population () const { return population != UINT_MAX; } |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 244 | unsigned int get_population () const |
| 245 | { |
Behdad Esfahbod | 347c1f7 | 2023-07-09 09:33:04 -0600 | [diff] [blame] | 246 | if (has_population ()) return population; |
| 247 | population = |
Behdad Esfahbod | 87271e1 | 2022-11-18 16:01:23 -0700 | [diff] [blame] | 248 | + hb_iter (v) |
Behdad Esfahbod | 68a2902 | 2022-11-18 16:02:45 -0700 | [diff] [blame] | 249 | | hb_reduce ([] (unsigned pop, const elt_t &_) { return pop + hb_popcount (_); }, 0u) |
Behdad Esfahbod | 87271e1 | 2022-11-18 16:01:23 -0700 | [diff] [blame] | 250 | ; |
Behdad Esfahbod | 347c1f7 | 2023-07-09 09:33:04 -0600 | [diff] [blame] | 251 | return population; |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 252 | } |
| 253 | |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 254 | bool next (hb_codepoint_t *codepoint) const |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 255 | { |
| 256 | unsigned int m = (*codepoint + 1) & MASK; |
| 257 | if (!m) |
| 258 | { |
| 259 | *codepoint = INVALID; |
| 260 | return false; |
| 261 | } |
| 262 | unsigned int i = m / ELT_BITS; |
| 263 | unsigned int j = m & ELT_MASK; |
| 264 | |
| 265 | const elt_t vv = v[i] & ~((elt_t (1) << j) - 1); |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 266 | for (const elt_t *p = &vv; i < len (); p = &v[++i]) |
| 267 | if (*p) |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 268 | { |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 269 | *codepoint = i * ELT_BITS + elt_get_min (*p); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 270 | return true; |
| 271 | } |
| 272 | |
| 273 | *codepoint = INVALID; |
| 274 | return false; |
| 275 | } |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 276 | bool previous (hb_codepoint_t *codepoint) const |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 277 | { |
| 278 | unsigned int m = (*codepoint - 1) & MASK; |
| 279 | if (m == MASK) |
| 280 | { |
| 281 | *codepoint = INVALID; |
| 282 | return false; |
| 283 | } |
| 284 | unsigned int i = m / ELT_BITS; |
| 285 | unsigned int j = m & ELT_MASK; |
| 286 | |
| 287 | /* Fancy mask to avoid shifting by elt_t bitsize, which is undefined. */ |
| 288 | const elt_t mask = j < 8 * sizeof (elt_t) - 1 ? |
| 289 | ((elt_t (1) << (j + 1)) - 1) : |
| 290 | (elt_t) -1; |
| 291 | const elt_t vv = v[i] & mask; |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 292 | const elt_t *p = &vv; |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 293 | while (true) |
| 294 | { |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 295 | if (*p) |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 296 | { |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 297 | *codepoint = i * ELT_BITS + elt_get_max (*p); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | if ((int) i <= 0) break; |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 301 | p = &v[--i]; |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | *codepoint = INVALID; |
| 305 | return false; |
| 306 | } |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 307 | hb_codepoint_t get_min () const |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 308 | { |
| 309 | for (unsigned int i = 0; i < len (); i++) |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 310 | if (v[i]) |
| 311 | return i * ELT_BITS + elt_get_min (v[i]); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 312 | return INVALID; |
| 313 | } |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 314 | hb_codepoint_t get_max () const |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 315 | { |
| 316 | for (int i = len () - 1; i >= 0; i--) |
Behdad Esfahbod | ec4812a | 2021-08-19 13:32:44 -0600 | [diff] [blame] | 317 | if (v[i]) |
| 318 | return i * ELT_BITS + elt_get_max (v[i]); |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static constexpr hb_codepoint_t INVALID = HB_SET_VALUE_INVALID; |
| 323 | |
| 324 | typedef unsigned long long elt_t; |
Behdad Esfahbod | 0d66529 | 2023-01-12 13:14:24 -0700 | [diff] [blame] | 325 | static constexpr unsigned PAGE_BITS_LOG_2 = 9; // 512 bits |
| 326 | static constexpr unsigned PAGE_BITS = 1 << PAGE_BITS_LOG_2; |
Andy John | 3125f5a | 2022-03-21 13:12:14 -0700 | [diff] [blame] | 327 | static_assert (1 << PAGE_BITS_LOG_2 == PAGE_BITS, ""); |
Behdad Esfahbod | 0d66529 | 2023-01-12 13:14:24 -0700 | [diff] [blame] | 328 | static_assert ((PAGE_BITS & ((PAGE_BITS) - 1)) == 0, ""); |
Andrew John | 0182988 | 2022-03-25 08:36:44 -0700 | [diff] [blame] | 329 | static constexpr unsigned PAGE_BITMASK = PAGE_BITS - 1; |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 330 | |
| 331 | static unsigned int elt_get_min (const elt_t &elt) { return hb_ctz (elt); } |
| 332 | static unsigned int elt_get_max (const elt_t &elt) { return hb_bit_storage (elt) - 1; } |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 333 | |
| 334 | typedef hb_vector_size_t<elt_t, PAGE_BITS / 8> vector_t; |
| 335 | |
| 336 | static constexpr unsigned ELT_BITS = sizeof (elt_t) * 8; |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 337 | static constexpr unsigned ELT_MASK = ELT_BITS - 1; |
Andrew John | 0182988 | 2022-03-25 08:36:44 -0700 | [diff] [blame] | 338 | |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 339 | static constexpr unsigned BITS = sizeof (vector_t) * 8; |
| 340 | static constexpr unsigned MASK = BITS - 1; |
| 341 | static_assert ((unsigned) PAGE_BITS == (unsigned) BITS, ""); |
| 342 | |
| 343 | elt_t &elt (hb_codepoint_t g) { return v[(g & MASK) / ELT_BITS]; } |
| 344 | const elt_t& elt (hb_codepoint_t g) const { return v[(g & MASK) / ELT_BITS]; } |
| 345 | static constexpr elt_t mask (hb_codepoint_t g) { return elt_t (1) << (g & ELT_MASK); } |
| 346 | |
Behdad Esfahbod | 347c1f7 | 2023-07-09 09:33:04 -0600 | [diff] [blame] | 347 | mutable unsigned population; |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 348 | vector_t v; |
| 349 | }; |
Behdad Esfahbod | 9b390f8 | 2021-08-15 12:34:55 -0600 | [diff] [blame] | 350 | |
| 351 | |
| 352 | #endif /* HB_BIT_PAGE_HH */ |