beta-0.89.2
[luatex.git] / source / libs / cairo / cairo-src / src / cairo-time.c
bloba0003fbfc2c5023b8a441f1d12e232e42d73aa3b
1 /* cairo - a vector graphics library with display and print output
3 * Copyright (c) 2007 Netlabs
4 * Copyright (c) 2006 Mozilla Corporation
5 * Copyright (c) 2006 Red Hat, Inc.
6 * Copyright (c) 2011 Andrea Canciani
8 * Permission to use, copy, modify, distribute, and sell this software
9 * and its documentation for any purpose is hereby granted without
10 * fee, provided that the above copyright notice appear in all copies
11 * and that both that copyright notice and this permission notice
12 * appear in supporting documentation, and that the name of
13 * the authors not be used in advertising or publicity pertaining to
14 * distribution of the software without specific, written prior
15 * permission. The authors make no representations about the
16 * suitability of this software for any purpose. It is provided "as
17 * is" without express or implied warranty.
19 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
20 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
22 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
23 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
24 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
25 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 * Authors: Peter Weilbacher <mozilla@weilbacher.org>
28 * Vladimir Vukicevic <vladimir@pobox.com>
29 * Carl Worth <cworth@cworth.org>
30 * Andrea Canciani <ranma42@gmail.com>
33 #include "cairoint.h"
35 #include "cairo-time-private.h"
37 #if HAVE_CLOCK_GETTIME
38 #if defined(CLOCK_MONOTONIC_RAW)
39 #define CAIRO_CLOCK CLOCK_MONOTONIC_RAW
40 #elif defined(CLOCK_MONOTONIC)
41 #define CAIRO_CLOCK CLOCK_MONOTONIC
42 #endif
43 #endif
45 #if defined(__APPLE__)
46 #include <mach/mach_time.h>
48 static cairo_always_inline double
49 _cairo_time_1s (void)
51 mach_timebase_info_data_t freq;
53 mach_timebase_info (&freq);
55 return 1000000000. * freq.denom / freq.numer;
58 cairo_time_t
59 _cairo_time_get (void)
61 return mach_absolute_time ();
64 #elif defined(__OS2__)
65 #define INCL_BASE
66 #include <os2.h>
68 static cairo_always_inline double
69 _cairo_time_1s (void)
71 ULONG freq;
73 DosTmrQueryFreq (&freq);
75 return freq;
78 cairo_time_t
79 _cairo_time_get (void)
81 QWORD t;
82 cairo_int64_t r;
84 DosTmrQueryTime (&t);
86 r = _cairo_int64_lsl (_cairo_int32_to_int64 (t.ulHi), 32);
87 r = _cairo_int64_add (r, _cairo_int32_to_int64 (t.ulLo));
89 return r;
92 #elif _WIN32
93 #define WIN32_LEAN_AND_MEAN
94 #include <windows.h>
96 static cairo_always_inline double
97 _cairo_time_1s (void)
99 LARGE_INTEGER freq;
101 QueryPerformanceFrequency (&freq);
103 return freq.QuadPart;
106 #ifndef HAVE_UINT64_T
107 static cairo_always_inline cairo_time_t
108 _cairo_time_from_large_integer (LARGE_INTEGER t)
110 cairo_int64_t r;
112 r = _cairo_int64_lsl (_cairo_int32_to_int64 (t.HighPart), 32);
113 r = _cairo_int64_add (r, _cairo_int32_to_int64 (t.LowPart));
115 return r;
117 #else
118 static cairo_always_inline cairo_time_t
119 _cairo_time_from_large_integer (LARGE_INTEGER t)
121 return t.QuadPart;
123 #endif
125 cairo_time_t
126 _cairo_time_get (void)
128 LARGE_INTEGER t;
130 QueryPerformanceCounter (&t);
132 return _cairo_time_from_large_integer(t);
135 #elif defined(CAIRO_CLOCK)
136 #include <time.h>
138 static cairo_always_inline double
139 _cairo_time_1s (void)
141 return 1000000000;
144 cairo_time_t
145 _cairo_time_get (void)
147 struct timespec t;
148 cairo_time_t r;
150 clock_gettime (CAIRO_CLOCK, &t);
152 r = _cairo_double_to_int64 (_cairo_time_1s ());
153 r = _cairo_int64_mul (r, _cairo_int32_to_int64 (t.tv_sec));
154 r = _cairo_int64_add (r, _cairo_int32_to_int64 (t.tv_nsec));
156 return r;
159 #else
160 #include <sys/time.h>
162 static cairo_always_inline double
163 _cairo_time_1s (void)
165 return 1000000;
168 cairo_time_t
169 _cairo_time_get (void)
171 struct timeval t;
172 cairo_time_t r;
174 gettimeofday (&t, NULL);
176 r = _cairo_double_to_int64 (_cairo_time_1s ());
177 r = _cairo_int64_mul (r, _cairo_int32_to_int64 (t.tv_sec));
178 r = _cairo_int64_add (r, _cairo_int32_to_int64 (t.tv_usec));
180 return r;
183 #endif
186 _cairo_time_cmp (const void *a,
187 const void *b)
189 const cairo_time_t *ta = a, *tb = b;
190 return _cairo_int64_cmp (*ta, *tb);
193 static double
194 _cairo_time_ticks_per_sec (void)
196 static double ticks = 0;
198 if (unlikely (ticks == 0))
199 ticks = _cairo_time_1s ();
201 return ticks;
204 static double
205 _cairo_time_s_per_tick (void)
207 static double s = 0;
209 if (unlikely (s == 0))
210 s = 1. / _cairo_time_ticks_per_sec ();
212 return s;
215 double
216 _cairo_time_to_s (cairo_time_t t)
218 return _cairo_int64_to_double (t) * _cairo_time_s_per_tick ();
221 cairo_time_t
222 _cairo_time_from_s (double t)
224 return _cairo_double_to_int64 (t * _cairo_time_ticks_per_sec ());