blob: 869c6789573fd18d1fe6afd148e28153245f894f [file] [log] [blame]
Behdad Esfahbod9b390f82021-08-15 12:34:55 -06001/*
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 Esfahbod43a40282022-11-18 15:54:34 -070033
34/* Compiler-assisted vectorization. */
35
36/* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))),
Behdad Esfahbod3e1c5242023-01-10 17:18:34 -070037 * basically a fixed-size bitset. We can't use the compiler type because hb_vector_t cannot
38 * guarantee alignment requirements. */
Behdad Esfahbod43a40282022-11-18 15:54:34 -070039template <typename elt_t, unsigned int byte_size>
40struct 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 Esfahboda180ae42023-01-16 13:06:30 -070045 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 Esfahbod43a40282022-11-18 15:54:34 -070055
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 Esfahbod744eb6b2022-11-18 15:56:06 -070081 hb_array_t<const elt_t> iter () const
82 { return hb_array (v); }
83
Behdad Esfahbod43a40282022-11-18 15:54:34 -070084 private:
85 static_assert (0 == byte_size % sizeof (elt_t), "");
86 elt_t v[byte_size / sizeof (elt_t)];
87};
88
89
Behdad Esfahbod9b390f82021-08-15 12:34:55 -060090struct hb_bit_page_t
91{
Behdad Esfahbod347c1f72023-07-09 09:33:04 -060092 void init0 () { v.init0 (); population = 0; }
93 void init1 () { v.init1 (); population = PAGE_BITS; }
94
95 void dirty () { population = UINT_MAX; }
Behdad Esfahbod9b390f82021-08-15 12:34:55 -060096
Behdad Esfahbod92d5ec22023-01-17 05:59:19 -070097 static inline constexpr unsigned len ()
Behdad Esfahbod9b390f82021-08-15 12:34:55 -060098 { return ARRAY_LENGTH_CONST (v); }
99
Behdad Esfahbod0340ba12023-09-05 14:56:09 +0300100 operator bool () const { return !is_empty (); }
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600101 bool is_empty () const
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600102 {
Behdad Esfahbod9b9a9c62023-07-09 09:55:00 -0600103 if (has_population ()) return !population;
Behdad Esfahbod744eb6b2022-11-18 15:56:06 -0700104 return
105 + hb_iter (v)
106 | hb_none
107 ;
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600108 }
Behdad Esfahbod58f848d2022-05-19 15:42:54 -0600109 uint32_t hash () const
Behdad Esfahbod124f9ae2022-05-19 12:58:02 -0600110 {
Behdad Esfahboda58bbe52023-05-09 12:06:35 -0600111 return hb_bytes_t ((const char *) &v, sizeof (v)).hash ();
Behdad Esfahbod124f9ae2022-05-19 12:58:02 -0600112 }
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600113
Behdad Esfahbod347c1f72023-07-09 09:33:04 -0600114 void add (hb_codepoint_t g) { elt (g) |= mask (g); dirty (); }
115 void del (hb_codepoint_t g) { elt (g) &= ~mask (g); dirty (); }
Behdad Esfahbod53fd4c92022-07-20 13:33:49 -0600116 void set (hb_codepoint_t g, bool value) { if (value) add (g); else del (g); }
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600117 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 Esfahbod0980e2b2023-07-09 15:13:57 -0600127 *la |= ~(mask (a) - 1llu);
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600128 la++;
129
Behdad Esfahbodac0efaf2022-11-22 12:50:36 -0700130 hb_memset (la, 0xff, (char *) lb - (char *) la);
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600131
Behdad Esfahbod0980e2b2023-07-09 15:13:57 -0600132 *lb |= ((mask (b) << 1) - 1llu);
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600133 }
Behdad Esfahbod347c1f72023-07-09 09:33:04 -0600134 dirty ();
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600135 }
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 Esfahbod0980e2b2023-07-09 15:13:57 -0600141 *la &= ~((mask (b) << 1llu) - mask(a));
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600142 else
143 {
144 *la &= mask (a) - 1;
145 la++;
146
Behdad Esfahbodac0efaf2022-11-22 12:50:36 -0700147 hb_memset (la, 0, (char *) lb - (char *) la);
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600148
Behdad Esfahbod0980e2b2023-07-09 15:13:57 -0600149 *lb &= ~((mask (b) << 1) - 1llu);
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600150 }
Behdad Esfahbod347c1f72023-07-09 09:33:04 -0600151 dirty ();
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600152 }
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 John01829882022-03-25 08:36:44 -0700156
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 Esfahbod33165f42022-11-29 15:14:15 -0700164 unsigned int start_v = start_value / ELT_BITS;
Andrew John01829882022-03-25 08:36:44 -0700165 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 Esfahbod33165f42022-11-29 15:14:15 -0700170 uint32_t v_base = base | (i * ELT_BITS);
Andrew John01829882022-03-25 08:36:44 -0700171 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 Esfahbod33165f42022-11-29 15:14:15 -0700195 unsigned int start_v = start_value / ELT_BITS;
Andrew John01829882022-03-25 08:36:44 -0700196 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 Esfahbod33165f42022-11-29 15:14:15 -0700201 uint32_t v_offset = i * ELT_BITS;
Andrew John01829882022-03-25 08:36:44 -0700202 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 Esfahbod0340ba12023-09-05 14:56:09 +0300222 bool operator == (const hb_bit_page_t &other) const { return is_equal (other); }
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600223 bool is_equal (const hb_bit_page_t &other) const
224 {
Behdad Esfahboda331e912022-11-26 14:59:37 -0700225 for (unsigned i = 0; i < len (); i++)
226 if (v[i] != other.v[i])
227 return false;
228 return true;
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600229 }
Behdad Esfahbod0340ba12023-09-05 14:56:09 +0300230 bool operator <= (const hb_bit_page_t &larger_page) const { return is_subset (larger_page); }
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600231 bool is_subset (const hb_bit_page_t &larger_page) const
232 {
Behdad Esfahbod2f4ed5e2023-07-09 09:45:46 -0600233 if (has_population () && larger_page.has_population () &&
234 population > larger_page.population)
235 return false;
236
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600237 for (unsigned i = 0; i < len (); i++)
238 if (~larger_page.v[i] & v[i])
239 return false;
240 return true;
241 }
242
Behdad Esfahbod347c1f72023-07-09 09:33:04 -0600243 bool has_population () const { return population != UINT_MAX; }
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600244 unsigned int get_population () const
245 {
Behdad Esfahbod347c1f72023-07-09 09:33:04 -0600246 if (has_population ()) return population;
247 population =
Behdad Esfahbod87271e12022-11-18 16:01:23 -0700248 + hb_iter (v)
Behdad Esfahbod68a29022022-11-18 16:02:45 -0700249 | hb_reduce ([] (unsigned pop, const elt_t &_) { return pop + hb_popcount (_); }, 0u)
Behdad Esfahbod87271e12022-11-18 16:01:23 -0700250 ;
Behdad Esfahbod347c1f72023-07-09 09:33:04 -0600251 return population;
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600252 }
253
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600254 bool next (hb_codepoint_t *codepoint) const
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600255 {
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 Esfahbodec4812a2021-08-19 13:32:44 -0600266 for (const elt_t *p = &vv; i < len (); p = &v[++i])
267 if (*p)
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600268 {
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600269 *codepoint = i * ELT_BITS + elt_get_min (*p);
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600270 return true;
271 }
272
273 *codepoint = INVALID;
274 return false;
275 }
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600276 bool previous (hb_codepoint_t *codepoint) const
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600277 {
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 Esfahbodec4812a2021-08-19 13:32:44 -0600292 const elt_t *p = &vv;
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600293 while (true)
294 {
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600295 if (*p)
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600296 {
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600297 *codepoint = i * ELT_BITS + elt_get_max (*p);
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600298 return true;
299 }
300 if ((int) i <= 0) break;
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600301 p = &v[--i];
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600302 }
303
304 *codepoint = INVALID;
305 return false;
306 }
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600307 hb_codepoint_t get_min () const
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600308 {
309 for (unsigned int i = 0; i < len (); i++)
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600310 if (v[i])
311 return i * ELT_BITS + elt_get_min (v[i]);
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600312 return INVALID;
313 }
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600314 hb_codepoint_t get_max () const
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600315 {
316 for (int i = len () - 1; i >= 0; i--)
Behdad Esfahbodec4812a2021-08-19 13:32:44 -0600317 if (v[i])
318 return i * ELT_BITS + elt_get_max (v[i]);
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600319 return 0;
320 }
321
322 static constexpr hb_codepoint_t INVALID = HB_SET_VALUE_INVALID;
323
324 typedef unsigned long long elt_t;
Behdad Esfahbod0d665292023-01-12 13:14:24 -0700325 static constexpr unsigned PAGE_BITS_LOG_2 = 9; // 512 bits
326 static constexpr unsigned PAGE_BITS = 1 << PAGE_BITS_LOG_2;
Andy John3125f5a2022-03-21 13:12:14 -0700327 static_assert (1 << PAGE_BITS_LOG_2 == PAGE_BITS, "");
Behdad Esfahbod0d665292023-01-12 13:14:24 -0700328 static_assert ((PAGE_BITS & ((PAGE_BITS) - 1)) == 0, "");
Andrew John01829882022-03-25 08:36:44 -0700329 static constexpr unsigned PAGE_BITMASK = PAGE_BITS - 1;
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600330
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 Esfahbod9b390f82021-08-15 12:34:55 -0600333
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 Esfahbod9b390f82021-08-15 12:34:55 -0600337 static constexpr unsigned ELT_MASK = ELT_BITS - 1;
Andrew John01829882022-03-25 08:36:44 -0700338
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600339 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 Esfahbod347c1f72023-07-09 09:33:04 -0600347 mutable unsigned population;
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600348 vector_t v;
349};
Behdad Esfahbod9b390f82021-08-15 12:34:55 -0600350
351
352#endif /* HB_BIT_PAGE_HH */