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