remove spurious optimization in udelay_pit()
authorMichael Davidson <md@google.com>
Fri, 15 Apr 2016 20:30:44 +0000 (13:30 -0700)
committerBarret Rhoden <brho@cs.berkeley.edu>
Fri, 15 Apr 2016 20:36:51 +0000 (16:36 -0400)
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 <md@google.com>
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/arch/x86/time.c

index d08d589..fed0b73 100644 (file)
@@ -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;