swr: [rasterizer] eliminate unused label warnings on gcc
[mesa.git] / src / gallium / drivers / swr / rasterizer / common / os.h
1 /****************************************************************************
2 * Copyright (C) 2014-2015 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 #endif
37 #include <windows.h>
38 #include <intrin.h>
39 #include <cstdint>
40
41 #if defined(MemoryFence)
42 // Windows.h defines MemoryFence as _mm_mfence, but this conflicts with llvm::sys::MemoryFence
43 #undef MemoryFence
44 #endif
45
46 #define OSALIGN(RWORD, WIDTH) __declspec(align(WIDTH)) RWORD
47 #define THREAD __declspec(thread)
48 #define INLINE __forceinline
49 #define DEBUGBREAK __debugbreak()
50
51 #define PRAGMA_WARNING_PUSH_DISABLE(...) \
52 __pragma(warning(push));\
53 __pragma(warning(disable:__VA_ARGS__));
54
55 #define PRAGMA_WARNING_POP() __pragma(warning(pop))
56
57 static inline void *AlignedMalloc(size_t _Size, size_t _Alignment)
58 {
59 return _aligned_malloc(_Size, _Alignment);
60 }
61
62 static inline void AlignedFree(void* p)
63 {
64 return _aligned_free(p);
65 }
66
67 #if defined(_WIN64)
68 #define BitScanReverseSizeT BitScanReverse64
69 #define BitScanForwardSizeT BitScanForward64
70 #define _mm_popcount_sizeT _mm_popcnt_u64
71 #else
72 #define BitScanReverseSizeT BitScanReverse
73 #define BitScanForwardSizeT BitScanForward
74 #define _mm_popcount_sizeT _mm_popcnt_u32
75 #endif
76
77 #elif defined(__APPLE__) || defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
78
79 #define SWR_API
80
81 #include <stdlib.h>
82 #include <string.h>
83 #include <x86intrin.h>
84 #include <stdint.h>
85 #include <sys/types.h>
86 #include <unistd.h>
87 #include <sys/stat.h>
88 #include <stdio.h>
89 #include <limits.h>
90
91 typedef void VOID;
92 typedef void* LPVOID;
93 typedef int INT;
94 typedef unsigned int UINT;
95 typedef void* HANDLE;
96 typedef int LONG;
97 typedef unsigned int DWORD;
98
99 #undef FALSE
100 #define FALSE 0
101
102 #undef TRUE
103 #define TRUE 1
104
105 #define MAX_PATH PATH_MAX
106
107 #define OSALIGN(RWORD, WIDTH) RWORD __attribute__((aligned(WIDTH)))
108 #define THREAD __thread
109 #ifndef INLINE
110 #define INLINE __inline
111 #endif
112 #define DEBUGBREAK asm ("int $3")
113
114 #if !defined(__CYGWIN__)
115
116 #ifndef __cdecl
117 #define __cdecl
118 #endif
119 #ifndef __stdcall
120 #define __stdcall
121 #endif
122
123 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
124 #define __declspec(x) __declspec_##x
125 #define __declspec_align(y) __attribute__((aligned(y)))
126 #define __declspec_deprecated __attribute__((deprecated))
127 #define __declspec_dllexport
128 #define __declspec_dllimport
129 #define __declspec_noinline __attribute__((__noinline__))
130 #define __declspec_nothrow __attribute__((nothrow))
131 #define __declspec_novtable
132 #define __declspec_thread __thread
133 #else
134 #define __declspec(X)
135 #endif
136
137 #endif
138
139 #define GCC_VERSION (__GNUC__ * 10000 \
140 + __GNUC_MINOR__ * 100 \
141 + __GNUC_PATCHLEVEL__)
142
143 #if !defined(__clang__) && (__GNUC__) && (GCC_VERSION < 40500)
144 inline
145 uint64_t __rdtsc()
146 {
147 long low, high;
148 asm volatile("rdtsc" : "=a"(low), "=d"(high));
149 return (low | ((uint64_t)high << 32));
150 }
151 #endif
152
153 #if !defined( __clang__) && !defined(__INTEL_COMPILER)
154 // Intrinsic not defined in gcc
155 static INLINE
156 void _mm256_storeu2_m128i(__m128i *hi, __m128i *lo, __m256i a)
157 {
158 _mm_storeu_si128((__m128i*)lo, _mm256_castsi256_si128(a));
159 _mm_storeu_si128((__m128i*)hi, _mm256_extractf128_si256(a, 0x1));
160 }
161 #endif
162
163 inline
164 unsigned char _BitScanForward(unsigned long *Index, unsigned long Mask)
165 {
166 *Index = __builtin_ctz(Mask);
167 return (Mask != 0);
168 }
169
170 inline
171 unsigned char _BitScanForward(unsigned int *Index, unsigned int Mask)
172 {
173 *Index = __builtin_ctz(Mask);
174 return (Mask != 0);
175 }
176
177 inline
178 unsigned char _BitScanReverse(unsigned long *Index, unsigned long Mask)
179 {
180 *Index = __builtin_clz(Mask);
181 return (Mask != 0);
182 }
183
184 inline
185 unsigned char _BitScanReverse(unsigned int *Index, unsigned int Mask)
186 {
187 *Index = __builtin_clz(Mask);
188 return (Mask != 0);
189 }
190
191 inline
192 void *AlignedMalloc(unsigned int size, unsigned int alignment)
193 {
194 void *ret;
195 if (posix_memalign(&ret, alignment, size))
196 {
197 return NULL;
198 }
199 return ret;
200 }
201
202 inline
203 unsigned char _bittest(const LONG *a, LONG b)
204 {
205 return ((*(unsigned *)(a) & (1 << b)) != 0);
206 }
207
208 static inline
209 void AlignedFree(void* p)
210 {
211 free(p);
212 }
213
214 #define _countof(a) (sizeof(a)/sizeof(*(a)))
215
216 #define sprintf_s sprintf
217 #define strcpy_s(dst,size,src) strncpy(dst,src,size)
218 #define GetCurrentProcessId getpid
219 pid_t gettid(void);
220 #define GetCurrentThreadId gettid
221
222 #define CreateDirectory(name, pSecurity) mkdir(name, 0777)
223
224 #define InterlockedCompareExchange(Dest, Exchange, Comparand) __sync_val_compare_and_swap(Dest, Comparand, Exchange)
225 #define InterlockedExchangeAdd(Addend, Value) __sync_fetch_and_add(Addend, Value)
226 #define InterlockedDecrement(Append) __sync_sub_and_fetch(Append, 1)
227 #define InterlockedDecrement64(Append) __sync_sub_and_fetch(Append, 1)
228 #define InterlockedIncrement(Append) __sync_add_and_fetch(Append, 1)
229 #define InterlockedAdd(Addend, Value) __sync_add_and_fetch(Addend, Value)
230 #define InterlockedAdd64(Addend, Value) __sync_add_and_fetch(Addend, Value)
231 #define _ReadWriteBarrier() asm volatile("" ::: "memory")
232
233 #define PRAGMA_WARNING_PUSH_DISABLE(...)
234 #define PRAGMA_WARNING_POP()
235
236 #else
237
238 #error Unsupported OS/system.
239
240 #endif
241
242 // Universal types
243 typedef uint8_t KILOBYTE[1024];
244 typedef KILOBYTE MEGABYTE[1024];
245 typedef MEGABYTE GIGABYTE[1024];
246
247 #define OSALIGNLINE(RWORD) OSALIGN(RWORD, 64)
248 #define OSALIGNSIMD(RWORD) OSALIGN(RWORD, KNOB_SIMD_BYTES)
249
250 #include "common/swr_assert.h"
251
252 #ifdef __GNUC__
253 #define ATTR_UNUSED __attribute__((unused))
254 #else
255 #define ATTR_UNUSED
256 #endif
257
258 #endif//__SWR_OS_H__