home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / misc / prinrval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.9 KB  |  131 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * file:            prinrval.c
  21.  * description:        implementation for the kernel interval timing functions
  22.  */
  23.  
  24. #include "primpl.h"
  25.  
  26. /*
  27.  *-----------------------------------------------------------------------
  28.  *
  29.  * _PR_InitClock --
  30.  *
  31.  *
  32.  *-----------------------------------------------------------------------
  33.  */
  34.  
  35. void _PR_InitClock(void)
  36. {
  37.     _PR_MD_INTERVAL_INIT();
  38. }
  39.  
  40. /*
  41.  * This version of interval times is based on the time of day
  42.  * capability offered by system. This isn't valid for two reasons:
  43.  * 1) The time of day is neither linear nor montonically increasing
  44.  * 2) The units here are milliseconds. That's not appropriate for our use.
  45.  */
  46.  
  47. PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow()
  48. {
  49.     if (!_pr_initialized) _PR_ImplicitInitialization();
  50.     return _PR_MD_GET_INTERVAL();
  51. }  /* PR_IntervalNow */
  52.  
  53. PR_EXTERN(PRUint32) PR_TicksPerSecond()
  54. {
  55.     if (!_pr_initialized) _PR_ImplicitInitialization();
  56.     return _PR_MD_INTERVAL_PER_SEC();
  57. }  /* PR_TicksPerSecond */
  58.  
  59. PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds)
  60. {
  61.     return seconds * PR_TicksPerSecond();
  62. }  /* PR_SecondsToInterval */
  63.  
  64. PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli)
  65. {
  66.     PRIntervalTime ticks;
  67.     PRUint64 tock, tps, msecPerSec, rounding;
  68.     LL_UI2L(tock, milli);
  69.     LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
  70.     LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1));
  71.     LL_I2L(tps, PR_TicksPerSecond());
  72.     LL_MUL(tock, tock, tps);
  73.     LL_ADD(tock, tock, rounding);
  74.     LL_DIV(tock, tock, msecPerSec);
  75.     LL_L2UI(ticks, tock);
  76.     return ticks;
  77. }  /* PR_MillisecondsToInterval */
  78.  
  79. PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro)
  80. {
  81.     PRIntervalTime ticks;
  82.     PRUint64 tock, tps, usecPerSec, rounding;
  83.     LL_UI2L(tock, micro);
  84.     LL_I2L(usecPerSec, PR_USEC_PER_SEC);
  85.     LL_I2L(rounding, (PR_USEC_PER_SEC >> 1));
  86.     LL_I2L(tps, PR_TicksPerSecond());
  87.     LL_MUL(tock, tock, tps);
  88.     LL_ADD(tock, tock, rounding);
  89.     LL_DIV(tock, tock, usecPerSec);
  90.     LL_L2UI(ticks, tock);
  91.     return ticks;
  92. }  /* PR_MicrosecondsToInterval */
  93.  
  94. PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks)
  95. {
  96.     return ticks / PR_TicksPerSecond();
  97. }  /* PR_IntervalToSeconds */
  98.  
  99. PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks)
  100. {
  101.     PRUint32 milli;
  102.     PRUint64 tock, tps, msecPerSec, rounding;
  103.     LL_UI2L(tock, ticks);
  104.     LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
  105.     LL_I2L(tps, PR_TicksPerSecond());
  106.     LL_USHR(rounding, tps, 1);
  107.     LL_MUL(tock, tock, msecPerSec);
  108.     LL_ADD(tock, tock, rounding);
  109.     LL_DIV(tock, tock, tps);
  110.     LL_L2UI(milli, tock);
  111.     return milli;
  112. }  /* PR_IntervalToMilliseconds */
  113.  
  114. PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks)
  115. {
  116.     PRUint32 micro;
  117.     PRUint64 tock, tps, usecPerSec, rounding;
  118.     LL_UI2L(tock, ticks);
  119.     LL_I2L(usecPerSec, PR_USEC_PER_SEC);
  120.     LL_I2L(tps, PR_TicksPerSecond());
  121.     LL_USHR(rounding, tps, 1);
  122.     LL_MUL(tock, tock, usecPerSec);
  123.     LL_ADD(tock, tock, rounding);
  124.     LL_DIV(tock, tock, tps);
  125.     LL_L2UI(micro, tock);
  126.     return micro;
  127. }  /* PR_IntervalToMicroseconds */
  128.  
  129. /* prinrval.c */
  130.  
  131.