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