/* Calculate the number of performance counter ticks required for a timeout. If TimeoutInMicroseconds is 0, return value is also 0, which is recognized as infinity. */
UINT64 CalculateTimeout(IN UINTN TimeoutInMicroseconds, OUT UINT64 *CurrentTime)
/* Calculate the number of performance counter ticks required for a timeout. If TimeoutInMicroseconds is 0, return value is also 0, which is recognized as infinity. */
UINT64 CalculateTimeout(IN UINTN TimeoutInMicroseconds, OUT UINT64 *CurrentTime)
{
  UINT64  TimeoutInSeconds;
  UINT64  TimestampCounterFreq;
  *CurrentTime = GetPerformanceCounter ();
  if (TimeoutInMicroseconds == 0) {
    return 0;
  }
  TimestampCounterFreq = GetPerformanceCounterProperties (NULL, NULL);
  if (DivU64x64Remainder (MAX_UINT64, TimeoutInMicroseconds, NULL) < TimestampCounterFreq) {
    TimeoutInSeconds = DivU64x32 (TimeoutInMicroseconds, 1000000);
    ASSERT (DivU64x64Remainder (MAX_UINT64, TimeoutInSeconds, NULL) >= TimestampCounterFreq);
    return MultU64x64 (TimestampCounterFreq, TimeoutInSeconds);
  } else {
    return DivU64x32 (
             MultU64x64 (
               TimestampCounterFreq,
               TimeoutInMicroseconds
               ),
             1000000
             );
  }
}