~ubuntu-branches/ubuntu/maverick/sflphone/maverick

« back to all changes in this revision

Viewing changes to sflphone-common/libs/pjproject/pjlib/src/pj/os_timestamp_common.c

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-06-03 15:59:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100603155946-ybe8d8o8zx8lp0m8
Tags: upstream-0.9.8.3
ImportĀ upstreamĀ versionĀ 0.9.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: os_timestamp_common.c 2560 2009-03-30 18:22:16Z bennylp $ */
 
2
/* 
 
3
 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
 
4
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 
19
 *
 
20
 *  Additional permission under GNU GPL version 3 section 7:
 
21
 *
 
22
 *  If you modify this program, or any covered work, by linking or
 
23
 *  combining it with the OpenSSL project's OpenSSL library (or a
 
24
 *  modified version of that library), containing parts covered by the
 
25
 *  terms of the OpenSSL or SSLeay licenses, Teluu Inc. (http://www.teluu.com)
 
26
 *  grants you additional permission to convey the resulting work.
 
27
 *  Corresponding Source for a non-source form of such a combination
 
28
 *  shall include the source code for the parts of OpenSSL used as well
 
29
 *  as that of the covered work.
 
30
 */
 
31
#include <pj/os.h>
 
32
#include <pj/compat/high_precision.h>
 
33
 
 
34
#if defined(PJ_HAS_HIGH_RES_TIMER) && PJ_HAS_HIGH_RES_TIMER != 0
 
35
 
 
36
#define U32MAX  (0xFFFFFFFFUL)
 
37
#define NANOSEC (1000000000UL)
 
38
#define USEC    (1000000UL)
 
39
#define MSEC    (1000)
 
40
 
 
41
#define u64tohighprec(u64)      ((pj_highprec_t)((pj_int64_t)(u64)))
 
42
 
 
43
static pj_highprec_t get_elapsed( const pj_timestamp *start,
 
44
                                  const pj_timestamp *stop )
 
45
{
 
46
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
 
47
    return u64tohighprec(stop->u64 - start->u64);
 
48
#else
 
49
    pj_highprec_t elapsed_hi, elapsed_lo;
 
50
 
 
51
    elapsed_hi = stop->u32.hi - start->u32.hi;
 
52
    elapsed_lo = stop->u32.lo - start->u32.lo;
 
53
 
 
54
    /* elapsed_hi = elapsed_hi * U32MAX */
 
55
    pj_highprec_mul(elapsed_hi, U32MAX);
 
56
 
 
57
    return elapsed_hi + elapsed_lo;
 
58
#endif
 
59
}
 
60
 
 
61
static pj_highprec_t elapsed_msec( const pj_timestamp *start,
 
62
                                   const pj_timestamp *stop )
 
63
{
 
64
    pj_timestamp ts_freq;
 
65
    pj_highprec_t freq, elapsed;
 
66
 
 
67
    if (pj_get_timestamp_freq(&ts_freq) != PJ_SUCCESS)
 
68
        return 0;
 
69
 
 
70
    /* Convert frequency timestamp */
 
71
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
 
72
    freq = u64tohighprec(ts_freq.u64);
 
73
#else
 
74
    freq = ts_freq.u32.hi;
 
75
    pj_highprec_mul(freq, U32MAX);
 
76
    freq += ts_freq.u32.lo;
 
77
#endif
 
78
 
 
79
    /* Avoid division by zero. */
 
80
    if (freq == 0) freq = 1;
 
81
 
 
82
    /* Get elapsed time in cycles. */
 
83
    elapsed = get_elapsed(start, stop);
 
84
 
 
85
    /* usec = elapsed * MSEC / freq */
 
86
    pj_highprec_mul(elapsed, MSEC);
 
87
    pj_highprec_div(elapsed, freq);
 
88
 
 
89
    return elapsed;
 
90
}
 
91
 
 
92
static pj_highprec_t elapsed_usec( const pj_timestamp *start,
 
93
                                   const pj_timestamp *stop )
 
