gallium/swr: Fix building swr with MSVC
[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 #define SWR_VISIBLE __declspec(dllexport)
34
35 #ifndef NOMINMAX
36 #undef UNICODE
37 #define NOMINMAX
38 #include <windows.h>
39 #undef NOMINMAX
40 #define UNICODE
41 #else
42 #include <windows.h>
43 #endif
44 #include <intrin.h>
45 #include <cstdint>
46
47 #if defined(MemoryFence)
48 // Windows.h defines MemoryFence as _mm_mfence, but this conflicts with llvm::sys::MemoryFence
49 #undef MemoryFence
50 #endif
51
52 #define OSALIGN(RWORD, WIDTH) __declspec(align(WIDTH)) RWORD
53
54 #if defined(_DEBUG)
55 // We compile Debug builds with inline function expansion enabled. This allows
56 // functions compiled with __forceinline to be inlined even in Debug builds.
57 // The inline_depth(0) pragma below will disable inline function expansion for
58 // normal INLINE / inline functions, but not for __forceinline functions.
59 // Our SIMD function wrappers (see simdlib.hpp) use __forceinline even in
60 // Debug builds.
61 #define INLINE inline
62 #pragma inline_depth(0)
63 #else
64 // Use of __forceinline increases compile time dramatically in release builds
65 // and provides almost 0 measurable benefit. Disable until we have a compelling
66 // use-case
67 // #define INLINE __forceinline
68 #define INLINE inline
69 #endif
70 #ifndef FORCEINLINE
71 #define FORCEINLINE __forceinline
72 #endif
73
74 #define DEBUGBREAK __debugbreak()
75
76 #define PRAGMA_WARNING_PUSH_DISABLE(...) \
77 __pragma(warning(push)); \
78 __pragma(warning(disable : __VA_ARGS__));
79
80 #define PRAGMA_WARNING_POP() __pragma(warning(pop))
81
82 static inline void* AlignedMalloc(size_t _Size, size_t _Alignment)
83 {
84 return _aligned_malloc(_Size, _Alignment);
85 }
86
87 static inline void AlignedFree(void* p)
88 {
89 return _aligned_free(p);
90 }
91
92 #if defined(_WIN64)
93 #define BitScanReverseSizeT BitScanReverse64
94 #define BitScanForwardSizeT BitScanForward64
95 #define _mm_popcount_sizeT _mm_popcnt_u64
96 #else
97 #define BitScanReverseSizeT BitScanReverse
98 #define BitScanForwardSizeT BitScanForward
99 #define _mm_popcount_sizeT _mm_popcnt_u32
100 #endif
101
102 #elif defined(__APPLE__) || defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
103
104 #define SWR_API
105 #define SWR_VISIBLE __attribute__((visibility("default")))
106
107 #include <stdlib.h>
108 #include <string.h>
109 #include <x86intrin.h>
110 #include <stdint.h>
111 #include <sys/types.h>
112 #include <unistd.h>
113 #include <sys/stat.h>
114 #include <stdio.h>
115 #include <limits.h>
116
117 typedef void VOID;
118 typedef void* LPVOID;
119 typedef int INT;
120 typedef unsigned int UINT;
121 typedef void* HANDLE;
122 typedef int LONG;
123 typedef unsigned int DWORD;
124
125 #undef FALSE
126 #define FALSE 0
127
128 #undef TRUE
129 #define TRUE 1
130
131 #define MAX_PATH PATH_MAX
132
133 #define OSALIGN(RWORD, WIDTH) RWORD __attribute__((aligned(WIDTH)))
134 #ifndef INLINE
135 #define INLINE __inline
136 #endif
137 #ifndef FORCEINLINE
138 #define FORCEINLINE INLINE
139 #endif
140 #define DEBUGBREAK asm("int $3")
141
142 #if !defined(__CYGWIN__)
143
144 #ifndef __cdecl
145 #define __cdecl
146 #endif
147 #ifndef __stdcall
148 #define __stdcall
149 #endif
150
151 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
152 #define __declspec(x) __declspec_##x
153 #define __declspec_align(y) __attribute__((aligned(y)))
154 #define __declspec_deprecated __attribute__((deprecated))
155 #define __declspec_dllexport
156 #define __declspec_dllimport
157 #define __declspec_noinline __attribute__((__noinline__))
158 #define __declspec_nothrow __attribute__((nothrow))
159 #define __declspec_novtable
160 #define __declspec_thread __thread
161 #else
162 #define __declspec(X)
163 #endif
164
165 #endif
166
167 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
168
169 #if !defined(__clang__) && (__GNUC__) && (GCC_VERSION < 40500)
170 inline uint64_t __rdtsc()
171 {
172 long low, high;
173 asm volatile("rdtsc" : "=a"(low), "=d"(high));
174 return (low | ((uint64_t)high << 32));
175 }
176 #endif
177
178 #if !defined(__clang__) && !defined(__INTEL_COMPILER)
179 // Intrinsic not defined in gcc < 10
180 #if (__GNUC__) && (GCC_VERSION < 100000)
181 static INLINE void _mm256_storeu2_m128i(__m128i* hi, __m128i* lo, __m256i a)
182 {
183 _mm_storeu_si128((__m128i*)lo, _mm256_castsi256_si128(a));
184 _mm_storeu_si128((__m128i*)hi, _mm256_extractf128_si256(a, 0x1));
185 }
186 #endif
187
188 // gcc prior to 4.9 doesn't have _mm*_undefined_*
189 #if (__GNUC__) && (GCC_VERSION < 40900)
190 #define _mm_undefined_si128 _mm_setzero_si128
191 #define _mm256_undefined_ps _mm256_setzero_ps
192 #endif
193 #endif
194
195 inline unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask)
196 {
197 *Index = __builtin_ctz(Mask);
198 return (Mask != 0);
199 }
200
201 inline unsigned char _BitScanForward(unsigned int* Index, unsigned int Mask)
202 {
203 *Index = __builtin_ctz(Mask);
204 return (Mask != 0);
205 }
206
207 inline unsigned char _BitScanReverse(unsigned long* Index, unsigned long Mask)
208 {
209 *Index = 63 - __builtin_clz(Mask);
210 return (Mask != 0);
211 }
212
213 inline unsigned char _BitScanReverse(unsigned int* Index, unsigned int Mask)
214 {
215 *Index = 31 - __builtin_clz(Mask);
216 return (Mask != 0);
217 }
218
219 #define _BitScanForward64 _BitScanForward
220 #define _BitScanReverse64 _BitScanReverse
221
222 inline void* AlignedMalloc(size_t size, size_t alignment)
223 {
224 void* ret;
225 if (posix_memalign(&ret, alignment, size))
226 {
227 return NULL;
228 }
229 return ret;
230 }
231
232 static inline void AlignedFree(void* p)
233 {
234 free(p);
235 }
236
237 #define _countof(a) (sizeof(a) / sizeof(*(a)))
238
239 #define sprintf_s sprintf
240 #define strcpy_s(dst, size, src) strncpy(dst, src, size)
241 #define GetCurrentProcessId getpid
242
243 #define InterlockedCompareExchange(Dest, Exchange, Comparand) \
244 __sync_val_compare_and_swap(Dest, Comparand, Exchange)
245 #define InterlockedExchangeAdd(Addend, Value) __sync_fetch_and_add(Addend, Value)
246 #define InterlockedDecrement(Append) __sync_sub_and_fetch(Append, 1)
247 #define InterlockedDecrement64(Append) __sync_sub_and_fetch(Append, 1)
248 #define InterlockedIncrement(Append) __sync_add_and_fetch(Append, 1)
249 #define InterlockedAdd(Addend, Value) __sync_add_and_fetch(Addend, Value)
250 #define InterlockedAdd64(Addend, Value) __sync_add_and_fetch(Addend, Value)
251 #define _ReadWriteBarrier() asm volatile("" ::: "memory")
252
253 #define PRAGMA_WARNING_PUSH_DISABLE(...)
254 #define PRAGMA_WARNING_POP()
255
256 #define ZeroMemory(dst, size) memset(dst, 0, size)
257 #else
258
259 #error Unsupported OS/system.
260
261 #endif
262
263 #define THREAD thread_local
264
265 // Universal types
266 typedef uint8_t KILOBYTE[1024];
267 typedef KILOBYTE MEGABYTE[1024];
268 typedef MEGABYTE GIGABYTE[1024];
269
270 #define OSALIGNLINE(RWORD) OSALIGN(RWORD, 64)
271 #define OSALIGNSIMD(RWORD) OSALIGN(RWORD, KNOB_SIMD_BYTES)
272 #define OSALIGNSIMD16(RWORD) OSALIGN(RWORD, KNOB_SIMD16_BYTES)
273
274 #include "common/swr_assert.h"
275
276 #ifdef __GNUC__
277 #define ATTR_UNUSED __attribute__((unused))
278 #else
279 #define ATTR_UNUSED
280 #endif
281
282 #define SWR_FUNC(_retType, _funcName, /* args */...) \
283 typedef _retType(SWR_API* PFN##_funcName)(__VA_ARGS__); \
284 _retType SWR_API _funcName(__VA_ARGS__);
285
286 // Defined in os.cpp
287 void SWR_API SetCurrentThreadName(const char* pThreadName);
288 void SWR_API CreateDirectoryPath(const std::string& path);
289
290 /// Execute Command (block until finished)
291 /// @returns process exit value
292 int SWR_API
293 ExecCmd(const std::string& cmd, ///< (In) Command line string
294 const char* pOptEnvStrings = nullptr, ///< (Optional In) Environment block for new process
295 std::string* pOptStdOut = nullptr, ///< (Optional Out) Standard Output text
296 std::string* pOptStdErr = nullptr, ///< (Optional Out) Standard Error text
297 const std::string* pOptStdIn = nullptr); ///< (Optional In) Standard Input text
298
299
300 /// Helper for setting up FP state
301 /// @returns old csr state
302 static INLINE uint32_t SetOptimalVectorCSR()
303 {
304 uint32_t oldCSR = _mm_getcsr();
305
306 uint32_t newCSR = (oldCSR & ~(_MM_ROUND_MASK | _MM_DENORMALS_ZERO_MASK | _MM_FLUSH_ZERO_MASK));
307 newCSR |= (_MM_ROUND_NEAREST | _MM_FLUSH_ZERO_ON | _MM_DENORMALS_ZERO_ON);
308 _mm_setcsr(newCSR);
309
310 return oldCSR;
311 }
312
313 /// Set Vector CSR state.
314 /// @param csrState - should be value returned from SetOptimalVectorCSR()
315 static INLINE void RestoreVectorCSR(uint32_t csrState)
316 {
317 _mm_setcsr(csrState);
318 }
319
320 #endif //__SWR_OS_H__