diff mbox

[edk2,1/2] MdeModulePkg: CoreStall: prove safety of round-up in non-iterating branch

Message ID 1410700015-23791-2-git-send-email-lersek@redhat.com
State New
Headers show

Commit Message

Laszlo Ersek Sept. 14, 2014, 1:06 p.m. UTC
The non-iterating branch of CoreStall() rounds up the number of ticks if
Remainder is nonzero. Explain in a comment why this is safe.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 MdeModulePkg/Core/Dxe/Misc/Stall.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
diff mbox

Patch

diff --git a/MdeModulePkg/Core/Dxe/Misc/Stall.c b/MdeModulePkg/Core/Dxe/Misc/Stall.c
index 73e9078..aae54fd 100644
--- a/MdeModulePkg/Core/Dxe/Misc/Stall.c
+++ b/MdeModulePkg/Core/Dxe/Misc/Stall.c
@@ -97,17 +97,30 @@  CoreStall (
     //
     Counter = DivU64x32Remainder (
                 MultU64x32 (Microseconds, 10),
                 gMetronome->TickPeriod,
                 &Remainder
                 );
     if (Remainder != 0) {
       //
       // If Remainder is not zero, then round Counter up by one tick.
       //
+      // This is safe, because the "worst case" (the largest quotient) emerges
+      // from the following values (largest numerator, smallest denominator):
+      //
+      // Microseconds == 0x1999_9999_9999_9999; therefore
+      // Numerator == 0xFFFF_FFFF_FFFF_FFFA, after multiplying by 0xA.
+      //
+      // Denominator == TickPeriod == 2 (with TickPeriod == 1, Remainder would
+      // be zero); therefore
+      // Quotient == 0x7FFF_FFFF_FFFF_FFFD, after division by 2.
+      //
+      // Hence the maximum pre-increment value of Counter is
+      // 0x7FFF_FFFF_FFFF_FFFD.
+      //
       Counter++;
     }
     CoreInternalWaitForTick (Counter);
   }
 
   return EFI_SUCCESS;
 }