diff --git a/sys/kern/subr_pctrie.c b/sys/kern/subr_pctrie.c --- a/sys/kern/subr_pctrie.c +++ b/sys/kern/subr_pctrie.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include /* smr.h depends on struct thread. */ #include @@ -259,21 +260,22 @@ } /* - * Returns the slot where two keys differ. + * Returns the level where two keys differ. * It cannot accept 2 equal keys. */ static __inline uint16_t pctrie_keydiff(uint64_t index1, uint64_t index2) { - uint16_t clev; KASSERT(index1 != index2, ("%s: passing the same key value %jx", __func__, (uintmax_t)index1)); + CTASSERT(sizeof(long long) >= sizeof(uint64_t)); - index1 ^= index2; - for (clev = PCTRIE_LIMIT;; clev--) - if (pctrie_slot(index1, clev) != 0) - return (clev); + /* + * From the highest-order bit where the indexes differ, + * compute the highest level in the trie where they differ. + */ + return ((flsll(index1 ^ index2) - 1) / PCTRIE_WIDTH); } /* diff --git a/sys/vm/vm_radix.c b/sys/vm/vm_radix.c --- a/sys/vm/vm_radix.c +++ b/sys/vm/vm_radix.c @@ -58,6 +58,7 @@ #include #include #include +#include #include #include #include @@ -285,21 +286,22 @@ } /* - * Returns the slot where two keys differ. + * Returns the level where two keys differ. * It cannot accept 2 equal keys. */ static __inline uint16_t vm_radix_keydiff(vm_pindex_t index1, vm_pindex_t index2) { - uint16_t clev; KASSERT(index1 != index2, ("%s: passing the same key value %jx", __func__, (uintmax_t)index1)); + CTASSERT(sizeof(long long) >= sizeof(vm_pindex_t)); - index1 ^= index2; - for (clev = VM_RADIX_LIMIT;; clev--) - if (vm_radix_slot(index1, clev) != 0) - return (clev); + /* + * From the highest-order bit where the indexes differ, + * compute the highest level in the trie where they differ. + */ + return ((flsll(index1 ^ index2) - 1) / VM_RADIX_WIDTH); } /*