swr: [rasterizer common] include cstddef for offsetof
[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 #if defined(_WIN64)
53 #define BitScanReverseSizeT BitScanReverse64
54 #define BitScanForwardSizeT BitScanForward64
55 #define _mm_popcount_sizeT _mm_popcnt_u64
56 #else
57 #define BitScanReverseSizeT BitScanReverse
58 #define BitScanForwardSizeT BitScanForward
59 #define _mm_popcount_sizeT _mm_popcnt_u32
60 #endif
61
62 #elif defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
63
64 #define SWR_API
65
66 #include <stdlib.h>
67 #include <string.h>
68 #include <x86intrin.h>
69 #include <stdint.h>
70 #include <sys/types.h>
71 #include <unistd.h>
72 #include <sys/stat.h>
73 #include <stdio.h>
74
75 typedef void VOID;
76 typedef void* LPVOID;
77 typedef int INT;
78 typedef unsigned int UINT;
79 typedef void* HANDLE;
80 typedef int LONG;
81 typedef unsigned int DWORD;
82
83 #undef FALSE
84 #define FALSE 0
85
86 #undef TRUE
87 #define TRUE 1
88
89 #define OSALIGN(RWORD, WIDTH) RWORD __attribute__((aligned(WIDTH)))
90 #define THREAD __thread
91 #ifndef INLINE
92 #define INLINE __inline
93 #endif
94 #define DEBUGBREAK asm ("int $3")
95 #if !defined(__CYGWIN__)
96 #define __cdecl
97 #define __stdcall
98 #define __declspec(X)
99 #endif
100
101 #define GCC_VERSION (__GNUC__ * 10000 \
102 + __GNUC_MINOR__ * 100 \
103 + __GNUC_PATCHLEVEL__)
104
105 #if !defined(__clang__) && (__GNUC__) && (GCC_VERSION < 40500)
106 inline
107 uint64_t __rdtsc()
108 {
109 long low, high;
110 asm volatile("rdtsc" : "=a"(low), "=d"(high));
111 return (low | ((uint64_t)high << 32));
112 }
113 #endif
114
115 #ifndef __clang__
116 // Intrinsic not defined in gcc
117 static INLINE
118 void _mm256_storeu2_m128i(__m128i *hi, __m128i *lo, __m256i a)
119 {
120 _mm_storeu_si128((__m128i*)lo, _mm256_castsi256_si128(a));
121 _mm_storeu_si128((__m128i*)hi, _mm256_extractf128_si256(a, 0x1));
122 }
123 #endif
124
125 inline
126 unsigned char _BitScanForward(unsigned long *Index, unsigned long Mask)
127 {
128 *Index = __builtin_ctz(Mask);
129 return (Mask != 0);
130 }
131
132 inline
133 unsigned char _BitScanForward(unsigned int *Index, unsigned int Mask)
134 {
135 *Index = __builtin_ctz(Mask);
136 return (Mask != 0);
137 }
138
139 inline
140 unsigned char _BitScanReverse(unsigned long *Index, unsigned long Mask)
141 {
142 *Index = __builtin_clz(Mask);
143 return (Mask != 0);
144 }
145
146 inline
147 unsigned char _BitScanReverse(unsigned int *Index, unsigned int Mask)
148 {
149 *Index = __builtin_clz(Mask);
150 return (Mask != 0);
151 }
152
153 inline
154 void *_aligned_malloc(unsigned int size, unsigned int alignment)
155 {
156 void *ret;
157 if (posix_memalign(&ret, alignment, size))
158 {
159 return NULL;
160 }
161 return ret;
162 }
163
164 inline
165 unsigned char _bittest(const LONG *a, LONG b)
166 {
167 return ((*(unsigned *)(a) & (1 << b)) != 0);
168 }
169
170 #define GetCurrentProcessId getpid
171 #define GetCurrentThreadId gettid
172
173 #define CreateDirectory(name, pSecurity) mkdir(name, 0777)
174
175 #define _aligned_free free
176 #define InterlockedCompareExchange(Dest, Exchange, Comparand) __sync_val_compare_and_swap(Dest, Comparand, Exchange)
177 #define InterlockedExchangeAdd(Addend, Value) __sync_fetch_and_add(Addend, Value)
178 #define InterlockedDecrement(Append) __sync_sub_and_fetch(Append, 1)
179 #define InterlockedDecrement64(Append) __sync_sub_and_fetch(Append, 1)
180 #define InterlockedIncrement(Append) __sync_add_and_fetch(Append, 1)
181 #define _ReadWriteBarrier() asm volatile("" ::: "memory")
182
183 #define PRAGMA_WARNING_PUSH_DISABLE(...)
184 #define PRAGMA_WARNING_POP()
185
186 #else
187
188 #error Unsupported OS/system.
189
190 #endif
191
192 // Universal types
193 typedef uint8_t KILOBYTE[1024];
194 typedef KILOBYTE MEGABYTE[1024];
195 typedef MEGABYTE GIGABYTE[1024];
196
197 #define OSALIGNLINE(RWORD) OSALIGN(RWORD, 64)
198 #define OSALIGNSIMD(RWORD) OSALIGN(RWORD, KNOB_SIMD_BYTES)
199
200 #include "common/swr_assert.h"
201
202 #endif//__SWR_OS_H__