1
/* $Id: os_timestamp_common.c 2560 2009-03-30 18:22:16Z bennylp $ */
3
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
* Additional permission under GNU GPL version 3 section 7:
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.
32
#include <pj/compat/high_precision.h>
34
#if defined(PJ_HAS_HIGH_RES_TIMER) && PJ_HAS_HIGH_RES_TIMER != 0
36
#define U32MAX (0xFFFFFFFFUL)
37
#define NANOSEC (1000000000UL)
38
#define USEC (1000000UL)
41
#define u64tohighprec(u64) ((pj_highprec_t)((pj_int64_t)(u64)))
43
static pj_highprec_t get_elapsed( const pj_timestamp *start,
44
const pj_timestamp *stop )
46
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
47
return u64tohighprec(stop->u64 - start->u64);
49
pj_highprec_t elapsed_hi, elapsed_lo;
51
elapsed_hi = stop->u32.hi - start->u32.hi;
52
elapsed_lo = stop->u32.lo - start->u32.lo;
54
/* elapsed_hi = elapsed_hi * U32MAX */
55
pj_highprec_mul(elapsed_hi, U32MAX);
57
return elapsed_hi + elapsed_lo;
61
static pj_highprec_t elapsed_msec( const pj_timestamp *start,
62
const pj_timestamp *stop )
65
pj_highprec_t freq, elapsed;
67
if (pj_get_timestamp_freq(&ts_freq) != PJ_SUCCESS)
70
/* Convert frequency timestamp */
71
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
72
freq = u64tohighprec(ts_freq.u64);
74
freq = ts_freq.u32.hi;
75
pj_highprec_mul(freq, U32MAX);
76
freq += ts_freq.u32.lo;
79
/* Avoid division by zero. */
80
if (freq == 0) freq = 1;
82
/* Get elapsed time in cycles. */
83
elapsed = get_elapsed(start, stop);
85
/* usec = elapsed * MSEC / freq */
86
pj_highprec_mul(elapsed, MSEC);
87
pj_highprec_div(elapsed, freq);
92
static pj_highprec_t elapsed_usec( const pj_timestamp *start,
93
const pj_timestamp *stop )
96
pj_highprec_t freq, elapsed;
98
if (pj_get_timestamp_freq(&ts_freq) != PJ_SUCCESS)
101
/* Convert frequency timestamp */
102
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
103
freq = u64tohighprec(ts_freq.u64);
105
freq = ts_freq.u32.hi;
106
pj_highprec_mul(freq, U32MAX);
107
freq += ts_freq.u32.lo;
110
/* Avoid division by zero. */
111
if (freq == 0) freq = 1;
113
/* Get elapsed time in cycles. */
114
elapsed = get_elapsed(start, stop);
116
/* usec = elapsed * USEC / freq */
117
pj_highprec_mul(elapsed, USEC);
118
pj_highprec_div(elapsed, freq);
123
PJ_DEF(pj_uint32_t) pj_elapsed_nanosec( const pj_timestamp *start,
124
const pj_timestamp *stop )
126
pj_timestamp ts_freq;
127
pj_highprec_t freq, elapsed;
129
if (pj_get_timestamp_freq(&ts_freq) != PJ_SUCCESS)
132
/* Convert frequency timestamp */
133
#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
134
freq = u64tohighprec(ts_freq.u64);
136
freq = ts_freq.u32.hi;
137
pj_highprec_mul(freq, U32MAX);
138
freq += ts_freq.u32.lo;
141
/* Avoid division by zero. */
142
if (freq == 0) freq = 1;
144
/* Get elapsed time in cycles. */
145
elapsed = get_elapsed(start, stop);
147
/* usec = elapsed * USEC / freq */
148
pj_highprec_mul(elapsed, NANOSEC);
149
pj_highprec_div(elapsed, freq);
151
return (pj_uint32_t)elapsed;
154
PJ_DEF(pj_uint32_t) pj_elapsed_usec( const pj_timestamp *start,
155
const pj_timestamp *stop )
157
return (pj_uint32_t)elapsed_usec(start, stop);
160
PJ_DEF(pj_uint32_t) pj_elapsed_msec( const pj_timestamp *start,
161
const pj_timestamp *stop )
163
return (pj_uint32_t)elapsed_msec(start, stop);
166
PJ_DEF(pj_uint64_t) pj_elapsed_msec64(const pj_timestamp *start,
167
const pj_timestamp *stop )
169
return (pj_uint64_t)elapsed_msec(start, stop);
172
PJ_DEF(pj_time_val) pj_elapsed_time( const pj_timestamp *start,
173
const pj_timestamp *stop )
175
pj_highprec_t elapsed = elapsed_msec(start, stop);
176
pj_time_val tv_elapsed;
178
if (PJ_HIGHPREC_VALUE_IS_ZERO(elapsed)) {
179
tv_elapsed.sec = tv_elapsed.msec = 0;
182
pj_highprec_t sec, msec;
185
pj_highprec_div(sec, MSEC);
186
tv_elapsed.sec = (long)sec;
189
pj_highprec_mod(msec, MSEC);
190
tv_elapsed.msec = (long)msec;
196
PJ_DEF(pj_uint32_t) pj_elapsed_cycle( const pj_timestamp *start,
197
const pj_timestamp *stop )
199
return stop->u32.lo - start->u32.lo;
202
#endif /* PJ_HAS_HIGH_RES_TIMER */