2 * Copyright © 2012,2017 Google, Inc.
3 * Copyright © 2021 Behdad Esfahbod
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Google Author(s): Behdad Esfahbod
28 #ifndef HB_BIT_PAGE_HH
29 #define HB_BIT_PAGE_HH
34 /* Compiler-assisted vectorization. */
36 /* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))),
37 * basically a fixed-size bitset. We can't use the compiler type because hb_vector_t cannot
38 * guarantee alignment requirements. */
39 template <typename elt_t
, unsigned int byte_size
>
40 struct hb_vector_size_t
42 elt_t
& operator [] (unsigned int i
) { return v
[i
]; }
43 const elt_t
& operator [] (unsigned int i
) const { return v
[i
]; }
47 for (unsigned int i
= 0; i
< ARRAY_LENGTH (v
); i
++)
52 for (unsigned int i
= 0; i
< ARRAY_LENGTH (v
); i
++)
56 template <typename Op
>
57 hb_vector_size_t
process (const Op
& op
) const
60 for (unsigned int i
= 0; i
< ARRAY_LENGTH (v
); i
++)
64 template <typename Op
>
65 hb_vector_size_t
process (const Op
& op
, const hb_vector_size_t
&o
) const
68 for (unsigned int i
= 0; i
< ARRAY_LENGTH (v
); i
++)
69 r
.v
[i
] = op (v
[i
], o
.v
[i
]);
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
); }
81 hb_array_t
<const elt_t
> iter () const
82 { return hb_array (v
); }
85 static_assert (0 == byte_size
% sizeof (elt_t
), "");
86 elt_t v
[byte_size
/ sizeof (elt_t
)];
92 void init0 () { v
.init0 (); population
= 0; }
93 void init1 () { v
.init1 (); population
= PAGE_BITS
; }
95 void dirty () { population
= UINT_MAX
; }
97 static inline constexpr unsigned len ()
98 { return ARRAY_LENGTH_CONST (v
); }
100 operator bool () const { return !is_empty (); }
101 bool is_empty () const
103 if (has_population ()) return !population
;
109 uint32_t hash () const
111 return hb_bytes_t ((const char *) &v
, sizeof (v
)).hash ();
114 void add (hb_codepoint_t g
) { elt (g
) |= mask (g
); dirty (); }
115 void del (hb_codepoint_t g
) { elt (g
) &= ~mask (g
); dirty (); }
116 void set (hb_codepoint_t g
, bool value
) { if (value
) add (g
); else del (g
); }
117 bool get (hb_codepoint_t g
) const { return elt (g
) & mask (g
); }
119 void add_range (hb_codepoint_t a
, hb_codepoint_t b
)
121 elt_t
*la
= &elt (a
);
122 elt_t
*lb
= &elt (b
);
124 *la
|= (mask (b
) << 1) - mask(a
);
127 *la
|= ~(mask (a
) - 1llu);
130 hb_memset (la
, 0xff, (char *) lb
- (char *) la
);
132 *lb
|= ((mask (b
) << 1) - 1llu);
136 void del_range (hb_codepoint_t a
, hb_codepoint_t b
)
138 elt_t
*la
= &elt (a
);
139 elt_t
*lb
= &elt (b
);
141 *la
&= ~((mask (b
) << 1llu) - mask(a
));
147 hb_memset (la
, 0, (char *) lb
- (char *) la
);
149 *lb
&= ~((mask (b
) << 1) - 1llu);
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
); }
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
,
162 unsigned int size
) const
164 unsigned int start_v
= start_value
/ ELT_BITS
;
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
++)
170 uint32_t v_base
= base
| (i
* ELT_BITS
);
171 for (unsigned int j
= start_bit
; j
< ELT_BITS
&& count
< size
; j
++)
173 if ((elt_t(1) << j
) & bits
) {
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
,
193 hb_codepoint_t
*next_value
) const
195 unsigned int start_v
= start_value
/ ELT_BITS
;
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
++)
201 uint32_t v_offset
= i
* ELT_BITS
;
202 for (unsigned int j
= start_bit
; j
< ELT_BITS
&& count
< size
; j
++)
204 if ((elt_t(1) << j
) & bits
)
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
++)
213 // Skip over this value;
214 *next_value
= value
+ 1;
222 bool operator == (const hb_bit_page_t
&other
) const { return is_equal (other
); }
223 bool is_equal (const hb_bit_page_t
&other
) const
225 for (unsigned i
= 0; i
< len (); i
++)
226 if (v
[i
] != other
.v
[i
])
230 bool operator <= (const hb_bit_page_t
&larger_page
) const { return is_subset (larger_page
); }
231 bool is_subset (const hb_bit_page_t
&larger_page
) const
233 if (has_population () && larger_page
.has_population () &&
234 population
> larger_page
.population
)
237 for (unsigned i
= 0; i
< len (); i
++)
238 if (~larger_page
.v
[i
] & v
[i
])
243 bool has_population () const { return population
!= UINT_MAX
; }
244 unsigned int get_population () const
246 if (has_population ()) return population
;
249 | hb_reduce ([] (unsigned pop
, const elt_t
&_
) { return pop
+ hb_popcount (_
); }, 0u)
254 bool next (hb_codepoint_t
*codepoint
) const
256 unsigned int m
= (*codepoint
+ 1) & MASK
;
259 *codepoint
= INVALID
;
262 unsigned int i
= m
/ ELT_BITS
;
263 unsigned int j
= m
& ELT_MASK
;
265 const elt_t vv
= v
[i
] & ~((elt_t (1) << j
) - 1);
266 for (const elt_t
*p
= &vv
; i
< len (); p
= &v
[++i
])
269 *codepoint
= i
* ELT_BITS
+ elt_get_min (*p
);
273 *codepoint
= INVALID
;
276 bool previous (hb_codepoint_t
*codepoint
) const
278 unsigned int m
= (*codepoint
- 1) & MASK
;
281 *codepoint
= INVALID
;
284 unsigned int i
= m
/ ELT_BITS
;
285 unsigned int j
= m
& ELT_MASK
;
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) :
291 const elt_t vv
= v
[i
] & mask
;
292 const elt_t
*p
= &vv
;
297 *codepoint
= i
* ELT_BITS
+ elt_get_max (*p
);
300 if ((int) i
<= 0) break;
304 *codepoint
= INVALID
;
307 hb_codepoint_t
get_min () const
309 for (unsigned int i
= 0; i
< len (); i
++)
311 return i
* ELT_BITS
+ elt_get_min (v
[i
]);
314 hb_codepoint_t
get_max () const
316 for (int i
= len () - 1; i
>= 0; i
--)
318 return i
* ELT_BITS
+ elt_get_max (v
[i
]);
322 static constexpr hb_codepoint_t INVALID
= HB_SET_VALUE_INVALID
;
324 typedef unsigned long long elt_t
;
325 static constexpr unsigned PAGE_BITS_LOG_2
= 9; // 512 bits
326 static constexpr unsigned PAGE_BITS
= 1 << PAGE_BITS_LOG_2
;
327 static_assert (1 << PAGE_BITS_LOG_2
== PAGE_BITS
, "");
328 static_assert ((PAGE_BITS
& ((PAGE_BITS
) - 1)) == 0, "");
329 static constexpr unsigned PAGE_BITMASK
= PAGE_BITS
- 1;
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; }
334 typedef hb_vector_size_t
<elt_t
, PAGE_BITS
/ 8> vector_t
;
336 static constexpr unsigned ELT_BITS
= sizeof (elt_t
) * 8;
337 static constexpr unsigned ELT_MASK
= ELT_BITS
- 1;
339 static constexpr unsigned BITS
= sizeof (vector_t
) * 8;
340 static constexpr unsigned MASK
= BITS
- 1;
341 static_assert ((unsigned) PAGE_BITS
== (unsigned) BITS
, "");
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
); }
347 mutable unsigned population
;
352 #endif /* HB_BIT_PAGE_HH */