1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
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:
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
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
22 ****************************************************************************/
28 #include "core/knobs.h"
30 #if (defined(FORCE_WINDOWS) || defined(_WIN32)) && !defined(FORCE_LINUX)
32 #define SWR_API __cdecl
44 #if defined(MemoryFence)
45 // Windows.h defines MemoryFence as _mm_mfence, but this conflicts with llvm::sys::MemoryFence
49 #define OSALIGN(RWORD, WIDTH) __declspec(align(WIDTH)) RWORD
50 #define INLINE __forceinline
51 #define DEBUGBREAK __debugbreak()
53 #define PRAGMA_WARNING_PUSH_DISABLE(...) \
54 __pragma(warning(push));\
55 __pragma(warning(disable:__VA_ARGS__));
57 #define PRAGMA_WARNING_POP() __pragma(warning(pop))
59 static inline void *AlignedMalloc(size_t _Size
, size_t _Alignment
)
61 return _aligned_malloc(_Size
, _Alignment
);
64 static inline void AlignedFree(void* p
)
66 return _aligned_free(p
);
70 #define BitScanReverseSizeT BitScanReverse64
71 #define BitScanForwardSizeT BitScanForward64
72 #define _mm_popcount_sizeT _mm_popcnt_u64
74 #define BitScanReverseSizeT BitScanReverse
75 #define BitScanForwardSizeT BitScanForward
76 #define _mm_popcount_sizeT _mm_popcnt_u32
79 #elif defined(__APPLE__) || defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
85 #include <x86intrin.h>
87 #include <sys/types.h>
96 typedef unsigned int UINT
;
99 typedef unsigned int DWORD
;
107 #define MAX_PATH PATH_MAX
109 #define OSALIGN(RWORD, WIDTH) RWORD __attribute__((aligned(WIDTH)))
111 #define INLINE __inline
113 #define DEBUGBREAK asm ("int $3")
115 #if !defined(__CYGWIN__)
124 #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
125 #define __declspec(x) __declspec_##x
126 #define __declspec_align(y) __attribute__((aligned(y)))
127 #define __declspec_deprecated __attribute__((deprecated))
128 #define __declspec_dllexport
129 #define __declspec_dllimport
130 #define __declspec_noinline __attribute__((__noinline__))
131 #define __declspec_nothrow __attribute__((nothrow))
132 #define __declspec_novtable
133 #define __declspec_thread __thread
135 #define __declspec(X)
140 #define GCC_VERSION (__GNUC__ * 10000 \
141 + __GNUC_MINOR__ * 100 \
142 + __GNUC_PATCHLEVEL__)
144 #if !defined(__clang__) && (__GNUC__) && (GCC_VERSION < 40500)
149 asm volatile("rdtsc" : "=a"(low
), "=d"(high
));
150 return (low
| ((uint64_t)high
<< 32));
154 #if !defined( __clang__) && !defined(__INTEL_COMPILER)
155 // Intrinsic not defined in gcc
157 void _mm256_storeu2_m128i(__m128i
*hi
, __m128i
*lo
, __m256i a
)
159 _mm_storeu_si128((__m128i
*)lo
, _mm256_castsi256_si128(a
));
160 _mm_storeu_si128((__m128i
*)hi
, _mm256_extractf128_si256(a
, 0x1));
165 unsigned char _BitScanForward(unsigned long *Index
, unsigned long Mask
)
167 *Index
= __builtin_ctz(Mask
);
172 unsigned char _BitScanForward(unsigned int *Index
, unsigned int Mask
)
174 *Index
= __builtin_ctz(Mask
);
179 unsigned char _BitScanReverse(unsigned long *Index
, unsigned long Mask
)
181 *Index
= __builtin_clz(Mask
);
186 unsigned char _BitScanReverse(unsigned int *Index
, unsigned int Mask
)
188 *Index
= __builtin_clz(Mask
);
193 void *AlignedMalloc(unsigned int size
, unsigned int alignment
)
196 if (posix_memalign(&ret
, alignment
, size
))
204 unsigned char _bittest(const LONG
*a
, LONG b
)
206 return ((*(unsigned *)(a
) & (1 << b
)) != 0);
210 void AlignedFree(void* p
)
215 #define _countof(a) (sizeof(a)/sizeof(*(a)))
217 #define sprintf_s sprintf
218 #define strcpy_s(dst,size,src) strncpy(dst,src,size)
219 #define GetCurrentProcessId getpid
221 #define GetCurrentThreadId gettid
223 #define CreateDirectory(name, pSecurity) mkdir(name, 0777)
225 #define InterlockedCompareExchange(Dest, Exchange, Comparand) __sync_val_compare_and_swap(Dest, Comparand, Exchange)
226 #define InterlockedExchangeAdd(Addend, Value) __sync_fetch_and_add(Addend, Value)
227 #define InterlockedDecrement(Append) __sync_sub_and_fetch(Append, 1)
228 #define InterlockedDecrement64(Append) __sync_sub_and_fetch(Append, 1)
229 #define InterlockedIncrement(Append) __sync_add_and_fetch(Append, 1)
230 #define InterlockedAdd(Addend, Value) __sync_add_and_fetch(Addend, Value)
231 #define InterlockedAdd64(Addend, Value) __sync_add_and_fetch(Addend, Value)
232 #define _ReadWriteBarrier() asm volatile("" ::: "memory")
234 #define PRAGMA_WARNING_PUSH_DISABLE(...)
235 #define PRAGMA_WARNING_POP()
239 #error Unsupported OS/system.
243 #define THREAD thread_local
246 typedef uint8_t KILOBYTE
[1024];
247 typedef KILOBYTE MEGABYTE
[1024];
248 typedef MEGABYTE GIGABYTE
[1024];
250 #define OSALIGNLINE(RWORD) OSALIGN(RWORD, 64)
251 #define OSALIGNSIMD(RWORD) OSALIGN(RWORD, KNOB_SIMD_BYTES)
252 #if ENABLE_AVX512_SIMD16
253 #define OSALIGNSIMD16(RWORD) OSALIGN(RWORD, KNOB_SIMD16_BYTES)
256 #include "common/swr_assert.h"
259 #define ATTR_UNUSED __attribute__((unused))