Skip to content

Commit b156a08

Browse files
committed
Add De Bruijn helper for O(1) priority lookup
This commit introduces a 32-entry De Bruijn lookup table to support constant-time bitmap index computation. This mechanism will be used in later commits to replace iterative bit-scanning when selecting the next runnable priority. The helper itself does not change any scheduling behavior yet, but lays the groundwork for the new O(1) scheduler’s priority computation path.
1 parent a600b44 commit b156a08

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

kernel/task.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,23 @@ void panic(int32_t ecode)
356356
hal_panic();
357357
}
358358

359+
/* RISC-V optimized priority finding using De Bruijn sequence */
360+
static const uint8_t debruijn_lut[32] = {
361+
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
362+
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
363+
364+
/* O(1) priority selection optimized for RISC-V */
365+
static inline uint8_t find_highest_ready_priority(uint32_t bitmap)
366+
{
367+
/* Isolate rightmost set bit (highest priority) */
368+
uint32_t isolated = bitmap & (-bitmap);
369+
370+
/* De Bruijn multiplication for O(1) bit position finding */
371+
uint32_t hash = (isolated * 0x077CB531U) >> 27;
372+
373+
return debruijn_lut[hash & 0x1F];
374+
}
375+
359376
/* Weak aliases for context switching functions. */
360377
void dispatch(void);
361378
void yield(void);

0 commit comments

Comments
 (0)