94
{
 
95
    pj_timestamp ts_freq;
 
96
    pj_highprec_t freq, elapsed;
 
97
 
 
98
    if (pj_get_timestamp_freq(&ts_freq) != PJ_SUCCESS)
 
99
        return 0;
 
100
 
 
101
    /* Convert frequency timestamp */
 
102
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
 
103
    freq = u64tohighprec(ts_freq.u64);
 
104
#else
 
105
    freq = ts_freq.u32.hi;
 
106
    pj_highprec_mul(freq, U32MAX);
 
107
    freq += ts_freq.u32.lo;
 
108
#endif
 
109
 
 
110
    /* Avoid division by zero. */
 
111
    if (freq == 0) freq = 1;
 
112
 
 
113
    /* Get elapsed time in cycles. */
 
114
    elapsed = get_elapsed(start, stop);
 
115
 
 
116
    /* usec = elapsed * USEC / freq */
 
117
    pj_highprec_mul(elapsed, USEC);
 
118
    pj_highprec_div(elapsed, freq);
 
119
 
 
120
    return elapsed;
 
121
}
 
122
 
 
123
PJ_DEF(pj_uint32_t) pj_elapsed_nanosec( const pj_timestamp *start,
 
124
                                        const pj_timestamp *stop )
 
125
{
 
126
    pj_timestamp ts_freq;
 
127
    pj_highprec_t freq, elapsed;
 
128
 
 
129
    if (pj_get_timestamp_freq(&ts_freq) != PJ_SUCCESS)
 
130
        return 0;
 
131
 
 
132
    /* Convert frequency timestamp */
 
133
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
 
134
    freq = u64tohighprec(ts_freq.u64);
 
135
#else
 
136
    freq = ts_freq.u32.hi;
 
137
    pj_highprec_mul(freq, U32MAX);
 
138
    freq += ts_freq.u32.lo;
 
139
#endif
 
140
 
 
141
    /* Avoid division by zero. */
 
142
    if (freq == 0) freq = 1;
 
143
 
 
144
    /* Get elapsed time in cycles. */
 
145
    elapsed = get_elapsed(start, stop);
 
146
 
 
147
    /* usec = elapsed * USEC / freq */
 
148
    pj_highprec_mul(elapsed, NANOSEC);
 
149
    pj_highprec_div(elapsed, freq);
 
150
 
 
151
    return (pj_uint32_t)elapsed;
 
152
}
 
153
 
 
154
PJ_DEF(pj_uint32_t) pj_elapsed_usec( const pj_timestamp *start,
 
155
                                     const pj_timestamp *stop )
 
156
{
 
157
    return (pj_uint32_t)elapsed_usec(start, stop);
 
158
}
 
159
 
 
160
PJ_DEF(pj_uint32_t) pj_elapsed_msec( const pj_timestamp *start,
 
161
                                     const pj_timestamp *stop )
 
162
{
 
163
    return (pj_uint32_t)elapsed_msec(start, stop);
 
164
}
 
165
 
 
166
PJ_DEF(pj_uint64_t) pj_elapsed_msec64(const pj_timestamp *start,
 
167
                                      const pj_timestamp *stop )
 
168
{
 
169
    return (pj_uint64_t)elapsed_msec(start, stop);
 
170
}
 
171
 
 
172
PJ_DEF(pj_time_val) pj_elapsed_time( const pj_timestamp *start,
 
173
                                     const pj_timestamp *stop )
 
174
{
 
175
    pj_highprec_t elapsed = elapsed_msec(start, stop);
 
176
    pj_time_val tv_elapsed;
 
177
 
 
178
    if (PJ_HIGHPREC_VALUE_IS_ZERO(elapsed)) {
 
179
        tv_elapsed.sec = tv_elapsed.msec = 0;
 
180
        return tv_elapsed;
 
181
    } else {
 
182
        pj_highprec_t sec, msec;
 
183
 
 
184
        sec = elapsed;
 
185
        pj_highprec_div(sec, MSEC);
 
186
        tv_elapsed.sec = (long)sec;
 
187
 
 
188
        msec = elapsed;
 
189
        pj_highprec_mod(msec, MSEC);
 
190
        tv_elapsed.msec = (long)msec;
 
191
 
 
192
        return tv_elapsed;
 
193
    }
 
194
}
 
195
 
 
196
PJ_DEF(pj_uint32_t) pj_elapsed_cycle( const pj_timestamp *start,
 
197
                                      const pj_timestamp *stop )
 
198
{
 
199
    return stop->u32.lo - start->u32.lo;
 
200
}
 
201
 
 
202
#endif  /* PJ_HAS_HIGH_RES_TIMER */
 
203