~ubuntu-branches/ubuntu/trusty/virtualbox-lts-xenial/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libs/xpcom18a4/nsprpub/pr/src/misc/prinrval.c

  • Committer: Package Import Robot
  • Author(s): Gianfranco Costamagna
  • Date: 2016-02-23 14:28:26 UTC
  • Revision ID: package-import@ubuntu.com-20160223142826-bdu69el2z6wa2a44
Tags: upstream-4.3.36-dfsg
ImportĀ upstreamĀ versionĀ 4.3.36-dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
16
 *
 
17
 * The Initial Developer of the Original Code is
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1998-2000
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
/*
 
39
 * file:                        prinrval.c
 
40
 * description:         implementation for the kernel interval timing functions
 
41
 */
 
42
 
 
43
#include "primpl.h"
 
44
 
 
45
/*
 
46
 *-----------------------------------------------------------------------
 
47
 *
 
48
 * _PR_InitClock --
 
49
 *
 
50
 *
 
51
 *-----------------------------------------------------------------------
 
52
 */
 
53
 
 
54
void _PR_InitClock(void)
 
55
{
 
56
    _PR_MD_INTERVAL_INIT();
 
57
#ifdef DEBUG
 
58
    {
 
59
        PRIntervalTime ticksPerSec = PR_TicksPerSecond();
 
60
 
 
61
        PR_ASSERT(ticksPerSec >= PR_INTERVAL_MIN);
 
62
        PR_ASSERT(ticksPerSec <= PR_INTERVAL_MAX);
 
63
    }
 
64
#endif /* DEBUG */
 
65
}
 
66
 
 
67
/*
 
68
 * This version of interval times is based on the time of day
 
69
 * capability offered by system. This isn't valid for two reasons:
 
70
 * 1) The time of day is neither linear nor montonically increasing
 
71
 * 2) The units here are milliseconds. That's not appropriate for our use.
 
72
 */
 
73
 
 
74
PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow(void)
 
75
{
 
76
    if (!_pr_initialized) _PR_ImplicitInitialization();
 
77
    return _PR_MD_GET_INTERVAL();
 
78
}  /* PR_IntervalNow */
 
79
 
 
80
PR_EXTERN(PRUint32) PR_TicksPerSecond(void)
 
81
{
 
82
    if (!_pr_initialized) _PR_ImplicitInitialization();
 
83
    return _PR_MD_INTERVAL_PER_SEC();
 
84
}  /* PR_TicksPerSecond */
 
85
 
 
86
PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds)
 
87
{
 
88
    return seconds * PR_TicksPerSecond();
 
89
}  /* PR_SecondsToInterval */
 
90
 
 
91
PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli)
 
92
{
 
93
    PRIntervalTime ticks;
 
94
    PRUint64 tock, tps, msecPerSec, rounding;
 
95
    LL_UI2L(tock, milli);
 
96
    LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
 
97
    LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1));
 
98
    LL_I2L(tps, PR_TicksPerSecond());
 
99
    LL_MUL(tock, tock, tps);
 
100
    LL_ADD(tock, tock, rounding);
 
101
    LL_DIV(tock, tock, msecPerSec);
 
102
    LL_L2UI(ticks, tock);
 
103
    return ticks;
 
104
}  /* PR_MillisecondsToInterval */
 
105
 
 
106
PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro)
 
107
{
 
108
    PRIntervalTime ticks;
 
109
    PRUint64 tock, tps, usecPerSec, rounding;
 
110
    LL_UI2L(tock, micro);
 
111
    LL_I2L(usecPerSec, PR_USEC_PER_SEC);
 
112
    LL_I2L(rounding, (PR_USEC_PER_SEC >> 1));
 
113
    LL_I2L(tps, PR_TicksPerSecond());
 
114
    LL_MUL(tock, tock, tps);
 
115
    LL_ADD(tock, tock, rounding);
 
116
    LL_DIV(tock, tock, usecPerSec);
 
117
    LL_L2UI(ticks, tock);
 
118
    return ticks;
 
119
}  /* PR_MicrosecondsToInterval */
 
120
 
 
121
PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks)
 
122
{
 
123
    return ticks / PR_TicksPerSecond();
 
124
}  /* PR_IntervalToSeconds */
 
125
 
 
126
PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks)
 
127
{
 
128
    PRUint32 milli;
 
129
    PRUint64 tock, tps, msecPerSec, rounding;
 
130
    LL_UI2L(tock, ticks);
 
131
    LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
 
132
    LL_I2L(tps, PR_TicksPerSecond());
 
133
    LL_USHR(rounding, tps, 1);
 
134
    LL_MUL(tock, tock, msecPerSec);
 
135
    LL_ADD(tock, tock, rounding);
 
136
    LL_DIV(tock, tock, tps);
 
137
    LL_L2UI(milli, tock);
 
138
    return milli;
 
139
}  /* PR_IntervalToMilliseconds */
 
140
 
 
141
PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks)
 
142
{
 
143
    PRUint32 micro;
 
144
    PRUint64 tock, tps, usecPerSec, rounding;
 
145
    LL_UI2L(tock, ticks);
 
146
    LL_I2L(usecPerSec, PR_USEC_PER_SEC);
 
147
    LL_I2L(tps, PR_TicksPerSecond());
 
148
    LL_USHR(rounding, tps, 1);
 
149
    LL_MUL(tock, tock, usecPerSec);
 
150
    LL_ADD(tock, tock, rounding);
 
151
    LL_DIV(tock, tock, tps);
 
152
    LL_L2UI(micro, tock);
 
153
    return micro;
 
154
}  /* PR_IntervalToMicroseconds */
 
155
 
 
156
/* prinrval.c */
 
157