1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/bitops.h>
6 #include <linux/log2.h>
9 #define EYTZINGER_BUG_ON(cond) BUG_ON(cond)
11 #define EYTZINGER_BUG_ON(cond)
15 * Traversal for trees in eytzinger layout - a full binary tree layed out in an
18 * Consider using an eytzinger tree any time you would otherwise be doing binary
19 * search over an array. Binary search is a worst case scenario for branch
20 * prediction and prefetching, but in an eytzinger tree every node's children
21 * are adjacent in memory, thus we can prefetch children before knowing the
22 * result of the comparison, assuming multiple nodes fit on a cacheline.
24 * Two variants are provided, for one based indexing and zero based indexing.
26 * Zero based indexing is more convenient, but one based indexing has better
27 * alignment and thus better performance because each new level of the tree
28 * starts at a power of two, and thus if element 0 was cacheline aligned, each
29 * new level will be as well.
32 static inline unsigned eytzinger1_child(unsigned i
, unsigned child
)
34 EYTZINGER_BUG_ON(child
> 1);
36 return (i
<< 1) + child
;
39 static inline unsigned eytzinger1_left_child(unsigned i
)
41 return eytzinger1_child(i
, 0);
44 static inline unsigned eytzinger1_right_child(unsigned i
)
46 return eytzinger1_child(i
, 1);
49 static inline unsigned eytzinger1_first(unsigned size
)
51 return size
? rounddown_pow_of_two(size
) : 0;
54 static inline unsigned eytzinger1_last(unsigned size
)
56 return rounddown_pow_of_two(size
+ 1) - 1;
60 * eytzinger1_next() and eytzinger1_prev() have the nice properties that
62 * eytzinger1_next(0) == eytzinger1_first())
63 * eytzinger1_prev(0) == eytzinger1_last())
65 * eytzinger1_prev(eytzinger1_first()) == 0
66 * eytzinger1_next(eytzinger1_last()) == 0
69 static inline unsigned eytzinger1_next(unsigned i
, unsigned size
)
71 EYTZINGER_BUG_ON(i
> size
);
73 if (eytzinger1_right_child(i
) <= size
) {
74 i
= eytzinger1_right_child(i
);
76 i
<<= __fls(size
+ 1) - __fls(i
);
85 static inline unsigned eytzinger1_prev(unsigned i
, unsigned size
)
87 EYTZINGER_BUG_ON(i
> size
);
89 if (eytzinger1_left_child(i
) <= size
) {
90 i
= eytzinger1_left_child(i
) + 1;
92 i
<<= __fls(size
+ 1) - __fls(i
);
102 static inline unsigned eytzinger1_extra(unsigned size
)
105 ? (size
+ 1 - rounddown_pow_of_two(size
)) << 1
109 static inline unsigned __eytzinger1_to_inorder(unsigned i
, unsigned size
,
112 unsigned b
= __fls(i
);
113 unsigned shift
= __fls(size
) - b
;
116 EYTZINGER_BUG_ON(!i
|| i
> size
);
127 * i -= (i - extra) >> 1;
130 i
+= (s
>> 1) & (s
>> 31);
135 static inline unsigned __inorder_to_eytzinger1(unsigned i
, unsigned size
,
141 EYTZINGER_BUG_ON(!i
|| i
> size
);
155 i
|= 1U << (__fls(size
) - shift
);
160 static inline unsigned eytzinger1_to_inorder(unsigned i
, unsigned size
)
162 return __eytzinger1_to_inorder(i
, size
, eytzinger1_extra(size
));
165 static inline unsigned inorder_to_eytzinger1(unsigned i
, unsigned size
)
167 return __inorder_to_eytzinger1(i
, size
, eytzinger1_extra(size
));
170 #define eytzinger1_for_each(_i, _size) \
171 for (unsigned (_i) = eytzinger1_first((_size)); \
173 (_i) = eytzinger1_next((_i), (_size)))
175 /* Zero based indexing version: */
177 static inline unsigned eytzinger0_child(unsigned i
, unsigned child
)
179 EYTZINGER_BUG_ON(child
> 1);
181 return (i
<< 1) + 1 + child
;
184 static inline unsigned eytzinger0_left_child(unsigned i
)
186 return eytzinger0_child(i
, 0);
189 static inline unsigned eytzinger0_right_child(unsigned i
)
191 return eytzinger0_child(i
, 1);
194 static inline unsigned eytzinger0_first(unsigned size
)
196 return eytzinger1_first(size
) - 1;
199 static inline unsigned eytzinger0_last(unsigned size
)
201 return eytzinger1_last(size
) - 1;
204 static inline unsigned eytzinger0_next(unsigned i
, unsigned size
)
206 return eytzinger1_next(i
+ 1, size
) - 1;
209 static inline unsigned eytzinger0_prev(unsigned i
, unsigned size
)
211 return eytzinger1_prev(i
+ 1, size
) - 1;
214 static inline unsigned eytzinger0_extra(unsigned size
)
216 return eytzinger1_extra(size
);
219 static inline unsigned __eytzinger0_to_inorder(unsigned i
, unsigned size
,
222 return __eytzinger1_to_inorder(i
+ 1, size
, extra
) - 1;
225 static inline unsigned __inorder_to_eytzinger0(unsigned i
, unsigned size
,
228 return __inorder_to_eytzinger1(i
+ 1, size
, extra
) - 1;
231 static inline unsigned eytzinger0_to_inorder(unsigned i
, unsigned size
)
233 return __eytzinger0_to_inorder(i
, size
, eytzinger0_extra(size
));
236 static inline unsigned inorder_to_eytzinger0(unsigned i
, unsigned size
)
238 return __inorder_to_eytzinger0(i
, size
, eytzinger0_extra(size
));
241 #define eytzinger0_for_each(_i, _size) \
242 for (unsigned (_i) = eytzinger0_first((_size)); \
244 (_i) = eytzinger0_next((_i), (_size)))
246 /* return greatest node <= @search, or -1 if not found */
247 static inline int eytzinger0_find_le(void *base
, size_t nr
, size_t size
,
248 cmp_func_t cmp
, const void *search
)
257 n
= eytzinger0_child(i
, cmp(base
+ i
* size
, search
) <= 0);
262 * @i was greater than @search, return previous node:
264 * if @i was leftmost/smallest element,
265 * eytzinger0_prev(eytzinger0_first())) returns -1, as expected
267 return eytzinger0_prev(i
, nr
);
273 static inline int eytzinger0_find_gt(void *base
, size_t nr
, size_t size
,
274 cmp_func_t cmp
, const void *search
)
276 ssize_t idx
= eytzinger0_find_le(base
, nr
, size
, cmp
, search
);
279 * if eytitzinger0_find_le() returned -1 - no element was <= search - we
280 * want to return the first element; next/prev identities mean this work
283 * similarly if find_le() returns last element, we should return -1;
284 * identities mean this all works out:
286 return eytzinger0_next(idx
, nr
);
289 static inline int eytzinger0_find_ge(void *base
, size_t nr
, size_t size
,
290 cmp_func_t cmp
, const void *search
)
292 ssize_t idx
= eytzinger0_find_le(base
, nr
, size
, cmp
, search
);
294 if (idx
< nr
&& !cmp(base
+ idx
* size
, search
))
297 return eytzinger0_next(idx
, nr
);
300 #define eytzinger0_find(base, nr, size, _cmp, search) \
302 void *_base = (base); \
303 const void *_search = (search); \
305 size_t _size = (size); \
310 (_res = _cmp(_search, _base + _i * _size))) \
311 _i = eytzinger0_child(_i, _res > 0); \
315 void eytzinger0_sort_r(void *, size_t, size_t,
316 cmp_r_func_t
, swap_r_func_t
, const void *);
317 void eytzinger0_sort(void *, size_t, size_t, cmp_func_t
, swap_func_t
);
319 #endif /* _EYTZINGER_H */