]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bcachefs: convert eytzinger0_find to be 1-based
authorAndreas Gruenbacher <agruenba@redhat.com>
Tue, 28 Jan 2025 09:56:37 +0000 (10:56 +0100)
committerKent Overstreet <kent.overstreet@linux.dev>
Sat, 15 Mar 2025 01:02:14 +0000 (21:02 -0400)
Several of the algorithms on eytzinger trees are implemented in terms of
the eytzinger0 primitives.  However, those algorithms can just as easily
be expressed in terms of the eytzinger1 primitives, and that leads to
better and easier to understand code.  Start by converting
eytzinger0_find().

Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/eytzinger.h

index e3713b7b4c27614216a2b6ce0b4f1dea3ca29fd0..90cd5648b17776f06f636bd4494cc51599a00c56 100644 (file)
@@ -290,17 +290,17 @@ static inline int eytzinger0_find_ge(void *base, size_t nr, size_t size,
 
 #define eytzinger0_find(base, nr, size, _cmp, search)                  \
 ({                                                                     \
-       void *_base             = (base);                               \
+       size_t _size            = (size);                               \
+       void *_base1            = (void *)(base) - _size;               \
        const void *_search     = (search);                             \
        size_t _nr              = (nr);                                 \
-       size_t _size            = (size);                               \
-       size_t _i               = 0;                                    \
+       size_t _i               = 1;                                    \
        int _res;                                                       \
                                                                        \
-       while (_i < _nr &&                                              \
-              (_res = _cmp(_search, _base + _i * _size)))              \
-               _i = eytzinger0_child(_i, _res > 0);                    \
-       _i;                                                             \
+       while (_i <= _nr &&                                             \
+              (_res = _cmp(_search, _base1 + _i * _size)))             \
+               _i = eytzinger1_child(_i, _res > 0);                    \
+       _i - 1;                                                         \
 })
 
 void eytzinger0_sort_r(void *, size_t, size_t,