From 2cb44cdfc2c3e5134849f0f43f8e7b38d1c531c9 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 30 Jan 2023 20:57:49 +0800 Subject: [PATCH] kernel: services: optimize find_next_thread() Use a multiply-based algorithm to count the number of leading zeroes. This results in a function that's just 20 instructions long. Signed-off-by: Sean Cross --- kernel/src/services.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 659998167..a708919b9 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -698,32 +698,32 @@ impl SystemServices { Ok(()) } - fn find_next_thread(thread_mask: usize, current_thread: usize) -> usize { + pub fn find_next_thread(thread_mask: usize, current_thread: usize) -> usize { + // From https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup + // This platform has a multiplier, so this is fast + fn trailing_zeros(v: usize) -> usize { + const MULTIPLY_DEBRUIJN_BIT_POSITION: [usize; 32] = [ + 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, + 16, 7, 26, 12, 18, 6, 11, 5, 10, 9, + ]; + + MULTIPLY_DEBRUIJN_BIT_POSITION[((!v.wrapping_sub(1) & v) * 0x077CB531) >> 27] + } // If there's only one thread runnable, run that one if thread_mask == 0 { panic!("no threads were available to run"); } - if thread_mask.count_ones() == 1 { - let new_tid = thread_mask.ilog2() as usize; - // println!( - // "thread mask: {:b} -- only one thread was ready -- picking thread {} ({:b})", - // thread_mask, - // new_tid, - // 1 << new_tid - // ); - new_tid + // if thread_mask.is_power_of_two() { + if thread_mask & (thread_mask - 1) == 0 { + trailing_zeros(thread_mask) } else { - let mut new_tmask = (1usize << current_thread).rotate_left(1); - // println!("looking for a new thread -- existing thread was {} ({:b}) and new mask is {:b} (starting with {:b})", current_thread, 1<