swr/rast: name threads to aid debugging
[mesa.git] / src / gallium / drivers / swr / rasterizer / common / os.h
1 /****************************************************************************
2 * Copyright (C) 2014-2017 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 ****************************************************************************/
23
24 #ifndef __SWR_OS_H__
25 #define __SWR_OS_H__
26
27 #include <cstddef>
28 #include "core/knobs.h"
29
30 #if (defined(FORCE_WINDOWS) || defined(_WIN32)) && !defined(FORCE_LINUX)
31
32 #define SWR_API __cdecl
33
34 #ifndef NOMINMAX
35 #define NOMINMAX
36 #include <windows.h>
37 #undef NOMINMAX
38 #else
39 #include <windows.h>
40 #endif
41 #include <intrin.h>
42 #include <cstdint>
43
44 #if defined(MemoryFence)
45 // Windows.h defines MemoryFence as _mm_mfence, but this conflicts with llvm::sys::MemoryFence
46 #undef MemoryFence
47 #endif
48
49 #define OSALIGN(RWORD, WIDTH) __declspec(align(WIDTH)) RWORD
50
51 #if defined(_DEBUG)
52 // We compile Debug builds with inline function expansion enabled. This allows
53 // functions compiled with __forceinline to be inlined even in Debug builds.
54 // The inline_depth(0) pragma below will disable inline function expansion for
55 // normal INLINE / inline functions, but not for __forceinline functions.
56 // Our SIMD function wrappers (see simdlib.hpp) use __forceinline even in
57 // Debug builds.
58 #define INLINE inline
59 #pragma inline_depth(0)
60 #else
61 #define INLINE __forceinline
62 #endif
63 #define DEBUGBREAK __debugbreak()
64
65 #define PRAGMA_WARNING_PUSH_DISABLE(...) \
66 __pragma(warning(push));\
67 __pragma(warning(disable:__VA_ARGS__));
68
69 #define PRAGMA_WARNING_POP() __pragma(warning(pop))
70
71 static inline void *AlignedMalloc(size_t _Size, size_t _Alignment)
72 {
73 return _aligned_malloc(_Size, _Alignment);
74 }
75
76 static inline void AlignedFree(void* p)
77 {
78 return _aligned_free(p);
79 }
80
81 #if defined(_WIN64)
82 #define BitScanReverseSizeT BitScanReverse64
83 #define BitScanForwardSizeT BitScanForward64
84 #define _mm_popcount_sizeT _mm_popcnt_u64
85 #else
86 #define BitScanReverseSizeT BitScanReverse
87 #define BitScanForwardSizeT BitScanForward
88 #define _mm_popcount_sizeT _mm_popcnt_u32
89 #endif
90
91 #elif defined(__APPLE__) || defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
92
93 #define SWR_API
94
95 #include <stdlib.h>
96 #include <string.h>
97 #include <x86intrin.h>
98 #include <stdint.h>
99 #include <sys/types.h>
100 #include <unistd.h>
101 #include <sys/stat.h>
102 #include <stdio.h>
103 #include <limits.h>
104
105 typedef void VOID;
106 typedef void* LPVOID;
107 typedef int INT;
108 typedef unsigned int UINT;
109 typedef void* HANDLE;
110 typedef int LONG;
111 typedef unsigned int DWORD;
112
113 #undef FALSE
114 #define FALSE 0
115
116 #undef TRUE
117 #define TRUE 1
118
119 #define MAX_PATH PATH_MAX
120
121 #define OSALIGN(RWORD, WIDTH) RWORD __attribute__((aligned(WIDTH)))
122 #ifndef INLINE
123 #define INLINE __inline
124 #endif
125 #define DEBUGBREAK asm ("int $3")
126
127 #if !defined(__CYGWIN__)
128
129 #ifndef __cdecl
130 #define __cdecl
131 #endif
132 #ifndef __stdcall
133 #define __stdcall
134 #endif
135
136 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
137 #define __declspec(x) __declspec_##x
138 #define __declspec_align(y) __attribute__((aligned(y)))
139 #define __declspec_deprecated __attribute__((deprecated))
140 #define __declspec_dllexport
141 #define __declspec_dllimport
142 #define __declspec_noinline __attribute__((__noinline__))
143 #define __declspec_nothrow __attribute__((nothrow))
144 #define __declspec_novtable
145 #define __declspec_thread __thread
146 #else
147 #define __declspec(X)
148 #endif
149
150 #endif
151
152 #define GCC_VERSION (__GNUC__ * 10000 \
153 + __GNUC_MINOR__ * 100 \
154 + __GNUC_PATCHLEVEL__)
155
156 #if !defined(__clang__) && (__GNUC__) && (GCC_VERSION < 40500)
157 inline
158 uint64_t __rdtsc()
159 {
160 long low, high;
161 asm volatile("rdtsc" : "=a"(low), "=d"(high));
162 return (low | ((uint64_t)high << 32));
163 }
164 #endif
165
166 #if !defined( __clang__) && !defined(__INTEL_COMPILER)
167 // Intrinsic not defined in gcc
168 static INLINE
169 void _mm256_storeu2_m128i(__m128i *hi, __m128i *lo, __m256i a)
170 {
171 _mm_storeu_si128((__m128i*)lo, _mm256_castsi256_si128(a));
172 _mm_storeu_si128((__m128i*)hi, _mm256_extractf128_si256(a, 0x1));
173 }
174 #endif
175
176 inline
177 unsigned char _BitScanForward(unsigned long *Index, unsigned long Mask)
178 {
179 *Index = __builtin_ctz(Mask);
180 return (Mask != 0);
181 }
182
183 inline
184 unsigned char _BitScanForward(unsigned int *Index, unsigned int Mask)
185 {
186 *Index = __builtin_ctz(Mask);
187 return (Mask != 0);
188 }
189
190 inline
191 unsigned char _BitScanReverse(unsigned long *Index, unsigned long Mask)
192 {
193 *Index = __builtin_clz(Mask);
194 return (Mask != 0);
195 }
196
197 inline
198 unsigned char _BitScanReverse(unsigned int *Index, unsigned int Mask)
199 {
200 *Index = __builtin_clz(Mask);
201 return (Mask != 0);
202 }
203
204 inline
205 void *AlignedMalloc(unsigned int size, unsigned int alignment)
206 {
207 void *ret;
208 if (posix_memalign(&ret, alignment, size))
209 {
210 return NULL;
211 }
212 return ret;
213 }
214
215 inline
216 unsigned char _bittest(const LONG *a, LONG b)
217 {
218 return ((*(unsigned *)(a) & (1 << b)) != 0);
219 }
220
221 static inline
222 void AlignedFree(void* p)
223 {
224 free(p);
225 }
226
227 #define _countof(a) (sizeof(a)/sizeof(*(a)))
228
229 #define sprintf_s sprintf
230 #define strcpy_s(dst,size,src) strncpy(dst,src,size)
231 #define GetCurrentProcessId getpid
232 pid_t gettid(void);
233 #define GetCurrentThreadId gettid
234
235 #define CreateDirectory(name, pSecurity) mkdir(name, 0777)
236
237 #define InterlockedCompareExchange(Dest, Exchange, Comparand) __sync_val_compare_and_swap(Dest, Comparand, Exchange)
238 #define InterlockedExchangeAdd(Addend, Value) __sync_fetch_and_add(Addend, Value)
239 #define InterlockedDecrement(Append) __sync_sub_and_fetch(Append, 1)
240 #define InterlockedDecrement64(Append) __sync_sub_and_fetch(Append, 1)
241 #define InterlockedIncrement(Append) __sync_add_and_fetch(Append, 1)
242 #define InterlockedAdd(Addend, Value) __sync_add_and_fetch(Addend, Value)
243 #define InterlockedAdd64(Addend, Value) __sync_add_and_fetch(Addend, Value)
244 #define _ReadWriteBarrier() asm volatile("" ::: "memory")
245
246 #define PRAGMA_WARNING_PUSH_DISABLE(...)
247 #define PRAGMA_WARNING_POP()
248
249 #else
250
251 #error Unsupported OS/system.
252
253 #endif
254
255 #define THREAD thread_local
256
257 // Universal types
258 typedef uint8_t KILOBYTE[1024];
259 typedef KILOBYTE MEGABYTE[1024];
260 typedef MEGABYTE GIGABYTE[1024];
261
262 #define OSALIGNLINE(RWORD) OSALIGN(RWORD, 64)
263 #define OSALIGNSIMD(RWORD) OSALIGN(RWORD, KNOB_SIMD_BYTES)
264 #if ENABLE_AVX512_SIMD16
265 #define OSALIGNSIMD16(RWORD) OSALIGN(RWORD, KNOB_SIMD16_BYTES)
266 #endif
267
268 #include "common/swr_assert.h"
269
270 #ifdef __GNUC__
271 #define ATTR_UNUSED __attribute__((unused))
272 #else
273 #define ATTR_UNUSED
274 #endif
275
276 // Defined in os.cpp
277 void SWR_API SetCurrentThreadName(const char* pThreadName);
278
279 #endif//__SWR_OS_H__