tgsi: Opcode information.
[mesa.git] / src / gallium / auxiliary / util / u_time.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * OS independent time-manipulation functions.
31 *
32 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
33 */
34
35
36 #include "pipe/p_config.h"
37
38 #if defined(PIPE_OS_LINUX)
39 #include <sys/time.h>
40 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
41 #include <windows.h>
42 #include <winddi.h>
43 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
44 #include <windows.h>
45 extern VOID KeQuerySystemTime(PLARGE_INTEGER);
46 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
47 #include <windows.h>
48 #else
49 #error Unsupported OS
50 #endif
51
52 #include "util/u_time.h"
53
54
55 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) || defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
56
57 static int64_t frequency = 0;
58
59 static INLINE void
60 util_time_get_frequency(void)
61 {
62 if(!frequency) {
63 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
64 LONGLONG temp;
65 EngQueryPerformanceFrequency(&temp);
66 frequency = temp;
67 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
68 LARGE_INTEGER temp;
69 QueryPerformanceFrequency(&temp);
70 frequency = temp.QuadPart;
71 #endif
72 }
73 }
74 #endif
75
76
77 void
78 util_time_get(struct util_time *t)
79 {
80 #if defined(PIPE_OS_LINUX)
81 gettimeofday(&t->tv, NULL);
82 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
83 LONGLONG temp;
84 EngQueryPerformanceCounter(&temp);
85 t->counter = temp;
86 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
87 /* Updated every 10 miliseconds, measured in units of 100 nanoseconds.
88 * http://msdn.microsoft.com/en-us/library/ms801642.aspx */
89 LARGE_INTEGER temp;
90 KeQuerySystemTime(&temp);
91 t->counter = temp.QuadPart;
92 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
93 LARGE_INTEGER temp;
94 QueryPerformanceCounter(&temp);
95 t->counter = temp.QuadPart;
96 #endif
97 }
98
99
100 void
101 util_time_add(const struct util_time *t1,
102 int64_t usecs,
103 struct util_time *t2)
104 {
105 #if defined(PIPE_OS_LINUX)
106 t2->tv.tv_sec = t1->tv.tv_sec + usecs / 1000000;
107 t2->tv.tv_usec = t1->tv.tv_usec + usecs % 1000000;
108 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) || defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
109 util_time_get_frequency();
110 t2->counter = t1->counter + (usecs * frequency + INT64_C(999999))/INT64_C(1000000);
111 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
112 /* 1 tick = 100 nano seconds. */
113 t2->counter = t1->counter + usecs * 10;
114 #elif
115 LARGE_INTEGER temp;
116 LONGLONG freq;
117 freq = temp.QuadPart;
118 t2->counter = t1->counter + (usecs * freq)/1000000L;
119 #endif
120 }
121
122
123 int64_t
124 util_time_diff(const struct util_time *t1,
125 const struct util_time *t2)
126 {
127 #if defined(PIPE_OS_LINUX)
128 return (t2->tv.tv_usec - t1->tv.tv_usec) +
129 (t2->tv.tv_sec - t1->tv.tv_sec)*1000000;
130 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) || defined(PIPE_SUBSYSTEM_WINDOWS_USER) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
131 util_time_get_frequency();
132 return (t2->counter - t1->counter)*INT64_C(1000000)/frequency;
133 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
134 return (t2->counter - t1->counter)/10;
135 #endif
136 }
137
138
139 /**
140 * Compare two time values.
141 *
142 * Not publicly available because it does not take in account wrap-arounds.
143 * Use util_time_timeout instead.
144 */
145 static INLINE int
146 util_time_compare(const struct util_time *t1,
147 const struct util_time *t2)
148 {
149 #if defined(PIPE_OS_LINUX)
150 if (t1->tv.tv_sec < t2->tv.tv_sec)
151 return -1;
152 else if(t1->tv.tv_sec > t2->tv.tv_sec)
153 return 1;
154 else if (t1->tv.tv_usec < t2->tv.tv_usec)
155 return -1;
156 else if(t1->tv.tv_usec > t2->tv.tv_usec)
157 return 1;
158 else
159 return 0;
160 #elif defined(PIPE_OS_WINDOWS)
161 if (t1->counter < t2->counter)
162 return -1;
163 else if(t1->counter > t2->counter)
164 return 1;
165 else
166 return 0;
167 #endif
168 }
169
170
171 boolean
172 util_time_timeout(const struct util_time *start,
173 const struct util_time *end,
174 const struct util_time *curr)
175 {
176 if(util_time_compare(start, end) <= 0)
177 return !(util_time_compare(start, curr) <= 0 && util_time_compare(curr, end) < 0);
178 else
179 return !(util_time_compare(start, curr) <= 0 || util_time_compare(curr, end) < 0);
180 }
181
182
183 #if defined(PIPE_SUBSYSYEM_WINDOWS_DISPLAY)
184 void util_time_sleep(unsigned usecs)
185 {
186 LONGLONG start, curr, end;
187
188 EngQueryPerformanceCounter(&start);
189
190 if(!frequency)
191 EngQueryPerformanceFrequency(&frequency);
192
193 end = start + (usecs * frequency + 999999LL)/1000000LL;
194
195 do {
196 EngQueryPerformanceCounter(&curr);
197 } while(start <= curr && curr < end ||
198 end < start && (curr < end || start <= curr));
199 }
200 #endif