Bug 1792034 [wpt PR 36019] - Make location.search always expect UTF-8, a=testonly
[gecko.git] / nsprpub / pr / src / misc / prinrval.c
blob79ff2cbca4d3bf8087058a0aa37ae84e6ccec15a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /*
7 * file: prinrval.c
8 * description: implementation for the kernel interval timing functions
9 */
11 #include "primpl.h"
14 *-----------------------------------------------------------------------
16 * _PR_InitClock --
19 *-----------------------------------------------------------------------
22 void _PR_InitClock(void)
24 _PR_MD_INTERVAL_INIT();
25 #ifdef DEBUG
27 PRIntervalTime ticksPerSec = PR_TicksPerSecond();
29 PR_ASSERT(ticksPerSec >= PR_INTERVAL_MIN);
30 PR_ASSERT(ticksPerSec <= PR_INTERVAL_MAX);
32 #endif /* DEBUG */
35 PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow(void)
37 if (!_pr_initialized) {
38 _PR_ImplicitInitialization();
40 return _PR_MD_GET_INTERVAL();
41 } /* PR_IntervalNow */
43 PR_EXTERN(PRUint32) PR_TicksPerSecond(void)
45 if (!_pr_initialized) {
46 _PR_ImplicitInitialization();
48 return _PR_MD_INTERVAL_PER_SEC();
49 } /* PR_TicksPerSecond */
51 PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds)
53 return seconds * PR_TicksPerSecond();
54 } /* PR_SecondsToInterval */
56 PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli)
58 PRIntervalTime ticks;
59 PRUint64 tock, tps, msecPerSec, rounding;
60 LL_UI2L(tock, milli);
61 LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
62 LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1));
63 LL_I2L(tps, PR_TicksPerSecond());
64 LL_MUL(tock, tock, tps);
65 LL_ADD(tock, tock, rounding);
66 LL_DIV(tock, tock, msecPerSec);
67 LL_L2UI(ticks, tock);
68 return ticks;
69 } /* PR_MillisecondsToInterval */
71 PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro)
73 PRIntervalTime ticks;
74 PRUint64 tock, tps, usecPerSec, rounding;
75 LL_UI2L(tock, micro);
76 LL_I2L(usecPerSec, PR_USEC_PER_SEC);
77 LL_I2L(rounding, (PR_USEC_PER_SEC >> 1));
78 LL_I2L(tps, PR_TicksPerSecond());
79 LL_MUL(tock, tock, tps);
80 LL_ADD(tock, tock, rounding);
81 LL_DIV(tock, tock, usecPerSec);
82 LL_L2UI(ticks, tock);
83 return ticks;
84 } /* PR_MicrosecondsToInterval */
86 PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks)
88 return ticks / PR_TicksPerSecond();
89 } /* PR_IntervalToSeconds */
91 PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks)
93 PRUint32 milli;
94 PRUint64 tock, tps, msecPerSec, rounding;
95 LL_UI2L(tock, ticks);
96 LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
97 LL_I2L(tps, PR_TicksPerSecond());
98 LL_USHR(rounding, tps, 1);
99 LL_MUL(tock, tock, msecPerSec);
100 LL_ADD(tock, tock, rounding);
101 LL_DIV(tock, tock, tps);
102 LL_L2UI(milli, tock);
103 return milli;
104 } /* PR_IntervalToMilliseconds */
106 PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks)
108 PRUint32 micro;
109 PRUint64 tock, tps, usecPerSec, rounding;
110 LL_UI2L(tock, ticks);
111 LL_I2L(usecPerSec, PR_USEC_PER_SEC);
112 LL_I2L(tps, PR_TicksPerSecond());
113 LL_USHR(rounding, tps, 1);
114 LL_MUL(tock, tock, usecPerSec);
115 LL_ADD(tock, tock, rounding);
116 LL_DIV(tock, tock, tps);
117 LL_L2UI(micro, tock);
118 return micro;
119 } /* PR_IntervalToMicroseconds */
121 /* prinrval.c */