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 ****************************************************************************/
27 #include "core/knobs.h"
29 #if (defined(FORCE_WINDOWS) || defined(_WIN32)) && !defined(FORCE_LINUX)
31 #define SWR_API __cdecl
33 #ifndef _CRT_SECURE_NO_WARNINGS
34 #define _CRT_SECURE_NO_WARNINGS
44 #define OSALIGN(RWORD, WIDTH) __declspec(align(WIDTH)) RWORD
45 #define THREAD __declspec(thread)
46 #define INLINE __forceinline
47 #define DEBUGBREAK __debugbreak()
49 #define PRAGMA_WARNING_PUSH_DISABLE(...) \
50 __pragma(warning(push));\
51 __pragma(warning(disable:__VA_ARGS__));
53 #define PRAGMA_WARNING_POP() __pragma(warning(pop))
57 #define BitScanForwardSizeT BitScanForward64
58 #define _mm_popcount_sizeT _mm_popcnt_u64
60 #define BitScanForwardSizeT BitScanForward
61 #define _mm_popcount_sizeT _mm_popcnt_u32
65 #elif defined(FORCE_LINUX) || defined(__linux__) || defined(__gnu_linux__)
72 #include <x86intrin.h>
74 #include <sys/types.h>
81 typedef unsigned int UINT
;
82 typedef uint64_t UINT64
;
85 typedef unsigned char UCHAR
;
86 typedef unsigned int DWORD
;
94 #define OSALIGN(RWORD, WIDTH) RWORD __attribute__((aligned(WIDTH)))
95 #define THREAD __thread
97 #define INLINE __inline
99 #define DEBUGBREAK asm ("int $3")
101 #define __declspec(X)
103 #define GCC_VERSION (__GNUC__ * 10000 \
104 + __GNUC_MINOR__ * 100 \
105 + __GNUC_PATCHLEVEL__)
107 #if !defined(__clang__) && (__GNUC__) && (GCC_VERSION < 40500)
112 asm volatile("rdtsc" : "=a"(low
), "=d"(high
));
113 return (low
| ((uint64_t)high
<< 32));
118 // Intrinsic not defined in gcc
120 void _mm256_storeu2_m128i(__m128i
*hi
, __m128i
*lo
, __m256i a
)
122 _mm_storeu_si128((__m128i
*)lo
, _mm256_castsi256_si128(a
));
123 _mm_storeu_si128((__m128i
*)hi
, _mm256_extractf128_si256(a
, 0x1));
128 unsigned char _BitScanForward(unsigned long *Index
, unsigned long Mask
)
130 *Index
= __builtin_ctz(Mask
);
135 unsigned char _BitScanForward(unsigned int *Index
, unsigned int Mask
)
137 *Index
= __builtin_ctz(Mask
);
142 unsigned char _BitScanReverse(unsigned long *Index
, unsigned long Mask
)
144 *Index
= __builtin_clz(Mask
);
149 unsigned char _BitScanReverse(unsigned int *Index
, unsigned int Mask
)
151 *Index
= __builtin_clz(Mask
);
156 void *_aligned_malloc(unsigned int size
, unsigned int alignment
)
159 if (posix_memalign(&ret
, alignment
, size
))
167 unsigned char _bittest(const LONG
*a
, LONG b
)
169 return ((*(unsigned *)(a
) & (1 << b
)) != 0);
172 #define GetCurrentProcessId getpid
174 #define CreateDirectory(name, pSecurity) mkdir(name, 0777)
178 unsigned int _mm_popcnt_u32(unsigned int v
)
180 return __builtin_popcount(v
);
184 #define _aligned_free free
185 #define InterlockedCompareExchange(Dest, Exchange, Comparand) __sync_val_compare_and_swap(Dest, Comparand, Exchange)
186 #define InterlockedExchangeAdd(Addend, Value) __sync_fetch_and_add(Addend, Value)
187 #define InterlockedDecrement(Append) __sync_sub_and_fetch(Append, 1)
188 #define InterlockedDecrement64(Append) __sync_sub_and_fetch(Append, 1)
189 #define InterlockedIncrement(Append) __sync_add_and_fetch(Append, 1)
190 #define _ReadWriteBarrier() asm volatile("" ::: "memory")
193 #define PRAGMA_WARNING_PUSH_DISABLE(...)
194 #define PRAGMA_WARNING_POP()
198 #error Unsupported OS/system.
203 typedef BYTE KILOBYTE
[1024];
204 typedef KILOBYTE MEGABYTE
[1024];
205 typedef MEGABYTE GIGABYTE
[1024];
207 #define OSALIGNLINE(RWORD) OSALIGN(RWORD, 64)
208 #if KNOB_SIMD_WIDTH == 8
209 #define OSALIGNSIMD(RWORD) OSALIGN(RWORD, 32)
212 #include "common/swr_assert.h"