Skip to content

Commit 9e21688

Browse files
committed
Implement De Bruijn-based top priority helper
Implement the helper function that uses a De Bruijn multiply-and-LUT approach to compute the index of the least-significant set bit in O(1) time complexity. This helper is not yet wired into the scheduler logic; integration will follow in a later commit. No functional change in this patch.
1 parent a0c40a0 commit 9e21688

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

kernel/task.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,18 @@ static const uint8_t debruijn_lut[32] = {
342342
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
343343
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
344344

345+
/* O(1) priority selection optimized for RISC-V */
346+
static inline uint8_t find_highest_ready_priority(uint32_t bitmap)
347+
{
348+
/* Isolate rightmost set bit (highest priority) */
349+
uint32_t isolated = bitmap & (-bitmap);
350+
351+
/* De Bruijn multiplication for O(1) bit position finding */
352+
uint32_t hash = (isolated * 0x077CB531U) >> 27;
353+
354+
return debruijn_lut[hash & 0x1F];
355+
}
356+
345357
/* Weak aliases for context switching functions. */
346358
void dispatch(void);
347359
void yield(void);

0 commit comments

Comments
 (0)