gcc has been optimizing division by a constant into a multiply and shift for at least 10 years now so there is no need to do this by hand as a special case - especially in a function that is going to delay for at least 1 microsecond anyway.
Also remove unnecessary casts - usec is a uint64_t so everything else in the expression is going to get promoted appropriately. Signed-off-by: Michael Davidson <[email protected]> --- kern/arch/x86/time.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/kern/arch/x86/time.c b/kern/arch/x86/time.c index d08d589..fed0b73 100644 --- a/kern/arch/x86/time.c +++ b/kern/arch/x86/time.c @@ -102,26 +102,16 @@ void udelay(uint64_t usec) void udelay_pit(uint64_t usec) { int64_t delta, prev_tick, tick, ticks_left; + + if (usec <= 0) + return; + prev_tick = getpit(); /* - * Calculate (n * (i8254_freq / 1e6)) without using floating point - * and without any avoidable overflows. + * Calculate ticks as (usec * (i8254_freq / 1e6)) rounded up + * without using floating point and without any avoidable overflows. */ - if (usec <= 0) - ticks_left = 0; - // some optimization from bsd code - else if (usec < 256) - /* - * Use fixed point to avoid a slow division by 1000000. - * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest. - * 2^15 is the first power of 2 that gives exact results - * for n between 0 and 256. - */ - ticks_left = ((uint64_t)usec * 39099 + (1 << 15) - 1) >> 15; - else - // round up the ticks left - ticks_left = ((uint64_t)usec * (long long)PIT_FREQ+ 999999) - / 1000000; + ticks_left = ((usec * PIT_FREQ) + 999999) / 1000000; while (ticks_left > 0) { tick = getpit(); delta = prev_tick - tick; -- 2.8.0.rc3.226.g39d4020 -- You received this message because you are subscribed to the Google Groups "Akaros" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. For more options, visit https://groups.google.com/d/optout.
