HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
architecture.hpp
1// Copyright Take Vos 2019-2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include <exception>
8#include <cstddef>
9#include <type_traits>
10#if defined(__APPLE__)
11#include <TargetConditionals.h>
12#endif
13
14namespace tt {
15
16#define TT_BT_DEBUG 'D'
17#define TT_BT_RELEASE 'R'
18
19#if defined(NDEBUG)
20#define TT_BUILD_TYPE TT_BT_RELEASE
21#else
22#define TT_BUILD_TYPE TT_BT_DEBUG
23#endif
24
25enum class build_type {
26 debug = TT_BT_DEBUG,
27 release = TT_BT_RELEASE,
28
29 current = TT_BUILD_TYPE
30};
31
32#define TT_OS_WINDOWS 'W'
33#define TT_OS_MACOS 'A'
34#define TT_OS_MOBILE 'M'
35#define TT_OS_OTHER 'O'
36
37#if defined(_WIN32)
38#define TT_OPERATING_SYSTEM TT_OS_WINDOWS
39#elif defined(TARGET_OS_MAC) && !defined(TARGET_OS_IPHONE)
40#define TT_OPERATING_SYSTEM TT_OS_MACOS
41#elif defined(TARGET_OS_IPHONE) || defined(__ANDROID__)
42#define TT_OPERATING_SYSTEM TT_OS_MOBILE
43#else
44#define TT_OPERATING_SYSTEM TT_OS_OTHER
45#endif
46
47enum class operating_system {
48 windows = TT_OS_WINDOWS,
49 macos = TT_OS_MACOS,
50 mobile = TT_OS_MOBILE,
51 other = TT_OS_OTHER,
52
53 current = TT_OPERATING_SYSTEM
54};
55
56#define TT_CC_MSVC 'm'
57#define TT_CC_GCC 'g'
58#define TT_CC_CLANG 'c'
59
60#if defined(__clang__)
61#define TT_COMPILER TT_CC_CLANG
62#elif defined(_MSC_BUILD)
63#define TT_COMPILER TT_CC_MSVC
64#elif defined(__GNUC__)
65#define TT_COMPILER TT_CC_GCC
66#else
67#error "Could not detect the compiler."
68#endif
69
70enum class compiler {
71 msvc = TT_CC_MSVC,
72 gcc = TT_CC_GCC,
73 clang = TT_CC_CLANG,
74
75 current = TT_COMPILER
76};
77
78
79#define TT_CPU_X64 'i'
80#define TT_CPU_ARM 'a'
81#define TT_CPU_UNKNOWN 'u'
82
83#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64)
84#define TT_PROCESSOR TT_CPU_X64
85#elif defined(__arm__) || defined(_M_ARM)
86#define TT_PROCESSOR TT_CPU_ARM
87#else
88#define TT_PROCESSOR TT_CPU_UNKNOWN
89#endif
90
91enum class processor {
92 x64 = TT_CPU_X64,
93 arm = TT_CPU_ARM,
94 unknown = TT_CPU_UNKNOWN,
95
96 current = TT_PROCESSOR
97};
98
99#if TT_PROCESSOR == TT_CPU_X64
100#if defined(__AVX512BW__) && defined(__AVX512CD__) && defined(__AVX512DQ__) && defined(__AVX512F__) && defined(__AVX512VL__)
101#define TT_X86_64_V4 1
102#define TT_X86_64_V3 1
103#define TT_X86_64_V2_5 1
104#define TT_X86_64_V2 1
105#define TT_X86_64_V1 1
106#define TT_HAS_SSE
107#define TT_HAS_SSE2
108#define TT_HAS_SSE3
109#define TT_HAS_SSE4_1
110#define TT_HAS_SSE4_2
111#define TT_HAS_SSSE3
112#define TT_HAS_AVX
113#define TT_HAS_AVX2
114#define TT_HAS_AVX512F
115#define TT_HAS_AVX512BW
116#define TT_HAS_AVX512CD
117#define TT_HAS_AVX512DQ
118#define TT_HAS_AVX512VL
119
120#elif defined(__AVX2__)
121#define TT_X86_64_V3 1
122#define TT_X86_64_V2_5 1
123#define TT_X86_64_V2 1
124#define TT_X86_64_V1 1
125#define TT_HAS_SSE
126#define TT_HAS_SSE2
127#define TT_HAS_SSE3
128#define TT_HAS_SSE4_1
129#define TT_HAS_SSE4_2
130#define TT_HAS_SSSE3
131#define TT_HAS_AVX
132#define TT_HAS_AVX2
133
134#elif defined(__AVX__)
135#define TT_X86_64_V2_5 1
136#define TT_X86_64_V2 1
137#define TT_X86_64_V1 1
138#define TT_HAS_SSE
139#define TT_HAS_SSE2
140#define TT_HAS_SSE3
141#define TT_HAS_SSE4_1
142#define TT_HAS_SSE4_2
143#define TT_HAS_SSSE3
144#define TT_HAS_AVX
145
146// x86_64_v2 can not be selected in MSVC, but can be in gcc and clang.
147#elif defined(__SSE4_2__) && defined(__SSSE3__)
148#define TT_X86_64_V2 1
149#define TT_X86_64_V1 1
150#define TT_HAS_SSE
151#define TT_HAS_SSE2
152#define TT_HAS_SSE3
153#define TT_HAS_SSE4_1
154#define TT_HAS_SSE4_2
155#define TT_HAS_SSSE3
156
157#else
158#define TT_X86_64_V1 1
159#define TT_HAS_SSE
160#define TT_HAS_SSE2
161#endif
162#endif
163
164#if defined(TT_X86_64_V1)
165constexpr bool x86_64_v1 = true;
166#else
167constexpr bool x86_64_v1 = false;
168#endif
169
170#if defined(TT_X86_64_V2)
171constexpr bool x86_64_v2 = true;
172#else
173constexpr bool x86_64_v2 = false;
174#endif
175
176#if defined(TT_X86_64_V2_5)
177constexpr bool x86_64_v2_5 = true;
178#else
179constexpr bool x86_64_v2_5 = false;
180#endif
181
182#if defined(TT_X86_64_V3)
183constexpr bool x86_64_v3 = true;
184#else
185constexpr bool x86_64_v3 = false;
186#endif
187
188#if defined(TT_X86_64_V4)
189constexpr bool x86_64_v4 = true;
190#else
191constexpr bool x86_64_v4 = false;
192#endif
193
194#if TT_COMPILER == TT_CC_MSVC
195#define tt_unreachable() __assume(0)
196#define tt_assume(condition) __assume(condition)
197#define tt_assume2(condition, msg) __assume(condition)
198#define tt_force_inline __forceinline
199#define tt_no_inline __declspec(noinline)
200#define tt_restrict __restrict
201#define tt_warning_push() _Pragma("warning( push )")
202#define tt_warning_pop() _Pragma("warning( pop )")
203#define tt_msvc_pragma(a) _Pragma(a)
204#define clang_suppress(a)
205
206#elif TT_COMPILER == TT_CC_CLANG
207#define tt_unreachable() __builtin_unreachable()
208#define tt_assume(condition) __builtin_assume(static_cast<bool>(condition))
209#define tt_assume2(condition, msg) __builtin_assume(static_cast<bool>(condition))
210#define tt_force_inline inline __attribute__((always_inline))
211#define tt_no_inline __attribute__((noinline))
212#define tt_restrict __restrict__
213#define tt_warning_push() _Pragma("warning(push)")
214#define tt_warning_pop() _Pragma("warning(push)")
215#define tt_msvc_pragma(a)
216#define clang_suppress(a) _Pragma(tt_stringify(clang diagnostic ignored a))
217
218#elif TT_COMPILER == TT_CC_GCC
219#define tt_unreachable() __builtin_unreachable()
220#define tt_assume(condition) do { if (!(condition)) tt_unreachable(); } while (false)
221#define tt_assume2(condition, msg) do { if (!(condition)) tt_unreachable(); } while (false)
222#define tt_force_inline inline __attribute__((always_inline))
223#define tt_no_inline __attribute__((noinline))
224#define tt_restrict __restrict__
225#define tt_warning_push() _Pragma("warning(push)")
226#define tt_warning_pop() _Pragma("warning(pop)")
227#define tt_msvc_pragma(a)
228#define clang_suppress(a)
229#define msvc_pragma(a)
230
231#else
232#define tt_unreachable() std::terminate()
233#define tt_assume(condition) static_assert(sizeof(condition) == 1)
234#define tt_assume2(condition, msg) static_assert(sizeof(condition) == 1, msg)
235#define tt_force_inline inline
236#define tt_no_inline
237#define tt_restrict
238#define tt_warning_push()
239#define tt_warning_pop()
240#define tt_msvc_pragma(a)
241#define clang_suppress(a)
242#define msvc_pragma(a)
243
244#endif
245
246#if TT_PROCESSOR == TT_CPU_X64
250constexpr size_t hardware_destructive_interference_size = 128;
251
255constexpr size_t hardware_constructive_interference_size = 64;
256#elif TT_PROCESSOR == TT_CPU_ARM
260constexpr size_t hardware_destructive_interference_size = 64;
261
265constexpr size_t hardware_constructive_interference_size = 64;
266
267#elif TT_PROCESSOR == TT_CPU_UNKNOWN
268constexpr size_t hardware_destructive_interference_size = 128;
269constexpr size_t hardware_constructive_interference_size = 64;
270#else
271#error "Missing implementation of hardware_destructive_interference_size and hardware_constructive_interference_size"
272#endif
273
274#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
275using os_handle = void *;
276using file_handle = os_handle;
277
278#elif TT_OPERATING_SYSTEM == TT_OS_MACOS
279using os_handle = int;
280using file_handle = int;
281
282#elif TT_OPERATING_SYSTEM == TT_OS_LINUX
283using os_handle = int;
284using file_handle = int;
285
286#else
287#error "file_handle Not implemented."
288#endif
289
290}