gallium/swr: add OpenSWR rasterizer
[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 "core/knobs.h"
28
29 #if (defined(FORCE_WINDOWS) || defined(_WIN32)) && !defined(FORCE_LINUX)
30
31 #define SWR_API __cdecl
32
33 #ifndef _CRT_SECURE_NO_WARNINGS
34 #define _CRT_SECURE_NO_WARNINGS
35 #endif
36
37 #ifndef NOMINMAX
38 #define NOMINMAX
39 #endif
40 #include "Windows.h"
41 #include <intrin.h>
42 #include <cstdint>
43
44 #define OSALIGN(RWORD, WIDTH) __declspec(align(WIDTH)) RWORD
45 #define THREAD __declspec(thread)
46 #define INLINE __forceinline
47 #define DEBUGBREAK __debugbreak()
48
49 #define PRAGMA_WARNING_PUSH_DISABLE(...) \
50 __pragma(warning(push));\
51 __pragma(warning(disable:__VA_ARGS__));
52
53 #define PRAGMA_WARNING_POP() __pragma(warning(pop))
54
55 #if defined(_WIN32)
56 #if defined(_WIN64)
57 #define BitScanForwardSizeT BitScanForward64
58 #define _mm_popcount_sizeT _mm_popcnt_u64
59 #else
60 #define BitScanForwardSizeT BitScanForward
61 #define _mm_popcount_sizeT _mm_popcnt_u32
62 #endif
63 #endif
64
65 #elif defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
66
67 #define SWR_API
68
69 #include <stdlib.h>
70 #include <string.h>
71 #include <X11/Xmd.h>
72 #include <x86intrin.h>
73 #include <stdint.h>
74 #include <sys/types.h>
75 #include <unistd.h>
76 #include <sys/stat.h>
77
78 typedef void VOID;
79 typedef void* LPVOID;
80 typedef CARD8 BOOL;
81 typedef wchar_t WCHAR;
82 typedef uint16_t UINT16;
83 typedef int INT;
84 typedef int INT32;
85 typedef unsigned int UINT;
86 typedef uint32_t UINT32;
87 typedef uint64_t UINT64;
88 typedef int64_t INT64;
89 typedef void* HANDLE;
90 typedef float FLOAT;
91 typedef int LONG;
92 typedef CARD8 BYTE;
93 typedef unsigned char UCHAR;
94 typedef unsigned int DWORD;
95
96 #undef FALSE
97 #define FALSE 0
98
99 #undef TRUE
100 #define TRUE 1
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 #define __cdecl
109 #define __declspec(X)
110
111 #define GCC_VERSION (__GNUC__ * 10000 \
112 + __GNUC_MINOR__ * 100 \
113 + __GNUC_PATCHLEVEL__)
114
115 #if !defined(__clang__) && (__GNUC__) && (GCC_VERSION < 40500)
116 inline
117 uint64_t __rdtsc()
118 {
119 long low, high;
120 asm volatile("rdtsc" : "=a"(low), "=d"(high));
121 return (low | ((uint64_t)high << 32));
122 }
123 #endif
124
125 #ifndef __clang__
126 // Intrinsic not defined in gcc
127 static INLINE
128 void _mm256_storeu2_m128i(__m128i *hi, __m128i *lo, __m256i a)
129 {
130 _mm_storeu_si128((__m128i*)lo, _mm256_castsi256_si128(a));
131 _mm_storeu_si128((__m128i*)hi, _mm256_extractf128_si256(a, 0x1));
132 }
133 #endif
134
135 inline
136 unsigned char _BitScanForward(unsigned long *Index, unsigned long Mask)
137 {
138 *Index = __builtin_ctz(Mask);
139 return (Mask != 0);
140 }
141
142 inline
143 unsigned char _BitScanForward(unsigned int *Index, unsigned int Mask)
144 {
145 *Index = __builtin_ctz(Mask);
146 return (Mask != 0);
147 }
148
149 inline
150 unsigned char _BitScanReverse(unsigned long *Index, unsigned long Mask)
151 {
152 *Index = __builtin_clz(Mask);
153 return (Mask != 0);
154 }
155
156 inline
157 unsigned char _BitScanReverse(unsigned int *Index, unsigned int Mask)
158 {
159 *Index = __builtin_clz(Mask);
160 return (Mask != 0);
161 }
162
163 inline
164 void *_aligned_malloc(unsigned int size, unsigned int alignment)
165 {
166 void *ret;
167 if (posix_memalign(&ret, alignment, size))
168 {
169 return NULL;
170 }
171 return ret;
172 }
173
174 inline
175 unsigned char _bittest(const LONG *a, LONG b)
176 {
177 return ((*(unsigned *)(a) & (1 << b)) != 0);
178 }
179
180 #define GetCurrentProcessId getpid
181
182 #define CreateDirectory(name, pSecurity) mkdir(name, 0777)
183
184 #if defined(_WIN32)
185 static inline
186 unsigned int _mm_popcnt_u32(unsigned int v)
187 {
188 return __builtin_popcount(v);
189 }
190 #endif
191
192 #define _aligned_free free
193 #define InterlockedCompareExchange(Dest, Exchange, Comparand) __sync_val_compare_and_swap(Dest, Comparand, Exchange)
194 #define InterlockedExchangeAdd(Addend, Value) __sync_fetch_and_add(Addend, Value)
195 #define InterlockedDecrement(Append) __sync_sub_and_fetch(Append, 1)
196 #define InterlockedIncrement(Append) __sync_add_and_fetch(Append, 1)
197 #define _ReadWriteBarrier() asm volatile("" ::: "memory")
198 #define __stdcall
199
200 #define PRAGMA_WARNING_PUSH_DISABLE(...)
201 #define PRAGMA_WARNING_POP()
202
203 #else
204
205 #error Unsupported OS/system.
206
207 #endif
208
209 // Universal types
210 typedef BYTE KILOBYTE[1024];
211 typedef KILOBYTE MEGABYTE[1024];
212 typedef MEGABYTE GIGABYTE[1024];
213
214 #define OSALIGNLINE(RWORD) OSALIGN(RWORD, 64)
215 #if KNOB_SIMD_WIDTH == 8
216 #define OSALIGNSIMD(RWORD) OSALIGN(RWORD, 32)
217 #endif
218
219 #include "common/swr_assert.h"
220
221 #endif//__SWR_OS_H__