From: Michael Davidson
Date: Fri, 15 Apr 2016 20:30:44 +0000 (-0700)
Subject: remove spurious optimization in udelay_pit()
X-Git-Tag: current~1500
X-Git-Url: http://akaros.cs.berkeley.edu/gitweb/?p=akaros.git;a=commitdiff_plain;h=c20f3b8bcb8c2cb9f66db6a5643118aa35da6fff
remove spurious optimization in udelay_pit()
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
Signed-off-by: Barret Rhoden
---
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;